Cleanup of main.[ch].
[pantumic.git] / src / screen.c
blobb1835dd6047a92d8f6b8fca9638a9f17c7f18cb9
1 /* Panel managing.
2 Copyright (C) 1994, 1995, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
3 2005, 2006, 2007, 2009 Free Software Foundation, Inc.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 Written by: 1995 Miguel de Icaza
16 1997, 1999 Timur Bakeyev
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
22 /** \file screen.c
23 * \brief Source: panel managin module
26 #include <config.h>
28 #include <errno.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <unistd.h>
34 #include "lib/global.h"
36 #include "lib/tty/tty.h"
37 #include "lib/tty/mouse.h" /* For Gpm_Event */
38 #include "lib/tty/key.h" /* XCTRL and ALT macros */
39 #include "lib/skin.h"
40 #include "lib/strescape.h"
41 #include "lib/filehighlight.h"
42 #include "lib/mcconfig.h"
43 #include "lib/vfs/mc-vfs/vfs.h"
44 #include "lib/unixcompat.h"
45 #include "lib/timefmt.h"
46 #include "lib/util.h"
47 #include "lib/widget.h"
48 #ifdef HAVE_CHARSET
49 #include "lib/charsets.h" /* get_codepage_id () */
50 #endif
52 #include "dir.h"
53 #include "boxes.h"
54 #include "tree.h"
55 #include "ext.h" /* regexp_command */
56 #include "layout.h" /* Most layout variables are here */
57 #include "cmd.h"
58 #include "command.h" /* cmdline */
59 #include "setup.h" /* For loading/saving panel options */
60 #include "user.h"
61 #include "execute.h"
62 #include "midnight.h"
63 #include "subshell.h" /* use_subshell */
64 #include "mountlist.h" /* my_statfs */
65 #include "selcodepage.h" /* select_charset (), SELECT_CHARSET_NO_TRANSLATE */
66 #include "keybind-defaults.h" /* global_keymap_t */
68 #include "panel.h"
70 /*** global variables ****************************************************************************/
72 /* If true, show the mini-info on the panel */
73 int show_mini_info = 1;
75 /* If true, use some usability hacks by Torben */
76 int torben_fj_mode = 0;
78 /* The hook list for the select file function */
79 hook_t *select_file_hook = NULL;
83 static const char *string_file_name (file_entry *, int);
84 static const char *string_file_size (file_entry *, int);
85 static const char *string_file_size_brief (file_entry *, int);
86 static const char *string_file_type (file_entry *, int);
87 static const char *string_file_mtime (file_entry *, int);
88 static const char *string_file_atime (file_entry *, int);
89 static const char *string_file_ctime (file_entry *, int);
90 static const char *string_file_permission (file_entry *, int);
91 static const char *string_file_perm_octal (file_entry *, int);
92 static const char *string_file_nlinks (file_entry *, int);
93 static const char *string_inode (file_entry *, int);
94 static const char *string_file_nuid (file_entry *, int);
95 static const char *string_file_ngid (file_entry *, int);
96 static const char *string_file_owner (file_entry *, int);
97 static const char *string_file_group (file_entry *, int);
98 static const char *string_marked (file_entry *, int);
99 static const char *string_space (file_entry *, int);
100 static const char *string_dot (file_entry *, int);
102 /* *INDENT-OFF* */
103 panel_field_t panel_fields[] = {
105 "unsorted", 12, 1, J_LEFT_FIT,
106 /* TRANSLATORS: one single character to represent 'unsorted' sort mode */
107 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
108 N_("sort|u"),
109 N_("&Unsorted"), TRUE, FALSE,
110 string_file_name,
111 (sortfn *) unsorted
115 "name", 12, 1, J_LEFT_FIT,
116 /* TRANSLATORS: one single character to represent 'name' sort mode */
117 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
118 N_("sort|n"),
119 N_("&Name"), TRUE, TRUE,
120 string_file_name,
121 (sortfn *) sort_name
125 "version", 12, 1, J_LEFT_FIT,
126 /* TRANSLATORS: one single character to represent 'version' sort mode */
127 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
128 N_("sort|v"),
129 N_("&Version"), TRUE, FALSE,
130 string_file_name,
131 (sortfn *) sort_vers
135 "extension", 12, 1, J_LEFT_FIT,
136 /* TRANSLATORS: one single character to represent 'extension' sort mode */
137 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
138 N_("sort|e"),
139 N_("&Extension"), TRUE, FALSE,
140 string_file_name, /* TODO: string_file_ext */
141 (sortfn *) sort_ext
145 "size", 7, 0, J_RIGHT,
146 /* TRANSLATORS: one single character to represent 'size' sort mode */
147 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
148 N_("sort|s"),
149 N_("&Size"), TRUE, TRUE,
150 string_file_size,
151 (sortfn *) sort_size
155 "bsize", 7, 0, J_RIGHT,
157 N_("Block Size"), FALSE, FALSE,
158 string_file_size_brief,
159 (sortfn *) sort_size
163 "type", 1, 0, J_LEFT,
165 "", FALSE, TRUE,
166 string_file_type,
167 NULL
171 "mtime", 12, 0, J_RIGHT,
172 /* TRANSLATORS: one single character to represent 'Modify time' sort mode */
173 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
174 N_("sort|m"),
175 N_("&Modify time"), TRUE, TRUE,
176 string_file_mtime,
177 (sortfn *) sort_time
181 "atime", 12, 0, J_RIGHT,
182 /* TRANSLATORS: one single character to represent 'Access time' sort mode */
183 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
184 N_("sort|a"),
185 N_("&Access time"), TRUE, TRUE,
186 string_file_atime,
187 (sortfn *) sort_atime
191 "ctime", 12, 0, J_RIGHT,
192 /* TRANSLATORS: one single character to represent 'Change time' sort mode */
193 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
194 N_("sort|h"),
195 N_("C&hange time"), TRUE, TRUE,
196 string_file_ctime,
197 (sortfn *) sort_ctime
201 "perm", 10, 0, J_LEFT,
203 N_("Permission"), FALSE, TRUE,
204 string_file_permission,
205 NULL
209 "mode", 6, 0, J_RIGHT,
211 N_("Perm"), FALSE, TRUE,
212 string_file_perm_octal,
213 NULL
217 "nlink", 2, 0, J_RIGHT,
219 N_("Nl"), FALSE, TRUE,
220 string_file_nlinks, NULL
224 "inode", 5, 0, J_RIGHT,
225 /* TRANSLATORS: one single character to represent 'inode' sort mode */
226 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
227 N_("sort|i"),
228 N_("&Inode"), TRUE, TRUE,
229 string_inode,
230 (sortfn *) sort_inode
234 "nuid", 5, 0, J_RIGHT,
236 N_("UID"), FALSE, FALSE,
237 string_file_nuid,
238 NULL
242 "ngid", 5, 0, J_RIGHT,
244 N_("GID"), FALSE, FALSE,
245 string_file_ngid,
246 NULL
250 "owner", 8, 0, J_LEFT_FIT,
252 N_("Owner"), FALSE, TRUE,
253 string_file_owner,
254 NULL
258 "group", 8, 0, J_LEFT_FIT,
260 N_("Group"), FALSE, TRUE,
261 string_file_group,
262 NULL
266 "mark", 1, 0, J_RIGHT,
268 " ", FALSE, TRUE,
269 string_marked,
270 NULL
274 "|", 1, 0, J_RIGHT,
276 " ", FALSE, TRUE,
277 NULL,
278 NULL
282 "space", 1, 0, J_RIGHT,
284 " ", FALSE, TRUE,
285 string_space,
286 NULL
290 "dot", 1, 0, J_RIGHT,
292 " ", FALSE, FALSE,
293 string_dot,
294 NULL
298 NULL, 0, 0, J_RIGHT, NULL, NULL, FALSE, FALSE, NULL, NULL
301 /* *INDENT-ON* */
303 extern int saving_setup;
305 /*** file scope macro definitions ****************************************************************/
307 #define ELEMENTS(arr) ( sizeof(arr) / sizeof((arr)[0]) )
309 #define NORMAL 0
310 #define SELECTED 1
311 #define MARKED 2
312 #define MARKED_SELECTED 3
313 #define STATUS 5
315 /* This macro extracts the number of available lines in a panel */
316 #define llines(p) (p->widget.lines-3 - (show_mini_info ? 2 : 0))
318 /*** file scope type declarations ****************************************************************/
320 typedef enum
322 MARK_DONT_MOVE = 0,
323 MARK_DOWN = 1,
324 MARK_FORCE_DOWN = 2,
325 MARK_FORCE_UP = 3
326 } mark_act_t;
329 * This describes a format item. The parse_display_format routine parses
330 * the user specified format and creates a linked list of format_e structures.
332 typedef struct format_e
334 struct format_e *next;
335 int requested_field_len;
336 int field_len;
337 align_crt_t just_mode;
338 int expand;
339 const char *(*string_fn) (file_entry *, int len);
340 char *title;
341 const char *id;
342 } format_e;
344 /*** file scope variables ************************************************************************/
346 static cb_ret_t panel_callback (Widget *, widget_msg_t msg, int parm);
347 static int panel_event (Gpm_Event * event, void *);
348 static void paint_frame (WPanel * panel);
349 static const char *panel_format (WPanel * panel);
350 static const char *mini_status_format (WPanel * panel);
352 static char *panel_sort_up_sign = NULL;
353 static char *panel_sort_down_sign = NULL;
355 static char *panel_hiddenfiles_sign_show = NULL;
356 static char *panel_hiddenfiles_sign_hide = NULL;
357 static char *panel_history_prev_item_sign = NULL;
358 static char *panel_history_next_item_sign = NULL;
359 static char *panel_history_show_list_sign = NULL;
361 static int mouse_marking = 0;
363 /*** file scope functions ************************************************************************/
364 /* --------------------------------------------------------------------------------------------- */
366 static void
367 set_colors (WPanel * panel)
369 (void) panel;
370 tty_set_normal_attrs ();
371 tty_setcolor (NORMAL_COLOR);
374 /* --------------------------------------------------------------------------------------------- */
375 /** Delete format string, it is a linked list */
377 static void
378 delete_format (format_e * format)
380 while (format != NULL)
382 format_e *next = format->next;
383 g_free (format->title);
384 g_free (format);
385 format = next;
389 /* --------------------------------------------------------------------------------------------- */
390 /** This code relies on the default justification!!! */
392 static void
393 add_permission_string (char *dest, int width, file_entry * fe, int attr, int color, int is_octal)
395 int i, r, l;
397 l = get_user_permissions (&fe->st);
399 if (is_octal)
401 /* Place of the access bit in octal mode */
402 l = width + l - 3;
403 r = l + 1;
405 else
407 /* The same to the triplet in string mode */
408 l = l * 3 + 1;
409 r = l + 3;
412 for (i = 0; i < width; i++)
414 if (i >= l && i < r)
416 if (attr == SELECTED || attr == MARKED_SELECTED)
417 tty_setcolor (MARKED_SELECTED_COLOR);
418 else
419 tty_setcolor (MARKED_COLOR);
421 else if (color >= 0)
422 tty_setcolor (color);
423 else
424 tty_lowlevel_setcolor (-color);
426 tty_print_char (dest[i]);
430 /* --------------------------------------------------------------------------------------------- */
431 /** String representations of various file attributes name */
433 static const char *
434 string_file_name (file_entry * fe, int len)
436 static char buffer[MC_MAXPATHLEN * MB_LEN_MAX + 1];
438 (void) len;
439 g_strlcpy (buffer, fe->fname, sizeof (buffer));
440 return buffer;
443 /* --------------------------------------------------------------------------------------------- */
445 static unsigned int
446 ilog10 (dev_t n)
448 unsigned int digits = 0;
451 digits++, n /= 10;
453 while (n != 0);
454 return digits;
457 /* --------------------------------------------------------------------------------------------- */
459 static void
460 format_device_number (char *buf, size_t bufsize, dev_t dev)
462 dev_t major_dev = major (dev);
463 dev_t minor_dev = minor (dev);
464 unsigned int major_digits = ilog10 (major_dev);
465 unsigned int minor_digits = ilog10 (minor_dev);
467 g_assert (bufsize >= 1);
468 if (major_digits + 1 + minor_digits + 1 <= bufsize)
470 g_snprintf (buf, bufsize, "%lu,%lu", (unsigned long) major_dev, (unsigned long) minor_dev);
472 else
474 g_strlcpy (buf, _("[dev]"), bufsize);
478 /* --------------------------------------------------------------------------------------------- */
479 /** size */
481 static const char *
482 string_file_size (file_entry * fe, int len)
484 static char buffer[BUF_TINY];
486 /* Don't ever show size of ".." since we don't calculate it */
487 if (!strcmp (fe->fname, ".."))
489 return _("UP--DIR");
492 #ifdef HAVE_STRUCT_STAT_ST_RDEV
493 if (S_ISBLK (fe->st.st_mode) || S_ISCHR (fe->st.st_mode))
494 format_device_number (buffer, len + 1, fe->st.st_rdev);
495 else
496 #endif
498 size_trunc_len (buffer, (unsigned int) len, fe->st.st_size, 0, panels_options.kilobyte_si);
500 return buffer;
503 /* --------------------------------------------------------------------------------------------- */
504 /** bsize */
506 static const char *
507 string_file_size_brief (file_entry * fe, int len)
509 if (S_ISLNK (fe->st.st_mode) && !fe->f.link_to_dir)
511 return _("SYMLINK");
514 if ((S_ISDIR (fe->st.st_mode) || fe->f.link_to_dir) && strcmp (fe->fname, ".."))
516 return _("SUB-DIR");
519 return string_file_size (fe, len);
522 /* --------------------------------------------------------------------------------------------- */
523 /** This functions return a string representation of a file entry type */
525 static const char *
526 string_file_type (file_entry * fe, int len)
528 static char buffer[2];
530 (void) len;
531 if (S_ISDIR (fe->st.st_mode))
532 buffer[0] = PATH_SEP;
533 else if (S_ISLNK (fe->st.st_mode))
535 if (fe->f.link_to_dir)
536 buffer[0] = '~';
537 else if (fe->f.stale_link)
538 buffer[0] = '!';
539 else
540 buffer[0] = '@';
542 else if (S_ISCHR (fe->st.st_mode))
543 buffer[0] = '-';
544 else if (S_ISSOCK (fe->st.st_mode))
545 buffer[0] = '=';
546 else if (S_ISDOOR (fe->st.st_mode))
547 buffer[0] = '>';
548 else if (S_ISBLK (fe->st.st_mode))
549 buffer[0] = '+';
550 else if (S_ISFIFO (fe->st.st_mode))
551 buffer[0] = '|';
552 else if (S_ISNAM (fe->st.st_mode))
553 buffer[0] = '#';
554 else if (!S_ISREG (fe->st.st_mode))
555 buffer[0] = '?'; /* non-regular of unknown kind */
556 else if (is_exe (fe->st.st_mode))
557 buffer[0] = '*';
558 else
559 buffer[0] = ' ';
560 buffer[1] = '\0';
561 return buffer;
564 /* --------------------------------------------------------------------------------------------- */
565 /** mtime */
567 static const char *
568 string_file_mtime (file_entry * fe, int len)
570 (void) len;
571 return file_date (fe->st.st_mtime);
574 /* --------------------------------------------------------------------------------------------- */
575 /** atime */
577 static const char *
578 string_file_atime (file_entry * fe, int len)
580 (void) len;
581 return file_date (fe->st.st_atime);
584 /* --------------------------------------------------------------------------------------------- */
585 /** ctime */
587 static const char *
588 string_file_ctime (file_entry * fe, int len)
590 (void) len;
591 return file_date (fe->st.st_ctime);
594 /* --------------------------------------------------------------------------------------------- */
595 /** perm */
597 static const char *
598 string_file_permission (file_entry * fe, int len)
600 (void) len;
601 return string_perm (fe->st.st_mode);
604 /* --------------------------------------------------------------------------------------------- */
605 /** mode */
607 static const char *
608 string_file_perm_octal (file_entry * fe, int len)
610 static char buffer[10];
612 (void) len;
613 g_snprintf (buffer, sizeof (buffer), "0%06lo", (unsigned long) fe->st.st_mode);
614 return buffer;
617 /* --------------------------------------------------------------------------------------------- */
618 /** nlink */
620 static const char *
621 string_file_nlinks (file_entry * fe, int len)
623 static char buffer[BUF_TINY];
625 (void) len;
626 g_snprintf (buffer, sizeof (buffer), "%16d", (int) fe->st.st_nlink);
627 return buffer;
630 /* --------------------------------------------------------------------------------------------- */
631 /** inode */
633 static const char *
634 string_inode (file_entry * fe, int len)
636 static char buffer[10];
638 (void) len;
639 g_snprintf (buffer, sizeof (buffer), "%lu", (unsigned long) fe->st.st_ino);
640 return buffer;
643 /* --------------------------------------------------------------------------------------------- */
644 /** nuid */
646 static const char *
647 string_file_nuid (file_entry * fe, int len)
649 static char buffer[10];
651 (void) len;
652 g_snprintf (buffer, sizeof (buffer), "%lu", (unsigned long) fe->st.st_uid);
653 return buffer;
656 /* --------------------------------------------------------------------------------------------- */
657 /** ngid */
659 static const char *
660 string_file_ngid (file_entry * fe, int len)
662 static char buffer[10];
664 (void) len;
665 g_snprintf (buffer, sizeof (buffer), "%lu", (unsigned long) fe->st.st_gid);
666 return buffer;
669 /* --------------------------------------------------------------------------------------------- */
670 /** owner */
672 static const char *
673 string_file_owner (file_entry * fe, int len)
675 (void) len;
676 return get_owner (fe->st.st_uid);
679 /* --------------------------------------------------------------------------------------------- */
680 /** group */
682 static const char *
683 string_file_group (file_entry * fe, int len)
685 (void) len;
686 return get_group (fe->st.st_gid);
689 /* --------------------------------------------------------------------------------------------- */
690 /** mark */
692 static const char *
693 string_marked (file_entry * fe, int len)
695 (void) len;
696 return fe->f.marked ? "*" : " ";
699 /* --------------------------------------------------------------------------------------------- */
700 /** space */
702 static const char *
703 string_space (file_entry * fe, int len)
705 (void) fe;
706 (void) len;
707 return " ";
710 /* --------------------------------------------------------------------------------------------- */
711 /** dot */
713 static const char *
714 string_dot (file_entry * fe, int len)
716 (void) fe;
717 (void) len;
718 return ".";
721 /* --------------------------------------------------------------------------------------------- */
723 static int
724 file_compute_color (int attr, file_entry * fe)
726 switch (attr)
728 case SELECTED:
729 return (SELECTED_COLOR);
730 case MARKED:
731 return (MARKED_COLOR);
732 case MARKED_SELECTED:
733 return (MARKED_SELECTED_COLOR);
734 case STATUS:
735 return (NORMAL_COLOR);
736 case NORMAL:
737 default:
738 if (!panels_options.filetype_mode)
739 return (NORMAL_COLOR);
742 return mc_fhl_get_color (mc_filehighlight, fe);
745 /* --------------------------------------------------------------------------------------------- */
746 /** Formats the file number file_index of panel in the buffer dest */
748 static void
749 format_file (char *dest, int limit, WPanel * panel, int file_index, int width, int attr,
750 int isstatus)
752 int color, length, empty_line;
753 const char *txt;
754 format_e *format, *home;
755 file_entry *fe;
757 (void) dest;
758 (void) limit;
759 length = 0;
760 empty_line = (file_index >= panel->count);
761 home = (isstatus) ? panel->status_format : panel->format;
762 fe = &panel->dir.list[file_index];
764 if (!empty_line)
765 color = file_compute_color (attr, fe);
766 else
767 color = NORMAL_COLOR;
769 for (format = home; format; format = format->next)
771 if (length == width)
772 break;
774 if (format->string_fn)
776 int len, perm;
777 char *preperad_text;
779 if (empty_line)
780 txt = " ";
781 else
782 txt = (*format->string_fn) (fe, format->field_len);
784 len = format->field_len;
785 if (len + length > width)
786 len = width - length;
787 if (len <= 0)
788 break;
790 perm = 0;
791 if (panels_options.permission_mode)
793 if (!strcmp (format->id, "perm"))
794 perm = 1;
795 else if (!strcmp (format->id, "mode"))
796 perm = 2;
799 if (color >= 0)
800 tty_setcolor (color);
801 else
802 tty_lowlevel_setcolor (-color);
804 preperad_text = (char *) str_fit_to_term (txt, len, format->just_mode);
805 if (perm)
806 add_permission_string (preperad_text, format->field_len, fe, attr, color, perm - 1);
807 else
808 tty_print_string (preperad_text);
810 length += len;
812 else
814 if (attr == SELECTED || attr == MARKED_SELECTED)
815 tty_setcolor (SELECTED_COLOR);
816 else
817 tty_setcolor (NORMAL_COLOR);
818 tty_print_one_vline (TRUE);
819 length++;
823 if (length < width)
824 tty_draw_hline (-1, -1, ' ', width - length);
827 /* --------------------------------------------------------------------------------------------- */
829 static void
830 repaint_file (WPanel * panel, int file_index, int mv, int attr, int isstatus)
832 int second_column = 0;
833 int width;
834 int offset = 0;
835 char buffer[BUF_MEDIUM];
837 gboolean panel_is_split = !isstatus && panel->split;
839 width = panel->widget.cols - 2;
841 if (panel_is_split)
843 second_column = (file_index - panel->top_file) / llines (panel);
844 width = width / 2 - 1;
846 if (second_column != 0)
848 offset = 1 + width;
849 /*width = (panel->widget.cols-2) - (panel->widget.cols-2)/2 - 1; */
850 width = panel->widget.cols - offset - 2;
854 /* Nothing to paint */
855 if (width <= 0)
856 return;
858 if (mv)
860 if (panel_is_split)
861 widget_move (&panel->widget,
862 (file_index - panel->top_file) % llines (panel) + 2, offset + 1);
863 else
864 widget_move (&panel->widget, file_index - panel->top_file + 2, 1);
867 format_file (buffer, sizeof (buffer), panel, file_index, width, attr, isstatus);
869 if (panel_is_split)
871 if (second_column)
872 tty_print_char (' ');
873 else
875 tty_setcolor (NORMAL_COLOR);
876 tty_print_one_vline (TRUE);
881 /* --------------------------------------------------------------------------------------------- */
883 static void
884 display_mini_info (WPanel * panel)
886 if (!show_mini_info)
887 return;
889 widget_move (&panel->widget, llines (panel) + 3, 1);
891 if (panel->searching)
893 tty_setcolor (INPUT_COLOR);
894 tty_print_char ('/');
895 tty_print_string (str_fit_to_term (panel->search_buffer, panel->widget.cols - 3, J_LEFT));
896 return;
899 /* Status resolves links and show them */
900 set_colors (panel);
902 if (S_ISLNK (panel->dir.list[panel->selected].st.st_mode))
904 char *lc_link, link_target[MC_MAXPATHLEN];
905 int len;
907 lc_link = concat_dir_and_file (panel->cwd, panel->dir.list[panel->selected].fname);
908 len = mc_readlink (lc_link, link_target, MC_MAXPATHLEN - 1);
909 g_free (lc_link);
910 if (len > 0)
912 link_target[len] = 0;
913 tty_print_string ("-> ");
914 tty_print_string (str_fit_to_term (link_target, panel->widget.cols - 5, J_LEFT_FIT));
916 else
917 tty_print_string (str_fit_to_term (_("<readlink failed>"),
918 panel->widget.cols - 2, J_LEFT));
920 else if (strcmp (panel->dir.list[panel->selected].fname, "..") == 0)
922 /* FIXME:
923 * while loading directory (do_load_dir() and do_reload_dir()),
924 * the actual stat info about ".." directory isn't got;
925 * so just don't display incorrect info about ".." directory */
926 tty_print_string (str_fit_to_term (_("UP--DIR"), panel->widget.cols - 2, J_LEFT));
928 else
929 /* Default behavior */
930 repaint_file (panel, panel->selected, 0, STATUS, 1);
933 /* --------------------------------------------------------------------------------------------- */
935 static void
936 paint_dir (WPanel * panel)
938 int i;
939 int color; /* Color value of the line */
940 int items; /* Number of items */
942 items = llines (panel) * (panel->split ? 2 : 1);
944 for (i = 0; i < items; i++)
946 if (i + panel->top_file >= panel->count)
947 color = 0;
948 else
950 color = 2 * (panel->dir.list[i + panel->top_file].f.marked);
951 color += (panel->selected == i + panel->top_file && panel->active);
953 repaint_file (panel, i + panel->top_file, 1, color, 0);
955 tty_set_normal_attrs ();
958 /* --------------------------------------------------------------------------------------------- */
960 static void
961 display_total_marked_size (WPanel * panel, int y, int x, gboolean size_only)
963 char buffer[BUF_SMALL], b_bytes[BUF_SMALL], *buf;
964 int cols;
966 if (panel->marked <= 0)
967 return;
969 buf = size_only ? b_bytes : buffer;
970 cols = panel->widget.cols - 2;
973 * This is a trick to use two ngettext() calls in one sentence.
974 * First make "N bytes", then insert it into "X in M files".
976 g_snprintf (b_bytes, sizeof (b_bytes),
977 ngettext ("%s byte", "%s bytes", (unsigned long) panel->total),
978 size_trunc_sep (panel->total, panels_options.kilobyte_si));
979 if (!size_only)
980 g_snprintf (buffer, sizeof (buffer),
981 ngettext ("%s in %d file", "%s in %d files", panel->marked),
982 b_bytes, panel->marked);
984 /* don't forget spaces around buffer content */
985 buf = (char *) str_trunc (buf, cols - 4);
987 if (x < 0)
988 /* center in panel */
989 x = (panel->widget.cols - str_term_width1 (buf)) / 2 - 1;
992 * y == llines (panel) + 2 for mini_info_separator
993 * y == panel->widget.lines - 1 for panel bottom frame
995 widget_move (&panel->widget, y, x);
996 tty_setcolor (MARKED_COLOR);
997 tty_printf (" %s ", buf);
1000 /* --------------------------------------------------------------------------------------------- */
1002 static void
1003 mini_info_separator (WPanel * panel)
1005 if (show_mini_info)
1007 const int y = llines (panel) + 2;
1009 tty_setcolor (NORMAL_COLOR);
1010 tty_draw_hline (panel->widget.y + y, panel->widget.x + 1,
1011 ACS_HLINE, panel->widget.cols - 2);
1012 /* Status displays total marked size.
1013 * Centered in panel, full format. */
1014 display_total_marked_size (panel, y, -1, FALSE);
1018 /* --------------------------------------------------------------------------------------------- */
1020 static void
1021 show_free_space (WPanel * panel)
1023 /* Used to figure out how many free space we have */
1024 static struct my_statfs myfs_stats;
1025 /* Old current working directory for displaying free space */
1026 static char *old_cwd = NULL;
1028 /* Don't try to stat non-local fs */
1029 if (!vfs_file_is_local (panel->cwd) || !free_space)
1030 return;
1032 if (old_cwd == NULL || strcmp (old_cwd, panel->cwd) != 0)
1034 char rpath[PATH_MAX];
1036 init_my_statfs ();
1037 g_free (old_cwd);
1038 old_cwd = g_strdup (panel->cwd);
1040 if (mc_realpath (panel->cwd, rpath) == NULL)
1041 return;
1043 my_statfs (&myfs_stats, rpath);
1046 if (myfs_stats.avail > 0 || myfs_stats.total > 0)
1048 char buffer1[6], buffer2[6], tmp[BUF_SMALL];
1049 size_trunc_len (buffer1, sizeof (buffer1) - 1, myfs_stats.avail, 1,
1050 panels_options.kilobyte_si);
1051 size_trunc_len (buffer2, sizeof (buffer2) - 1, myfs_stats.total, 1,
1052 panels_options.kilobyte_si);
1053 g_snprintf (tmp, sizeof (tmp), " %s/%s (%d%%) ", buffer1, buffer2,
1054 myfs_stats.total >
1055 0 ? (int) (100 * (double) myfs_stats.avail / myfs_stats.total) : 0);
1056 widget_move (&panel->widget, panel->widget.lines - 1,
1057 panel->widget.cols - 2 - (int) strlen (tmp));
1058 tty_setcolor (NORMAL_COLOR);
1059 tty_print_string (tmp);
1063 /* --------------------------------------------------------------------------------------------- */
1065 static void
1066 show_dir (WPanel * panel)
1068 gchar *tmp;
1069 set_colors (panel);
1070 draw_box (panel->widget.owner,
1071 panel->widget.y, panel->widget.x, panel->widget.lines, panel->widget.cols, FALSE);
1073 if (show_mini_info)
1075 widget_move (&panel->widget, llines (panel) + 2, 0);
1076 tty_print_alt_char (ACS_LTEE, FALSE);
1077 widget_move (&panel->widget, llines (panel) + 2, panel->widget.cols - 1);
1078 tty_print_alt_char (ACS_RTEE, FALSE);
1081 widget_move (&panel->widget, 0, 1);
1082 tty_print_string (panel_history_prev_item_sign);
1084 tmp = panels_options.show_dot_files ? panel_hiddenfiles_sign_show : panel_hiddenfiles_sign_hide;
1085 tmp = g_strdup_printf ("%s[%s]%s", tmp, panel_history_show_list_sign,
1086 panel_history_next_item_sign);
1088 widget_move (&panel->widget, 0, panel->widget.cols - 6);
1089 tty_print_string (tmp);
1091 g_free (tmp);
1093 if (panel->active)
1094 tty_setcolor (REVERSE_COLOR);
1096 widget_move (&panel->widget, 0, 3);
1098 tty_printf (" %s ",
1099 str_term_trim (strip_home_and_password (panel->cwd),
1100 min (max (panel->widget.cols - 12, 0), panel->widget.cols)));
1102 if (!show_mini_info)
1104 if (panel->marked == 0)
1106 /* Show size of curret file in the bottom of panel */
1107 if (S_ISREG (panel->dir.list[panel->selected].st.st_mode))
1109 char buffer[BUF_SMALL];
1111 g_snprintf (buffer, sizeof (buffer), " %s ",
1112 size_trunc_sep (panel->dir.list[panel->selected].st.st_size,
1113 panels_options.kilobyte_si));
1114 tty_setcolor (NORMAL_COLOR);
1115 widget_move (&panel->widget, panel->widget.lines - 1, 4);
1116 tty_print_string (buffer);
1119 else
1121 /* Show total size of marked files
1122 * In the bottom of panel, display size only. */
1123 display_total_marked_size (panel, panel->widget.lines - 1, 2, TRUE);
1127 show_free_space (panel);
1129 if (panel->active)
1130 tty_set_normal_attrs ();
1133 /* --------------------------------------------------------------------------------------------- */
1134 /** To be used only by long_frame and full_frame to adjust top_file */
1136 static void
1137 adjust_top_file (WPanel * panel)
1139 int old_top = panel->top_file;
1141 if (panel->selected - old_top > llines (panel))
1142 panel->top_file = panel->selected;
1143 if (old_top - panel->count > llines (panel))
1144 panel->top_file = panel->count - llines (panel);
1147 /* --------------------------------------------------------------------------------------------- */
1148 /** Repaint everything, including frame and separator */
1150 static void
1151 paint_panel (WPanel * panel)
1153 paint_frame (panel); /* including show_dir */
1154 paint_dir (panel);
1155 mini_info_separator (panel);
1156 display_mini_info (panel);
1157 panel->dirty = 0;
1160 /* --------------------------------------------------------------------------------------------- */
1161 /** add "#enc:encodning" to end of path */
1162 /* if path end width a previous #enc:, only encoding is changed no additional
1163 * #enc: is appended
1164 * retun new string
1167 static char *
1168 add_encoding_to_path (const char *path, const char *encoding)
1170 char *result;
1171 char *semi;
1172 char *slash;
1174 semi = g_strrstr (path, VFS_ENCODING_PREFIX);
1176 if (semi != NULL)
1178 slash = strchr (semi, PATH_SEP);
1179 if (slash != NULL)
1181 result = g_strconcat (path, PATH_SEP_STR VFS_ENCODING_PREFIX, encoding, (char *) NULL);
1183 else
1185 *semi = '\0';
1186 result = g_strconcat (path, PATH_SEP_STR VFS_ENCODING_PREFIX, encoding, (char *) NULL);
1187 *semi = '#';
1190 else
1192 result = g_strconcat (path, PATH_SEP_STR VFS_ENCODING_PREFIX, encoding, (char *) NULL);
1195 return result;
1198 /* --------------------------------------------------------------------------------------------- */
1200 static char *
1201 panel_save_name (WPanel * panel)
1203 /* If the program is shuting down */
1204 if ((midnight_shutdown && auto_save_setup) || saving_setup)
1205 return g_strdup (panel->panel_name);
1206 else
1207 return g_strconcat ("Temporal:", panel->panel_name, (char *) NULL);
1210 /* --------------------------------------------------------------------------------------------- */
1212 static void
1213 panel_destroy (WPanel * p)
1215 size_t i;
1217 char *name = panel_save_name (p);
1219 panel_save_setup (p, name);
1220 panel_clean_dir (p);
1222 /* save and clean history */
1223 if (p->dir_history)
1225 history_put (p->hist_name, p->dir_history);
1227 p->dir_history = g_list_first (p->dir_history);
1228 g_list_foreach (p->dir_history, (GFunc) g_free, NULL);
1229 g_list_free (p->dir_history);
1232 g_free (p->hist_name);
1234 delete_format (p->format);
1235 delete_format (p->status_format);
1237 g_free (p->user_format);
1238 for (i = 0; i < LIST_TYPES; i++)
1239 g_free (p->user_status_format[i]);
1240 g_free (p->dir.list);
1241 g_free (p->panel_name);
1242 g_free (name);
1245 /* --------------------------------------------------------------------------------------------- */
1247 static void
1248 panel_format_modified (WPanel * panel)
1250 panel->format_modified = 1;
1253 /* --------------------------------------------------------------------------------------------- */
1255 static void
1256 panel_paint_sort_info (WPanel * panel)
1258 const char *sort_sign = (panel->reverse) ? panel_sort_down_sign : panel_sort_up_sign;
1259 char *str;
1261 if (*panel->current_sort_field->hotkey == '\0')
1262 return;
1264 str = g_strdup_printf ("%s%s", sort_sign, Q_ (panel->current_sort_field->hotkey));
1266 widget_move (&panel->widget, 1, 1);
1267 tty_print_string (str);
1268 g_free (str);
1271 /* --------------------------------------------------------------------------------------------- */
1273 static gchar *
1274 panel_get_title_without_hotkey (const char *title)
1276 char *translated_title;
1277 char *hkey;
1279 if (title == NULL)
1280 return NULL;
1281 if (title[0] == '\0')
1282 return g_strdup ("");
1284 translated_title = g_strdup (_(title));
1286 hkey = strchr (translated_title, '&');
1287 if ((hkey != NULL) && (hkey[1] != '\0'))
1288 memmove ((void *) hkey, (void *) hkey + 1, strlen (hkey));
1290 return translated_title;
1293 /* --------------------------------------------------------------------------------------------- */
1295 static void
1296 paint_frame (WPanel * panel)
1298 int side, width;
1299 GString *format_txt;
1301 if (!panel->split)
1302 adjust_top_file (panel);
1304 widget_erase (&panel->widget);
1305 show_dir (panel);
1307 widget_move (&panel->widget, 1, 1);
1309 for (side = 0; side <= panel->split; side++)
1311 format_e *format;
1313 if (side)
1315 tty_setcolor (NORMAL_COLOR);
1316 tty_print_one_vline (TRUE);
1317 width = panel->widget.cols - panel->widget.cols / 2 - 1;
1319 else if (panel->split)
1320 width = panel->widget.cols / 2 - 3;
1321 else
1322 width = panel->widget.cols - 2;
1324 format_txt = g_string_new ("");
1325 for (format = panel->format; format; format = format->next)
1327 if (format->string_fn)
1329 g_string_set_size (format_txt, 0);
1331 if (panel->list_type == list_long
1332 && strcmp (format->id, panel->current_sort_field->id) == 0)
1333 g_string_append (format_txt,
1334 (panel->reverse) ? panel_sort_down_sign : panel_sort_up_sign);
1336 g_string_append (format_txt, format->title);
1337 if (strcmp (format->id, "name") == 0 && panel->filter && *panel->filter)
1339 g_string_append (format_txt, " [");
1340 g_string_append (format_txt, panel->filter);
1341 g_string_append (format_txt, "]");
1344 tty_setcolor (HEADER_COLOR);
1345 tty_print_string (str_fit_to_term (format_txt->str, format->field_len,
1346 J_CENTER_LEFT));
1347 width -= format->field_len;
1349 else
1351 tty_setcolor (NORMAL_COLOR);
1352 tty_print_one_vline (TRUE);
1353 width--;
1356 g_string_free (format_txt, TRUE);
1358 if (width > 0)
1359 tty_draw_hline (-1, -1, ' ', width);
1362 if (panel->list_type != list_long)
1363 panel_paint_sort_info (panel);
1366 /* --------------------------------------------------------------------------------------------- */
1368 static const char *
1369 parse_panel_size (WPanel * panel, const char *format, int isstatus)
1371 int frame = frame_half;
1372 format = skip_separators (format);
1374 if (!strncmp (format, "full", 4))
1376 frame = frame_full;
1377 format += 4;
1379 else if (!strncmp (format, "half", 4))
1381 frame = frame_half;
1382 format += 4;
1385 if (!isstatus)
1387 panel->frame_size = frame;
1388 panel->split = 0;
1391 /* Now, the optional column specifier */
1392 format = skip_separators (format);
1394 if (*format == '1' || *format == '2')
1396 if (!isstatus)
1397 panel->split = *format == '2';
1398 format++;
1401 if (!isstatus)
1402 panel_update_cols (&(panel->widget), panel->frame_size);
1404 return skip_separators (format);
1407 /* Format is:
1409 all := panel_format? format
1410 panel_format := [full|half] [1|2]
1411 format := one_format_e
1412 | format , one_format_e
1414 one_format_e := just format.id [opt_size]
1415 just := [<=>]
1416 opt_size := : size [opt_expand]
1417 size := [0-9]+
1418 opt_expand := +
1422 /* --------------------------------------------------------------------------------------------- */
1424 static format_e *
1425 parse_display_format (WPanel * panel, const char *format, char **error, int isstatus,
1426 int *res_total_cols)
1428 format_e *darr, *old = 0, *home = 0; /* The formats we return */
1429 int total_cols = 0; /* Used columns by the format */
1430 int set_justify; /* flag: set justification mode? */
1431 align_crt_t justify = J_LEFT; /* Which mode. */
1432 size_t i;
1434 static size_t i18n_timelength = 0; /* flag: check ?Time length at startup */
1436 *error = 0;
1438 if (i18n_timelength == 0)
1440 i18n_timelength = i18n_checktimelength (); /* Musn't be 0 */
1442 for (i = 0; panel_fields[i].id != NULL; i++)
1443 if (strcmp ("time", panel_fields[i].id + 1) == 0)
1444 panel_fields[i].min_size = i18n_timelength;
1448 * This makes sure that the panel and mini status full/half mode
1449 * setting is equal
1451 format = parse_panel_size (panel, format, isstatus);
1453 while (*format)
1454 { /* format can be an empty string */
1455 int found = 0;
1457 darr = g_new0 (format_e, 1);
1459 /* I'm so ugly, don't look at me :-) */
1460 if (!home)
1461 home = old = darr;
1463 old->next = darr;
1464 darr->next = 0;
1465 old = darr;
1467 format = skip_separators (format);
1469 if (strchr ("<=>", *format))
1471 set_justify = 1;
1472 switch (*format)
1474 case '<':
1475 justify = J_LEFT;
1476 break;
1477 case '=':
1478 justify = J_CENTER;
1479 break;
1480 case '>':
1481 default:
1482 justify = J_RIGHT;
1483 break;
1485 format = skip_separators (format + 1);
1487 else
1488 set_justify = 0;
1490 for (i = 0; panel_fields[i].id != NULL; i++)
1492 size_t klen = strlen (panel_fields[i].id);
1494 if (strncmp (format, panel_fields[i].id, klen) != 0)
1495 continue;
1497 format += klen;
1499 darr->requested_field_len = panel_fields[i].min_size;
1500 darr->string_fn = panel_fields[i].string_fn;
1501 darr->title = panel_get_title_without_hotkey (panel_fields[i].title_hotkey);
1503 darr->id = panel_fields[i].id;
1504 darr->expand = panel_fields[i].expands;
1505 darr->just_mode = panel_fields[i].default_just;
1507 if (set_justify)
1509 if (IS_FIT (darr->just_mode))
1510 darr->just_mode = MAKE_FIT (justify);
1511 else
1512 darr->just_mode = justify;
1514 found = 1;
1516 format = skip_separators (format);
1518 /* If we have a size specifier */
1519 if (*format == ':')
1521 int req_length;
1523 /* If the size was specified, we don't want
1524 * auto-expansion by default
1526 darr->expand = 0;
1527 format++;
1528 req_length = atoi (format);
1529 darr->requested_field_len = req_length;
1531 format = skip_numbers (format);
1533 /* Now, if they insist on expansion */
1534 if (*format == '+')
1536 darr->expand = 1;
1537 format++;
1542 break;
1544 if (!found)
1546 char *tmp_format = g_strdup (format);
1548 int pos = min (8, strlen (format));
1549 delete_format (home);
1550 tmp_format[pos] = 0;
1551 *error =
1552 g_strconcat (_("Unknown tag on display format:"), " ", tmp_format, (char *) NULL);
1553 g_free (tmp_format);
1554 return 0;
1556 total_cols += darr->requested_field_len;
1559 *res_total_cols = total_cols;
1560 return home;
1563 /* --------------------------------------------------------------------------------------------- */
1565 static format_e *
1566 use_display_format (WPanel * panel, const char *format, char **error, int isstatus)
1568 #define MAX_EXPAND 4
1569 int expand_top = 0; /* Max used element in expand */
1570 int usable_columns; /* Usable columns in the panel */
1571 int total_cols = 0;
1572 int i;
1573 format_e *darr, *home;
1575 if (!format)
1576 format = DEFAULT_USER_FORMAT;
1578 home = parse_display_format (panel, format, error, isstatus, &total_cols);
1580 if (*error)
1581 return 0;
1583 panel->dirty = 1;
1585 /* Status needn't to be split */
1586 usable_columns = ((panel->widget.cols - 2) / ((isstatus)
1588 : (panel->split + 1))) - (!isstatus
1589 && panel->split);
1591 /* Look for the expandable fields and set field_len based on the requested field len */
1592 for (darr = home; darr && expand_top < MAX_EXPAND; darr = darr->next)
1594 darr->field_len = darr->requested_field_len;
1595 if (darr->expand)
1596 expand_top++;
1599 /* If we used more columns than the available columns, adjust that */
1600 if (total_cols > usable_columns)
1602 int pdif, dif = total_cols - usable_columns;
1604 while (dif)
1606 pdif = dif;
1607 for (darr = home; darr; darr = darr->next)
1609 if (dif && darr->field_len - 1)
1611 darr->field_len--;
1612 dif--;
1616 /* avoid endless loop if num fields > 40 */
1617 if (pdif == dif)
1618 break;
1620 total_cols = usable_columns; /* give up, the rest should be truncated */
1623 /* Expand the available space */
1624 if ((usable_columns > total_cols) && expand_top)
1626 int spaces = (usable_columns - total_cols) / expand_top;
1627 int extra = (usable_columns - total_cols) % expand_top;
1629 for (i = 0, darr = home; darr && (i < expand_top); darr = darr->next)
1630 if (darr->expand)
1632 darr->field_len += (spaces + ((i == 0) ? extra : 0));
1633 i++;
1636 return home;
1639 /* --------------------------------------------------------------------------------------------- */
1640 /** Given the panel->view_type returns the format string to be parsed */
1642 static const char *
1643 panel_format (WPanel * panel)
1645 switch (panel->list_type)
1648 case list_long:
1649 return "full perm space nlink space owner space group space size space mtime space name";
1651 case list_brief:
1652 return "half 2 type name";
1654 case list_user:
1655 return panel->user_format;
1657 default:
1658 case list_full:
1659 return "half type name | size | mtime";
1663 /* --------------------------------------------------------------------------------------------- */
1665 static const char *
1666 mini_status_format (WPanel * panel)
1668 if (panel->user_mini_status)
1669 return panel->user_status_format[panel->list_type];
1671 switch (panel->list_type)
1674 case list_long:
1675 return "full perm space nlink space owner space group space size space mtime space name";
1677 case list_brief:
1678 return "half type name space bsize space perm space";
1680 case list_full:
1681 return "half type name";
1683 default:
1684 case list_user:
1685 return panel->user_format;
1689 /* */
1690 /* Panel operation commands */
1691 /* */
1693 /* --------------------------------------------------------------------------------------------- */
1694 /** Used to emulate Lynx's entering leaving a directory with the arrow keys */
1696 static cb_ret_t
1697 maybe_cd (int move_up_dir)
1699 if (panels_options.navigate_with_arrows && (cmdline->buffer[0] == '\0'))
1701 if (move_up_dir)
1703 do_cd ("..", cd_exact);
1704 return MSG_HANDLED;
1707 if (S_ISDIR (selection (current_panel)->st.st_mode)
1708 || link_isdir (selection (current_panel)))
1710 do_cd (selection (current_panel)->fname, cd_exact);
1711 return MSG_HANDLED;
1714 return MSG_NOT_HANDLED;
1717 /* --------------------------------------------------------------------------------------------- */
1718 /** Returns the number of items in the given panel */
1720 static int
1721 ITEMS (WPanel * p)
1723 if (p->split)
1724 return llines (p) * 2;
1725 else
1726 return llines (p);
1729 /* --------------------------------------------------------------------------------------------- */
1731 static void
1732 unselect_item (WPanel * panel)
1734 repaint_file (panel, panel->selected, 1, 2 * selection (panel)->f.marked, 0);
1737 /* --------------------------------------------------------------------------------------------- */
1739 static void
1740 move_down (WPanel * panel)
1742 if (panel->selected + 1 == panel->count)
1743 return;
1745 unselect_item (panel);
1746 panel->selected++;
1747 if (panels_options.scroll_pages && panel->selected - panel->top_file == ITEMS (panel))
1749 /* Scroll window half screen */
1750 panel->top_file += ITEMS (panel) / 2;
1751 if (panel->top_file > panel->count - ITEMS (panel))
1752 panel->top_file = panel->count - ITEMS (panel);
1753 paint_dir (panel);
1755 select_item (panel);
1758 /* --------------------------------------------------------------------------------------------- */
1760 static void
1761 move_up (WPanel * panel)
1763 if (panel->selected == 0)
1764 return;
1766 unselect_item (panel);
1767 panel->selected--;
1768 if (panels_options.scroll_pages && panel->selected < panel->top_file)
1770 /* Scroll window half screen */
1771 panel->top_file -= ITEMS (panel) / 2;
1772 if (panel->top_file < 0)
1773 panel->top_file = 0;
1774 paint_dir (panel);
1776 select_item (panel);
1779 /* --------------------------------------------------------------------------------------------- */
1780 /** Changes the selection by lines (may be negative) */
1782 static void
1783 move_selection (WPanel * panel, int lines)
1785 int new_pos;
1786 int adjust = 0;
1788 new_pos = panel->selected + lines;
1789 if (new_pos >= panel->count)
1790 new_pos = panel->count - 1;
1792 if (new_pos < 0)
1793 new_pos = 0;
1795 unselect_item (panel);
1796 panel->selected = new_pos;
1798 if (panel->selected - panel->top_file >= ITEMS (panel))
1800 panel->top_file += lines;
1801 adjust = 1;
1804 if (panel->selected - panel->top_file < 0)
1806 panel->top_file += lines;
1807 adjust = 1;
1810 if (adjust)
1812 if (panel->top_file > panel->selected)
1813 panel->top_file = panel->selected;
1814 if (panel->top_file < 0)
1815 panel->top_file = 0;
1816 paint_dir (panel);
1818 select_item (panel);
1821 /* --------------------------------------------------------------------------------------------- */
1823 static cb_ret_t
1824 move_left (WPanel * panel)
1826 if (panel->split)
1828 move_selection (panel, -llines (panel));
1829 return MSG_HANDLED;
1831 else
1832 return maybe_cd (1); /* cd .. */
1835 /* --------------------------------------------------------------------------------------------- */
1837 static int
1838 move_right (WPanel * panel)
1840 if (panel->split)
1842 move_selection (panel, llines (panel));
1843 return MSG_HANDLED;
1845 else
1846 return maybe_cd (0); /* cd (selection) */
1849 /* --------------------------------------------------------------------------------------------- */
1851 static void
1852 prev_page (WPanel * panel)
1854 int items;
1856 if (!panel->selected && !panel->top_file)
1857 return;
1858 unselect_item (panel);
1859 items = ITEMS (panel);
1860 if (panel->top_file < items)
1861 items = panel->top_file;
1862 if (!items)
1863 panel->selected = 0;
1864 else
1865 panel->selected -= items;
1866 panel->top_file -= items;
1868 select_item (panel);
1869 paint_dir (panel);
1872 /* --------------------------------------------------------------------------------------------- */
1874 static void
1875 ctrl_prev_page (WPanel * panel)
1877 (void) panel;
1878 do_cd ("..", cd_exact);
1881 /* --------------------------------------------------------------------------------------------- */
1883 static void
1884 next_page (WPanel * panel)
1886 int items;
1888 if (panel->selected == panel->count - 1)
1889 return;
1890 unselect_item (panel);
1891 items = ITEMS (panel);
1892 if (panel->top_file > panel->count - 2 * items)
1893 items = panel->count - items - panel->top_file;
1894 if (panel->top_file + items < 0)
1895 items = -panel->top_file;
1896 if (!items)
1897 panel->selected = panel->count - 1;
1898 else
1899 panel->selected += items;
1900 panel->top_file += items;
1902 select_item (panel);
1903 paint_dir (panel);
1906 /* --------------------------------------------------------------------------------------------- */
1908 static void
1909 ctrl_next_page (WPanel * panel)
1911 if ((S_ISDIR (selection (panel)->st.st_mode) || link_isdir (selection (panel))))
1913 do_cd (selection (panel)->fname, cd_exact);
1917 /* --------------------------------------------------------------------------------------------- */
1919 static void
1920 goto_top_file (WPanel * panel)
1922 unselect_item (panel);
1923 panel->selected = panel->top_file;
1924 select_item (panel);
1927 /* --------------------------------------------------------------------------------------------- */
1929 static void
1930 goto_middle_file (WPanel * panel)
1932 unselect_item (panel);
1933 panel->selected = panel->top_file + (ITEMS (panel) / 2);
1934 select_item (panel);
1937 /* --------------------------------------------------------------------------------------------- */
1939 static void
1940 goto_bottom_file (WPanel * panel)
1942 unselect_item (panel);
1943 panel->selected = panel->top_file + ITEMS (panel) - 1;
1944 select_item (panel);
1947 /* --------------------------------------------------------------------------------------------- */
1949 static void
1950 move_home (WPanel * panel)
1952 if (panel->selected == 0)
1953 return;
1954 unselect_item (panel);
1956 if (torben_fj_mode)
1958 int middle_pos = panel->top_file + (ITEMS (panel) / 2);
1960 if (panel->selected > middle_pos)
1962 goto_middle_file (panel);
1963 return;
1965 if (panel->selected != panel->top_file)
1967 goto_top_file (panel);
1968 return;
1972 panel->top_file = 0;
1973 panel->selected = 0;
1975 paint_dir (panel);
1976 select_item (panel);
1979 /* --------------------------------------------------------------------------------------------- */
1981 static void
1982 move_end (WPanel * panel)
1984 if (panel->selected == panel->count - 1)
1985 return;
1986 unselect_item (panel);
1987 if (torben_fj_mode)
1989 int middle_pos = panel->top_file + (ITEMS (panel) / 2);
1991 if (panel->selected < middle_pos)
1993 goto_middle_file (panel);
1994 return;
1996 if (panel->selected != (panel->top_file + ITEMS (panel) - 1))
1998 goto_bottom_file (panel);
1999 return;
2003 panel->selected = panel->count - 1;
2004 paint_dir (panel);
2005 select_item (panel);
2008 /* --------------------------------------------------------------------------------------------- */
2010 static void
2011 do_mark_file (WPanel * panel, mark_act_t do_move)
2013 do_file_mark (panel, panel->selected, selection (panel)->f.marked ? 0 : 1);
2014 if ((panels_options.mark_moves_down && do_move == MARK_DOWN) || do_move == MARK_FORCE_DOWN)
2015 move_down (panel);
2016 else if (do_move == MARK_FORCE_UP)
2017 move_up (panel);
2020 /* --------------------------------------------------------------------------------------------- */
2022 static void
2023 mark_file (WPanel * panel)
2025 do_mark_file (panel, MARK_DOWN);
2028 /* --------------------------------------------------------------------------------------------- */
2030 static void
2031 mark_file_up (WPanel * panel)
2033 do_mark_file (panel, MARK_FORCE_UP);
2036 /* --------------------------------------------------------------------------------------------- */
2038 static void
2039 mark_file_down (WPanel * panel)
2041 do_mark_file (panel, MARK_FORCE_DOWN);
2044 /* --------------------------------------------------------------------------------------------- */
2045 /** Incremental search of a file name in the panel.
2046 * @param panel instance of WPanel structure
2047 * @param c_code key code
2050 static void
2051 do_search (WPanel * panel, int c_code)
2053 size_t l;
2054 int i, sel;
2055 gboolean wrapped = FALSE;
2056 char *act;
2057 mc_search_t *search;
2058 char *reg_exp, *esc_str;
2059 gboolean is_found = FALSE;
2061 l = strlen (panel->search_buffer);
2062 if (c_code == KEY_BACKSPACE)
2064 if (l != 0)
2066 act = panel->search_buffer + l;
2067 str_prev_noncomb_char (&act, panel->search_buffer);
2068 act[0] = '\0';
2070 panel->search_chpoint = 0;
2072 else
2074 if (c_code != 0 && (gsize) panel->search_chpoint < sizeof (panel->search_char))
2076 panel->search_char[panel->search_chpoint] = c_code;
2077 panel->search_chpoint++;
2080 if (panel->search_chpoint > 0)
2082 switch (str_is_valid_char (panel->search_char, panel->search_chpoint))
2084 case -2:
2085 return;
2086 case -1:
2087 panel->search_chpoint = 0;
2088 return;
2089 default:
2090 if (l + panel->search_chpoint < sizeof (panel->search_buffer))
2092 memcpy (panel->search_buffer + l, panel->search_char, panel->search_chpoint);
2093 l += panel->search_chpoint;
2094 *(panel->search_buffer + l) = '\0';
2095 panel->search_chpoint = 0;
2101 reg_exp = g_strdup_printf ("%s*", panel->search_buffer);
2102 esc_str = strutils_escape (reg_exp, -1, ",|\\{}[]", TRUE);
2103 search = mc_search_new (esc_str, -1);
2104 search->search_type = MC_SEARCH_T_GLOB;
2105 search->is_entire_line = TRUE;
2106 switch (panels_options.qsearch_mode)
2108 case QSEARCH_CASE_SENSITIVE:
2109 search->is_case_sensitive = TRUE;
2110 break;
2111 case QSEARCH_CASE_INSENSITIVE:
2112 search->is_case_sensitive = FALSE;
2113 break;
2114 default:
2115 search->is_case_sensitive = panel->case_sensitive;
2116 break;
2118 sel = panel->selected;
2119 for (i = panel->selected; !wrapped || i != panel->selected; i++)
2121 if (i >= panel->count)
2123 i = 0;
2124 if (wrapped)
2125 break;
2126 wrapped = TRUE;
2128 if (mc_search_run (search, panel->dir.list[i].fname, 0, panel->dir.list[i].fnamelen, NULL))
2130 sel = i;
2131 is_found = TRUE;
2132 break;
2135 if (is_found)
2137 unselect_item (panel);
2138 panel->selected = sel;
2139 select_item (panel);
2140 paint_panel (panel);
2142 else if (c_code != KEY_BACKSPACE)
2144 act = panel->search_buffer + l;
2145 str_prev_noncomb_char (&act, panel->search_buffer);
2146 act[0] = '\0';
2148 mc_search_free (search);
2149 g_free (reg_exp);
2150 g_free (esc_str);
2153 /* --------------------------------------------------------------------------------------------- */
2154 /** Start new search.
2155 * @param panel instance of WPanel structure
2158 static void
2159 start_search (WPanel * panel)
2161 if (panel->searching)
2163 if (panel->selected + 1 == panel->count)
2164 panel->selected = 0;
2165 else
2166 move_down (panel);
2168 /* in case if there was no search string we need to recall
2169 previous string, with which we ended previous searching */
2170 if (panel->search_buffer[0] == '\0')
2171 g_strlcpy (panel->search_buffer, panel->prev_search_buffer,
2172 sizeof (panel->search_buffer));
2174 do_search (panel, 0);
2176 else
2178 panel->searching = TRUE;
2179 panel->search_buffer[0] = '\0';
2180 panel->search_char[0] = '\0';
2181 panel->search_chpoint = 0;
2182 display_mini_info (panel);
2183 mc_refresh ();
2187 /* --------------------------------------------------------------------------------------------- */
2189 static void
2190 stop_search (WPanel * panel)
2192 panel->searching = FALSE;
2194 /* if user had overrdied search string, we need to store it
2195 to the previous_search_buffer */
2196 if (panel->search_buffer[0] != '\0')
2197 g_strlcpy (panel->prev_search_buffer, panel->search_buffer,
2198 sizeof (panel->prev_search_buffer));
2200 display_mini_info (panel);
2203 /* --------------------------------------------------------------------------------------------- */
2204 /** Return 1 if the Enter key has been processed, 0 otherwise */
2206 static int
2207 do_enter_on_file_entry (file_entry * fe)
2209 char *full_name;
2212 * Directory or link to directory - change directory.
2213 * Try the same for the entries on which mc_lstat() has failed.
2215 if (S_ISDIR (fe->st.st_mode) || link_isdir (fe) || (fe->st.st_mode == 0))
2217 if (!do_cd (fe->fname, cd_exact))
2218 message (D_ERROR, MSG_ERROR, _("Cannot change directory"));
2219 return 1;
2222 /* Try associated command */
2223 if (regex_command (fe->fname, "Open", NULL) != 0)
2224 return 1;
2226 /* Check if the file is executable */
2227 full_name = concat_dir_and_file (current_panel->cwd, fe->fname);
2228 if (!is_exe (fe->st.st_mode) || !if_link_is_exe (full_name, fe))
2230 g_free (full_name);
2231 return 0;
2233 g_free (full_name);
2235 if (confirm_execute)
2237 if (query_dialog
2238 (_("The Midnight Commander"),
2239 _("Do you really want to execute?"), D_NORMAL, 2, _("&Yes"), _("&No")) != 0)
2240 return 1;
2243 if (!vfs_current_is_local ())
2245 char *tmp;
2246 int ret;
2248 tmp = concat_dir_and_file (vfs_get_current_dir (), fe->fname);
2249 ret = mc_setctl (tmp, VFS_SETCTL_RUN, NULL);
2250 g_free (tmp);
2251 /* We took action only if the dialog was shown or the execution
2252 * was successful */
2253 return confirm_execute || (ret == 0);
2257 char *tmp = name_quote (fe->fname, 0);
2258 char *cmd = g_strconcat (".", PATH_SEP_STR, tmp, (char *) NULL);
2259 g_free (tmp);
2260 shell_execute (cmd, 0);
2261 g_free (cmd);
2264 #if HAVE_CHARSET
2265 source_codepage = default_source_codepage;
2266 #endif
2268 return 1;
2271 /* --------------------------------------------------------------------------------------------- */
2273 static int
2274 do_enter (WPanel * panel)
2276 return do_enter_on_file_entry (selection (panel));
2279 /* --------------------------------------------------------------------------------------------- */
2281 static void
2282 chdir_other_panel (WPanel * panel)
2284 char *new_dir;
2285 char *sel_entry = NULL;
2287 if (get_other_type () != view_listing)
2289 set_display_type (get_other_index (), view_listing);
2292 if (!S_ISDIR (panel->dir.list[panel->selected].st.st_mode))
2294 new_dir = concat_dir_and_file (panel->cwd, "..");
2295 sel_entry = strrchr (panel->cwd, PATH_SEP);
2297 else
2298 new_dir = concat_dir_and_file (panel->cwd, panel->dir.list[panel->selected].fname);
2300 change_panel ();
2301 do_cd (new_dir, cd_exact);
2302 if (sel_entry)
2303 try_to_select (current_panel, sel_entry);
2304 change_panel ();
2306 move_down (panel);
2308 g_free (new_dir);
2311 /* --------------------------------------------------------------------------------------------- */
2313 * Make the current directory of the current panel also the current
2314 * directory of the other panel. Put the other panel to the listing
2315 * mode if needed. If the current panel is panelized, the other panel
2316 * doesn't become panelized.
2319 static void
2320 sync_other_panel (WPanel * panel)
2322 if (get_other_type () != view_listing)
2324 set_display_type (get_other_index (), view_listing);
2327 do_panel_cd (other_panel, current_panel->cwd, cd_exact);
2329 /* try to select current filename on the other panel */
2330 if (!panel->is_panelized)
2332 try_to_select (other_panel, selection (panel)->fname);
2336 /* --------------------------------------------------------------------------------------------- */
2338 static void
2339 chdir_to_readlink (WPanel * panel)
2341 char *new_dir;
2343 if (get_other_type () != view_listing)
2344 return;
2346 if (S_ISLNK (panel->dir.list[panel->selected].st.st_mode))
2348 char buffer[MC_MAXPATHLEN], *p;
2349 int i;
2350 struct stat st;
2352 i = readlink (selection (panel)->fname, buffer, MC_MAXPATHLEN - 1);
2353 if (i < 0)
2354 return;
2355 if (mc_stat (selection (panel)->fname, &st) < 0)
2356 return;
2357 buffer[i] = 0;
2358 if (!S_ISDIR (st.st_mode))
2360 p = strrchr (buffer, PATH_SEP);
2361 if (p && !p[1])
2363 *p = 0;
2364 p = strrchr (buffer, PATH_SEP);
2366 if (!p)
2367 return;
2368 p[1] = 0;
2370 if (*buffer == PATH_SEP)
2371 new_dir = g_strdup (buffer);
2372 else
2373 new_dir = concat_dir_and_file (panel->cwd, buffer);
2375 change_panel ();
2376 do_cd (new_dir, cd_exact);
2377 change_panel ();
2379 move_down (panel);
2381 g_free (new_dir);
2385 /* --------------------------------------------------------------------------------------------- */
2387 static gsize
2388 panel_get_format_field_count (WPanel * panel)
2390 format_e *format;
2391 gsize lc_index;
2392 for (lc_index = 0, format = panel->format; format != NULL; format = format->next, lc_index++);
2393 return lc_index;
2396 /* --------------------------------------------------------------------------------------------- */
2398 function return 0 if not found and REAL_INDEX+1 if found
2401 static gsize
2402 panel_get_format_field_index_by_name (WPanel * panel, const char *name)
2404 format_e *format;
2405 gsize lc_index;
2407 for (lc_index = 1, format = panel->format;
2408 !(format == NULL || strcmp (format->title, name) == 0); format = format->next, lc_index++);
2409 if (format == NULL)
2410 lc_index = 0;
2412 return lc_index;
2415 /* --------------------------------------------------------------------------------------------- */
2417 static format_e *
2418 panel_get_format_field_by_index (WPanel * panel, gsize lc_index)
2420 format_e *format;
2421 for (format = panel->format;
2422 !(format == NULL || lc_index == 0); format = format->next, lc_index--);
2423 return format;
2426 /* --------------------------------------------------------------------------------------------- */
2428 static const panel_field_t *
2429 panel_get_sortable_field_by_format (WPanel * panel, gsize lc_index)
2431 const panel_field_t *pfield;
2432 format_e *format;
2434 format = panel_get_format_field_by_index (panel, lc_index);
2435 if (format == NULL)
2436 return NULL;
2437 pfield = panel_get_field_by_title (format->title);
2438 if (pfield == NULL)
2439 return NULL;
2440 if (pfield->sort_routine == NULL)
2441 return NULL;
2442 return pfield;
2445 /* --------------------------------------------------------------------------------------------- */
2447 static void
2448 panel_toggle_sort_order_prev (WPanel * panel)
2450 gsize lc_index, i;
2451 gchar *title;
2453 const panel_field_t *pfield = NULL;
2455 title = panel_get_title_without_hotkey (panel->current_sort_field->title_hotkey);
2456 lc_index = panel_get_format_field_index_by_name (panel, title);
2457 g_free (title);
2459 if (lc_index > 1)
2461 /* search for prev sortable column in panel format */
2462 for (i = lc_index - 1;
2463 i != 0 && (pfield = panel_get_sortable_field_by_format (panel, i - 1)) == NULL; i--);
2466 if (pfield == NULL)
2468 /* Sortable field not found. Try to search in each array */
2469 for (i = panel_get_format_field_count (panel);
2470 i != 0 && (pfield = panel_get_sortable_field_by_format (panel, i - 1)) == NULL; i--);
2472 if (pfield == NULL)
2473 return;
2474 panel->current_sort_field = pfield;
2475 panel_set_sort_order (panel, panel->current_sort_field);
2478 /* --------------------------------------------------------------------------------------------- */
2480 static void
2481 panel_toggle_sort_order_next (WPanel * panel)
2483 gsize lc_index, i;
2484 const panel_field_t *pfield = NULL;
2485 gsize format_field_count = panel_get_format_field_count (panel);
2486 gchar *title;
2488 title = panel_get_title_without_hotkey (panel->current_sort_field->title_hotkey);
2489 lc_index = panel_get_format_field_index_by_name (panel, title);
2490 g_free (title);
2492 if (lc_index != 0 && lc_index != format_field_count)
2494 /* search for prev sortable column in panel format */
2495 for (i = lc_index;
2496 i != format_field_count
2497 && (pfield = panel_get_sortable_field_by_format (panel, i)) == NULL; i++);
2500 if (pfield == NULL)
2502 /* Sortable field not found. Try to search in each array */
2503 for (i = 0;
2504 i != format_field_count
2505 && (pfield = panel_get_sortable_field_by_format (panel, i)) == NULL; i++);
2507 if (pfield == NULL)
2508 return;
2509 panel->current_sort_field = pfield;
2510 panel_set_sort_order (panel, panel->current_sort_field);
2513 /* --------------------------------------------------------------------------------------------- */
2515 static void
2516 panel_select_sort_order (WPanel * panel)
2518 const panel_field_t *sort_order;
2519 sort_order = sort_box (panel->current_sort_field, &panel->reverse,
2520 &panel->case_sensitive, &panel->exec_first);
2521 if (sort_order == NULL)
2522 return;
2523 panel->current_sort_field = sort_order;
2524 panel_set_sort_order (panel, panel->current_sort_field);
2528 /* --------------------------------------------------------------------------------------------- */
2530 static void
2531 panel_set_sort_type_by_id (WPanel * panel, const char *name)
2533 const panel_field_t *sort_order;
2535 if (strcmp (panel->current_sort_field->id, name) != 0)
2537 sort_order = panel_get_field_by_id (name);
2538 if (sort_order == NULL)
2539 return;
2540 panel->current_sort_field = sort_order;
2542 else
2544 panel->reverse = !panel->reverse;
2546 panel_set_sort_order (panel, panel->current_sort_field);
2549 /* --------------------------------------------------------------------------------------------- */
2551 * If we moved to the parent directory move the selection pointer to
2552 * the old directory name; If we leave VFS dir, remove FS specificator.
2554 * You do _NOT_ want to add any vfs aware code here. <pavel@ucw.cz>
2557 static const char *
2558 get_parent_dir_name (const char *cwd, const char *lwd)
2560 size_t llen, clen;
2562 llen = strlen (lwd);
2563 clen = strlen (cwd);
2565 if (llen > clen)
2567 const char *p;
2569 p = strrchr (lwd, PATH_SEP);
2571 if ((p != NULL)
2572 && (strncmp (cwd, lwd, (size_t) (p - lwd)) == 0)
2573 && (clen == (size_t) (p - lwd)
2574 || ((p == lwd) && (cwd[0] == PATH_SEP) && (cwd[1] == '\0'))))
2575 return (p + 1);
2578 return NULL;
2581 /* --------------------------------------------------------------------------------------------- */
2582 /** Wrapper for do_subshell_chdir, check for availability of subshell */
2584 static void
2585 subshell_chdir (const char *directory)
2587 #ifdef HAVE_SUBSHELL_SUPPORT
2588 if (use_subshell && vfs_current_is_local ())
2589 do_subshell_chdir (directory, FALSE, TRUE);
2590 #endif /* HAVE_SUBSHELL_SUPPORT */
2593 /* --------------------------------------------------------------------------------------------- */
2595 * Changes the current directory of the panel.
2596 * Don't record change in the directory history.
2599 static gboolean
2600 _do_panel_cd (WPanel * panel, const char *new_dir, enum cd_enum cd_type)
2602 const char *directory;
2603 char *olddir;
2604 char temp[MC_MAXPATHLEN];
2605 char *translated_url;
2607 if (cd_type == cd_parse_command)
2609 while (*new_dir == ' ')
2610 new_dir++;
2613 olddir = g_strdup (panel->cwd);
2614 new_dir = translated_url = vfs_translate_url (new_dir);
2616 /* Convert *new_path to a suitable pathname, handle ~user */
2618 if (cd_type == cd_parse_command)
2620 if (!strcmp (new_dir, "-"))
2622 strcpy (temp, panel->lwd);
2623 new_dir = temp;
2626 directory = *new_dir ? new_dir : home_dir;
2628 if (mc_chdir (directory) == -1)
2630 strcpy (panel->cwd, olddir);
2631 g_free (olddir);
2632 g_free (translated_url);
2633 return FALSE;
2635 g_free (translated_url);
2637 /* Success: save previous directory, shutdown status of previous dir */
2638 strcpy (panel->lwd, olddir);
2639 input_free_completions (cmdline);
2641 mc_get_current_wd (panel->cwd, sizeof (panel->cwd) - 2);
2643 vfs_release_path (olddir);
2645 subshell_chdir (panel->cwd);
2647 /* Reload current panel */
2648 panel_clean_dir (panel);
2649 panel->count =
2650 do_load_dir (panel->cwd, &panel->dir, panel->current_sort_field->sort_routine,
2651 panel->reverse, panel->case_sensitive, panel->exec_first, panel->filter);
2652 try_to_select (panel, get_parent_dir_name (panel->cwd, olddir));
2653 load_hint (0);
2654 panel->dirty = 1;
2655 update_xterm_title_path ();
2657 g_free (olddir);
2659 return TRUE;
2662 /* --------------------------------------------------------------------------------------------- */
2664 static void
2665 directory_history_next (WPanel * panel)
2667 GList *nextdir;
2669 nextdir = g_list_next (panel->dir_history);
2671 if ((nextdir != NULL) && (_do_panel_cd (panel, (char *) nextdir->data, cd_exact)))
2672 panel->dir_history = nextdir;
2675 /* --------------------------------------------------------------------------------------------- */
2677 static void
2678 directory_history_prev (WPanel * panel)
2680 GList *prevdir;
2682 prevdir = g_list_previous (panel->dir_history);
2684 if ((prevdir != NULL) && (_do_panel_cd (panel, (char *) prevdir->data, cd_exact)))
2685 panel->dir_history = prevdir;
2688 /* --------------------------------------------------------------------------------------------- */
2690 static void
2691 directory_history_list (WPanel * panel)
2693 char *s;
2695 s = history_show (&panel->dir_history, &panel->widget);
2697 if (s != NULL)
2699 if (_do_panel_cd (panel, s, cd_exact))
2700 directory_history_add (panel, panel->cwd);
2701 else
2702 message (D_ERROR, MSG_ERROR, _("Cannot change directory"));
2703 g_free (s);
2707 /* --------------------------------------------------------------------------------------------- */
2709 static cb_ret_t
2710 panel_execute_cmd (WPanel * panel, unsigned long command)
2712 int res = MSG_HANDLED;
2714 if (command != CK_PanelStartSearch)
2715 stop_search (panel);
2717 switch (command)
2719 case CK_PanelChdirOtherPanel:
2720 chdir_other_panel (panel);
2721 break;
2722 case CK_PanelChdirToReadlink:
2723 chdir_to_readlink (panel);
2724 break;
2725 case CK_PanelCmdCopyLocal:
2726 copy_cmd_local ();
2727 break;
2728 case CK_PanelCmdDeleteLocal:
2729 delete_cmd_local ();
2730 break;
2731 case CK_PanelCmdDoEnter:
2732 do_enter (panel);
2733 break;
2734 case CK_PanelCmdViewSimple:
2735 view_simple_cmd ();
2736 break;
2737 case CK_PanelCmdEditNew:
2738 edit_cmd_new ();
2739 break;
2740 case CK_PanelCmdRenameLocal:
2741 rename_cmd_local ();
2742 break;
2743 case CK_PanelCmdReverseSelection:
2744 reverse_selection_cmd ();
2745 break;
2746 case CK_PanelCmdSelect:
2747 select_cmd ();
2748 break;
2749 case CK_PanelCmdUnselect:
2750 unselect_cmd ();
2751 break;
2752 case CK_PanelNextPage:
2753 next_page (panel);
2754 break;
2755 case CK_PanelPrevPage:
2756 prev_page (panel);
2757 break;
2758 case CK_PanelCtrlNextPage:
2759 ctrl_next_page (panel);
2760 break;
2761 case CK_PanelCtrlPrevPage:
2762 ctrl_prev_page (panel);
2763 break;
2764 case CK_PanelDirectoryHistoryList:
2765 directory_history_list (panel);
2766 break;
2767 case CK_PanelDirectoryHistoryNext:
2768 directory_history_next (panel);
2769 break;
2770 case CK_PanelDirectoryHistoryPrev:
2771 directory_history_prev (panel);
2772 break;
2773 case CK_PanelGotoBottomFile:
2774 goto_bottom_file (panel);
2775 break;
2776 case CK_PanelGotoMiddleFile:
2777 goto_middle_file (panel);
2778 break;
2779 case CK_PanelGotoTopFile:
2780 goto_top_file (panel);
2781 break;
2782 case CK_PanelMarkFile:
2783 mark_file (panel);
2784 break;
2785 case CK_PanelMarkFileUp:
2786 mark_file_up (panel);
2787 break;
2788 case CK_PanelMarkFileDown:
2789 mark_file_down (panel);
2790 break;
2791 case CK_PanelMoveUp:
2792 move_up (panel);
2793 break;
2794 case CK_PanelMoveDown:
2795 move_down (panel);
2796 break;
2797 case CK_PanelMoveLeft:
2798 res = move_left (panel);
2799 break;
2800 case CK_PanelMoveRight:
2801 res = move_right (panel);
2802 break;
2803 case CK_PanelMoveEnd:
2804 move_end (panel);
2805 break;
2806 case CK_PanelMoveHome:
2807 move_home (panel);
2808 break;
2809 case CK_PanelSetPanelEncoding:
2810 panel_change_encoding (panel);
2811 break;
2812 case CK_PanelStartSearch:
2813 start_search (panel);
2814 break;
2815 case CK_PanelStopSearch:
2816 break;
2817 case CK_PanelSyncOtherPanel:
2818 sync_other_panel (panel);
2819 break;
2820 case CK_PanelSelectSortOrder:
2821 panel_select_sort_order (panel);
2822 break;
2823 case CK_PanelToggleSortOrderPrev:
2824 panel_toggle_sort_order_prev (panel);
2825 break;
2826 case CK_PanelToggleSortOrderNext:
2827 panel_toggle_sort_order_next (panel);
2828 break;
2829 case CK_PanelReverseSort:
2830 panel->reverse = !panel->reverse;
2831 panel_set_sort_order (panel, panel->current_sort_field);
2832 break;
2833 case CK_PanelSortOrderByName:
2834 panel_set_sort_type_by_id (panel, "name");
2835 break;
2836 case CK_PanelSortOrderByExt:
2837 panel_set_sort_type_by_id (panel, "extension");
2838 break;
2839 case CK_PanelSortOrderBySize:
2840 panel_set_sort_type_by_id (panel, "size");
2841 break;
2842 case CK_PanelSortOrderByMTime:
2843 panel_set_sort_type_by_id (panel, "mtime");
2844 break;
2846 return res;
2849 /* --------------------------------------------------------------------------------------------- */
2851 static cb_ret_t
2852 panel_key (WPanel * panel, int key)
2854 size_t i;
2856 for (i = 0; panel_map[i].key != 0; i++)
2857 if (key == panel_map[i].key)
2858 return panel_execute_cmd (panel, panel_map[i].command);
2860 if (torben_fj_mode && key == ALT ('h'))
2862 goto_middle_file (panel);
2863 return MSG_HANDLED;
2866 if (is_abort_char (key))
2868 stop_search (panel);
2869 return MSG_HANDLED;
2872 /* Do not eat characters not meant for the panel below ' ' (e.g. C-l). */
2873 if ((key >= ' ' && key <= 255) || key == KEY_BACKSPACE)
2875 if (panel->searching)
2877 do_search (panel, key);
2878 return MSG_HANDLED;
2881 if (!command_prompt)
2883 start_search (panel);
2884 do_search (panel, key);
2885 return MSG_HANDLED;
2889 return MSG_NOT_HANDLED;
2892 /* --------------------------------------------------------------------------------------------- */
2894 static cb_ret_t
2895 panel_callback (Widget * w, widget_msg_t msg, int parm)
2897 WPanel *panel = (WPanel *) w;
2898 WButtonBar *bb;
2900 switch (msg)
2902 case WIDGET_DRAW:
2903 paint_panel (panel);
2904 return MSG_HANDLED;
2906 case WIDGET_FOCUS:
2907 current_panel = panel;
2908 panel->active = 1;
2909 if (mc_chdir (panel->cwd) != 0)
2911 char *cwd = strip_password (g_strdup (panel->cwd), 1);
2912 message (D_ERROR, MSG_ERROR, _("Cannot chdir to \"%s\"\n%s"),
2913 cwd, unix_error_string (errno));
2914 g_free (cwd);
2916 else
2917 subshell_chdir (panel->cwd);
2919 update_xterm_title_path ();
2920 select_item (panel);
2921 show_dir (panel);
2922 paint_dir (panel);
2923 panel->dirty = 0;
2925 bb = find_buttonbar (panel->widget.owner);
2926 midnight_set_buttonbar (bb);
2927 buttonbar_redraw (bb);
2928 return MSG_HANDLED;
2930 case WIDGET_UNFOCUS:
2931 /* Janne: look at this for the multiple panel options */
2932 stop_search (panel);
2933 panel->active = 0;
2934 show_dir (panel);
2935 unselect_item (panel);
2936 return MSG_HANDLED;
2938 case WIDGET_KEY:
2939 return panel_key (panel, parm);
2941 case WIDGET_COMMAND:
2942 return panel_execute_cmd (panel, parm);
2944 case WIDGET_DESTROY:
2945 panel_destroy (panel);
2946 free_my_statfs ();
2947 return MSG_HANDLED;
2949 default:
2950 return default_proc (msg, parm);
2954 /* --------------------------------------------------------------------------------------------- */
2955 /* */
2956 /* Panel mouse events support routines */
2957 /* */
2959 static void
2960 mouse_toggle_mark (WPanel * panel)
2962 do_mark_file (panel, MARK_DONT_MOVE);
2963 mouse_marking = selection (panel)->f.marked;
2966 /* --------------------------------------------------------------------------------------------- */
2968 static void
2969 mouse_set_mark (WPanel * panel)
2971 if (mouse_marking && !(selection (panel)->f.marked))
2972 do_mark_file (panel, MARK_DONT_MOVE);
2973 else if (!mouse_marking && (selection (panel)->f.marked))
2974 do_mark_file (panel, MARK_DONT_MOVE);
2977 /* --------------------------------------------------------------------------------------------- */
2979 static int
2980 mark_if_marking (WPanel * panel, Gpm_Event * event)
2982 if (event->buttons & GPM_B_RIGHT)
2984 if (event->type & GPM_DOWN)
2985 mouse_toggle_mark (panel);
2986 else
2987 mouse_set_mark (panel);
2988 return 1;
2990 return 0;
2993 /* --------------------------------------------------------------------------------------------- */
2994 /** Determine which column was clicked, and sort the panel on
2995 * that column, or reverse sort on that column if already
2996 * sorted on that column.
2999 static void
3000 mouse_sort_col (Gpm_Event * event, WPanel * panel)
3002 int i;
3003 const char *lc_sort_name = NULL;
3004 panel_field_t *col_sort_format = NULL;
3005 format_e *format;
3006 gchar *title;
3008 for (i = 0, format = panel->format; format != NULL; format = format->next)
3010 i += format->field_len;
3011 if (event->x < i + 1)
3013 /* found column */
3014 lc_sort_name = format->title;
3015 break;
3019 if (lc_sort_name == NULL)
3020 return;
3022 for (i = 0; panel_fields[i].id != NULL; i++)
3024 title = panel_get_title_without_hotkey (panel_fields[i].title_hotkey);
3025 if (!strcmp (lc_sort_name, title) && panel_fields[i].sort_routine)
3027 col_sort_format = &panel_fields[i];
3028 g_free (title);
3029 break;
3031 g_free (title);
3034 if (!col_sort_format)
3035 return;
3037 if (panel->current_sort_field == col_sort_format)
3039 /* reverse the sort if clicked column is already the sorted column */
3040 panel->reverse = !panel->reverse;
3042 else
3044 /* new sort is forced to be ascending */
3045 panel->reverse = 0;
3047 panel_set_sort_order (panel, col_sort_format);
3051 /* --------------------------------------------------------------------------------------------- */
3053 * Mouse callback of the panel minus repainting.
3054 * If the event is redirected to the menu, *redir is set to TRUE.
3056 static int
3057 do_panel_event (Gpm_Event * event, WPanel * panel, gboolean * redir)
3059 const int lines = llines (panel);
3060 const gboolean is_active = dlg_widget_active (panel);
3061 const gboolean mouse_down = (event->type & GPM_DOWN) != 0;
3063 *redir = FALSE;
3065 /* 1st line */
3066 if (mouse_down && event->y == 1)
3068 /* "<" button */
3069 if (event->x == 2)
3071 directory_history_prev (panel);
3072 return MOU_NORMAL;
3075 /* "." button show/hide hidden files */
3076 if (event->x == panel->widget.cols - 5)
3078 panel->widget.owner->callback (panel->widget.owner, NULL,
3079 DLG_ACTION, CK_ToggleShowHidden, NULL);
3080 repaint_screen ();
3081 return MOU_NORMAL;
3084 /* ">" button */
3085 if (event->x == panel->widget.cols - 1)
3087 directory_history_next (panel);
3088 return MOU_NORMAL;
3091 /* "^" button */
3092 if (event->x >= panel->widget.cols - 4 && event->x <= panel->widget.cols - 2)
3094 directory_history_list (panel);
3095 return MOU_NORMAL;
3098 /* rest of the upper frame, the menu is invisible - call menu */
3099 if (!menubar_visible)
3101 *redir = TRUE;
3102 event->x += panel->widget.x;
3103 return the_menubar->widget.mouse (event, the_menubar);
3106 /* no other events on 1st line */
3107 return MOU_NORMAL;
3110 /* sort on clicked column; don't handle wheel events */
3111 if (mouse_down && (event->buttons & (GPM_B_UP | GPM_B_DOWN)) == 0 && event->y == 2)
3113 mouse_sort_col (event, panel);
3114 return MOU_NORMAL;
3117 /* Mouse wheel events */
3118 if (mouse_down && (event->buttons & GPM_B_UP))
3120 if (is_active)
3122 if (panels_options.mouse_move_pages && (panel->top_file > 0))
3123 prev_page (panel);
3124 else /* We are in first page */
3125 move_up (panel);
3127 return MOU_NORMAL;
3130 if (mouse_down && (event->buttons & GPM_B_DOWN))
3132 if (is_active)
3134 if (panels_options.mouse_move_pages && (panel->top_file + ITEMS (panel) < panel->count))
3135 next_page (panel);
3136 else /* We are in last page */
3137 move_down (panel);
3139 return MOU_NORMAL;
3142 event->y -= 2;
3143 if ((event->type & (GPM_DOWN | GPM_DRAG)))
3145 int my_index;
3147 if (!is_active)
3148 change_panel ();
3150 if (panel->top_file + event->y > panel->count)
3151 my_index = panel->count - 1;
3152 else
3154 my_index = panel->top_file + event->y - 1;
3155 if (panel->split && (event->x > ((panel->widget.cols - 2) / 2)))
3156 my_index += llines (panel);
3158 if (my_index >= panel->count)
3159 my_index = panel->count - 1;
3162 if (my_index != panel->selected)
3164 unselect_item (panel);
3165 panel->selected = my_index;
3166 select_item (panel);
3169 /* This one is new */
3170 mark_if_marking (panel, event);
3172 else if ((event->type & (GPM_UP | GPM_DOUBLE)) == (GPM_UP | GPM_DOUBLE))
3174 if (event->y > 0 && event->y <= lines)
3175 do_enter (panel);
3177 return MOU_NORMAL;
3180 /* --------------------------------------------------------------------------------------------- */
3181 /** Mouse callback of the panel */
3183 static int
3184 panel_event (Gpm_Event * event, void *data)
3186 WPanel *panel = data;
3187 int ret;
3188 gboolean redir;
3190 ret = do_panel_event (event, panel, &redir);
3191 if (!redir)
3192 paint_panel (panel);
3194 return ret;
3197 /* --------------------------------------------------------------------------------------------- */
3199 static void
3200 reload_panelized (WPanel * panel)
3202 int i, j;
3203 dir_list *list = &panel->dir;
3205 if (panel != current_panel)
3207 int ret;
3208 ret = mc_chdir (panel->cwd);
3211 for (i = 0, j = 0; i < panel->count; i++)
3213 if (list->list[i].f.marked)
3215 /* Unmark the file in advance. In case the following mc_lstat
3216 * fails we are done, else we have to mark the file again
3217 * (Note: do_file_mark depends on a valid "list->list [i].buf").
3218 * IMO that's the best way to update the panel's summary status
3219 * -- Norbert
3221 do_file_mark (panel, i, 0);
3223 if (mc_lstat (list->list[i].fname, &list->list[i].st))
3225 g_free (list->list[i].fname);
3226 continue;
3228 if (list->list[i].f.marked)
3229 do_file_mark (panel, i, 1);
3230 if (j != i)
3231 list->list[j] = list->list[i];
3232 j++;
3234 if (j == 0)
3235 panel->count = set_zero_dir (list) ? 1 : 0;
3236 else
3237 panel->count = j;
3239 if (panel != current_panel)
3241 int ret;
3242 ret = mc_chdir (current_panel->cwd);
3246 /* --------------------------------------------------------------------------------------------- */
3248 static void
3249 update_one_panel_widget (WPanel * panel, int force_update, const char *current_file)
3251 int free_pointer;
3252 char *my_current_file = NULL;
3254 if (force_update & UP_RELOAD)
3256 panel->is_panelized = 0;
3257 mc_setctl (panel->cwd, VFS_SETCTL_FLUSH, 0);
3258 memset (&(panel->dir_stat), 0, sizeof (panel->dir_stat));
3261 /* If current_file == -1 (an invalid pointer) then preserve selection */
3262 if (current_file == UP_KEEPSEL)
3264 free_pointer = 1;
3265 my_current_file = g_strdup (panel->dir.list[panel->selected].fname);
3266 current_file = my_current_file;
3268 else
3269 free_pointer = 0;
3271 if (panel->is_panelized)
3272 reload_panelized (panel);
3273 else
3274 panel_reload (panel);
3276 try_to_select (panel, current_file);
3277 panel->dirty = 1;
3279 if (free_pointer)
3280 g_free (my_current_file);
3283 /* --------------------------------------------------------------------------------------------- */
3285 static void
3286 update_one_panel (int which, int force_update, const char *current_file)
3288 if (get_display_type (which) == view_listing)
3290 WPanel *panel;
3291 panel = (WPanel *) get_panel_widget (which);
3292 update_one_panel_widget (panel, force_update, current_file);
3296 /* --------------------------------------------------------------------------------------------- */
3297 /*** public functions ****************************************************************************/
3298 /* --------------------------------------------------------------------------------------------- */
3300 char *
3301 remove_encoding_from_path (const char *path)
3303 GString *ret;
3304 GString *tmp_path, *tmp_conv;
3305 char *tmp;
3307 ret = g_string_new ("");
3308 tmp_conv = g_string_new ("");
3309 tmp_path = g_string_new (path);
3311 while ((tmp = g_strrstr (tmp_path->str, PATH_SEP_STR VFS_ENCODING_PREFIX)) != NULL)
3313 const char *enc;
3314 GIConv converter;
3315 char *tmp2;
3317 enc = vfs_get_encoding ((const char *) tmp);
3318 converter = enc != NULL ? str_crt_conv_to (enc) : str_cnv_to_term;
3319 if (converter == INVALID_CONV)
3320 converter = str_cnv_to_term;
3322 tmp2 = tmp + 1;
3323 while (*tmp2 != '\0' && *tmp2 != PATH_SEP)
3324 tmp2++;
3326 if (*tmp2 != '\0')
3328 str_vfs_convert_from (converter, tmp2, tmp_conv);
3329 g_string_prepend (ret, tmp_conv->str);
3330 g_string_set_size (tmp_conv, 0);
3333 g_string_set_size (tmp_path, tmp - tmp_path->str);
3334 str_close_conv (converter);
3337 g_string_prepend (ret, tmp_path->str);
3338 g_string_free (tmp_path, TRUE);
3339 g_string_free (tmp_conv, TRUE);
3341 return g_string_free (ret, FALSE);
3344 /* --------------------------------------------------------------------------------------------- */
3346 * Repaint the contents of the panels without frames. To schedule panel
3347 * for repainting, set panel->dirty to 1. There are many reasons why
3348 * the panels need to be repainted, and this is a costly operation, so
3349 * it's done once per event.
3352 void
3353 update_dirty_panels (void)
3355 if (current_panel->dirty)
3356 paint_panel (current_panel);
3358 if ((get_other_type () == view_listing) && other_panel->dirty)
3359 paint_panel (other_panel);
3362 /* --------------------------------------------------------------------------------------------- */
3364 static void
3365 do_select (WPanel * panel, int i)
3367 if (i != panel->selected)
3369 panel->dirty = 1;
3370 panel->selected = i;
3371 panel->top_file = panel->selected - (panel->widget.lines - 2) / 2;
3372 if (panel->top_file < 0)
3373 panel->top_file = 0;
3377 /* --------------------------------------------------------------------------------------------- */
3379 static void
3380 do_try_to_select (WPanel * panel, const char *name)
3382 int i;
3383 char *subdir;
3385 if (!name)
3387 do_select (panel, 0);
3388 return;
3391 /* We only want the last component of the directory,
3392 * and from this only the name without suffix. */
3393 subdir = vfs_strip_suffix_from_filename (x_basename (name));
3395 /* Search that subdirectory, if found select it */
3396 for (i = 0; i < panel->count; i++)
3398 if (strcmp (subdir, panel->dir.list[i].fname) == 0)
3400 do_select (panel, i);
3401 g_free (subdir);
3402 return;
3406 /* Try to select a file near the file that is missing */
3407 if (panel->selected >= panel->count)
3408 do_select (panel, panel->count - 1);
3409 g_free (subdir);
3412 /* --------------------------------------------------------------------------------------------- */
3414 void
3415 try_to_select (WPanel * panel, const char *name)
3417 do_try_to_select (panel, name);
3418 select_item (panel);
3421 /* --------------------------------------------------------------------------------------------- */
3423 void
3424 panel_update_cols (Widget * widget, int frame_size)
3426 int cols, origin;
3428 if (horizontal_split)
3430 widget->cols = COLS;
3431 return;
3434 if (frame_size == frame_full)
3436 cols = COLS;
3437 origin = 0;
3439 else
3441 if (widget == get_panel_widget (0))
3443 cols = first_panel_size;
3444 origin = 0;
3446 else
3448 cols = COLS - first_panel_size;
3449 origin = first_panel_size;
3453 widget->cols = cols;
3454 widget->x = origin;
3457 /* --------------------------------------------------------------------------------------------- */
3459 void
3460 panel_clean_dir (WPanel * panel)
3462 int count = panel->count;
3464 panel->count = 0;
3465 panel->top_file = 0;
3466 panel->selected = 0;
3467 panel->marked = 0;
3468 panel->dirs_marked = 0;
3469 panel->total = 0;
3470 panel->searching = FALSE;
3471 panel->is_panelized = 0;
3472 panel->dirty = 1;
3474 clean_dir (&panel->dir, count);
3477 /* --------------------------------------------------------------------------------------------- */
3478 /** Panel creation.
3479 * @param panel_name the name of the panel for setup retieving
3480 * @returns new instance of WPanel
3483 WPanel *
3484 panel_new (const char *panel_name)
3486 return panel_new_with_dir (panel_name, NULL);
3489 /* --------------------------------------------------------------------------------------------- */
3490 /** Panel creation for specified directory.
3491 * @param panel_name specifies the name of the panel for setup retieving
3492 * @param the path of working panel directory. If path is NULL then panel will be created for current directory
3493 * @returns new instance of WPanel
3496 WPanel *
3497 panel_new_with_dir (const char *panel_name, const char *wpath)
3499 WPanel *panel;
3500 char *section;
3501 int i, err;
3502 char curdir[MC_MAXPATHLEN] = "\0";
3504 panel = g_new0 (WPanel, 1);
3506 /* No know sizes of the panel at startup */
3507 init_widget (&panel->widget, 0, 0, 0, 0, panel_callback, panel_event);
3509 /* We do not want the cursor */
3510 widget_want_cursor (panel->widget, 0);
3512 if (wpath != NULL)
3514 g_strlcpy (panel->cwd, wpath, sizeof (panel->cwd));
3515 mc_get_current_wd (curdir, sizeof (curdir) - 2);
3517 else
3518 mc_get_current_wd (panel->cwd, sizeof (panel->cwd) - 2);
3520 strcpy (panel->lwd, ".");
3522 panel->hist_name = g_strconcat ("Dir Hist ", panel_name, (char *) NULL);
3523 panel->dir_history = history_get (panel->hist_name);
3524 directory_history_add (panel, panel->cwd);
3526 panel->dir.list = g_new (file_entry, MIN_FILES);
3527 panel->dir.size = MIN_FILES;
3528 panel->active = 0;
3529 panel->filter = 0;
3530 panel->split = 0;
3531 panel->top_file = 0;
3532 panel->selected = 0;
3533 panel->marked = 0;
3534 panel->total = 0;
3535 panel->reverse = 0;
3536 panel->dirty = 1;
3537 panel->searching = FALSE;
3538 panel->dirs_marked = 0;
3539 panel->is_panelized = 0;
3540 panel->format = 0;
3541 panel->status_format = 0;
3542 panel->format_modified = 1;
3544 panel->panel_name = g_strdup (panel_name);
3545 panel->user_format = g_strdup (DEFAULT_USER_FORMAT);
3547 panel->codepage = SELECT_CHARSET_NO_TRANSLATE;
3549 for (i = 0; i < LIST_TYPES; i++)
3550 panel->user_status_format[i] = g_strdup (DEFAULT_USER_FORMAT);
3552 panel->search_buffer[0] = '\0';
3553 panel->prev_search_buffer[0] = '\0';
3554 panel->frame_size = frame_half;
3556 section = g_strconcat ("Temporal:", panel->panel_name, (char *) NULL);
3557 if (!mc_config_has_group (mc_main_config, section))
3559 g_free (section);
3560 section = g_strdup (panel->panel_name);
3562 panel_load_setup (panel, section);
3563 g_free (section);
3565 /* Load format strings */
3566 err = set_panel_formats (panel);
3567 if (err != 0)
3568 set_panel_formats (panel);
3570 #ifdef HAVE_CHARSET
3572 const char *enc = vfs_get_encoding (panel->cwd);
3573 if (enc != NULL)
3574 panel->codepage = get_codepage_index (enc);
3576 #endif
3578 if (mc_chdir (panel->cwd) != 0)
3580 panel->codepage = SELECT_CHARSET_NO_TRANSLATE;
3581 mc_get_current_wd (panel->cwd, sizeof (panel->cwd) - 2);
3584 /* Load the default format */
3585 panel->count =
3586 do_load_dir (panel->cwd, &panel->dir, panel->current_sort_field->sort_routine,
3587 panel->reverse, panel->case_sensitive, panel->exec_first, panel->filter);
3589 /* Restore old right path */
3590 if (curdir[0] != '\0')
3591 err = mc_chdir (curdir);
3593 return panel;
3596 /* --------------------------------------------------------------------------------------------- */
3598 void
3599 panel_reload (WPanel * panel)
3601 struct stat current_stat;
3603 if (panels_options.fast_reload && !stat (panel->cwd, &current_stat)
3604 && current_stat.st_ctime == panel->dir_stat.st_ctime
3605 && current_stat.st_mtime == panel->dir_stat.st_mtime)
3606 return;
3608 while (mc_chdir (panel->cwd) == -1)
3610 char *last_slash;
3612 if (panel->cwd[0] == PATH_SEP && panel->cwd[1] == 0)
3614 panel_clean_dir (panel);
3615 panel->count = set_zero_dir (&panel->dir) ? 1 : 0;
3616 return;
3618 last_slash = strrchr (panel->cwd, PATH_SEP);
3619 if (!last_slash || last_slash == panel->cwd)
3620 strcpy (panel->cwd, PATH_SEP_STR);
3621 else
3622 *last_slash = 0;
3623 memset (&(panel->dir_stat), 0, sizeof (panel->dir_stat));
3624 show_dir (panel);
3627 panel->count =
3628 do_reload_dir (panel->cwd, &panel->dir, panel->current_sort_field->sort_routine,
3629 panel->count, panel->reverse, panel->case_sensitive,
3630 panel->exec_first, panel->filter);
3632 panel->dirty = 1;
3633 if (panel->selected >= panel->count)
3634 do_select (panel, panel->count - 1);
3636 recalculate_panel_summary (panel);
3639 /* --------------------------------------------------------------------------------------------- */
3640 /* Switches the panel to the mode specified in the format */
3641 /* Seting up both format and status string. Return: 0 - on success; */
3642 /* 1 - format error; 2 - status error; 3 - errors in both formats. */
3645 set_panel_formats (WPanel * p)
3647 format_e *form;
3648 char *err = NULL;
3649 int retcode = 0;
3651 form = use_display_format (p, panel_format (p), &err, 0);
3653 if (err != NULL)
3655 g_free (err);
3656 retcode = 1;
3658 else
3660 delete_format (p->format);
3661 p->format = form;
3664 if (show_mini_info)
3666 form = use_display_format (p, mini_status_format (p), &err, 1);
3668 if (err != NULL)
3670 g_free (err);
3671 retcode += 2;
3673 else
3675 delete_format (p->status_format);
3676 p->status_format = form;
3680 panel_format_modified (p);
3681 panel_update_cols (&(p->widget), p->frame_size);
3683 if (retcode)
3684 message (D_ERROR, _("Warning"),
3685 _("User supplied format looks invalid, reverting to default."));
3686 if (retcode & 0x01)
3688 g_free (p->user_format);
3689 p->user_format = g_strdup (DEFAULT_USER_FORMAT);
3691 if (retcode & 0x02)
3693 g_free (p->user_status_format[p->list_type]);
3694 p->user_status_format[p->list_type] = g_strdup (DEFAULT_USER_FORMAT);
3697 return retcode;
3700 /* --------------------------------------------------------------------------------------------- */
3702 /* Select current item and readjust the panel */
3703 void
3704 select_item (WPanel * panel)
3706 int items = ITEMS (panel);
3708 /* Although currently all over the code we set the selection and
3709 top file to decent values before calling select_item, I could
3710 forget it someday, so it's better to do the actual fitting here */
3712 if (panel->top_file < 0)
3713 panel->top_file = 0;
3715 if (panel->selected < 0)
3716 panel->selected = 0;
3718 if (panel->selected > panel->count - 1)
3719 panel->selected = panel->count - 1;
3721 if (panel->top_file > panel->count - 1)
3722 panel->top_file = panel->count - 1;
3724 if ((panel->count - panel->top_file) < items)
3726 panel->top_file = panel->count - items;
3727 if (panel->top_file < 0)
3728 panel->top_file = 0;
3731 if (panel->selected < panel->top_file)
3732 panel->top_file = panel->selected;
3734 if ((panel->selected - panel->top_file) >= items)
3735 panel->top_file = panel->selected - items + 1;
3737 panel->dirty = 1;
3739 execute_hooks (select_file_hook);
3742 /* --------------------------------------------------------------------------------------------- */
3743 /** Clears all files in the panel, used only when one file was marked */
3744 void
3745 unmark_files (WPanel * panel)
3747 int i;
3749 if (!panel->marked)
3750 return;
3751 for (i = 0; i < panel->count; i++)
3752 file_mark (panel, i, 0);
3754 panel->dirs_marked = 0;
3755 panel->marked = 0;
3756 panel->total = 0;
3759 /* --------------------------------------------------------------------------------------------- */
3760 /** Recalculate the panels summary information, used e.g. when marked
3761 files might have been removed by an external command */
3763 void
3764 recalculate_panel_summary (WPanel * panel)
3766 int i;
3768 panel->marked = 0;
3769 panel->dirs_marked = 0;
3770 panel->total = 0;
3772 for (i = 0; i < panel->count; i++)
3773 if (panel->dir.list[i].f.marked)
3775 /* do_file_mark will return immediately if newmark == oldmark.
3776 So we have to first unmark it to get panel's summary information
3777 updated. (Norbert) */
3778 panel->dir.list[i].f.marked = 0;
3779 do_file_mark (panel, i, 1);
3783 /* --------------------------------------------------------------------------------------------- */
3784 /** This routine marks a file or a directory */
3786 void
3787 do_file_mark (WPanel * panel, int idx, int mark)
3789 if (panel->dir.list[idx].f.marked == mark)
3790 return;
3792 /* Only '..' can't be marked, '.' isn't visible */
3793 if (!strcmp (panel->dir.list[idx].fname, ".."))
3794 return;
3796 file_mark (panel, idx, mark);
3797 if (panel->dir.list[idx].f.marked)
3799 panel->marked++;
3800 if (S_ISDIR (panel->dir.list[idx].st.st_mode))
3802 if (panel->dir.list[idx].f.dir_size_computed)
3803 panel->total += panel->dir.list[idx].st.st_size;
3804 panel->dirs_marked++;
3806 else
3807 panel->total += panel->dir.list[idx].st.st_size;
3808 set_colors (panel);
3810 else
3812 if (S_ISDIR (panel->dir.list[idx].st.st_mode))
3814 if (panel->dir.list[idx].f.dir_size_computed)
3815 panel->total -= panel->dir.list[idx].st.st_size;
3816 panel->dirs_marked--;
3818 else
3819 panel->total -= panel->dir.list[idx].st.st_size;
3820 panel->marked--;
3824 /* --------------------------------------------------------------------------------------------- */
3826 * Changes the current directory of the panel.
3827 * Record change in the directory history.
3829 gboolean
3830 do_panel_cd (struct WPanel *panel, const char *new_dir, enum cd_enum cd_type)
3832 gboolean r;
3834 r = _do_panel_cd (panel, new_dir, cd_type);
3835 if (r)
3836 directory_history_add (panel, panel->cwd);
3837 return r;
3840 /* --------------------------------------------------------------------------------------------- */
3842 void
3843 file_mark (WPanel * panel, int lc_index, int val)
3845 if (panel->dir.list[lc_index].f.marked != val)
3847 panel->dir.list[lc_index].f.marked = val;
3848 panel->dirty = 1;
3852 /* --------------------------------------------------------------------------------------------- */
3854 void
3855 panel_re_sort (WPanel * panel)
3857 char *filename;
3858 int i;
3860 if (panel == NULL)
3861 return;
3863 filename = g_strdup (selection (panel)->fname);
3864 unselect_item (panel);
3865 do_sort (&panel->dir, panel->current_sort_field->sort_routine, panel->count - 1, panel->reverse,
3866 panel->case_sensitive, panel->exec_first);
3867 panel->selected = -1;
3868 for (i = panel->count; i; i--)
3870 if (!strcmp (panel->dir.list[i - 1].fname, filename))
3872 panel->selected = i - 1;
3873 break;
3876 g_free (filename);
3877 panel->top_file = panel->selected - ITEMS (panel) / 2;
3878 select_item (panel);
3879 panel->dirty = 1;
3882 /* --------------------------------------------------------------------------------------------- */
3884 void
3885 panel_set_sort_order (WPanel * panel, const panel_field_t * sort_order)
3887 if (sort_order == 0)
3888 return;
3890 panel->current_sort_field = sort_order;
3892 /* The directory is already sorted, we have to load the unsorted stuff */
3893 if (sort_order->sort_routine == (sortfn *) unsorted)
3895 char *current_file;
3897 current_file = g_strdup (panel->dir.list[panel->selected].fname);
3898 panel_reload (panel);
3899 try_to_select (panel, current_file);
3900 g_free (current_file);
3902 panel_re_sort (panel);
3905 /* --------------------------------------------------------------------------------------------- */
3907 * Change panel encoding.
3908 * @param panel WPanel object
3911 void
3912 panel_change_encoding (WPanel * panel)
3914 const char *encoding = NULL;
3915 char *cd_path;
3916 #ifdef HAVE_CHARSET
3917 char *errmsg;
3918 int r;
3920 r = select_charset (-1, -1, panel->codepage, FALSE);
3922 if (r == SELECT_CHARSET_CANCEL)
3923 return; /* Cancel */
3925 panel->codepage = r;
3927 if (panel->codepage == SELECT_CHARSET_NO_TRANSLATE)
3929 /* No translation */
3930 g_free (init_translation_table (display_codepage, display_codepage));
3931 cd_path = remove_encoding_from_path (panel->cwd);
3932 do_panel_cd (panel, cd_path, cd_parse_command);
3933 g_free (cd_path);
3934 return;
3937 errmsg = init_translation_table (panel->codepage, display_codepage);
3938 if (errmsg != NULL)
3940 message (D_ERROR, MSG_ERROR, "%s", errmsg);
3941 g_free (errmsg);
3942 return;
3945 encoding = get_codepage_id (panel->codepage);
3946 #endif
3947 if (encoding != NULL)
3949 const char *enc;
3951 enc = vfs_get_encoding (panel->cwd);
3953 /* don't add current encoding */
3954 if ((enc == NULL) || (strcmp (encoding, enc) != 0))
3956 cd_path = add_encoding_to_path (panel->cwd, encoding);
3957 if (!do_panel_cd (panel, cd_path, cd_parse_command))
3958 message (D_ERROR, MSG_ERROR, _("Cannot chdir to \"%s\""), cd_path);
3959 g_free (cd_path);
3964 /* --------------------------------------------------------------------------------------------- */
3966 * This routine reloads the directory in both panels. It tries to
3967 * select current_file in current_panel and other_file in other_panel.
3968 * If current_file == -1 then it automatically sets current_file and
3969 * other_file to the currently selected files in the panels.
3971 * if force_update has the UP_ONLY_CURRENT bit toggled on, then it
3972 * will not reload the other panel.
3975 void
3976 update_panels (int force_update, const char *current_file)
3978 int reload_other = !(force_update & UP_ONLY_CURRENT);
3979 WPanel *panel;
3980 int ret;
3982 update_one_panel (get_current_index (), force_update, current_file);
3983 if (reload_other)
3984 update_one_panel (get_other_index (), force_update, UP_KEEPSEL);
3986 if (get_current_type () == view_listing)
3987 panel = (WPanel *) get_panel_widget (get_current_index ());
3988 else
3989 panel = (WPanel *) get_panel_widget (get_other_index ());
3991 ret = mc_chdir (panel->cwd);
3994 /* --------------------------------------------------------------------------------------------- */
3996 void
3997 directory_history_add (struct WPanel *panel, const char *dir)
3999 char *tmp;
4001 tmp = g_strdup (dir);
4002 strip_password (tmp, 1);
4004 panel->dir_history = list_append_unique (panel->dir_history, tmp);
4007 /* --------------------------------------------------------------------------------------------- */
4009 gsize
4010 panel_get_num_of_sortable_fields (void)
4012 gsize ret = 0, lc_index;
4014 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
4015 if (panel_fields[lc_index].is_user_choice)
4016 ret++;
4017 return ret;
4020 /* --------------------------------------------------------------------------------------------- */
4022 const char **
4023 panel_get_sortable_fields (gsize * array_size)
4025 char **ret;
4026 gsize lc_index, i;
4028 lc_index = panel_get_num_of_sortable_fields ();
4030 ret = g_try_new0 (char *, lc_index + 1);
4031 if (ret == NULL)
4032 return NULL;
4034 if (array_size != NULL)
4035 *array_size = lc_index;
4037 lc_index = 0;
4039 for (i = 0; panel_fields[i].id != NULL; i++)
4040 if (panel_fields[i].is_user_choice)
4041 ret[lc_index++] = g_strdup (_(panel_fields[i].title_hotkey));
4042 return (const char **) ret;
4045 /* --------------------------------------------------------------------------------------------- */
4047 const panel_field_t *
4048 panel_get_field_by_id (const char *name)
4050 gsize lc_index;
4051 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
4052 if (panel_fields[lc_index].id != NULL && strcmp (name, panel_fields[lc_index].id) == 0)
4053 return &panel_fields[lc_index];
4054 return NULL;
4057 /* --------------------------------------------------------------------------------------------- */
4059 const panel_field_t *
4060 panel_get_field_by_title_hotkey (const char *name)
4062 gsize lc_index;
4063 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
4064 if (panel_fields[lc_index].title_hotkey != NULL &&
4065 strcmp (name, _(panel_fields[lc_index].title_hotkey)) == 0)
4066 return &panel_fields[lc_index];
4067 return NULL;
4070 /* --------------------------------------------------------------------------------------------- */
4072 const panel_field_t *
4073 panel_get_field_by_title (const char *name)
4075 gsize lc_index;
4076 gchar *title = NULL;
4078 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
4080 title = panel_get_title_without_hotkey (panel_fields[lc_index].title_hotkey);
4081 if (panel_fields[lc_index].title_hotkey != NULL && strcmp (name, title) == 0)
4083 g_free (title);
4084 return &panel_fields[lc_index];
4087 g_free (title);
4088 return NULL;
4091 /* --------------------------------------------------------------------------------------------- */
4093 gsize
4094 panel_get_num_of_user_possible_fields (void)
4096 gsize ret = 0, lc_index;
4098 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
4099 if (panel_fields[lc_index].use_in_user_format)
4100 ret++;
4101 return ret;
4104 /* --------------------------------------------------------------------------------------------- */
4106 const char **
4107 panel_get_user_possible_fields (gsize * array_size)
4109 char **ret;
4110 gsize lc_index, i;
4112 lc_index = panel_get_num_of_user_possible_fields ();
4114 ret = g_try_new0 (char *, lc_index + 1);
4115 if (ret == NULL)
4116 return NULL;
4118 if (array_size != NULL)
4119 *array_size = lc_index;
4121 lc_index = 0;
4123 for (i = 0; panel_fields[i].id != NULL; i++)
4124 if (panel_fields[i].use_in_user_format)
4125 ret[lc_index++] = g_strdup (_(panel_fields[i].title_hotkey));
4126 return (const char **) ret;
4129 /* --------------------------------------------------------------------------------------------- */
4131 void
4132 panel_init (void)
4134 panel_sort_up_sign = mc_skin_get ("widget-common", "sort-sign-up", "'");
4135 panel_sort_down_sign = mc_skin_get ("widget-common", "sort-sign-down", ",");
4137 panel_hiddenfiles_sign_show = mc_skin_get ("widget-panel", "hiddenfiles-sign-show", ".");
4138 panel_hiddenfiles_sign_hide = mc_skin_get ("widget-panel", "hiddenfiles-sign-hide", ".");
4139 panel_history_prev_item_sign = mc_skin_get ("widget-panel", "history-prev-item-sign", "<");
4140 panel_history_next_item_sign = mc_skin_get ("widget-panel", "history-next-item-sign", ">");
4141 panel_history_show_list_sign = mc_skin_get ("widget-panel", "history-show-list-sign", "^");
4145 /* --------------------------------------------------------------------------------------------- */
4147 void
4148 panel_deinit (void)
4150 g_free (panel_sort_up_sign);
4151 g_free (panel_sort_down_sign);
4153 g_free (panel_hiddenfiles_sign_show);
4154 g_free (panel_hiddenfiles_sign_hide);
4155 g_free (panel_history_prev_item_sign);
4156 g_free (panel_history_next_item_sign);
4157 g_free (panel_history_show_list_sign);
4161 /* --------------------------------------------------------------------------------------------- */