Drop old mouse API and use the new one.
[midnight-commander.git] / src / filemanager / panel.c
bloba598a3f5992330f170297511d925d9803b73f562
1 /*
2 Panel managing.
4 Copyright (C) 1994-2016
5 Free Software Foundation, Inc.
7 Written by:
8 Miguel de Icaza, 1995
9 Timur Bakeyev, 1997, 1999
10 Slava Zanko <slavazanko@gmail.com>, 2013
11 Andrew Borodin <aborodin@vmail.ru>, 2013-2016
13 This file is part of the Midnight Commander.
15 The Midnight Commander is free software: you can redistribute it
16 and/or modify it under the terms of the GNU General Public License as
17 published by the Free Software Foundation, either version 3 of the License,
18 or (at your option) any later version.
20 The Midnight Commander is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
25 You should have received a copy of the GNU General Public License
26 along with this program. If not, see <http://www.gnu.org/licenses/>.
29 /** \file panel.c
30 * \brief Source: panel managin module
33 #include <config.h>
35 #include <errno.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
40 #include "lib/global.h"
42 #include "lib/tty/tty.h"
43 #include "lib/tty/key.h" /* XCTRL and ALT macros */
44 #include "lib/skin.h"
45 #include "lib/strescape.h"
46 #include "lib/mcconfig.h"
47 #include "lib/vfs/vfs.h"
48 #include "lib/unixcompat.h"
49 #include "lib/search.h"
50 #include "lib/timefmt.h" /* file_date() */
51 #include "lib/util.h"
52 #include "lib/widget.h"
53 #ifdef HAVE_CHARSET
54 #include "lib/charsets.h" /* get_codepage_id () */
55 #endif
56 #include "lib/event.h"
58 #include "src/setup.h" /* For loading/saving panel options */
59 #include "src/execute.h"
60 #ifdef HAVE_CHARSET
61 #include "src/selcodepage.h" /* select_charset (), SELECT_CHARSET_NO_TRANSLATE */
62 #endif
63 #include "src/keybind-defaults.h" /* global_keymap_t */
64 #ifdef ENABLE_SUBSHELL
65 #include "src/subshell/subshell.h" /* do_subshell_chdir() */
66 #endif
68 #include "dir.h"
69 #include "boxes.h"
70 #include "tree.h"
71 #include "ext.h" /* regexp_command */
72 #include "layout.h" /* Most layout variables are here */
73 #include "cmd.h"
74 #include "command.h" /* cmdline */
75 #include "usermenu.h"
76 #include "midnight.h"
77 #include "mountlist.h" /* my_statfs */
79 #include "panel.h"
81 /*** global variables ****************************************************************************/
83 /* The hook list for the select file function */
84 hook_t *select_file_hook = NULL;
86 /* *INDENT-OFF* */
87 panelized_panel_t panelized_panel = { {NULL, 0, -1}, NULL };
88 /* *INDENT-ON* */
90 static const char *string_file_name (file_entry_t *, int);
91 static const char *string_file_size (file_entry_t *, int);
92 static const char *string_file_size_brief (file_entry_t *, int);
93 static const char *string_file_type (file_entry_t *, int);
94 static const char *string_file_mtime (file_entry_t *, int);
95 static const char *string_file_atime (file_entry_t *, int);
96 static const char *string_file_ctime (file_entry_t *, int);
97 static const char *string_file_permission (file_entry_t *, int);
98 static const char *string_file_perm_octal (file_entry_t *, int);
99 static const char *string_file_nlinks (file_entry_t *, int);
100 static const char *string_inode (file_entry_t *, int);
101 static const char *string_file_nuid (file_entry_t *, int);
102 static const char *string_file_ngid (file_entry_t *, int);
103 static const char *string_file_owner (file_entry_t *, int);
104 static const char *string_file_group (file_entry_t *, int);
105 static const char *string_marked (file_entry_t *, int);
106 static const char *string_space (file_entry_t *, int);
107 static const char *string_dot (file_entry_t *, int);
109 mc_fhl_t *mc_filehighlight = NULL;
111 /*** file scope macro definitions ****************************************************************/
113 #define NORMAL 0
114 #define SELECTED 1
115 #define MARKED 2
116 #define MARKED_SELECTED 3
117 #define STATUS 5
119 /*** file scope type declarations ****************************************************************/
121 typedef enum
123 MARK_DONT_MOVE = 0,
124 MARK_DOWN = 1,
125 MARK_FORCE_DOWN = 2,
126 MARK_FORCE_UP = 3
127 } mark_act_t;
130 * This describes a format item. The parse_display_format routine parses
131 * the user specified format and creates a linked list of format_e structures.
133 typedef struct format_e
135 struct format_e *next;
136 int requested_field_len;
137 int field_len;
138 align_crt_t just_mode;
139 gboolean expand;
140 const char *(*string_fn) (file_entry_t *, int len);
141 char *title;
142 const char *id;
143 } format_e;
145 /* File name scroll states */
146 typedef enum
148 FILENAME_NOSCROLL = 1,
149 FILENAME_SCROLL_LEFT = 2,
150 FILENAME_SCROLL_RIGHT = 4
151 } filename_scroll_flag_t;
153 /*** file scope variables ************************************************************************/
155 /* *INDENT-OFF* */
156 static panel_field_t panel_fields[] = {
158 "unsorted", 12, TRUE, J_LEFT_FIT,
159 /* TRANSLATORS: one single character to represent 'unsorted' sort mode */
160 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
161 N_("sort|u"),
162 N_("&Unsorted"), TRUE, FALSE,
163 string_file_name,
164 (GCompareFunc) unsorted
168 "name", 12, TRUE, J_LEFT_FIT,
169 /* TRANSLATORS: one single character to represent 'name' sort mode */
170 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
171 N_("sort|n"),
172 N_("&Name"), TRUE, TRUE,
173 string_file_name,
174 (GCompareFunc) sort_name
178 "version", 12, TRUE, J_LEFT_FIT,
179 /* TRANSLATORS: one single character to represent 'version' sort mode */
180 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
181 N_("sort|v"),
182 N_("&Version"), TRUE, FALSE,
183 string_file_name,
184 (GCompareFunc) sort_vers
188 "extension", 12, TRUE, J_LEFT_FIT,
189 /* TRANSLATORS: one single character to represent 'extension' sort mode */
190 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
191 N_("sort|e"),
192 N_("E&xtension"), TRUE, FALSE,
193 string_file_name, /* TODO: string_file_ext */
194 (GCompareFunc) sort_ext
198 "size", 7, FALSE, J_RIGHT,
199 /* TRANSLATORS: one single character to represent 'size' sort mode */
200 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
201 N_("sort|s"),
202 N_("&Size"), TRUE, TRUE,
203 string_file_size,
204 (GCompareFunc) sort_size
208 "bsize", 7, FALSE, J_RIGHT,
210 N_("Block Size"), FALSE, FALSE,
211 string_file_size_brief,
212 (GCompareFunc) sort_size
216 "type", 1, FALSE, J_LEFT,
218 "", FALSE, TRUE,
219 string_file_type,
220 NULL
224 "mtime", 12, FALSE, J_RIGHT,
225 /* TRANSLATORS: one single character to represent 'Modify time' sort mode */
226 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
227 N_("sort|m"),
228 N_("&Modify time"), TRUE, TRUE,
229 string_file_mtime,
230 (GCompareFunc) sort_time
234 "atime", 12, FALSE, J_RIGHT,
235 /* TRANSLATORS: one single character to represent 'Access time' sort mode */
236 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
237 N_("sort|a"),
238 N_("&Access time"), TRUE, TRUE,
239 string_file_atime,
240 (GCompareFunc) sort_atime
244 "ctime", 12, FALSE, J_RIGHT,
245 /* TRANSLATORS: one single character to represent 'Change time' sort mode */
246 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
247 N_("sort|h"),
248 N_("C&hange time"), TRUE, TRUE,
249 string_file_ctime,
250 (GCompareFunc) sort_ctime
254 "perm", 10, FALSE, J_LEFT,
256 N_("Permission"), FALSE, TRUE,
257 string_file_permission,
258 NULL
262 "mode", 6, FALSE, J_RIGHT,
264 N_("Perm"), FALSE, TRUE,
265 string_file_perm_octal,
266 NULL
270 "nlink", 2, FALSE, J_RIGHT,
272 N_("Nl"), FALSE, TRUE,
273 string_file_nlinks, NULL
277 "inode", 5, FALSE, J_RIGHT,
278 /* TRANSLATORS: one single character to represent 'inode' sort mode */
279 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
280 N_("sort|i"),
281 N_("&Inode"), TRUE, TRUE,
282 string_inode,
283 (GCompareFunc) sort_inode
287 "nuid", 5, FALSE, J_RIGHT,
289 N_("UID"), FALSE, FALSE,
290 string_file_nuid,
291 NULL
295 "ngid", 5, FALSE, J_RIGHT,
297 N_("GID"), FALSE, FALSE,
298 string_file_ngid,
299 NULL
303 "owner", 8, FALSE, J_LEFT_FIT,
305 N_("Owner"), FALSE, TRUE,
306 string_file_owner,
307 NULL
311 "group", 8, FALSE, J_LEFT_FIT,
313 N_("Group"), FALSE, TRUE,
314 string_file_group,
315 NULL
319 "mark", 1, FALSE, J_RIGHT,
321 " ", FALSE, TRUE,
322 string_marked,
323 NULL
327 "|", 1, FALSE, J_RIGHT,
329 " ", FALSE, TRUE,
330 NULL,
331 NULL
335 "space", 1, FALSE, J_RIGHT,
337 " ", FALSE, TRUE,
338 string_space,
339 NULL
343 "dot", 1, FALSE, J_RIGHT,
345 " ", FALSE, FALSE,
346 string_dot,
347 NULL
351 NULL, 0, FALSE, J_RIGHT, NULL, NULL, FALSE, FALSE, NULL, NULL
354 /* *INDENT-ON* */
356 static char *panel_sort_up_sign = NULL;
357 static char *panel_sort_down_sign = NULL;
359 static char *panel_hiddenfiles_sign_show = NULL;
360 static char *panel_hiddenfiles_sign_hide = NULL;
361 static char *panel_history_prev_item_sign = NULL;
362 static char *panel_history_next_item_sign = NULL;
363 static char *panel_history_show_list_sign = NULL;
364 static char *panel_filename_scroll_left_char = NULL;
365 static char *panel_filename_scroll_right_char = NULL;
367 /* Panel that selection started */
368 static WPanel *mouse_mark_panel = NULL;
370 static int mouse_marking = 0;
371 static int state_mark = 0;
373 /* --------------------------------------------------------------------------------------------- */
374 /*** file scope functions ************************************************************************/
375 /* --------------------------------------------------------------------------------------------- */
377 static void
378 set_colors (const WPanel * panel)
380 (void) panel;
382 tty_set_normal_attrs ();
383 tty_setcolor (NORMAL_COLOR);
386 /* --------------------------------------------------------------------------------------------- */
387 /** Delete format string, it is a linked list */
389 static void
390 delete_format (format_e * format)
392 while (format != NULL)
394 format_e *next = format->next;
395 g_free (format->title);
396 g_free (format);
397 format = next;
401 /* --------------------------------------------------------------------------------------------- */
402 /** Extract the number of available lines in a panel */
404 static int
405 panel_lines (const WPanel * p)
407 /* 3 lines are: top frame, column header, botton frame */
408 return (WIDGET (p)->lines - 3 - (panels_options.show_mini_info ? 2 : 0));
411 /* --------------------------------------------------------------------------------------------- */
412 /** This code relies on the default justification!!! */
414 static void
415 add_permission_string (const char *dest, int width, file_entry_t * fe, int attr, int color,
416 gboolean is_octal)
418 int i, r, l;
420 l = get_user_permissions (&fe->st);
422 if (is_octal)
424 /* Place of the access bit in octal mode */
425 l = width + l - 3;
426 r = l + 1;
428 else
430 /* The same to the triplet in string mode */
431 l = l * 3 + 1;
432 r = l + 3;
435 for (i = 0; i < width; i++)
437 if (i >= l && i < r)
439 if (attr == SELECTED || attr == MARKED_SELECTED)
440 tty_setcolor (MARKED_SELECTED_COLOR);
441 else
442 tty_setcolor (MARKED_COLOR);
444 else if (color >= 0)
445 tty_setcolor (color);
446 else
447 tty_lowlevel_setcolor (-color);
449 tty_print_char (dest[i]);
453 /* --------------------------------------------------------------------------------------------- */
454 /** String representations of various file attributes name */
456 static const char *
457 string_file_name (file_entry_t * fe, int len)
459 static char buffer[MC_MAXPATHLEN * MB_LEN_MAX + 1];
461 (void) len;
463 g_strlcpy (buffer, fe->fname, sizeof (buffer));
464 return buffer;
467 /* --------------------------------------------------------------------------------------------- */
469 static unsigned int
470 ilog10 (dev_t n)
472 unsigned int digits = 0;
476 digits++, n /= 10;
478 while (n != 0);
480 return digits;
483 /* --------------------------------------------------------------------------------------------- */
485 static void
486 format_device_number (char *buf, size_t bufsize, dev_t dev)
488 dev_t major_dev = major (dev);
489 dev_t minor_dev = minor (dev);
490 unsigned int major_digits = ilog10 (major_dev);
491 unsigned int minor_digits = ilog10 (minor_dev);
493 g_assert (bufsize >= 1);
495 if (major_digits + 1 + minor_digits + 1 <= bufsize)
496 g_snprintf (buf, bufsize, "%lu,%lu", (unsigned long) major_dev, (unsigned long) minor_dev);
497 else
498 g_strlcpy (buf, _("[dev]"), bufsize);
501 /* --------------------------------------------------------------------------------------------- */
502 /** size */
504 static const char *
505 string_file_size (file_entry_t * fe, int len)
507 static char buffer[BUF_TINY];
509 /* Don't ever show size of ".." since we don't calculate it */
510 if (DIR_IS_DOTDOT (fe->fname))
511 return _("UP--DIR");
513 #ifdef HAVE_STRUCT_STAT_ST_RDEV
514 if (S_ISBLK (fe->st.st_mode) || S_ISCHR (fe->st.st_mode))
515 format_device_number (buffer, len + 1, fe->st.st_rdev);
516 else
517 #endif
518 size_trunc_len (buffer, (unsigned int) len, fe->st.st_size, 0, panels_options.kilobyte_si);
520 return buffer;
523 /* --------------------------------------------------------------------------------------------- */
524 /** bsize */
526 static const char *
527 string_file_size_brief (file_entry_t * fe, int len)
529 if (S_ISLNK (fe->st.st_mode) && !fe->f.link_to_dir)
530 return _("SYMLINK");
532 if ((S_ISDIR (fe->st.st_mode) || fe->f.link_to_dir) && !DIR_IS_DOTDOT (fe->fname))
533 return _("SUB-DIR");
535 return string_file_size (fe, len);
538 /* --------------------------------------------------------------------------------------------- */
539 /** This functions return a string representation of a file entry type */
541 static const char *
542 string_file_type (file_entry_t * fe, int len)
544 static char buffer[2];
546 (void) len;
548 if (S_ISDIR (fe->st.st_mode))
549 buffer[0] = PATH_SEP;
550 else if (S_ISLNK (fe->st.st_mode))
552 if (fe->f.link_to_dir)
553 buffer[0] = '~';
554 else if (fe->f.stale_link)
555 buffer[0] = '!';
556 else
557 buffer[0] = '@';
559 else if (S_ISCHR (fe->st.st_mode))
560 buffer[0] = '-';
561 else if (S_ISSOCK (fe->st.st_mode))
562 buffer[0] = '=';
563 else if (S_ISDOOR (fe->st.st_mode))
564 buffer[0] = '>';
565 else if (S_ISBLK (fe->st.st_mode))
566 buffer[0] = '+';
567 else if (S_ISFIFO (fe->st.st_mode))
568 buffer[0] = '|';
569 else if (S_ISNAM (fe->st.st_mode))
570 buffer[0] = '#';
571 else if (!S_ISREG (fe->st.st_mode))
572 buffer[0] = '?'; /* non-regular of unknown kind */
573 else if (is_exe (fe->st.st_mode))
574 buffer[0] = '*';
575 else
576 buffer[0] = ' ';
577 buffer[1] = '\0';
578 return buffer;
581 /* --------------------------------------------------------------------------------------------- */
582 /** mtime */
584 static const char *
585 string_file_mtime (file_entry_t * fe, int len)
587 (void) len;
589 return file_date (fe->st.st_mtime);
592 /* --------------------------------------------------------------------------------------------- */
593 /** atime */
595 static const char *
596 string_file_atime (file_entry_t * fe, int len)
598 (void) len;
600 return file_date (fe->st.st_atime);
603 /* --------------------------------------------------------------------------------------------- */
604 /** ctime */
606 static const char *
607 string_file_ctime (file_entry_t * fe, int len)
609 (void) len;
611 return file_date (fe->st.st_ctime);
614 /* --------------------------------------------------------------------------------------------- */
615 /** perm */
617 static const char *
618 string_file_permission (file_entry_t * fe, int len)
620 (void) len;
622 return string_perm (fe->st.st_mode);
625 /* --------------------------------------------------------------------------------------------- */
626 /** mode */
628 static const char *
629 string_file_perm_octal (file_entry_t * fe, int len)
631 static char buffer[10];
633 (void) len;
635 g_snprintf (buffer, sizeof (buffer), "0%06lo", (unsigned long) fe->st.st_mode);
636 return buffer;
639 /* --------------------------------------------------------------------------------------------- */
640 /** nlink */
642 static const char *
643 string_file_nlinks (file_entry_t * fe, int len)
645 static char buffer[BUF_TINY];
647 (void) len;
649 g_snprintf (buffer, sizeof (buffer), "%16d", (int) fe->st.st_nlink);
650 return buffer;
653 /* --------------------------------------------------------------------------------------------- */
654 /** inode */
656 static const char *
657 string_inode (file_entry_t * fe, int len)
659 static char buffer[10];
661 (void) len;
663 g_snprintf (buffer, sizeof (buffer), "%lu", (unsigned long) fe->st.st_ino);
664 return buffer;
667 /* --------------------------------------------------------------------------------------------- */
668 /** nuid */
670 static const char *
671 string_file_nuid (file_entry_t * fe, int len)
673 static char buffer[10];
675 (void) len;
677 g_snprintf (buffer, sizeof (buffer), "%lu", (unsigned long) fe->st.st_uid);
678 return buffer;
681 /* --------------------------------------------------------------------------------------------- */
682 /** ngid */
684 static const char *
685 string_file_ngid (file_entry_t * fe, int len)
687 static char buffer[10];
689 (void) len;
691 g_snprintf (buffer, sizeof (buffer), "%lu", (unsigned long) fe->st.st_gid);
692 return buffer;
695 /* --------------------------------------------------------------------------------------------- */
696 /** owner */
698 static const char *
699 string_file_owner (file_entry_t * fe, int len)
701 (void) len;
703 return get_owner (fe->st.st_uid);
706 /* --------------------------------------------------------------------------------------------- */
707 /** group */
709 static const char *
710 string_file_group (file_entry_t * fe, int len)
712 (void) len;
714 return get_group (fe->st.st_gid);
717 /* --------------------------------------------------------------------------------------------- */
718 /** mark */
720 static const char *
721 string_marked (file_entry_t * fe, int len)
723 (void) len;
725 return fe->f.marked ? "*" : " ";
728 /* --------------------------------------------------------------------------------------------- */
729 /** space */
731 static const char *
732 string_space (file_entry_t * fe, int len)
734 (void) fe;
735 (void) len;
737 return " ";
740 /* --------------------------------------------------------------------------------------------- */
741 /** dot */
743 static const char *
744 string_dot (file_entry_t * fe, int len)
746 (void) fe;
747 (void) len;
749 return ".";
752 /* --------------------------------------------------------------------------------------------- */
754 static int
755 file_compute_color (int attr, file_entry_t * fe)
757 switch (attr)
759 case SELECTED:
760 return (SELECTED_COLOR);
761 case MARKED:
762 return (MARKED_COLOR);
763 case MARKED_SELECTED:
764 return (MARKED_SELECTED_COLOR);
765 case STATUS:
766 return (NORMAL_COLOR);
767 case NORMAL:
768 default:
769 if (!panels_options.filetype_mode)
770 return (NORMAL_COLOR);
773 return mc_fhl_get_color (mc_filehighlight, fe);
776 /* --------------------------------------------------------------------------------------------- */
777 /** Returns the number of items in the given panel */
779 static int
780 panel_items (const WPanel * p)
782 return panel_lines (p) * p->list_cols;
785 /* --------------------------------------------------------------------------------------------- */
786 /** Formats the file number file_index of panel in the buffer dest */
788 static filename_scroll_flag_t
789 format_file (WPanel * panel, int file_index, int width, int attr, gboolean isstatus,
790 int *field_length)
792 int color = NORMAL_COLOR;
793 int length = 0;
794 format_e *format, *home;
795 file_entry_t *fe = NULL;
796 filename_scroll_flag_t res = FILENAME_NOSCROLL;
798 *field_length = 0;
800 if (file_index < panel->dir.len)
802 fe = &panel->dir.list[file_index];
803 color = file_compute_color (attr, fe);
806 home = isstatus ? panel->status_format : panel->format;
808 for (format = home; format != NULL && length != width; format = format->next)
810 if (format->string_fn != NULL)
812 const char *txt = " ";
813 int len, perm = 0;
814 const char *prepared_text;
815 int name_offset = 0;
817 if (fe != NULL)
818 txt = format->string_fn (fe, format->field_len);
820 len = format->field_len;
821 if (len + length > width)
822 len = width - length;
823 if (len <= 0)
824 break;
826 if (!isstatus && panel->content_shift > -1 && strcmp (format->id, "name") == 0)
828 int str_len;
829 int i;
831 *field_length = len + 1;
833 str_len = str_length (txt);
834 i = max (0, str_len - len);
835 panel->max_shift = max (panel->max_shift, i);
836 i = min (panel->content_shift, i);
838 if (i > -1)
840 name_offset = str_offset_to_pos (txt, i);
841 if (str_len > len)
843 res = FILENAME_SCROLL_LEFT;
844 if (str_length (txt + name_offset) > len)
845 res |= FILENAME_SCROLL_RIGHT;
850 if (panels_options.permission_mode)
852 if (strcmp (format->id, "perm") == 0)
853 perm = 1;
854 else if (strcmp (format->id, "mode") == 0)
855 perm = 2;
858 if (color >= 0)
859 tty_setcolor (color);
860 else
861 tty_lowlevel_setcolor (-color);
863 if (!isstatus && panel->content_shift > -1)
864 prepared_text =
865 str_fit_to_term (txt + name_offset, len, HIDE_FIT (format->just_mode));
866 else
867 prepared_text = str_fit_to_term (txt, len, format->just_mode);
869 if (perm != 0 && fe != NULL)
870 add_permission_string (prepared_text, format->field_len, fe, attr, color,
871 perm != 1);
872 else
873 tty_print_string (prepared_text);
875 length += len;
877 else
879 if (attr == SELECTED || attr == MARKED_SELECTED)
880 tty_setcolor (SELECTED_COLOR);
881 else
882 tty_setcolor (NORMAL_COLOR);
883 tty_print_one_vline (TRUE);
884 length++;
888 if (length < width)
890 int y, x;
892 tty_getyx (&y, &x);
893 tty_draw_hline (y, x, ' ', width - length);
896 return res;
899 /* --------------------------------------------------------------------------------------------- */
901 static void
902 repaint_file (WPanel * panel, int file_index, gboolean mv, int attr, gboolean isstatus)
904 Widget *w = WIDGET (panel);
906 int nth_column = 0;
907 int width;
908 int offset = 0;
909 filename_scroll_flag_t ret_frm;
910 int ypos = 0;
911 gboolean panel_is_split;
912 int fln = 0;
914 panel_is_split = !isstatus && panel->list_cols > 1;
915 width = w->cols - 2;
917 if (panel_is_split)
919 nth_column = (file_index - panel->top_file) / panel_lines (panel);
920 width /= panel->list_cols;
922 offset = width * nth_column;
924 if (nth_column + 1 >= panel->list_cols)
925 width = w->cols - offset - 2;
928 /* Nothing to paint */
929 if (width <= 0)
930 return;
932 if (mv)
934 ypos = file_index - panel->top_file;
936 if (panel_is_split)
937 ypos %= panel_lines (panel);
939 ypos += 2; /* top frame and header */
940 widget_move (w, ypos, offset + 1);
943 ret_frm = format_file (panel, file_index, width, attr, isstatus, &fln);
945 if (panel_is_split && nth_column + 1 < panel->list_cols)
947 tty_setcolor (NORMAL_COLOR);
948 tty_print_one_vline (TRUE);
951 if (ret_frm != FILENAME_NOSCROLL && mv)
953 if (!panel_is_split && fln > 0)
955 if (panel->list_type != list_long)
956 width = fln;
957 else
959 offset = width - fln + 1;
960 width = fln - 1;
964 widget_move (w, ypos, offset);
965 tty_setcolor (NORMAL_COLOR);
966 tty_print_string (panel_filename_scroll_left_char);
968 if ((ret_frm & FILENAME_SCROLL_RIGHT) != 0)
970 offset += width;
971 if (nth_column + 1 >= panel->list_cols)
972 offset++;
974 widget_move (w, ypos, offset);
975 tty_setcolor (NORMAL_COLOR);
976 tty_print_string (panel_filename_scroll_right_char);
981 /* --------------------------------------------------------------------------------------------- */
983 static void
984 display_mini_info (WPanel * panel)
986 Widget *w = WIDGET (panel);
988 if (!panels_options.show_mini_info)
989 return;
991 widget_move (w, panel_lines (panel) + 3, 1);
993 if (panel->searching)
995 tty_setcolor (INPUT_COLOR);
996 tty_print_char ('/');
997 tty_print_string (str_fit_to_term (panel->search_buffer, w->cols - 3, J_LEFT));
998 return;
1001 /* Status resolves links and show them */
1002 set_colors (panel);
1004 if (S_ISLNK (panel->dir.list[panel->selected].st.st_mode))
1006 char link_target[MC_MAXPATHLEN];
1007 vfs_path_t *lc_link_vpath;
1008 int len;
1010 lc_link_vpath =
1011 vfs_path_append_new (panel->cwd_vpath, panel->dir.list[panel->selected].fname, NULL);
1012 len = mc_readlink (lc_link_vpath, link_target, MC_MAXPATHLEN - 1);
1013 vfs_path_free (lc_link_vpath);
1014 if (len > 0)
1016 link_target[len] = 0;
1017 tty_print_string ("-> ");
1018 tty_print_string (str_fit_to_term (link_target, w->cols - 5, J_LEFT_FIT));
1020 else
1021 tty_print_string (str_fit_to_term (_("<readlink failed>"), w->cols - 2, J_LEFT));
1023 else if (DIR_IS_DOTDOT (panel->dir.list[panel->selected].fname))
1025 /* FIXME:
1026 * while loading directory (dir_list_load() and dir_list_reload()),
1027 * the actual stat info about ".." directory isn't got;
1028 * so just don't display incorrect info about ".." directory */
1029 tty_print_string (str_fit_to_term (_("UP--DIR"), w->cols - 2, J_LEFT));
1031 else
1032 /* Default behavior */
1033 repaint_file (panel, panel->selected, FALSE, STATUS, TRUE);
1036 /* --------------------------------------------------------------------------------------------- */
1038 static void
1039 paint_dir (WPanel * panel)
1041 int i;
1042 int items; /* Number of items */
1044 items = panel_items (panel);
1045 /* reset max len of filename because we have the new max length for the new file list */
1046 panel->max_shift = -1;
1048 for (i = 0; i < items; i++)
1050 int color = 0; /* Color value of the line */
1052 if (i + panel->top_file < panel->dir.len)
1054 color = 2 * (panel->dir.list[i + panel->top_file].f.marked);
1055 color += (panel->selected == i + panel->top_file && panel->active);
1058 repaint_file (panel, i + panel->top_file, TRUE, color, FALSE);
1061 tty_set_normal_attrs ();
1064 /* --------------------------------------------------------------------------------------------- */
1066 static void
1067 display_total_marked_size (const WPanel * panel, int y, int x, gboolean size_only)
1069 Widget *w = WIDGET (panel);
1071 char buffer[BUF_SMALL], b_bytes[BUF_SMALL], *buf;
1072 int cols;
1074 if (panel->marked <= 0)
1075 return;
1077 buf = size_only ? b_bytes : buffer;
1078 cols = w->cols - 2;
1081 * This is a trick to use two ngettext() calls in one sentence.
1082 * First make "N bytes", then insert it into "X in M files".
1084 g_snprintf (b_bytes, sizeof (b_bytes),
1085 ngettext ("%s byte", "%s bytes", panel->total),
1086 size_trunc_sep (panel->total, panels_options.kilobyte_si));
1087 if (!size_only)
1088 g_snprintf (buffer, sizeof (buffer),
1089 ngettext ("%s in %d file", "%s in %d files", panel->marked),
1090 b_bytes, panel->marked);
1092 /* don't forget spaces around buffer content */
1093 buf = (char *) str_trunc (buf, cols - 4);
1095 if (x < 0)
1096 /* center in panel */
1097 x = (w->cols - str_term_width1 (buf)) / 2 - 1;
1100 * y == panel_lines (panel) + 2 for mini_info_separator
1101 * y == w->lines - 1 for panel bottom frame
1103 widget_move (w, y, x);
1104 tty_setcolor (MARKED_COLOR);
1105 tty_printf (" %s ", buf);
1108 /* --------------------------------------------------------------------------------------------- */
1110 static void
1111 mini_info_separator (const WPanel * panel)
1113 if (panels_options.show_mini_info)
1115 Widget *w = WIDGET (panel);
1116 int y;
1118 y = panel_lines (panel) + 2;
1120 tty_setcolor (NORMAL_COLOR);
1121 tty_draw_hline (w->y + y, w->x + 1, ACS_HLINE, w->cols - 2);
1122 /* Status displays total marked size.
1123 * Centered in panel, full format. */
1124 display_total_marked_size (panel, y, -1, FALSE);
1128 /* --------------------------------------------------------------------------------------------- */
1130 static void
1131 show_free_space (const WPanel * panel)
1133 /* Used to figure out how many free space we have */
1134 static struct my_statfs myfs_stats;
1135 /* Old current working directory for displaying free space */
1136 static char *old_cwd = NULL;
1138 /* Don't try to stat non-local fs */
1139 if (!vfs_file_is_local (panel->cwd_vpath) || !free_space)
1140 return;
1142 if (old_cwd == NULL || strcmp (old_cwd, vfs_path_as_str (panel->cwd_vpath)) != 0)
1144 char rpath[PATH_MAX];
1146 init_my_statfs ();
1147 g_free (old_cwd);
1148 old_cwd = g_strdup (vfs_path_as_str (panel->cwd_vpath));
1150 if (mc_realpath (old_cwd, rpath) == NULL)
1151 return;
1153 my_statfs (&myfs_stats, rpath);
1156 if (myfs_stats.avail != 0 || myfs_stats.total != 0)
1158 Widget *w = WIDGET (panel);
1159 char buffer1[6], buffer2[6], tmp[BUF_SMALL];
1161 size_trunc_len (buffer1, sizeof (buffer1) - 1, myfs_stats.avail, 1,
1162 panels_options.kilobyte_si);
1163 size_trunc_len (buffer2, sizeof (buffer2) - 1, myfs_stats.total, 1,
1164 panels_options.kilobyte_si);
1165 g_snprintf (tmp, sizeof (tmp), " %s/%s (%d%%) ", buffer1, buffer2,
1166 myfs_stats.total == 0 ? 0 :
1167 (int) (100 * (long double) myfs_stats.avail / myfs_stats.total));
1168 widget_move (w, w->lines - 1, w->cols - 2 - (int) strlen (tmp));
1169 tty_setcolor (NORMAL_COLOR);
1170 tty_print_string (tmp);
1174 /* --------------------------------------------------------------------------------------------- */
1176 * Prepare path string for showing in panel's header.
1177 * Passwords will removed, also home dir will replaced by ~
1179 * @param panel WPanel object
1181 * @return newly allocated string.
1184 static char *
1185 panel_correct_path_to_show (const WPanel * panel)
1187 vfs_path_t *last_vpath;
1188 const vfs_path_element_t *path_element;
1189 char *return_path;
1190 int elements_count;
1192 elements_count = vfs_path_elements_count (panel->cwd_vpath);
1194 /* get last path element */
1195 path_element = vfs_path_element_clone (vfs_path_get_by_index (panel->cwd_vpath, -1));
1197 if (elements_count > 1 && (strcmp (path_element->class->name, "cpiofs") == 0 ||
1198 strcmp (path_element->class->name, "extfs") == 0 ||
1199 strcmp (path_element->class->name, "tarfs") == 0))
1201 const char *archive_name;
1202 const vfs_path_element_t *prev_path_element;
1204 /* get previous path element for catching archive name */
1205 prev_path_element = vfs_path_get_by_index (panel->cwd_vpath, -2);
1206 archive_name = strrchr (prev_path_element->path, PATH_SEP);
1207 if (archive_name != NULL)
1208 last_vpath = vfs_path_from_str_flags (archive_name + 1, VPF_NO_CANON);
1209 else
1211 last_vpath = vfs_path_from_str_flags (prev_path_element->path, VPF_NO_CANON);
1212 last_vpath->relative = TRUE;
1215 else
1217 last_vpath = vfs_path_new ();
1218 last_vpath->relative = TRUE;
1221 vfs_path_add_element (last_vpath, path_element);
1222 return_path =
1223 vfs_path_to_str_flags (last_vpath, 0,
1224 VPF_STRIP_HOME | VPF_STRIP_PASSWORD | VPF_HIDE_CHARSET);
1225 vfs_path_free (last_vpath);
1227 return return_path;
1230 /* --------------------------------------------------------------------------------------------- */
1232 * Get Current path element encoding
1234 * @param panel WPanel object
1236 * @return newly allocated string or NULL if path charset is same as system charset
1239 #ifdef HAVE_CHARSET
1240 static char *
1241 panel_get_encoding_info_str (const WPanel * panel)
1243 char *ret_str = NULL;
1244 const vfs_path_element_t *path_element;
1246 path_element = vfs_path_get_by_index (panel->cwd_vpath, -1);
1247 if (path_element->encoding != NULL)
1248 ret_str = g_strdup_printf ("[%s]", path_element->encoding);
1250 return ret_str;
1252 #endif
1254 /* --------------------------------------------------------------------------------------------- */
1256 static void
1257 show_dir (const WPanel * panel)
1259 Widget *w = WIDGET (panel);
1261 gchar *tmp;
1263 set_colors (panel);
1264 tty_draw_box (w->y, w->x, w->lines, w->cols, FALSE);
1266 if (panels_options.show_mini_info)
1268 int y;
1270 y = panel_lines (panel) + 2;
1272 widget_move (w, y, 0);
1273 tty_print_alt_char (ACS_LTEE, FALSE);
1274 widget_move (w, y, w->cols - 1);
1275 tty_print_alt_char (ACS_RTEE, FALSE);
1278 widget_move (w, 0, 1);
1279 tty_print_string (panel_history_prev_item_sign);
1281 tmp = panels_options.show_dot_files ? panel_hiddenfiles_sign_show : panel_hiddenfiles_sign_hide;
1282 tmp = g_strdup_printf ("%s[%s]%s", tmp, panel_history_show_list_sign,
1283 panel_history_next_item_sign);
1285 widget_move (w, 0, w->cols - 6);
1286 tty_print_string (tmp);
1288 g_free (tmp);
1290 widget_move (w, 0, 3);
1292 if (panel->is_panelized)
1293 tty_printf (" %s ", _("Panelize"));
1294 #ifdef HAVE_CHARSET
1295 else
1297 tmp = panel_get_encoding_info_str (panel);
1298 if (tmp != NULL)
1300 tty_printf ("%s", tmp);
1301 widget_move (w, 0, 3 + strlen (tmp));
1302 g_free (tmp);
1305 #endif
1307 if (panel->active)
1308 tty_setcolor (REVERSE_COLOR);
1310 tmp = panel_correct_path_to_show (panel);
1311 tty_printf (" %s ", str_term_trim (tmp, min (max (w->cols - 12, 0), w->cols)));
1312 g_free (tmp);
1314 if (!panels_options.show_mini_info)
1316 if (panel->marked == 0)
1318 /* Show size of curret file in the bottom of panel */
1319 if (S_ISREG (panel->dir.list[panel->selected].st.st_mode))
1321 char buffer[BUF_SMALL];
1323 g_snprintf (buffer, sizeof (buffer), " %s ",
1324 size_trunc_sep (panel->dir.list[panel->selected].st.st_size,
1325 panels_options.kilobyte_si));
1326 tty_setcolor (NORMAL_COLOR);
1327 widget_move (w, w->lines - 1, 4);
1328 tty_print_string (buffer);
1331 else
1333 /* Show total size of marked files
1334 * In the bottom of panel, display size only. */
1335 display_total_marked_size (panel, w->lines - 1, 2, TRUE);
1339 show_free_space (panel);
1341 if (panel->active)
1342 tty_set_normal_attrs ();
1345 /* --------------------------------------------------------------------------------------------- */
1347 static void
1348 adjust_top_file (WPanel * panel)
1350 int items;
1352 items = panel_items (panel);
1354 if (panel->dir.len <= items)
1356 /* If all files fit, show them all. */
1357 panel->top_file = 0;
1359 else
1361 int i;
1363 /* top_file has to be in the range [selected-items+1, selected] so that
1364 the selected file is visible.
1365 top_file should be in the range [0, count-items] so that there's
1366 no empty space wasted.
1367 Within these ranges, adjust it by as little as possible. */
1369 if (panel->top_file < 0)
1370 panel->top_file = 0;
1372 i = panel->selected - items + 1;
1373 if (panel->top_file < i)
1374 panel->top_file = i;
1376 i = panel->dir.len - items;
1377 if (panel->top_file > i)
1378 panel->top_file = i;
1380 if (panel->top_file > panel->selected)
1381 panel->top_file = panel->selected;
1385 /* --------------------------------------------------------------------------------------------- */
1386 /** add "#enc:encodning" to end of path */
1387 /* if path end width a previous #enc:, only encoding is changed no additional
1388 * #enc: is appended
1389 * retun new string
1392 static char *
1393 panel_save_name (WPanel * panel)
1395 /* If the program is shuting down */
1396 if ((mc_global.midnight_shutdown && auto_save_setup) || saving_setup)
1397 return g_strdup (panel->panel_name);
1399 return g_strconcat ("Temporal:", panel->panel_name, (char *) NULL);
1402 /* --------------------------------------------------------------------------------------------- */
1404 static void
1405 directory_history_add (WPanel * panel, const vfs_path_t * vpath)
1407 char *tmp;
1409 tmp = vfs_path_to_str_flags (vpath, 0, VPF_STRIP_PASSWORD);
1410 panel->dir_history = list_append_unique (panel->dir_history, tmp);
1411 panel->dir_history_current = panel->dir_history;
1414 /* --------------------------------------------------------------------------------------------- */
1416 /* "history_load" event handler */
1417 static gboolean
1418 panel_load_history (const gchar * event_group_name, const gchar * event_name,
1419 gpointer init_data, gpointer data)
1421 WPanel *p = PANEL (init_data);
1422 ev_history_load_save_t *ev = (ev_history_load_save_t *) data;
1424 (void) event_group_name;
1425 (void) event_name;
1427 if (ev->receiver == NULL || ev->receiver == WIDGET (p))
1429 if (ev->cfg != NULL)
1430 p->dir_history = history_load (ev->cfg, p->hist_name);
1431 else
1432 p->dir_history = history_get (p->hist_name);
1434 directory_history_add (p, p->cwd_vpath);
1437 return TRUE;
1440 /* --------------------------------------------------------------------------------------------- */
1442 /* "history_save" event handler */
1443 static gboolean
1444 panel_save_history (const gchar * event_group_name, const gchar * event_name,
1445 gpointer init_data, gpointer data)
1447 WPanel *p = PANEL (init_data);
1449 (void) event_group_name;
1450 (void) event_name;
1452 if (p->dir_history != NULL)
1454 ev_history_load_save_t *ev = (ev_history_load_save_t *) data;
1456 history_save (ev->cfg, p->hist_name, p->dir_history);
1459 return TRUE;
1462 /* --------------------------------------------------------------------------------------------- */
1464 static void
1465 panel_destroy (WPanel * p)
1467 size_t i;
1469 if (panels_options.auto_save_setup)
1471 char *name;
1473 name = panel_save_name (p);
1474 panel_save_setup (p, name);
1475 g_free (name);
1478 panel_clean_dir (p);
1480 /* clean history */
1481 if (p->dir_history != NULL)
1483 /* directory history is already saved before this moment */
1484 p->dir_history = g_list_first (p->dir_history);
1485 g_list_free_full (p->dir_history, g_free);
1487 g_free (p->hist_name);
1489 delete_format (p->format);
1490 delete_format (p->status_format);
1492 g_free (p->user_format);
1493 for (i = 0; i < LIST_TYPES; i++)
1494 g_free (p->user_status_format[i]);
1496 g_free (p->dir.list);
1497 g_free (p->panel_name);
1499 vfs_path_free (p->lwd_vpath);
1500 vfs_path_free (p->cwd_vpath);
1503 /* --------------------------------------------------------------------------------------------- */
1505 static inline void
1506 panel_format_modified (WPanel * panel)
1508 panel->format_modified = 1;
1511 /* --------------------------------------------------------------------------------------------- */
1513 static void
1514 panel_paint_sort_info (const WPanel * panel)
1516 if (*panel->sort_field->hotkey != '\0')
1518 const char *sort_sign =
1519 panel->sort_info.reverse ? panel_sort_up_sign : panel_sort_down_sign;
1520 char *str;
1522 str = g_strdup_printf ("%s%s", sort_sign, Q_ (panel->sort_field->hotkey));
1523 widget_move (panel, 1, 1);
1524 tty_print_string (str);
1525 g_free (str);
1529 /* --------------------------------------------------------------------------------------------- */
1531 static const char *
1532 panel_get_title_without_hotkey (const char *title)
1534 static char translated_title[BUF_TINY];
1536 if (title == NULL || title[0] == '\0')
1537 translated_title[0] = '\0';
1538 else
1540 char *hkey;
1542 g_snprintf (translated_title, sizeof (translated_title), "%s", _(title));
1544 hkey = strchr (translated_title, '&');
1545 if (hkey != NULL && hkey[1] != '\0')
1546 memmove (hkey, hkey + 1, strlen (hkey));
1549 return translated_title;
1552 /* --------------------------------------------------------------------------------------------- */
1554 static void
1555 panel_print_header (const WPanel * panel)
1557 Widget *w = WIDGET (panel);
1559 int y, x;
1560 int i;
1561 GString *format_txt;
1563 widget_move (w, 1, 1);
1564 tty_getyx (&y, &x);
1565 tty_setcolor (NORMAL_COLOR);
1566 tty_draw_hline (y, x, ' ', w->cols - 2);
1568 format_txt = g_string_new ("");
1570 for (i = 0; i < panel->list_cols; i++)
1572 format_e *format;
1574 for (format = panel->format; format != NULL; format = format->next)
1576 if (format->string_fn != NULL)
1578 g_string_set_size (format_txt, 0);
1580 if (panel->list_type == list_long
1581 && strcmp (format->id, panel->sort_field->id) == 0)
1582 g_string_append (format_txt,
1583 panel->sort_info.reverse
1584 ? panel_sort_up_sign : panel_sort_down_sign);
1586 g_string_append (format_txt, format->title);
1588 if (panel->filter != NULL && *panel->filter != '\0'
1589 && strcmp (format->id, "name") == 0)
1591 g_string_append (format_txt, " [");
1592 g_string_append (format_txt, panel->filter);
1593 g_string_append (format_txt, "]");
1596 tty_setcolor (HEADER_COLOR);
1597 tty_print_string (str_fit_to_term (format_txt->str, format->field_len,
1598 J_CENTER_LEFT));
1600 else
1602 tty_setcolor (NORMAL_COLOR);
1603 tty_print_one_vline (TRUE);
1607 if (i < panel->list_cols - 1)
1609 tty_setcolor (NORMAL_COLOR);
1610 tty_print_one_vline (TRUE);
1614 g_string_free (format_txt, TRUE);
1616 if (panel->list_type != list_long)
1617 panel_paint_sort_info (panel);
1620 /* --------------------------------------------------------------------------------------------- */
1622 static const char *
1623 parse_panel_size (WPanel * panel, const char *format, gboolean isstatus)
1625 panel_display_t frame = frame_half;
1626 format = skip_separators (format);
1628 if (strncmp (format, "full", 4) == 0)
1630 frame = frame_full;
1631 format += 4;
1633 else if (strncmp (format, "half", 4) == 0)
1635 frame = frame_half;
1636 format += 4;
1639 if (!isstatus)
1641 panel->frame_size = frame;
1642 panel->list_cols = 1;
1645 /* Now, the optional column specifier */
1646 format = skip_separators (format);
1648 if (g_ascii_isdigit (*format))
1650 if (!isstatus && panel->list_type == list_brief)
1652 panel->brief_cols = g_ascii_digit_value (*format);
1653 if (panel->brief_cols < 1)
1654 panel->brief_cols = 1;
1656 panel->list_cols = panel->brief_cols;
1659 format++;
1662 if (!isstatus)
1663 panel_update_cols (WIDGET (panel), panel->frame_size);
1665 return skip_separators (format);
1668 /* Format is:
1670 all := panel_format? format
1671 panel_format := [full|half] [1-9]
1672 format := one_format_e
1673 | format , one_format_e
1675 one_format_e := just format.id [opt_size]
1676 just := [<=>]
1677 opt_size := : size [opt_expand]
1678 size := [0-9]+
1679 opt_expand := +
1683 /* --------------------------------------------------------------------------------------------- */
1685 static format_e *
1686 parse_display_format (WPanel * panel, const char *format, char **error, gboolean isstatus,
1687 int *res_total_cols)
1689 format_e *darr, *old = NULL, *home = NULL; /* The formats we return */
1690 int total_cols = 0; /* Used columns by the format */
1691 size_t i;
1693 static size_t i18n_timelength = 0; /* flag: check ?Time length at startup */
1695 *error = NULL;
1697 if (i18n_timelength == 0)
1699 i18n_timelength = i18n_checktimelength (); /* Musn't be 0 */
1701 for (i = 0; panel_fields[i].id != NULL; i++)
1702 if (strcmp ("time", panel_fields[i].id + 1) == 0)
1703 panel_fields[i].min_size = i18n_timelength;
1707 * This makes sure that the panel and mini status full/half mode
1708 * setting is equal
1710 format = parse_panel_size (panel, format, isstatus);
1712 while (*format != '\0')
1713 { /* format can be an empty string */
1714 align_crt_t justify; /* Which mode. */
1715 gboolean set_justify = TRUE; /* flag: set justification mode? */
1716 gboolean found = FALSE;
1718 darr = g_new0 (format_e, 1);
1720 /* I'm so ugly, don't look at me :-) */
1721 if (home == NULL)
1722 home = old = darr;
1724 old->next = darr;
1725 darr->next = NULL;
1726 old = darr;
1728 format = skip_separators (format);
1730 switch (*format)
1732 case '<':
1733 justify = J_LEFT;
1734 format = skip_separators (format + 1);
1735 break;
1736 case '=':
1737 justify = J_CENTER;
1738 format = skip_separators (format + 1);
1739 break;
1740 case '>':
1741 justify = J_RIGHT;
1742 format = skip_separators (format + 1);
1743 break;
1744 default:
1745 justify = J_LEFT;
1746 set_justify = FALSE;
1747 break;
1750 for (i = 0; panel_fields[i].id != NULL; i++)
1752 size_t klen;
1754 klen = strlen (panel_fields[i].id);
1756 if (strncmp (format, panel_fields[i].id, klen) != 0)
1757 continue;
1759 format += klen;
1761 darr->requested_field_len = panel_fields[i].min_size;
1762 darr->string_fn = panel_fields[i].string_fn;
1763 darr->title = g_strdup (panel_get_title_without_hotkey (panel_fields[i].title_hotkey));
1765 darr->id = panel_fields[i].id;
1766 darr->expand = panel_fields[i].expands;
1767 darr->just_mode = panel_fields[i].default_just;
1769 if (set_justify)
1771 if (IS_FIT (darr->just_mode))
1772 darr->just_mode = MAKE_FIT (justify);
1773 else
1774 darr->just_mode = justify;
1776 found = TRUE;
1778 format = skip_separators (format);
1780 /* If we have a size specifier */
1781 if (*format == ':')
1783 int req_length;
1785 /* If the size was specified, we don't want
1786 * auto-expansion by default
1788 darr->expand = FALSE;
1789 format++;
1790 req_length = atoi (format);
1791 darr->requested_field_len = req_length;
1793 format = skip_numbers (format);
1795 /* Now, if they insist on expansion */
1796 if (*format == '+')
1798 darr->expand = TRUE;
1799 format++;
1804 break;
1807 if (!found)
1809 size_t pos;
1810 char *tmp_format;
1812 pos = strlen (format);
1813 if (pos > 8)
1814 pos = 8;
1816 tmp_format = g_strndup (format, pos);
1817 delete_format (home);
1818 *error =
1819 g_strconcat (_("Unknown tag on display format:"), " ", tmp_format, (char *) NULL);
1820 g_free (tmp_format);
1822 return NULL;
1825 total_cols += darr->requested_field_len;
1828 *res_total_cols = total_cols;
1829 return home;
1832 /* --------------------------------------------------------------------------------------------- */
1834 static format_e *
1835 use_display_format (WPanel * panel, const char *format, char **error, gboolean isstatus)
1837 #define MAX_EXPAND 4
1838 int expand_top = 0; /* Max used element in expand */
1839 int usable_columns; /* Usable columns in the panel */
1840 int total_cols = 0;
1841 format_e *darr, *home;
1843 if (format == NULL)
1844 format = DEFAULT_USER_FORMAT;
1846 home = parse_display_format (panel, format, error, isstatus, &total_cols);
1848 if (*error != NULL)
1849 return NULL;
1851 panel->dirty = 1;
1853 usable_columns = WIDGET (panel)->cols - 2;
1854 /* Status needn't to be split */
1855 if (!isstatus)
1857 usable_columns /= panel->list_cols;
1858 if (panel->list_cols > 1)
1859 usable_columns--;
1862 /* Look for the expandable fields and set field_len based on the requested field len */
1863 for (darr = home; darr != NULL && expand_top < MAX_EXPAND; darr = darr->next)
1865 darr->field_len = darr->requested_field_len;
1866 if (darr->expand)
1867 expand_top++;
1870 /* If we used more columns than the available columns, adjust that */
1871 if (total_cols > usable_columns)
1873 int dif;
1874 int pdif = 0;
1876 dif = total_cols - usable_columns;
1878 while (dif != 0 && pdif != dif)
1880 pdif = dif;
1882 for (darr = home; darr; darr = darr->next)
1883 if (dif != 0 && darr->field_len != 1)
1885 darr->field_len--;
1886 dif--;
1890 total_cols = usable_columns; /* give up, the rest should be truncated */
1893 /* Expand the available space */
1894 if (usable_columns > total_cols && expand_top != 0)
1896 int i;
1897 int spaces = (usable_columns - total_cols) / expand_top;
1899 for (i = 0, darr = home; darr && (i < expand_top); darr = darr->next)
1900 if (darr->expand)
1902 darr->field_len += spaces;
1903 if (i == 0)
1904 darr->field_len += (usable_columns - total_cols) % expand_top;
1905 i++;
1908 return home;
1911 /* --------------------------------------------------------------------------------------------- */
1912 /** Given the panel->view_type returns the format string to be parsed */
1914 static const char *
1915 panel_format (WPanel * panel)
1918 switch (panel->list_type)
1920 case list_long:
1921 return "full perm space nlink space owner space group space size space mtime space name";
1923 case list_brief:
1925 static char format[BUF_TINY];
1926 int brief_cols = panel->brief_cols;
1928 if (brief_cols < 1)
1929 brief_cols = 2;
1931 if (brief_cols > 9)
1932 brief_cols = 9;
1934 g_snprintf (format, sizeof (format), "half %d type name", brief_cols);
1935 return format;
1938 case list_user:
1939 return panel->user_format;
1941 default:
1942 case list_full:
1943 return "half type name | size | mtime";
1947 /* --------------------------------------------------------------------------------------------- */
1949 static const char *
1950 mini_status_format (WPanel * panel)
1952 if (panel->user_mini_status)
1953 return panel->user_status_format[panel->list_type];
1955 switch (panel->list_type)
1957 case list_long:
1958 return "full perm space nlink space owner space group space size space mtime space name";
1960 case list_brief:
1961 return "half type name space bsize space perm space";
1963 case list_full:
1964 return "half type name";
1966 default:
1967 case list_user:
1968 return panel->user_format;
1972 /* */
1973 /* Panel operation commands */
1974 /* */
1976 /* --------------------------------------------------------------------------------------------- */
1977 /** Used to emulate Lynx's entering leaving a directory with the arrow keys */
1979 static cb_ret_t
1980 maybe_cd (gboolean move_up_dir)
1982 if (panels_options.navigate_with_arrows && (cmdline->buffer[0] == '\0'))
1984 if (move_up_dir)
1986 vfs_path_t *up_dir;
1988 up_dir = vfs_path_from_str ("..");
1989 do_cd (up_dir, cd_exact);
1990 vfs_path_free (up_dir);
1991 return MSG_HANDLED;
1994 if (S_ISDIR (selection (current_panel)->st.st_mode)
1995 || link_isdir (selection (current_panel)))
1997 vfs_path_t *vpath;
1999 vpath = vfs_path_from_str (selection (current_panel)->fname);
2000 do_cd (vpath, cd_exact);
2001 vfs_path_free (vpath);
2002 return MSG_HANDLED;
2006 return MSG_NOT_HANDLED;
2009 /* --------------------------------------------------------------------------------------------- */
2011 /* if command line is empty then do 'cd ..' */
2012 static cb_ret_t
2013 force_maybe_cd (void)
2015 if (cmdline->buffer[0] == '\0')
2017 vfs_path_t *up_dir = vfs_path_from_str ("..");
2018 do_cd (up_dir, cd_exact);
2019 vfs_path_free (up_dir);
2020 return MSG_HANDLED;
2023 return MSG_NOT_HANDLED;
2026 /* --------------------------------------------------------------------------------------------- */
2028 static inline void
2029 unselect_item (WPanel * panel)
2031 repaint_file (panel, panel->selected, TRUE, 2 * selection (panel)->f.marked, FALSE);
2034 /* --------------------------------------------------------------------------------------------- */
2035 /** Select/unselect all the files like a current file by extension */
2037 static void
2038 panel_select_ext_cmd (void)
2040 gboolean do_select = !selection (current_panel)->f.marked;
2041 char *filename = selection (current_panel)->fname;
2042 char *reg_exp, *cur_file_ext;
2043 mc_search_t *search;
2044 int i;
2046 if (filename == NULL)
2047 return;
2049 cur_file_ext = strutils_regex_escape (extension (filename));
2051 if (cur_file_ext[0] != '\0')
2052 reg_exp = g_strconcat ("^.*\\.", cur_file_ext, "$", (char *) NULL);
2053 else
2054 reg_exp = g_strdup ("^[^\\.]+$");
2056 g_free (cur_file_ext);
2058 search = mc_search_new (reg_exp, NULL);
2059 search->search_type = MC_SEARCH_T_REGEX;
2060 search->is_case_sensitive = FALSE;
2062 for (i = 0; i < current_panel->dir.len; i++)
2064 file_entry_t *file_entry = &current_panel->dir.list[i];
2066 if (DIR_IS_DOTDOT (file_entry->fname) || S_ISDIR (file_entry->st.st_mode))
2067 continue;
2069 if (!mc_search_run (search, file_entry->fname, 0, file_entry->fnamelen, NULL))
2070 continue;
2072 do_file_mark (current_panel, i, do_select);
2075 mc_search_free (search);
2076 g_free (reg_exp);
2080 /* --------------------------------------------------------------------------------------------- */
2082 static void
2083 move_down (WPanel * panel)
2085 int items;
2087 if (panel->selected + 1 == panel->dir.len)
2088 return;
2090 unselect_item (panel);
2091 panel->selected++;
2093 items = panel_items (panel);
2095 if (panels_options.scroll_pages && panel->selected - panel->top_file == items)
2097 /* Scroll window half screen */
2098 panel->top_file += items / 2;
2099 if (panel->top_file > panel->dir.len - items)
2100 panel->top_file = panel->dir.len - items;
2101 paint_dir (panel);
2103 select_item (panel);
2106 /* --------------------------------------------------------------------------------------------- */
2108 static void
2109 move_up (WPanel * panel)
2111 if (panel->selected == 0)
2112 return;
2114 unselect_item (panel);
2115 panel->selected--;
2117 if (panels_options.scroll_pages && panel->selected < panel->top_file)
2119 /* Scroll window half screen */
2120 panel->top_file -= panel_items (panel) / 2;
2121 if (panel->top_file < 0)
2122 panel->top_file = 0;
2123 paint_dir (panel);
2125 select_item (panel);
2128 /* --------------------------------------------------------------------------------------------- */
2129 /** Changes the selection by lines (may be negative) */
2131 static void
2132 move_selection (WPanel * panel, int lines)
2134 int new_pos;
2135 gboolean adjust = FALSE;
2137 new_pos = panel->selected + lines;
2138 if (new_pos >= panel->dir.len)
2139 new_pos = panel->dir.len - 1;
2141 if (new_pos < 0)
2142 new_pos = 0;
2144 unselect_item (panel);
2145 panel->selected = new_pos;
2147 if (panel->selected - panel->top_file >= panel_items (panel))
2149 panel->top_file += lines;
2150 adjust = TRUE;
2153 if (panel->selected - panel->top_file < 0)
2155 panel->top_file += lines;
2156 adjust = TRUE;
2159 if (adjust)
2161 if (panel->top_file > panel->selected)
2162 panel->top_file = panel->selected;
2163 if (panel->top_file < 0)
2164 panel->top_file = 0;
2165 paint_dir (panel);
2167 select_item (panel);
2170 /* --------------------------------------------------------------------------------------------- */
2172 static cb_ret_t
2173 move_left (WPanel * panel)
2175 if (panel->list_cols > 1)
2177 move_selection (panel, -panel_lines (panel));
2178 return MSG_HANDLED;
2181 return maybe_cd (TRUE); /* cd .. */
2184 /* --------------------------------------------------------------------------------------------- */
2186 static cb_ret_t
2187 move_right (WPanel * panel)
2189 if (panel->list_cols > 1)
2191 move_selection (panel, panel_lines (panel));
2192 return MSG_HANDLED;
2195 return maybe_cd (FALSE); /* cd (selection) */
2198 /* --------------------------------------------------------------------------------------------- */
2200 static void
2201 prev_page (WPanel * panel)
2203 int items;
2205 if (!panel->selected && !panel->top_file)
2206 return;
2208 unselect_item (panel);
2209 items = panel_items (panel);
2210 if (panel->top_file < items)
2211 items = panel->top_file;
2212 if (items == 0)
2213 panel->selected = 0;
2214 else
2215 panel->selected -= items;
2216 panel->top_file -= items;
2218 select_item (panel);
2219 paint_dir (panel);
2222 /* --------------------------------------------------------------------------------------------- */
2224 static void
2225 goto_parent_dir (WPanel * panel)
2227 if (!panel->is_panelized)
2229 vfs_path_t *up_dir;
2231 up_dir = vfs_path_from_str ("..");
2232 do_cd (up_dir, cd_exact);
2233 vfs_path_free (up_dir);
2235 else
2237 char *fname = panel->dir.list[panel->selected].fname;
2238 const char *bname;
2239 vfs_path_t *dname_vpath;
2241 if (g_path_is_absolute (fname))
2242 fname = g_strdup (fname);
2243 else
2244 fname =
2245 mc_build_filename (vfs_path_as_str (panelized_panel.root_vpath), fname,
2246 (char *) NULL);
2248 bname = x_basename (fname);
2250 if (bname == fname)
2251 dname_vpath = vfs_path_from_str (".");
2252 else
2254 char *dname;
2256 dname = g_strndup (fname, bname - fname);
2257 dname_vpath = vfs_path_from_str (dname);
2258 g_free (dname);
2261 do_cd (dname_vpath, cd_exact);
2262 try_to_select (panel, bname);
2264 vfs_path_free (dname_vpath);
2265 g_free (fname);
2269 /* --------------------------------------------------------------------------------------------- */
2271 static void
2272 next_page (WPanel * panel)
2274 int items;
2276 if (panel->selected == panel->dir.len - 1)
2277 return;
2279 unselect_item (panel);
2280 items = panel_items (panel);
2281 if (panel->top_file > panel->dir.len - 2 * items)
2282 items = panel->dir.len - items - panel->top_file;
2283 if (panel->top_file + items < 0)
2284 items = -panel->top_file;
2285 if (items == 0)
2286 panel->selected = panel->dir.len - 1;
2287 else
2288 panel->selected += items;
2289 panel->top_file += items;
2291 select_item (panel);
2292 paint_dir (panel);
2295 /* --------------------------------------------------------------------------------------------- */
2297 static void
2298 goto_child_dir (WPanel * panel)
2300 if ((S_ISDIR (selection (panel)->st.st_mode) || link_isdir (selection (panel))))
2302 vfs_path_t *vpath;
2304 vpath = vfs_path_from_str (selection (panel)->fname);
2305 do_cd (vpath, cd_exact);
2306 vfs_path_free (vpath);
2310 /* --------------------------------------------------------------------------------------------- */
2312 static void
2313 goto_top_file (WPanel * panel)
2315 unselect_item (panel);
2316 panel->selected = panel->top_file;
2317 select_item (panel);
2320 /* --------------------------------------------------------------------------------------------- */
2322 static void
2323 goto_middle_file (WPanel * panel)
2325 unselect_item (panel);
2326 panel->selected = panel->top_file + panel_items (panel) / 2;
2327 select_item (panel);
2330 /* --------------------------------------------------------------------------------------------- */
2332 static void
2333 goto_bottom_file (WPanel * panel)
2335 unselect_item (panel);
2336 panel->selected = panel->top_file + panel_items (panel) - 1;
2337 select_item (panel);
2340 /* --------------------------------------------------------------------------------------------- */
2342 static void
2343 move_home (WPanel * panel)
2345 if (panel->selected == 0)
2346 return;
2348 unselect_item (panel);
2350 if (panels_options.torben_fj_mode)
2352 int middle_pos = panel->top_file + panel_items (panel) / 2;
2354 if (panel->selected > middle_pos)
2356 goto_middle_file (panel);
2357 return;
2359 if (panel->selected != panel->top_file)
2361 goto_top_file (panel);
2362 return;
2366 panel->top_file = 0;
2367 panel->selected = 0;
2369 paint_dir (panel);
2370 select_item (panel);
2373 /* --------------------------------------------------------------------------------------------- */
2375 static void
2376 move_end (WPanel * panel)
2378 if (panel->selected == panel->dir.len - 1)
2379 return;
2381 unselect_item (panel);
2383 if (panels_options.torben_fj_mode)
2385 int items, middle_pos;
2387 items = panel_items (panel);
2388 middle_pos = panel->top_file + items / 2;
2390 if (panel->selected < middle_pos)
2392 goto_middle_file (panel);
2393 return;
2395 if (panel->selected != panel->top_file + items - 1)
2397 goto_bottom_file (panel);
2398 return;
2402 panel->selected = panel->dir.len - 1;
2403 paint_dir (panel);
2404 select_item (panel);
2407 /* --------------------------------------------------------------------------------------------- */
2409 static void
2410 do_mark_file (WPanel * panel, mark_act_t do_move)
2412 do_file_mark (panel, panel->selected, selection (panel)->f.marked ? 0 : 1);
2413 if ((panels_options.mark_moves_down && do_move == MARK_DOWN) || do_move == MARK_FORCE_DOWN)
2414 move_down (panel);
2415 else if (do_move == MARK_FORCE_UP)
2416 move_up (panel);
2419 /* --------------------------------------------------------------------------------------------- */
2421 static inline void
2422 mark_file (WPanel * panel)
2424 do_mark_file (panel, MARK_DOWN);
2427 /* --------------------------------------------------------------------------------------------- */
2429 static inline void
2430 mark_file_up (WPanel * panel)
2432 do_mark_file (panel, MARK_FORCE_UP);
2435 /* --------------------------------------------------------------------------------------------- */
2437 static inline void
2438 mark_file_down (WPanel * panel)
2440 do_mark_file (panel, MARK_FORCE_DOWN);
2443 /* --------------------------------------------------------------------------------------------- */
2445 static void
2446 mark_file_right (WPanel * panel)
2448 int lines;
2450 if (state_mark < 0)
2451 state_mark = selection (panel)->f.marked ? 0 : 1;
2453 lines = panel_lines (panel);
2454 lines = min (lines, panel->dir.len - panel->selected - 1);
2455 for (; lines != 0; lines--)
2457 do_file_mark (panel, panel->selected, state_mark);
2458 move_down (panel);
2460 do_file_mark (panel, panel->selected, state_mark);
2463 /* --------------------------------------------------------------------------------------------- */
2465 static void
2466 mark_file_left (WPanel * panel)
2468 int lines;
2470 if (state_mark < 0)
2471 state_mark = selection (panel)->f.marked ? 0 : 1;
2473 lines = panel_lines (panel);
2474 lines = min (lines, panel->selected + 1);
2475 for (; lines != 0; lines--)
2477 do_file_mark (panel, panel->selected, state_mark);
2478 move_up (panel);
2480 do_file_mark (panel, panel->selected, state_mark);
2483 /* --------------------------------------------------------------------------------------------- */
2485 static void
2486 panel_select_unselect_files (WPanel * panel, const char *title, const char *history_name,
2487 gboolean do_select)
2489 int files_only = (panels_options.select_flags & SELECT_FILES_ONLY) != 0;
2490 int case_sens = (panels_options.select_flags & SELECT_MATCH_CASE) != 0;
2491 int shell_patterns = (panels_options.select_flags & SELECT_SHELL_PATTERNS) != 0;
2493 char *reg_exp;
2494 mc_search_t *search;
2495 int i;
2497 quick_widget_t quick_widgets[] = {
2498 /* *INDENT-OFF* */
2499 QUICK_INPUT (INPUT_LAST_TEXT, history_name, &reg_exp, NULL,
2500 FALSE, FALSE, INPUT_COMPLETE_FILENAMES),
2501 QUICK_START_COLUMNS,
2502 QUICK_CHECKBOX (N_("&Files only"), &files_only, NULL),
2503 QUICK_CHECKBOX (N_("&Using shell patterns"), &shell_patterns, NULL),
2504 QUICK_NEXT_COLUMN,
2505 QUICK_CHECKBOX (N_("&Case sensitive"), &case_sens, NULL),
2506 QUICK_STOP_COLUMNS,
2507 QUICK_END
2508 /* *INDENT-ON* */
2511 quick_dialog_t qdlg = {
2512 -1, -1, 50,
2513 title, "[Select/Unselect Files]",
2514 quick_widgets, NULL, NULL
2517 if (quick_dialog (&qdlg) == B_CANCEL)
2518 return;
2520 if (reg_exp == NULL || *reg_exp == '\0')
2522 g_free (reg_exp);
2523 return;
2526 search = mc_search_new (reg_exp, NULL);
2527 search->search_type = (shell_patterns != 0) ? MC_SEARCH_T_GLOB : MC_SEARCH_T_REGEX;
2528 search->is_entire_line = TRUE;
2529 search->is_case_sensitive = case_sens != 0;
2531 for (i = 0; i < panel->dir.len; i++)
2533 if (DIR_IS_DOTDOT (panel->dir.list[i].fname))
2534 continue;
2535 if (S_ISDIR (panel->dir.list[i].st.st_mode) && files_only != 0)
2536 continue;
2538 if (mc_search_run (search, panel->dir.list[i].fname, 0, panel->dir.list[i].fnamelen, NULL))
2539 do_file_mark (panel, i, do_select);
2542 mc_search_free (search);
2543 g_free (reg_exp);
2545 /* result flags */
2546 panels_options.select_flags = 0;
2547 if (case_sens != 0)
2548 panels_options.select_flags |= SELECT_MATCH_CASE;
2549 if (files_only != 0)
2550 panels_options.select_flags |= SELECT_FILES_ONLY;
2551 if (shell_patterns != 0)
2552 panels_options.select_flags |= SELECT_SHELL_PATTERNS;
2555 /* --------------------------------------------------------------------------------------------- */
2557 static void
2558 panel_select_files (WPanel * panel)
2560 panel_select_unselect_files (panel, _("Select"), ":select_cmd: Select ", TRUE);
2563 /* --------------------------------------------------------------------------------------------- */
2565 static void
2566 panel_unselect_files (WPanel * panel)
2568 panel_select_unselect_files (panel, _("Unselect"), ":unselect_cmd: Unselect ", FALSE);
2571 /* --------------------------------------------------------------------------------------------- */
2573 static void
2574 panel_select_invert_files (WPanel * panel)
2576 int i;
2578 for (i = 0; i < panel->dir.len; i++)
2580 file_entry_t *file = &panel->dir.list[i];
2582 if (!panels_options.reverse_files_only || !S_ISDIR (file->st.st_mode))
2583 do_file_mark (panel, i, !file->f.marked);
2587 /* --------------------------------------------------------------------------------------------- */
2588 /** Incremental search of a file name in the panel.
2589 * @param panel instance of WPanel structure
2590 * @param c_code key code
2593 static void
2594 do_search (WPanel * panel, int c_code)
2596 size_t l;
2597 int i, sel;
2598 gboolean wrapped = FALSE;
2599 char *act;
2600 mc_search_t *search;
2601 char *reg_exp, *esc_str;
2602 gboolean is_found = FALSE;
2604 l = strlen (panel->search_buffer);
2605 if (c_code == KEY_BACKSPACE)
2607 if (l != 0)
2609 act = panel->search_buffer + l;
2610 str_prev_noncomb_char (&act, panel->search_buffer);
2611 act[0] = '\0';
2613 panel->search_chpoint = 0;
2615 else
2617 if (c_code != 0 && (gsize) panel->search_chpoint < sizeof (panel->search_char))
2619 panel->search_char[panel->search_chpoint] = c_code;
2620 panel->search_chpoint++;
2623 if (panel->search_chpoint > 0)
2625 switch (str_is_valid_char (panel->search_char, panel->search_chpoint))
2627 case -2:
2628 return;
2629 case -1:
2630 panel->search_chpoint = 0;
2631 return;
2632 default:
2633 if (l + panel->search_chpoint < sizeof (panel->search_buffer))
2635 memcpy (panel->search_buffer + l, panel->search_char, panel->search_chpoint);
2636 l += panel->search_chpoint;
2637 *(panel->search_buffer + l) = '\0';
2638 panel->search_chpoint = 0;
2644 reg_exp = g_strdup_printf ("%s*", panel->search_buffer);
2645 esc_str = strutils_escape (reg_exp, -1, ",|\\{}[]", TRUE);
2646 search = mc_search_new (esc_str, NULL);
2647 search->search_type = MC_SEARCH_T_GLOB;
2648 search->is_entire_line = TRUE;
2650 switch (panels_options.qsearch_mode)
2652 case QSEARCH_CASE_SENSITIVE:
2653 search->is_case_sensitive = TRUE;
2654 break;
2655 case QSEARCH_CASE_INSENSITIVE:
2656 search->is_case_sensitive = FALSE;
2657 break;
2658 default:
2659 search->is_case_sensitive = panel->sort_info.case_sensitive;
2660 break;
2663 sel = panel->selected;
2665 for (i = panel->selected; !wrapped || i != panel->selected; i++)
2667 if (i >= panel->dir.len)
2669 i = 0;
2670 if (wrapped)
2671 break;
2672 wrapped = TRUE;
2674 if (mc_search_run (search, panel->dir.list[i].fname, 0, panel->dir.list[i].fnamelen, NULL))
2676 sel = i;
2677 is_found = TRUE;
2678 break;
2681 if (is_found)
2683 unselect_item (panel);
2684 panel->selected = sel;
2685 select_item (panel);
2686 widget_redraw (WIDGET (panel));
2688 else if (c_code != KEY_BACKSPACE)
2690 act = panel->search_buffer + l;
2691 str_prev_noncomb_char (&act, panel->search_buffer);
2692 act[0] = '\0';
2694 mc_search_free (search);
2695 g_free (reg_exp);
2696 g_free (esc_str);
2699 /* --------------------------------------------------------------------------------------------- */
2700 /** Start new search.
2701 * @param panel instance of WPanel structure
2704 static void
2705 start_search (WPanel * panel)
2707 if (panel->searching)
2709 if (panel->selected + 1 == panel->dir.len)
2710 panel->selected = 0;
2711 else
2712 move_down (panel);
2714 /* in case if there was no search string we need to recall
2715 previous string, with which we ended previous searching */
2716 if (panel->search_buffer[0] == '\0')
2717 g_strlcpy (panel->search_buffer, panel->prev_search_buffer,
2718 sizeof (panel->search_buffer));
2720 do_search (panel, 0);
2722 else
2724 panel->searching = TRUE;
2725 panel->search_buffer[0] = '\0';
2726 panel->search_char[0] = '\0';
2727 panel->search_chpoint = 0;
2728 display_mini_info (panel);
2729 mc_refresh ();
2733 /* --------------------------------------------------------------------------------------------- */
2735 static void
2736 stop_search (WPanel * panel)
2738 panel->searching = FALSE;
2740 /* if user had overrdied search string, we need to store it
2741 to the previous_search_buffer */
2742 if (panel->search_buffer[0] != '\0')
2743 g_strlcpy (panel->prev_search_buffer, panel->search_buffer,
2744 sizeof (panel->prev_search_buffer));
2746 display_mini_info (panel);
2749 /* --------------------------------------------------------------------------------------------- */
2750 /** Return TRUE if the Enter key has been processed, FALSE otherwise */
2752 static gboolean
2753 do_enter_on_file_entry (file_entry_t * fe)
2755 vfs_path_t *full_name_vpath;
2756 gboolean ok;
2759 * Directory or link to directory - change directory.
2760 * Try the same for the entries on which mc_lstat() has failed.
2762 if (S_ISDIR (fe->st.st_mode) || link_isdir (fe) || (fe->st.st_mode == 0))
2764 vfs_path_t *fname_vpath;
2766 fname_vpath = vfs_path_from_str (fe->fname);
2767 if (!do_cd (fname_vpath, cd_exact))
2768 message (D_ERROR, MSG_ERROR, _("Cannot change directory"));
2769 vfs_path_free (fname_vpath);
2770 return TRUE;
2773 full_name_vpath = vfs_path_append_new (current_panel->cwd_vpath, fe->fname, NULL);
2775 /* Try associated command */
2776 ok = regex_command (full_name_vpath, "Open") != 0;
2777 vfs_path_free (full_name_vpath);
2778 if (ok)
2779 return TRUE;
2781 /* Check if the file is executable */
2782 full_name_vpath = vfs_path_append_new (current_panel->cwd_vpath, fe->fname, NULL);
2783 ok = (is_exe (fe->st.st_mode) && if_link_is_exe (full_name_vpath, fe));
2784 vfs_path_free (full_name_vpath);
2785 if (!ok)
2786 return FALSE;
2788 if (confirm_execute)
2790 if (query_dialog
2791 (_("The Midnight Commander"),
2792 _("Do you really want to execute?"), D_NORMAL, 2, _("&Yes"), _("&No")) != 0)
2793 return TRUE;
2796 if (!vfs_current_is_local ())
2798 int ret;
2799 vfs_path_t *tmp_vpath;
2801 tmp_vpath = vfs_path_append_new (vfs_get_raw_current_dir (), fe->fname, NULL);
2802 ret = mc_setctl (tmp_vpath, VFS_SETCTL_RUN, NULL);
2803 vfs_path_free (tmp_vpath);
2804 /* We took action only if the dialog was shown or the execution
2805 * was successful */
2806 return confirm_execute || (ret == 0);
2810 char *tmp, *cmd;
2812 tmp = name_quote (fe->fname, FALSE);
2813 cmd = g_strconcat (".", PATH_SEP_STR, tmp, (char *) NULL);
2814 g_free (tmp);
2815 shell_execute (cmd, 0);
2816 g_free (cmd);
2819 #ifdef HAVE_CHARSET
2820 mc_global.source_codepage = default_source_codepage;
2821 #endif
2823 return TRUE;
2826 /* --------------------------------------------------------------------------------------------- */
2828 static inline gboolean
2829 do_enter (WPanel * panel)
2831 return do_enter_on_file_entry (selection (panel));
2834 /* --------------------------------------------------------------------------------------------- */
2836 static void
2837 chdir_other_panel (WPanel * panel)
2839 const file_entry_t *entry = &panel->dir.list[panel->selected];
2841 vfs_path_t *new_dir_vpath;
2842 char *sel_entry = NULL;
2844 if (get_other_type () != view_listing)
2845 set_display_type (get_other_index (), view_listing);
2847 if (S_ISDIR (entry->st.st_mode) || entry->f.link_to_dir)
2848 new_dir_vpath = vfs_path_append_new (panel->cwd_vpath, entry->fname, NULL);
2849 else
2851 new_dir_vpath = vfs_path_append_new (panel->cwd_vpath, "..", (char *) NULL);
2852 sel_entry = strrchr (vfs_path_get_last_path_str (panel->cwd_vpath), PATH_SEP);
2855 change_panel ();
2856 do_cd (new_dir_vpath, cd_exact);
2857 vfs_path_free (new_dir_vpath);
2859 if (sel_entry)
2860 try_to_select (current_panel, sel_entry);
2861 change_panel ();
2863 move_down (panel);
2866 /* --------------------------------------------------------------------------------------------- */
2868 * Make the current directory of the current panel also the current
2869 * directory of the other panel. Put the other panel to the listing
2870 * mode if needed. If the current panel is panelized, the other panel
2871 * doesn't become panelized.
2874 static void
2875 panel_sync_other (const WPanel * panel)
2877 if (get_other_type () != view_listing)
2878 set_display_type (get_other_index (), view_listing);
2880 do_panel_cd (other_panel, current_panel->cwd_vpath, cd_exact);
2882 /* try to select current filename on the other panel */
2883 if (!panel->is_panelized)
2884 try_to_select (other_panel, selection (panel)->fname);
2887 /* --------------------------------------------------------------------------------------------- */
2889 static void
2890 chdir_to_readlink (WPanel * panel)
2892 vfs_path_t *new_dir_vpath;
2893 char buffer[MC_MAXPATHLEN];
2894 int i;
2895 struct stat st;
2896 vfs_path_t *panel_fname_vpath;
2897 gboolean ok;
2899 if (get_other_type () != view_listing)
2900 return;
2902 if (!S_ISLNK (panel->dir.list[panel->selected].st.st_mode))
2903 return;
2905 i = readlink (selection (panel)->fname, buffer, MC_MAXPATHLEN - 1);
2906 if (i < 0)
2907 return;
2909 panel_fname_vpath = vfs_path_from_str (selection (panel)->fname);
2910 ok = (mc_stat (panel_fname_vpath, &st) >= 0);
2911 vfs_path_free (panel_fname_vpath);
2912 if (!ok)
2913 return;
2915 buffer[i] = 0;
2916 if (!S_ISDIR (st.st_mode))
2918 char *p;
2920 p = strrchr (buffer, PATH_SEP);
2921 if (p && !p[1])
2923 *p = 0;
2924 p = strrchr (buffer, PATH_SEP);
2926 if (!p)
2927 return;
2928 p[1] = 0;
2930 if (IS_PATH_SEP (*buffer))
2931 new_dir_vpath = vfs_path_from_str (buffer);
2932 else
2933 new_dir_vpath = vfs_path_append_new (panel->cwd_vpath, buffer, NULL);
2935 change_panel ();
2936 do_cd (new_dir_vpath, cd_exact);
2937 vfs_path_free (new_dir_vpath);
2938 change_panel ();
2940 move_down (panel);
2943 /* --------------------------------------------------------------------------------------------- */
2945 static gsize
2946 panel_get_format_field_count (WPanel * panel)
2948 format_e *format;
2949 gsize lc_index;
2951 for (lc_index = 0, format = panel->format; format != NULL; format = format->next, lc_index++)
2954 return lc_index;
2957 /* --------------------------------------------------------------------------------------------- */
2959 function return 0 if not found and REAL_INDEX+1 if found
2962 static gsize
2963 panel_get_format_field_index_by_name (WPanel * panel, const char *name)
2965 format_e *format;
2966 gsize lc_index;
2968 for (lc_index = 1, format = panel->format;
2969 format != NULL && strcmp (format->title, name) != 0; format = format->next, lc_index++)
2972 if (format == NULL)
2973 lc_index = 0;
2975 return lc_index;
2978 /* --------------------------------------------------------------------------------------------- */
2980 static format_e *
2981 panel_get_format_field_by_index (WPanel * panel, gsize lc_index)
2983 format_e *format;
2985 for (format = panel->format; format != NULL && lc_index != 0; format = format->next, lc_index--)
2988 return format;
2991 /* --------------------------------------------------------------------------------------------- */
2993 static const panel_field_t *
2994 panel_get_sortable_field_by_format (WPanel * panel, gsize lc_index)
2996 const panel_field_t *pfield;
2997 format_e *format;
2999 format = panel_get_format_field_by_index (panel, lc_index);
3000 if (format == NULL)
3001 return NULL;
3003 pfield = panel_get_field_by_title (format->title);
3004 if (pfield == NULL)
3005 return NULL;
3006 if (pfield->sort_routine == NULL)
3007 return NULL;
3008 return pfield;
3011 /* --------------------------------------------------------------------------------------------- */
3013 static void
3014 panel_toggle_sort_order_prev (WPanel * panel)
3016 gsize lc_index, i;
3017 const char *title;
3018 const panel_field_t *pfield = NULL;
3020 title = panel_get_title_without_hotkey (panel->sort_field->title_hotkey);
3021 lc_index = panel_get_format_field_index_by_name (panel, title);
3023 if (lc_index > 1)
3025 /* search for prev sortable column in panel format */
3026 for (i = lc_index - 1;
3027 i != 0 && (pfield = panel_get_sortable_field_by_format (panel, i - 1)) == NULL; i--)
3031 if (pfield == NULL)
3033 /* Sortable field not found. Try to search in each array */
3034 for (i = panel_get_format_field_count (panel);
3035 i != 0 && (pfield = panel_get_sortable_field_by_format (panel, i - 1)) == NULL; i--)
3039 if (pfield != NULL)
3041 panel->sort_field = pfield;
3042 panel_set_sort_order (panel, pfield);
3046 /* --------------------------------------------------------------------------------------------- */
3048 static void
3049 panel_toggle_sort_order_next (WPanel * panel)
3051 gsize lc_index, i;
3052 const panel_field_t *pfield = NULL;
3053 gsize format_field_count;
3054 const char *title;
3056 format_field_count = panel_get_format_field_count (panel);
3057 title = panel_get_title_without_hotkey (panel->sort_field->title_hotkey);
3058 lc_index = panel_get_format_field_index_by_name (panel, title);
3060 if (lc_index != 0 && lc_index != format_field_count)
3062 /* search for prev sortable column in panel format */
3063 for (i = lc_index;
3064 i != format_field_count
3065 && (pfield = panel_get_sortable_field_by_format (panel, i)) == NULL; i++)
3069 if (pfield == NULL)
3071 /* Sortable field not found. Try to search in each array */
3072 for (i = 0;
3073 i != format_field_count
3074 && (pfield = panel_get_sortable_field_by_format (panel, i)) == NULL; i++)
3078 if (pfield != NULL)
3080 panel->sort_field = pfield;
3081 panel_set_sort_order (panel, pfield);
3085 /* --------------------------------------------------------------------------------------------- */
3087 static void
3088 panel_select_sort_order (WPanel * panel)
3090 const panel_field_t *sort_order;
3092 sort_order = sort_box (&panel->sort_info, panel->sort_field);
3093 if (sort_order != NULL)
3095 panel->sort_field = sort_order;
3096 panel_set_sort_order (panel, sort_order);
3100 /* --------------------------------------------------------------------------------------------- */
3103 * panel_content_scroll_left:
3104 * @param panel the pointer to the panel on which we operate
3106 * scroll long filename to the left (decrement scroll pointer)
3110 static void
3111 panel_content_scroll_left (WPanel * panel)
3113 if (panel->content_shift > -1)
3115 if (panel->content_shift > panel->max_shift)
3116 panel->content_shift = panel->max_shift;
3118 panel->content_shift--;
3119 show_dir (panel);
3120 paint_dir (panel);
3124 /* --------------------------------------------------------------------------------------------- */
3127 * panel_content_scroll_right:
3128 * @param panel the pointer to the panel on which we operate
3130 * scroll long filename to the right (increment scroll pointer)
3134 static void
3135 panel_content_scroll_right (WPanel * panel)
3137 if (panel->content_shift < 0 || panel->content_shift < panel->max_shift)
3139 panel->content_shift++;
3140 show_dir (panel);
3141 paint_dir (panel);
3145 /* --------------------------------------------------------------------------------------------- */
3147 static void
3148 panel_set_sort_type_by_id (WPanel * panel, const char *name)
3150 if (strcmp (panel->sort_field->id, name) != 0)
3152 const panel_field_t *sort_order;
3154 sort_order = panel_get_field_by_id (name);
3155 if (sort_order == NULL)
3156 return;
3158 panel->sort_field = sort_order;
3160 else
3161 panel->sort_info.reverse = !panel->sort_info.reverse;
3163 panel_set_sort_order (panel, panel->sort_field);
3166 /* --------------------------------------------------------------------------------------------- */
3168 * If we moved to the parent directory move the selection pointer to
3169 * the old directory name; If we leave VFS dir, remove FS specificator.
3171 * You do _NOT_ want to add any vfs aware code here. <pavel@ucw.cz>
3174 static const char *
3175 get_parent_dir_name (const vfs_path_t * cwd_vpath, const vfs_path_t * lwd_vpath)
3177 size_t llen, clen;
3178 const char *p, *cwd, *lwd;
3180 llen = vfs_path_len (lwd_vpath);
3181 clen = vfs_path_len (cwd_vpath);
3183 if (llen <= clen)
3184 return NULL;
3186 cwd = vfs_path_as_str (cwd_vpath);
3187 lwd = vfs_path_as_str (lwd_vpath);
3189 p = g_strrstr (lwd, VFS_PATH_URL_DELIMITER);
3191 if (p == NULL)
3193 p = strrchr (lwd, PATH_SEP);
3195 if ((p != NULL)
3196 && (strncmp (cwd, lwd, (size_t) (p - lwd)) == 0)
3197 && (clen == (size_t) (p - lwd) || (p == lwd && IS_PATH_SEP (cwd[0]) && cwd[1] == '\0')))
3198 return (p + 1);
3200 return NULL;
3203 /* skip VFS prefix */
3204 while (--p > lwd && !IS_PATH_SEP (*p))
3206 /* get last component */
3207 while (--p > lwd && !IS_PATH_SEP (*p))
3210 /* return last component */
3211 return (p != lwd || IS_PATH_SEP (*p)) ? p + 1 : p;
3214 /* --------------------------------------------------------------------------------------------- */
3215 /** Wrapper for do_subshell_chdir, check for availability of subshell */
3217 static void
3218 subshell_chdir (const vfs_path_t * vpath)
3220 #ifdef ENABLE_SUBSHELL
3221 if (mc_global.tty.use_subshell && vfs_current_is_local ())
3222 do_subshell_chdir (vpath, FALSE);
3223 #else /* ENABLE_SUBSHELL */
3224 (void) vpath;
3225 #endif /* ENABLE_SUBSHELL */
3228 /* --------------------------------------------------------------------------------------------- */
3230 * Changes the current directory of the panel.
3231 * Don't record change in the directory history.
3234 static gboolean
3235 _do_panel_cd (WPanel * panel, const vfs_path_t * new_dir_vpath, enum cd_enum cd_type)
3237 vfs_path_t *olddir_vpath;
3239 /* Convert *new_path to a suitable pathname, handle ~user */
3240 if (cd_type == cd_parse_command)
3242 const vfs_path_element_t *element;
3244 element = vfs_path_get_by_index (new_dir_vpath, 0);
3245 if (strcmp (element->path, "-") == 0)
3246 new_dir_vpath = panel->lwd_vpath;
3249 if (mc_chdir (new_dir_vpath) == -1)
3250 return FALSE;
3252 /* Success: save previous directory, shutdown status of previous dir */
3253 olddir_vpath = vfs_path_clone (panel->cwd_vpath);
3254 panel_set_lwd (panel, panel->cwd_vpath);
3255 input_free_completions (cmdline);
3257 vfs_path_free (panel->cwd_vpath);
3258 vfs_setup_cwd ();
3259 panel->cwd_vpath = vfs_path_clone (vfs_get_raw_current_dir ());
3261 vfs_release_path (olddir_vpath);
3263 subshell_chdir (panel->cwd_vpath);
3265 /* Reload current panel */
3266 panel_clean_dir (panel);
3268 dir_list_load (&panel->dir, panel->cwd_vpath, panel->sort_field->sort_routine,
3269 &panel->sort_info, panel->filter);
3270 try_to_select (panel, get_parent_dir_name (panel->cwd_vpath, olddir_vpath));
3272 load_hint (FALSE);
3273 panel->dirty = 1;
3274 update_xterm_title_path ();
3276 vfs_path_free (olddir_vpath);
3278 return TRUE;
3281 /* --------------------------------------------------------------------------------------------- */
3283 static void
3284 directory_history_next (WPanel * panel)
3286 gboolean ok;
3290 GList *next;
3292 ok = TRUE;
3293 next = g_list_next (panel->dir_history_current);
3294 if (next != NULL)
3296 vfs_path_t *data_vpath;
3298 data_vpath = vfs_path_from_str ((char *) next->data);
3299 ok = _do_panel_cd (panel, data_vpath, cd_exact);
3300 vfs_path_free (data_vpath);
3301 panel->dir_history_current = next;
3303 /* skip directories that present in history but absent in file system */
3305 while (!ok);
3308 /* --------------------------------------------------------------------------------------------- */
3310 static void
3311 directory_history_prev (WPanel * panel)
3313 gboolean ok;
3317 GList *prev;
3319 ok = TRUE;
3320 prev = g_list_previous (panel->dir_history_current);
3321 if (prev != NULL)
3323 vfs_path_t *data_vpath;
3325 data_vpath = vfs_path_from_str ((char *) prev->data);
3326 ok = _do_panel_cd (panel, data_vpath, cd_exact);
3327 vfs_path_free (data_vpath);
3328 panel->dir_history_current = prev;
3330 /* skip directories that present in history but absent in file system */
3332 while (!ok);
3335 /* --------------------------------------------------------------------------------------------- */
3337 static void
3338 directory_history_list (WPanel * panel)
3340 char *s;
3341 gboolean ok = FALSE;
3342 size_t pos;
3344 pos = g_list_position (panel->dir_history_current, panel->dir_history);
3346 s = history_show (&panel->dir_history, WIDGET (panel), pos);
3347 if (s != NULL)
3349 vfs_path_t *s_vpath;
3351 s_vpath = vfs_path_from_str (s);
3352 ok = _do_panel_cd (panel, s_vpath, cd_exact);
3353 if (ok)
3354 directory_history_add (panel, panel->cwd_vpath);
3355 else
3356 message (D_ERROR, MSG_ERROR, _("Cannot change directory"));
3357 vfs_path_free (s_vpath);
3358 g_free (s);
3361 if (!ok)
3363 /* Since history is fully modified in history_show(), panel->dir_history actually
3364 * points to the invalid place. Try restore current postition here. */
3366 size_t i;
3368 panel->dir_history_current = panel->dir_history;
3370 for (i = 0; i <= pos; i++)
3372 GList *prev;
3374 prev = g_list_previous (panel->dir_history_current);
3375 if (prev == NULL)
3376 break;
3378 panel->dir_history_current = prev;
3383 /* --------------------------------------------------------------------------------------------- */
3385 static cb_ret_t
3386 panel_execute_cmd (WPanel * panel, long command)
3388 int res = MSG_HANDLED;
3390 if (command != CK_Search)
3391 stop_search (panel);
3393 switch (command)
3395 case CK_Up:
3396 case CK_Down:
3397 case CK_Left:
3398 case CK_Right:
3399 case CK_Bottom:
3400 case CK_Top:
3401 case CK_PageDown:
3402 case CK_PageUp:
3403 /* reset state of marks flag */
3404 state_mark = -1;
3405 break;
3406 default:
3407 break;
3410 switch (command)
3412 case CK_PanelOtherCd:
3413 chdir_other_panel (panel);
3414 break;
3415 case CK_PanelOtherCdLink:
3416 chdir_to_readlink (panel);
3417 break;
3418 case CK_CopySingle:
3419 copy_cmd_local ();
3420 break;
3421 case CK_DeleteSingle:
3422 delete_cmd_local ();
3423 break;
3424 case CK_Enter:
3425 do_enter (panel);
3426 break;
3427 case CK_ViewRaw:
3428 view_raw_cmd ();
3429 break;
3430 case CK_EditNew:
3431 edit_cmd_new ();
3432 break;
3433 case CK_MoveSingle:
3434 rename_cmd_local ();
3435 break;
3436 case CK_SelectInvert:
3437 panel_select_invert_files (panel);
3438 break;
3439 case CK_Select:
3440 panel_select_files (panel);
3441 break;
3442 case CK_SelectExt:
3443 panel_select_ext_cmd ();
3444 break;
3445 case CK_Unselect:
3446 panel_unselect_files (panel);
3447 break;
3448 case CK_PageDown:
3449 next_page (panel);
3450 break;
3451 case CK_PageUp:
3452 prev_page (panel);
3453 break;
3454 case CK_CdChild:
3455 goto_child_dir (panel);
3456 break;
3457 case CK_CdParent:
3458 goto_parent_dir (panel);
3459 break;
3460 case CK_History:
3461 directory_history_list (panel);
3462 break;
3463 case CK_HistoryNext:
3464 directory_history_next (panel);
3465 break;
3466 case CK_HistoryPrev:
3467 directory_history_prev (panel);
3468 break;
3469 case CK_BottomOnScreen:
3470 goto_bottom_file (panel);
3471 break;
3472 case CK_MiddleOnScreen:
3473 goto_middle_file (panel);
3474 break;
3475 case CK_TopOnScreen:
3476 goto_top_file (panel);
3477 break;
3478 case CK_Mark:
3479 mark_file (panel);
3480 break;
3481 case CK_MarkUp:
3482 mark_file_up (panel);
3483 break;
3484 case CK_MarkDown:
3485 mark_file_down (panel);
3486 break;
3487 case CK_MarkLeft:
3488 mark_file_left (panel);
3489 break;
3490 case CK_MarkRight:
3491 mark_file_right (panel);
3492 break;
3493 case CK_CdParentSmart:
3494 res = force_maybe_cd ();
3495 break;
3496 case CK_Up:
3497 move_up (panel);
3498 break;
3499 case CK_Down:
3500 move_down (panel);
3501 break;
3502 case CK_Left:
3503 res = move_left (panel);
3504 break;
3505 case CK_Right:
3506 res = move_right (panel);
3507 break;
3508 case CK_Bottom:
3509 move_end (panel);
3510 break;
3511 case CK_Top:
3512 move_home (panel);
3513 break;
3514 #ifdef HAVE_CHARSET
3515 case CK_SelectCodepage:
3516 panel_change_encoding (panel);
3517 break;
3518 #endif
3519 case CK_ScrollLeft:
3520 panel_content_scroll_left (panel);
3521 break;
3522 case CK_ScrollRight:
3523 panel_content_scroll_right (panel);
3524 break;
3525 case CK_Search:
3526 start_search (panel);
3527 break;
3528 case CK_SearchStop:
3529 break;
3530 case CK_PanelOtherSync:
3531 panel_sync_other (panel);
3532 break;
3533 case CK_Sort:
3534 panel_select_sort_order (panel);
3535 break;
3536 case CK_SortPrev:
3537 panel_toggle_sort_order_prev (panel);
3538 break;
3539 case CK_SortNext:
3540 panel_toggle_sort_order_next (panel);
3541 break;
3542 case CK_SortReverse:
3543 panel->sort_info.reverse = !panel->sort_info.reverse;
3544 panel_set_sort_order (panel, panel->sort_field);
3545 break;
3546 case CK_SortByName:
3547 panel_set_sort_type_by_id (panel, "name");
3548 break;
3549 case CK_SortByExt:
3550 panel_set_sort_type_by_id (panel, "extension");
3551 break;
3552 case CK_SortBySize:
3553 panel_set_sort_type_by_id (panel, "size");
3554 break;
3555 case CK_SortByMTime:
3556 panel_set_sort_type_by_id (panel, "mtime");
3557 break;
3558 default:
3559 res = MSG_NOT_HANDLED;
3560 break;
3563 return res;
3566 /* --------------------------------------------------------------------------------------------- */
3568 static cb_ret_t
3569 panel_key (WPanel * panel, int key)
3571 size_t i;
3573 if (is_abort_char (key))
3575 stop_search (panel);
3576 return MSG_HANDLED;
3579 if (panel->searching && ((key >= ' ' && key <= 255) || key == KEY_BACKSPACE))
3581 do_search (panel, key);
3582 return MSG_HANDLED;
3585 for (i = 0; panel_map[i].key != 0; i++)
3586 if (key == panel_map[i].key)
3587 return panel_execute_cmd (panel, panel_map[i].command);
3589 if (panels_options.torben_fj_mode && key == ALT ('h'))
3591 goto_middle_file (panel);
3592 return MSG_HANDLED;
3595 if (!command_prompt && ((key >= ' ' && key <= 255) || key == KEY_BACKSPACE))
3597 start_search (panel);
3598 do_search (panel, key);
3599 return MSG_HANDLED;
3602 return MSG_NOT_HANDLED;
3605 /* --------------------------------------------------------------------------------------------- */
3607 static cb_ret_t
3608 panel_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
3610 WPanel *panel = PANEL (w);
3611 WButtonBar *bb;
3613 switch (msg)
3615 case MSG_INIT:
3616 /* subscribe to "history_load" event */
3617 mc_event_add (w->owner->event_group, MCEVENT_HISTORY_LOAD, panel_load_history, w, NULL);
3618 /* subscribe to "history_save" event */
3619 mc_event_add (w->owner->event_group, MCEVENT_HISTORY_SAVE, panel_save_history, w, NULL);
3620 return MSG_HANDLED;
3622 case MSG_DRAW:
3623 /* Repaint everything, including frame and separator */
3624 widget_erase (w);
3625 show_dir (panel);
3626 panel_print_header (panel);
3627 adjust_top_file (panel);
3628 paint_dir (panel);
3629 mini_info_separator (panel);
3630 display_mini_info (panel);
3631 panel->dirty = 0;
3632 return MSG_HANDLED;
3634 case MSG_FOCUS:
3635 state_mark = -1;
3636 current_panel = panel;
3637 panel->active = 1;
3639 if (mc_chdir (panel->cwd_vpath) != 0)
3641 char *cwd;
3643 cwd = vfs_path_to_str_flags (panel->cwd_vpath, 0, VPF_STRIP_PASSWORD);
3644 message (D_ERROR, MSG_ERROR, _("Cannot chdir to \"%s\"\n%s"),
3645 cwd, unix_error_string (errno));
3646 g_free (cwd);
3648 else
3649 subshell_chdir (panel->cwd_vpath);
3651 update_xterm_title_path ();
3652 select_item (panel);
3653 show_dir (panel);
3654 paint_dir (panel);
3655 panel->dirty = 0;
3657 bb = find_buttonbar (w->owner);
3658 midnight_set_buttonbar (bb);
3659 widget_redraw (WIDGET (bb));
3660 return MSG_HANDLED;
3662 case MSG_UNFOCUS:
3663 /* Janne: look at this for the multiple panel options */
3664 stop_search (panel);
3665 panel->active = 0;
3666 show_dir (panel);
3667 unselect_item (panel);
3668 return MSG_HANDLED;
3670 case MSG_KEY:
3671 return panel_key (panel, parm);
3673 case MSG_ACTION:
3674 return panel_execute_cmd (panel, parm);
3676 case MSG_DESTROY:
3677 /* unsubscribe from "history_load" event */
3678 mc_event_del (w->owner->event_group, MCEVENT_HISTORY_LOAD, panel_load_history, w);
3679 /* unsubscribe from "history_save" event */
3680 mc_event_del (w->owner->event_group, MCEVENT_HISTORY_SAVE, panel_save_history, w);
3681 panel_destroy (panel);
3682 free_my_statfs ();
3683 return MSG_HANDLED;
3685 default:
3686 return widget_default_callback (w, sender, msg, parm, data);
3690 /* --------------------------------------------------------------------------------------------- */
3691 /* */
3692 /* Panel mouse events support routines */
3693 /* */
3695 static void
3696 mouse_toggle_mark (WPanel * panel)
3698 do_mark_file (panel, MARK_DONT_MOVE);
3699 mouse_marking = selection (panel)->f.marked;
3700 mouse_mark_panel = current_panel;
3703 /* --------------------------------------------------------------------------------------------- */
3705 static void
3706 mouse_set_mark (WPanel * panel)
3708 if (mouse_mark_panel == panel)
3710 if (mouse_marking && !(selection (panel)->f.marked))
3711 do_mark_file (panel, MARK_DONT_MOVE);
3712 else if (!mouse_marking && (selection (panel)->f.marked))
3713 do_mark_file (panel, MARK_DONT_MOVE);
3717 /* --------------------------------------------------------------------------------------------- */
3719 static gboolean
3720 mark_if_marking (WPanel * panel, const mouse_event_t * event)
3722 if ((event->buttons & GPM_B_RIGHT) != 0)
3724 if (event->msg == MSG_MOUSE_DOWN)
3725 mouse_toggle_mark (panel);
3726 else
3727 mouse_set_mark (panel);
3728 return TRUE;
3731 return FALSE;
3734 /* --------------------------------------------------------------------------------------------- */
3735 /** Determine which column was clicked, and sort the panel on
3736 * that column, or reverse sort on that column if already
3737 * sorted on that column.
3740 static void
3741 mouse_sort_col (WPanel * panel, int x)
3743 int i;
3744 const char *lc_sort_name = NULL;
3745 panel_field_t *col_sort_format = NULL;
3746 format_e *format;
3748 for (i = 0, format = panel->format; format != NULL; format = format->next)
3750 i += format->field_len;
3751 if (x < i + 1)
3753 /* found column */
3754 lc_sort_name = format->title;
3755 break;
3759 if (lc_sort_name == NULL)
3760 return;
3762 for (i = 0; panel_fields[i].id != NULL; i++)
3764 const char *title;
3766 title = panel_get_title_without_hotkey (panel_fields[i].title_hotkey);
3767 if (strcmp (title, lc_sort_name) == 0 && panel_fields[i].sort_routine != NULL)
3769 col_sort_format = &panel_fields[i];
3770 break;
3774 if (col_sort_format == NULL)
3775 return;
3777 if (panel->sort_field == col_sort_format)
3779 /* reverse the sort if clicked column is already the sorted column */
3780 panel->sort_info.reverse = !panel->sort_info.reverse;
3782 else
3784 /* new sort is forced to be ascending */
3785 panel->sort_info.reverse = FALSE;
3787 panel_set_sort_order (panel, col_sort_format);
3790 /* --------------------------------------------------------------------------------------------- */
3792 static void
3793 panel_mouse_callback (Widget * w, mouse_msg_t msg, mouse_event_t * event)
3795 WPanel *panel = PANEL (w);
3796 gboolean is_active;
3798 is_active = widget_is_active (w);
3800 switch (msg)
3802 case MSG_MOUSE_DOWN:
3803 if (event->y == 0)
3805 /* top frame */
3806 if (event->x == 1)
3807 /* "<" button */
3808 directory_history_prev (panel);
3809 else if (event->x == w->cols - 2)
3810 /* ">" button */
3811 directory_history_next (panel);
3812 else if (event->x >= w->cols - 5 && event->x <= w->cols - 3)
3813 /* "^" button */
3814 directory_history_list (panel);
3815 else if (event->x == w->cols - 6)
3816 /* "." button show/hide hidden files */
3817 send_message (midnight_dlg, NULL, MSG_ACTION, CK_ShowHidden, NULL);
3818 else
3820 /* no other events on 1st line, return MOU_UNHANDLED */
3821 event->result.abort = TRUE;
3822 /* avoid extra panel redraw */
3823 panel->dirty = 0;
3825 break;
3828 if (event->y == 1)
3830 /* sort on clicked column */
3831 mouse_sort_col (panel, event->x + 1);
3832 break;
3835 if (!is_active)
3836 change_panel ();
3837 /* fall through */
3839 case MSG_MOUSE_DRAG:
3841 int y, last, my_index;
3843 last = panel->dir.len - 1;
3844 y = event->y - 2;
3846 if (panel->top_file + y > last)
3847 my_index = last;
3848 else
3850 my_index = panel->top_file + y;
3852 if (panel->list_type == list_brief && panel->list_cols > 1)
3854 int width, lines;
3856 width = (w->cols - 2) / panel->list_cols;
3857 lines = panel_lines (panel);
3858 my_index += lines * (event->x / width);
3861 if (my_index > last)
3862 my_index = last;
3865 if (my_index != panel->selected)
3867 unselect_item (panel);
3868 panel->selected = my_index;
3869 select_item (panel);
3872 /* This one is new */
3873 mark_if_marking (panel, event);
3875 break;
3877 case MSG_MOUSE_UP:
3878 break;
3880 case MSG_MOUSE_CLICK:
3881 if ((event->count & GPM_DOUBLE) != 0)
3883 int y, lines;
3885 y = event->y - 1;
3886 lines = panel_lines (panel);
3888 if (y <= lines)
3889 do_enter (panel);
3891 break;
3893 case MSG_MOUSE_MOVE:
3894 break;
3896 case MSG_MOUSE_SCROLL_UP:
3897 if (is_active)
3899 if (panels_options.mouse_move_pages && panel->top_file > 0)
3900 prev_page (panel);
3901 else /* We are in first page */
3902 move_up (panel);
3904 break;
3906 case MSG_MOUSE_SCROLL_DOWN:
3907 if (is_active)
3909 if (panels_options.mouse_move_pages
3910 && panel->top_file + panel_items (panel) < panel->dir.len)
3911 next_page (panel);
3912 else /* We are in last page */
3913 move_down (panel);
3915 break;
3917 default:
3918 break;
3921 if (panel->dirty)
3922 widget_redraw (w);
3925 /* --------------------------------------------------------------------------------------------- */
3927 static void
3928 reload_panelized (WPanel * panel)
3930 int i, j;
3931 dir_list *list = &panel->dir;
3933 /* refresh current VFS directory required for vfs_path_from_str() */
3934 (void) mc_chdir (panel->cwd_vpath);
3936 for (i = 0, j = 0; i < list->len; i++)
3938 vfs_path_t *vpath;
3940 if (list->list[i].f.marked)
3942 /* Unmark the file in advance. In case the following mc_lstat
3943 * fails we are done, else we have to mark the file again
3944 * (Note: do_file_mark depends on a valid "list->list [i].buf").
3945 * IMO that's the best way to update the panel's summary status
3946 * -- Norbert
3948 do_file_mark (panel, i, 0);
3950 vpath = vfs_path_from_str (list->list[i].fname);
3951 if (mc_lstat (vpath, &list->list[i].st) != 0)
3952 g_free (list->list[i].fname);
3953 else
3955 if (list->list[i].f.marked)
3956 do_file_mark (panel, i, 1);
3957 if (j != i)
3958 list->list[j] = list->list[i];
3959 j++;
3961 vfs_path_free (vpath);
3963 if (j == 0)
3964 dir_list_init (list);
3965 else
3966 list->len = j;
3968 if (panel != current_panel)
3969 (void) mc_chdir (current_panel->cwd_vpath);
3972 /* --------------------------------------------------------------------------------------------- */
3974 static void
3975 update_one_panel_widget (WPanel * panel, panel_update_flags_t flags, const char *current_file)
3977 gboolean free_pointer;
3978 char *my_current_file = NULL;
3980 if ((flags & UP_RELOAD) != 0)
3982 panel->is_panelized = FALSE;
3983 mc_setctl (panel->cwd_vpath, VFS_SETCTL_FLUSH, 0);
3984 memset (&(panel->dir_stat), 0, sizeof (panel->dir_stat));
3987 /* If current_file == -1 (an invalid pointer) then preserve selection */
3988 free_pointer = current_file == UP_KEEPSEL;
3990 if (free_pointer)
3992 my_current_file = g_strdup (panel->dir.list[panel->selected].fname);
3993 current_file = my_current_file;
3996 if (panel->is_panelized)
3997 reload_panelized (panel);
3998 else
3999 panel_reload (panel);
4001 try_to_select (panel, current_file);
4002 panel->dirty = 1;
4004 if (free_pointer)
4005 g_free (my_current_file);
4008 /* --------------------------------------------------------------------------------------------- */
4010 static void
4011 update_one_panel (int which, panel_update_flags_t flags, const char *current_file)
4013 if (get_display_type (which) == view_listing)
4015 WPanel *panel;
4017 panel = PANEL (get_panel_widget (which));
4018 if (panel->is_panelized)
4019 flags &= ~UP_RELOAD;
4020 update_one_panel_widget (panel, flags, current_file);
4024 /* --------------------------------------------------------------------------------------------- */
4026 static void
4027 do_select (WPanel * panel, int i)
4029 if (i != panel->selected)
4031 panel->dirty = 1;
4032 panel->selected = i;
4033 panel->top_file = panel->selected - (WIDGET (panel)->lines - 2) / 2;
4034 if (panel->top_file < 0)
4035 panel->top_file = 0;
4039 /* --------------------------------------------------------------------------------------------- */
4041 static void
4042 do_try_to_select (WPanel * panel, const char *name)
4044 int i;
4045 char *subdir;
4047 if (!name)
4049 do_select (panel, 0);
4050 return;
4053 /* We only want the last component of the directory,
4054 * and from this only the name without suffix.
4055 * Cut prefix if the panel is not panelized */
4057 if (panel->is_panelized)
4058 subdir = vfs_strip_suffix_from_filename (name);
4059 else
4060 subdir = vfs_strip_suffix_from_filename (x_basename (name));
4062 /* Search that subdir or filename without prefix (if not panelized panel), select it if found */
4063 for (i = 0; i < panel->dir.len; i++)
4065 if (strcmp (subdir, panel->dir.list[i].fname) == 0)
4067 do_select (panel, i);
4068 g_free (subdir);
4069 return;
4073 /* Try to select a file near the file that is missing */
4074 if (panel->selected >= panel->dir.len)
4075 do_select (panel, panel->dir.len - 1);
4076 g_free (subdir);
4079 /* --------------------------------------------------------------------------------------------- */
4081 /* event callback */
4082 static gboolean
4083 event_update_panels (const gchar * event_group_name, const gchar * event_name,
4084 gpointer init_data, gpointer data)
4086 (void) event_group_name;
4087 (void) event_name;
4088 (void) init_data;
4089 (void) data;
4091 update_panels (UP_RELOAD, UP_KEEPSEL);
4093 return TRUE;
4096 /* --------------------------------------------------------------------------------------------- */
4098 /* event callback */
4099 static gboolean
4100 panel_save_current_file_to_clip_file (const gchar * event_group_name, const gchar * event_name,
4101 gpointer init_data, gpointer data)
4103 (void) event_group_name;
4104 (void) event_name;
4105 (void) init_data;
4106 (void) data;
4108 if (current_panel->marked == 0)
4109 mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_text_to_file",
4110 (gpointer) selection (current_panel)->fname);
4111 else
4113 int i;
4114 gboolean first = TRUE;
4115 char *flist = NULL;
4117 for (i = 0; i < current_panel->dir.len; i++)
4118 if (current_panel->dir.list[i].f.marked != 0)
4119 { /* Skip the unmarked ones */
4120 if (first)
4122 flist = g_strdup (current_panel->dir.list[i].fname);
4123 first = FALSE;
4125 else
4127 /* Add empty lines after the file */
4128 char *tmp;
4130 tmp =
4131 g_strconcat (flist, "\n", current_panel->dir.list[i].fname, (char *) NULL);
4132 g_free (flist);
4133 flist = tmp;
4137 mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_text_to_file", (gpointer) flist);
4138 g_free (flist);
4140 return TRUE;
4143 /* --------------------------------------------------------------------------------------------- */
4145 static vfs_path_t *
4146 panel_recursive_cd_to_parent (const vfs_path_t * vpath)
4148 vfs_path_t *cwd_vpath;
4150 cwd_vpath = vfs_path_clone (vpath);
4152 while (mc_chdir (cwd_vpath) < 0)
4154 const char *panel_cwd_path;
4155 vfs_path_t *tmp_vpath;
4157 /* check if path contains only '/' */
4158 panel_cwd_path = vfs_path_as_str (cwd_vpath);
4159 if (IS_PATH_SEP (panel_cwd_path[0]) && panel_cwd_path[1] == '\0')
4160 return NULL;
4162 tmp_vpath = vfs_path_vtokens_get (cwd_vpath, 0, -1);
4163 vfs_path_free (cwd_vpath);
4164 cwd_vpath = vfs_path_build_filename (PATH_SEP_STR, vfs_path_as_str (tmp_vpath), NULL);
4165 vfs_path_free (tmp_vpath);
4168 return cwd_vpath;
4171 /* --------------------------------------------------------------------------------------------- */
4172 /*** public functions ****************************************************************************/
4173 /* --------------------------------------------------------------------------------------------- */
4175 void
4176 try_to_select (WPanel * panel, const char *name)
4178 do_try_to_select (panel, name);
4179 select_item (panel);
4182 /* --------------------------------------------------------------------------------------------- */
4184 void
4185 panel_clean_dir (WPanel * panel)
4187 panel->top_file = 0;
4188 panel->selected = 0;
4189 panel->marked = 0;
4190 panel->dirs_marked = 0;
4191 panel->total = 0;
4192 panel->searching = FALSE;
4193 panel->is_panelized = FALSE;
4194 panel->dirty = 1;
4195 panel->content_shift = -1;
4196 panel->max_shift = -1;
4198 dir_list_clean (&panel->dir);
4201 /* --------------------------------------------------------------------------------------------- */
4203 * Set Up panel's current dir object
4205 * @param panel panel object
4206 * @param path_str string contain path
4209 void
4210 panel_set_cwd (WPanel * panel, const vfs_path_t * vpath)
4212 if (vpath != panel->cwd_vpath) /* check if new vpath is not the panel->cwd_vpath object */
4214 vfs_path_free (panel->cwd_vpath);
4215 panel->cwd_vpath = vfs_path_clone (vpath);
4219 /* --------------------------------------------------------------------------------------------- */
4221 * Set Up panel's last working dir object
4223 * @param panel panel object
4224 * @param path_str string contain path
4227 void
4228 panel_set_lwd (WPanel * panel, const vfs_path_t * vpath)
4230 if (vpath != panel->lwd_vpath) /* check if new vpath is not the panel->lwd_vpath object */
4232 vfs_path_free (panel->lwd_vpath);
4233 panel->lwd_vpath = vfs_path_clone (vpath);
4237 /* --------------------------------------------------------------------------------------------- */
4239 * Panel creation for specified directory.
4241 * @param panel_name specifies the name of the panel for setup retieving
4242 * @param wpath the path of working panel directory. If path is NULL then panel will be created
4243 * for current directory
4245 * @return new instance of WPanel
4248 WPanel *
4249 panel_new_with_dir (const char *panel_name, const vfs_path_t * vpath)
4251 WPanel *panel;
4252 Widget *w;
4253 char *section;
4254 int i, err;
4255 char *curdir = NULL;
4257 panel = g_new0 (WPanel, 1);
4258 w = WIDGET (panel);
4259 /* No know sizes of the panel at startup */
4260 widget_init (w, 0, 0, 0, 0, panel_callback, panel_mouse_callback);
4261 /* We do not want the cursor */
4262 widget_want_cursor (w, FALSE);
4264 if (vpath != NULL)
4266 curdir = _vfs_get_cwd ();
4267 panel_set_cwd (panel, vpath);
4269 else
4271 vfs_setup_cwd ();
4272 panel->cwd_vpath = vfs_path_clone (vfs_get_raw_current_dir ());
4275 panel_set_lwd (panel, vfs_get_raw_current_dir ());
4277 panel->hist_name = g_strconcat ("Dir Hist ", panel_name, (char *) NULL);
4278 /* directories history will be get later */
4280 panel->dir.size = DIR_LIST_MIN_SIZE;
4281 panel->dir.list = g_new (file_entry_t, panel->dir.size);
4282 panel->dir.len = 0;
4283 panel->active = 0;
4284 panel->filter = NULL;
4285 panel->list_cols = 1;
4286 panel->brief_cols = 2;
4287 panel->top_file = 0;
4288 panel->selected = 0;
4289 panel->marked = 0;
4290 panel->total = 0;
4291 panel->dirty = 1;
4292 panel->searching = FALSE;
4293 panel->dirs_marked = 0;
4294 panel->is_panelized = FALSE;
4295 panel->format = NULL;
4296 panel->status_format = NULL;
4297 panel->format_modified = 1;
4298 panel->content_shift = -1;
4299 panel->max_shift = -1;
4301 panel->panel_name = g_strdup (panel_name);
4302 panel->user_format = g_strdup (DEFAULT_USER_FORMAT);
4304 #ifdef HAVE_CHARSET
4305 panel->codepage = SELECT_CHARSET_NO_TRANSLATE;
4306 #endif
4308 for (i = 0; i < LIST_TYPES; i++)
4309 panel->user_status_format[i] = g_strdup (DEFAULT_USER_FORMAT);
4311 panel->search_buffer[0] = '\0';
4312 panel->prev_search_buffer[0] = '\0';
4313 panel->frame_size = frame_half;
4315 section = g_strconcat ("Temporal:", panel->panel_name, (char *) NULL);
4316 if (!mc_config_has_group (mc_main_config, section))
4318 g_free (section);
4319 section = g_strdup (panel->panel_name);
4321 panel_load_setup (panel, section);
4322 g_free (section);
4324 /* Load format strings */
4325 err = set_panel_formats (panel);
4326 if (err != 0)
4327 set_panel_formats (panel);
4329 #ifdef HAVE_CHARSET
4331 const vfs_path_element_t *path_element;
4333 path_element = vfs_path_get_by_index (panel->cwd_vpath, -1);
4334 if (path_element->encoding != NULL)
4335 panel->codepage = get_codepage_index (path_element->encoding);
4337 #endif
4339 if (mc_chdir (panel->cwd_vpath) != 0)
4341 #ifdef HAVE_CHARSET
4342 panel->codepage = SELECT_CHARSET_NO_TRANSLATE;
4343 #endif
4344 vfs_setup_cwd ();
4345 vfs_path_free (panel->cwd_vpath);
4346 panel->cwd_vpath = vfs_path_clone (vfs_get_raw_current_dir ());
4349 /* Load the default format */
4350 dir_list_load (&panel->dir, panel->cwd_vpath, panel->sort_field->sort_routine,
4351 &panel->sort_info, panel->filter);
4353 /* Restore old right path */
4354 if (curdir != NULL)
4356 vfs_path_t *tmp_vpath;
4358 tmp_vpath = vfs_path_from_str (curdir);
4359 err = mc_chdir (tmp_vpath);
4360 vfs_path_free (tmp_vpath);
4362 g_free (curdir);
4364 return panel;
4367 /* --------------------------------------------------------------------------------------------- */
4369 void
4370 panel_reload (WPanel * panel)
4372 struct stat current_stat;
4373 vfs_path_t *cwd_vpath;
4375 if (panels_options.fast_reload && stat (vfs_path_as_str (panel->cwd_vpath), &current_stat) == 0
4376 && current_stat.st_ctime == panel->dir_stat.st_ctime
4377 && current_stat.st_mtime == panel->dir_stat.st_mtime)
4378 return;
4380 cwd_vpath = panel_recursive_cd_to_parent (panel->cwd_vpath);
4381 vfs_path_free (panel->cwd_vpath);
4383 if (cwd_vpath == NULL)
4385 panel->cwd_vpath = vfs_path_from_str (PATH_SEP_STR);
4386 panel_clean_dir (panel);
4387 dir_list_init (&panel->dir);
4388 return;
4391 panel->cwd_vpath = cwd_vpath;
4392 memset (&(panel->dir_stat), 0, sizeof (panel->dir_stat));
4393 show_dir (panel);
4395 dir_list_reload (&panel->dir, panel->cwd_vpath, panel->sort_field->sort_routine,
4396 &panel->sort_info, panel->filter);
4398 panel->dirty = 1;
4399 if (panel->selected >= panel->dir.len)
4400 do_select (panel, panel->dir.len - 1);
4402 recalculate_panel_summary (panel);
4405 /* --------------------------------------------------------------------------------------------- */
4406 /* Switches the panel to the mode specified in the format */
4407 /* Seting up both format and status string. Return: 0 - on success; */
4408 /* 1 - format error; 2 - status error; 3 - errors in both formats. */
4411 set_panel_formats (WPanel * p)
4413 format_e *form;
4414 char *err = NULL;
4415 int retcode = 0;
4417 form = use_display_format (p, panel_format (p), &err, FALSE);
4419 if (err != NULL)
4421 g_free (err);
4422 retcode = 1;
4424 else
4426 delete_format (p->format);
4427 p->format = form;
4430 if (panels_options.show_mini_info)
4432 form = use_display_format (p, mini_status_format (p), &err, TRUE);
4434 if (err != NULL)
4436 g_free (err);
4437 retcode += 2;
4439 else
4441 delete_format (p->status_format);
4442 p->status_format = form;
4446 panel_format_modified (p);
4447 panel_update_cols (WIDGET (p), p->frame_size);
4449 if (retcode)
4450 message (D_ERROR, _("Warning"),
4451 _("User supplied format looks invalid, reverting to default."));
4452 if (retcode & 0x01)
4454 g_free (p->user_format);
4455 p->user_format = g_strdup (DEFAULT_USER_FORMAT);
4457 if (retcode & 0x02)
4459 g_free (p->user_status_format[p->list_type]);
4460 p->user_status_format[p->list_type] = g_strdup (DEFAULT_USER_FORMAT);
4463 return retcode;
4466 /* --------------------------------------------------------------------------------------------- */
4468 void
4469 panel_update_cols (Widget * widget, panel_display_t frame_size)
4471 int cols, origin;
4473 /* don't touch panel if it is not in dialog yet */
4474 /* if panel is not in dialog it is not in widgets list
4475 and cannot be compared with get_panel_widget() result */
4476 if (widget->owner == NULL)
4477 return;
4479 if (panels_layout.horizontal_split)
4481 widget->cols = COLS;
4482 return;
4485 if (frame_size == frame_full)
4487 cols = COLS;
4488 origin = 0;
4490 else if (widget == get_panel_widget (0))
4492 cols = panels_layout.left_panel_size;
4493 origin = 0;
4495 else
4497 cols = COLS - panels_layout.left_panel_size;
4498 origin = panels_layout.left_panel_size;
4501 widget->cols = cols;
4502 widget->x = origin;
4505 /* --------------------------------------------------------------------------------------------- */
4507 /* Select current item and readjust the panel */
4508 void
4509 select_item (WPanel * panel)
4511 /* Although currently all over the code we set the selection and
4512 top file to decent values before calling select_item, I could
4513 forget it someday, so it's better to do the actual fitting here */
4515 if (panel->selected < 0)
4516 panel->selected = 0;
4518 if (panel->selected > panel->dir.len - 1)
4519 panel->selected = panel->dir.len - 1;
4521 adjust_top_file (panel);
4523 panel->dirty = 1;
4525 execute_hooks (select_file_hook);
4528 /* --------------------------------------------------------------------------------------------- */
4529 /** Clears all files in the panel, used only when one file was marked */
4530 void
4531 unmark_files (WPanel * panel)
4533 int i;
4535 if (!panel->marked)
4536 return;
4538 for (i = 0; i < panel->dir.len; i++)
4539 file_mark (panel, i, 0);
4541 panel->dirs_marked = 0;
4542 panel->marked = 0;
4543 panel->total = 0;
4546 /* --------------------------------------------------------------------------------------------- */
4547 /** Recalculate the panels summary information, used e.g. when marked
4548 files might have been removed by an external command */
4550 void
4551 recalculate_panel_summary (WPanel * panel)
4553 int i;
4555 panel->marked = 0;
4556 panel->dirs_marked = 0;
4557 panel->total = 0;
4559 for (i = 0; i < panel->dir.len; i++)
4560 if (panel->dir.list[i].f.marked)
4562 /* do_file_mark will return immediately if newmark == oldmark.
4563 So we have to first unmark it to get panel's summary information
4564 updated. (Norbert) */
4565 panel->dir.list[i].f.marked = 0;
4566 do_file_mark (panel, i, 1);
4570 /* --------------------------------------------------------------------------------------------- */
4571 /** This routine marks a file or a directory */
4573 void
4574 do_file_mark (WPanel * panel, int idx, int mark)
4576 if (panel->dir.list[idx].f.marked == mark)
4577 return;
4579 /* Only '..' can't be marked, '.' isn't visible */
4580 if (DIR_IS_DOTDOT (panel->dir.list[idx].fname))
4581 return;
4583 file_mark (panel, idx, mark);
4584 if (panel->dir.list[idx].f.marked)
4586 panel->marked++;
4588 if (S_ISDIR (panel->dir.list[idx].st.st_mode))
4590 if (panel->dir.list[idx].f.dir_size_computed)
4591 panel->total += (uintmax_t) panel->dir.list[idx].st.st_size;
4592 panel->dirs_marked++;
4594 else
4595 panel->total += (uintmax_t) panel->dir.list[idx].st.st_size;
4597 set_colors (panel);
4599 else
4601 if (S_ISDIR (panel->dir.list[idx].st.st_mode))
4603 if (panel->dir.list[idx].f.dir_size_computed)
4604 panel->total -= (uintmax_t) panel->dir.list[idx].st.st_size;
4605 panel->dirs_marked--;
4607 else
4608 panel->total -= (uintmax_t) panel->dir.list[idx].st.st_size;
4610 panel->marked--;
4614 /* --------------------------------------------------------------------------------------------- */
4616 * Changes the current directory of the panel.
4617 * Record change in the directory history.
4619 gboolean
4620 do_panel_cd (WPanel * panel, const vfs_path_t * new_dir_vpath, enum cd_enum cd_type)
4622 gboolean r;
4624 r = _do_panel_cd (panel, new_dir_vpath, cd_type);
4625 if (r)
4626 directory_history_add (panel, panel->cwd_vpath);
4627 return r;
4630 /* --------------------------------------------------------------------------------------------- */
4632 void
4633 file_mark (WPanel * panel, int lc_index, int val)
4635 if (panel->dir.list[lc_index].f.marked != val)
4637 panel->dir.list[lc_index].f.marked = val;
4638 panel->dirty = 1;
4642 /* --------------------------------------------------------------------------------------------- */
4644 void
4645 panel_re_sort (WPanel * panel)
4647 char *filename;
4648 int i;
4650 if (panel == NULL)
4651 return;
4653 filename = g_strdup (selection (panel)->fname);
4654 unselect_item (panel);
4655 dir_list_sort (&panel->dir, panel->sort_field->sort_routine, &panel->sort_info);
4656 panel->selected = -1;
4658 for (i = panel->dir.len; i != 0; i--)
4659 if (strcmp (panel->dir.list[i - 1].fname, filename) == 0)
4661 panel->selected = i - 1;
4662 break;
4665 g_free (filename);
4666 panel->top_file = panel->selected - panel_items (panel) / 2;
4667 select_item (panel);
4668 panel->dirty = 1;
4671 /* --------------------------------------------------------------------------------------------- */
4673 void
4674 panel_set_sort_order (WPanel * panel, const panel_field_t * sort_order)
4676 if (sort_order == NULL)
4677 return;
4679 panel->sort_field = sort_order;
4681 /* The directory is already sorted, we have to load the unsorted stuff */
4682 if (sort_order->sort_routine == (GCompareFunc) unsorted)
4684 char *current_file;
4686 current_file = g_strdup (panel->dir.list[panel->selected].fname);
4687 panel_reload (panel);
4688 try_to_select (panel, current_file);
4689 g_free (current_file);
4691 panel_re_sort (panel);
4694 /* --------------------------------------------------------------------------------------------- */
4696 #ifdef HAVE_CHARSET
4699 * Change panel encoding.
4700 * @param panel WPanel object
4703 void
4704 panel_change_encoding (WPanel * panel)
4706 const char *encoding = NULL;
4707 char *errmsg;
4708 int r;
4710 r = select_charset (-1, -1, panel->codepage, FALSE);
4712 if (r == SELECT_CHARSET_CANCEL)
4713 return; /* Cancel */
4715 panel->codepage = r;
4717 if (panel->codepage == SELECT_CHARSET_NO_TRANSLATE)
4719 /* No translation */
4720 vfs_path_t *cd_path_vpath;
4722 g_free (init_translation_table (mc_global.display_codepage, mc_global.display_codepage));
4723 cd_path_vpath = remove_encoding_from_path (panel->cwd_vpath);
4724 do_panel_cd (panel, cd_path_vpath, cd_parse_command);
4725 show_dir (panel);
4726 vfs_path_free (cd_path_vpath);
4727 return;
4730 errmsg = init_translation_table (panel->codepage, mc_global.display_codepage);
4731 if (errmsg != NULL)
4733 message (D_ERROR, MSG_ERROR, "%s", errmsg);
4734 g_free (errmsg);
4735 return;
4738 encoding = get_codepage_id (panel->codepage);
4739 if (encoding != NULL)
4741 vfs_path_change_encoding (panel->cwd_vpath, encoding);
4743 if (!do_panel_cd (panel, panel->cwd_vpath, cd_parse_command))
4744 message (D_ERROR, MSG_ERROR, _("Cannot chdir to \"%s\""),
4745 vfs_path_as_str (panel->cwd_vpath));
4749 /* --------------------------------------------------------------------------------------------- */
4752 * Remove encode info from last path element.
4755 vfs_path_t *
4756 remove_encoding_from_path (const vfs_path_t * vpath)
4758 vfs_path_t *ret_vpath;
4759 GString *tmp_conv;
4760 int indx;
4762 ret_vpath = vfs_path_new ();
4764 tmp_conv = g_string_new ("");
4766 for (indx = 0; indx < vfs_path_elements_count (vpath); indx++)
4768 GIConv converter;
4769 vfs_path_element_t *path_element;
4771 path_element = vfs_path_element_clone (vfs_path_get_by_index (vpath, indx));
4773 if (path_element->encoding == NULL)
4775 vfs_path_add_element (ret_vpath, path_element);
4776 continue;
4779 converter = str_crt_conv_to (path_element->encoding);
4780 if (converter == INVALID_CONV)
4782 vfs_path_add_element (ret_vpath, path_element);
4783 continue;
4786 MC_PTR_FREE (path_element->encoding);
4788 str_vfs_convert_from (converter, path_element->path, tmp_conv);
4790 g_free (path_element->path);
4791 path_element->path = g_strndup (tmp_conv->str, tmp_conv->len);
4793 g_string_set_size (tmp_conv, 0);
4795 str_close_conv (converter);
4796 str_close_conv (path_element->dir.converter);
4797 path_element->dir.converter = INVALID_CONV;
4798 vfs_path_add_element (ret_vpath, path_element);
4800 g_string_free (tmp_conv, TRUE);
4801 return ret_vpath;
4803 #endif /* HAVE_CHARSET */
4805 /* --------------------------------------------------------------------------------------------- */
4808 * This routine reloads the directory in both panels. It tries to
4809 * select current_file in current_panel and other_file in other_panel.
4810 * If current_file == -1 then it automatically sets current_file and
4811 * other_file to the currently selected files in the panels.
4813 * If flags has the UP_ONLY_CURRENT bit toggled on, then it
4814 * will not reload the other panel.
4816 * @param flags for reload panel
4817 * @param current_file name of the current file
4820 void
4821 update_panels (panel_update_flags_t flags, const char *current_file)
4823 WPanel *panel;
4825 /* first, update other panel... */
4826 if ((flags & UP_ONLY_CURRENT) == 0)
4827 update_one_panel (get_other_index (), flags, UP_KEEPSEL);
4828 /* ...then current one */
4829 update_one_panel (get_current_index (), flags, current_file);
4831 if (get_current_type () == view_listing)
4832 panel = PANEL (get_panel_widget (get_current_index ()));
4833 else
4834 panel = PANEL (get_panel_widget (get_other_index ()));
4836 if (!panel->is_panelized)
4837 (void) mc_chdir (panel->cwd_vpath);
4840 /* --------------------------------------------------------------------------------------------- */
4842 gsize
4843 panel_get_num_of_sortable_fields (void)
4845 gsize ret = 0, lc_index;
4847 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
4848 if (panel_fields[lc_index].is_user_choice)
4849 ret++;
4850 return ret;
4853 /* --------------------------------------------------------------------------------------------- */
4855 const char **
4856 panel_get_sortable_fields (gsize * array_size)
4858 char **ret;
4859 gsize lc_index, i;
4861 lc_index = panel_get_num_of_sortable_fields ();
4863 ret = g_try_new0 (char *, lc_index + 1);
4864 if (ret == NULL)
4865 return NULL;
4867 if (array_size != NULL)
4868 *array_size = lc_index;
4870 lc_index = 0;
4872 for (i = 0; panel_fields[i].id != NULL; i++)
4873 if (panel_fields[i].is_user_choice)
4874 ret[lc_index++] = g_strdup (_(panel_fields[i].title_hotkey));
4876 return (const char **) ret;
4879 /* --------------------------------------------------------------------------------------------- */
4881 const panel_field_t *
4882 panel_get_field_by_id (const char *name)
4884 gsize lc_index;
4886 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
4887 if (panel_fields[lc_index].id != NULL && strcmp (name, panel_fields[lc_index].id) == 0)
4888 return &panel_fields[lc_index];
4890 return NULL;
4893 /* --------------------------------------------------------------------------------------------- */
4895 const panel_field_t *
4896 panel_get_field_by_title_hotkey (const char *name)
4898 gsize lc_index;
4900 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
4901 if (panel_fields[lc_index].title_hotkey != NULL &&
4902 strcmp (name, _(panel_fields[lc_index].title_hotkey)) == 0)
4903 return &panel_fields[lc_index];
4905 return NULL;
4908 /* --------------------------------------------------------------------------------------------- */
4910 const panel_field_t *
4911 panel_get_field_by_title (const char *name)
4913 gsize lc_index;
4915 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
4917 const char *title;
4919 title = panel_get_title_without_hotkey (panel_fields[lc_index].title_hotkey);
4920 if (strcmp (title, name) == 0)
4921 return &panel_fields[lc_index];
4924 return NULL;
4927 /* --------------------------------------------------------------------------------------------- */
4929 gsize
4930 panel_get_num_of_user_possible_fields (void)
4932 gsize ret = 0, lc_index;
4934 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
4935 if (panel_fields[lc_index].use_in_user_format)
4936 ret++;
4938 return ret;
4941 /* --------------------------------------------------------------------------------------------- */
4943 const char **
4944 panel_get_user_possible_fields (gsize * array_size)
4946 char **ret;
4947 gsize lc_index, i;
4949 lc_index = panel_get_num_of_user_possible_fields ();
4951 ret = g_try_new0 (char *, lc_index + 1);
4952 if (ret == NULL)
4953 return NULL;
4955 if (array_size != NULL)
4956 *array_size = lc_index;
4958 lc_index = 0;
4960 for (i = 0; panel_fields[i].id != NULL; i++)
4961 if (panel_fields[i].use_in_user_format)
4962 ret[lc_index++] = g_strdup (_(panel_fields[i].title_hotkey));
4964 return (const char **) ret;
4967 /* --------------------------------------------------------------------------------------------- */
4969 void
4970 panel_init (void)
4972 panel_sort_up_sign = mc_skin_get ("widget-common", "sort-sign-up", "'");
4973 panel_sort_down_sign = mc_skin_get ("widget-common", "sort-sign-down", ".");
4975 panel_hiddenfiles_sign_show = mc_skin_get ("widget-panel", "hiddenfiles-sign-show", ".");
4976 panel_hiddenfiles_sign_hide = mc_skin_get ("widget-panel", "hiddenfiles-sign-hide", ".");
4977 panel_history_prev_item_sign = mc_skin_get ("widget-panel", "history-prev-item-sign", "<");
4978 panel_history_next_item_sign = mc_skin_get ("widget-panel", "history-next-item-sign", ">");
4979 panel_history_show_list_sign = mc_skin_get ("widget-panel", "history-show-list-sign", "^");
4980 panel_filename_scroll_left_char =
4981 mc_skin_get ("widget-panel", "filename-scroll-left-char", "{");
4982 panel_filename_scroll_right_char =
4983 mc_skin_get ("widget-panel", "filename-scroll-right-char", "}");
4985 mc_event_add (MCEVENT_GROUP_FILEMANAGER, "update_panels", event_update_panels, NULL, NULL);
4986 mc_event_add (MCEVENT_GROUP_FILEMANAGER, "panel_save_current_file_to_clip_file",
4987 panel_save_current_file_to_clip_file, NULL, NULL);
4990 /* --------------------------------------------------------------------------------------------- */
4992 void
4993 panel_deinit (void)
4995 g_free (panel_sort_up_sign);
4996 g_free (panel_sort_down_sign);
4998 g_free (panel_hiddenfiles_sign_show);
4999 g_free (panel_hiddenfiles_sign_hide);
5000 g_free (panel_history_prev_item_sign);
5001 g_free (panel_history_next_item_sign);
5002 g_free (panel_history_show_list_sign);
5003 g_free (panel_filename_scroll_left_char);
5004 g_free (panel_filename_scroll_right_char);
5007 /* --------------------------------------------------------------------------------------------- */
5009 gboolean
5010 do_cd (const vfs_path_t * new_dir_vpath, enum cd_enum exact)
5012 gboolean res;
5013 const vfs_path_t *_new_dir_vpath = new_dir_vpath;
5015 if (current_panel->is_panelized)
5017 size_t new_vpath_len;
5019 new_vpath_len = vfs_path_len (new_dir_vpath);
5020 if (vfs_path_equal_len (new_dir_vpath, panelized_panel.root_vpath, new_vpath_len))
5021 _new_dir_vpath = panelized_panel.root_vpath;
5024 res = do_panel_cd (current_panel, _new_dir_vpath, exact);
5026 #ifdef HAVE_CHARSET
5027 if (res)
5029 const vfs_path_element_t *path_element;
5031 path_element = vfs_path_get_by_index (current_panel->cwd_vpath, -1);
5032 if (path_element->encoding != NULL)
5033 current_panel->codepage = get_codepage_index (path_element->encoding);
5034 else
5035 current_panel->codepage = SELECT_CHARSET_NO_TRANSLATE;
5037 #endif /* HAVE_CHARSET */
5039 return res;
5042 /* --------------------------------------------------------------------------------------------- */