mc_fhl_init_from_standard_files(): fixed logic and file read order for config files
[midnight-commander.git] / src / screen.c
blob492f1bdb705ba2d428a674d95e274ee5f74f2d0a
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"
46 #include "dir.h"
47 #include "panel.h"
48 #include "boxes.h"
49 #include "tree.h"
50 #include "ext.h" /* regexp_command */
51 #include "layout.h" /* Most layout variables are here */
52 #include "wtools.h" /* for message (...) */
53 #include "cmd.h"
54 #include "command.h" /* cmdline */
55 #include "setup.h" /* For loading/saving panel options */
56 #include "user.h"
57 #include "execute.h"
58 #include "widget.h"
59 #include "menu.h" /* menubar_visible */
60 #include "main-widgets.h"
61 #include "main.h"
62 #include "mountlist.h" /* my_statfs */
63 #include "selcodepage.h" /* select_charset (), SELECT_CHARSET_NO_TRANSLATE */
64 #ifdef HAVE_CHARSET
65 #include "charsets.h" /* get_codepage_id () */
66 #endif
67 #include "cmddef.h" /* CK_ cmd name const */
68 #include "keybind.h" /* global_keymap_t */
70 #define ELEMENTS(arr) ( sizeof(arr) / sizeof((arr)[0]) )
72 #define NORMAL 0
73 #define SELECTED 1
74 #define MARKED 2
75 #define MARKED_SELECTED 3
76 #define STATUS 5
78 typedef enum
80 MARK_DONT_MOVE = 0,
81 MARK_DOWN = 1,
82 MARK_FORCE_DOWN = 2,
83 MARK_FORCE_UP = 3
84 } mark_act_t;
87 * This describes a format item. The parse_display_format routine parses
88 * the user specified format and creates a linked list of format_e structures.
90 typedef struct format_e
92 struct format_e *next;
93 int requested_field_len;
94 int field_len;
95 align_crt_t just_mode;
96 int expand;
97 const char *(*string_fn) (file_entry *, int len);
98 char *title;
99 const char *id;
100 } format_e;
102 /* If true, show the mini-info on the panel */
103 int show_mini_info = 1;
105 /* If true, use some usability hacks by Torben */
106 int torben_fj_mode = 0;
108 /* The hook list for the select file function */
109 Hook *select_file_hook = 0;
111 static cb_ret_t panel_callback (Widget *, widget_msg_t msg, int parm);
112 static int panel_event (Gpm_Event * event, void *);
113 static void paint_frame (WPanel * panel);
114 static const char *panel_format (WPanel * panel);
115 static const char *mini_status_format (WPanel * panel);
117 static char *panel_sort_up_sign = NULL;
118 static char *panel_sort_down_sign = NULL;
120 static char *panel_hiddenfiles_sign_show = NULL;
121 static char *panel_hiddenfiles_sign_hide = NULL;
122 static char *panel_history_prev_item_sign = NULL;
123 static char *panel_history_next_item_sign = NULL;
124 static char *panel_history_show_list_sign = NULL;
126 /* This macro extracts the number of available lines in a panel */
127 #define llines(p) (p->widget.lines-3 - (show_mini_info ? 2 : 0))
129 static void
130 set_colors (WPanel * panel)
132 (void) panel;
133 tty_set_normal_attrs ();
134 tty_setcolor (NORMAL_COLOR);
137 /* Delete format string, it is a linked list */
138 static void
139 delete_format (format_e * format)
141 while (format != NULL)
143 format_e *next = format->next;
144 g_free (format->title);
145 g_free (format);
146 format = next;
150 /* This code relies on the default justification!!! */
151 static void
152 add_permission_string (char *dest, int width, file_entry * fe, int attr, int color, int is_octal)
154 int i, r, l;
156 l = get_user_permissions (&fe->st);
158 if (is_octal)
160 /* Place of the access bit in octal mode */
161 l = width + l - 3;
162 r = l + 1;
164 else
166 /* The same to the triplet in string mode */
167 l = l * 3 + 1;
168 r = l + 3;
171 for (i = 0; i < width; i++)
173 if (i >= l && i < r)
175 if (attr == SELECTED || attr == MARKED_SELECTED)
176 tty_setcolor (MARKED_SELECTED_COLOR);
177 else
178 tty_setcolor (MARKED_COLOR);
180 else if (color >= 0)
181 tty_setcolor (color);
182 else
183 tty_lowlevel_setcolor (-color);
185 tty_print_char (dest[i]);
189 /* String representations of various file attributes */
190 /* name */
191 static const char *
192 string_file_name (file_entry * fe, int len)
194 static char buffer[MC_MAXPATHLEN * MB_LEN_MAX + 1];
196 (void) len;
197 g_strlcpy (buffer, fe->fname, sizeof (buffer));
198 return buffer;
201 static unsigned int
202 ilog10 (dev_t n)
204 unsigned int digits = 0;
207 digits++, n /= 10;
209 while (n != 0);
210 return digits;
213 static void
214 format_device_number (char *buf, size_t bufsize, dev_t dev)
216 dev_t major_dev = major (dev);
217 dev_t minor_dev = minor (dev);
218 unsigned int major_digits = ilog10 (major_dev);
219 unsigned int minor_digits = ilog10 (minor_dev);
221 g_assert (bufsize >= 1);
222 if (major_digits + 1 + minor_digits + 1 <= bufsize)
224 g_snprintf (buf, bufsize, "%lu,%lu", (unsigned long) major_dev, (unsigned long) minor_dev);
226 else
228 g_strlcpy (buf, _("[dev]"), bufsize);
232 /* size */
233 static const char *
234 string_file_size (file_entry * fe, int len)
236 static char buffer[BUF_TINY];
238 /* Don't ever show size of ".." since we don't calculate it */
239 if (!strcmp (fe->fname, ".."))
241 return _("UP--DIR");
244 #ifdef HAVE_STRUCT_STAT_ST_RDEV
245 if (S_ISBLK (fe->st.st_mode) || S_ISCHR (fe->st.st_mode))
246 format_device_number (buffer, len + 1, fe->st.st_rdev);
247 else
248 #endif
250 size_trunc_len (buffer, (unsigned int) len, fe->st.st_size, 0, panels_options.kilobyte_si);
252 return buffer;
255 /* bsize */
256 static const char *
257 string_file_size_brief (file_entry * fe, int len)
259 if (S_ISLNK (fe->st.st_mode) && !fe->f.link_to_dir)
261 return _("SYMLINK");
264 if ((S_ISDIR (fe->st.st_mode) || fe->f.link_to_dir) && strcmp (fe->fname, ".."))
266 return _("SUB-DIR");
269 return string_file_size (fe, len);
272 /* This functions return a string representation of a file entry */
273 /* type */
274 static const char *
275 string_file_type (file_entry * fe, int len)
277 static char buffer[2];
279 (void) len;
280 if (S_ISDIR (fe->st.st_mode))
281 buffer[0] = PATH_SEP;
282 else if (S_ISLNK (fe->st.st_mode))
284 if (fe->f.link_to_dir)
285 buffer[0] = '~';
286 else if (fe->f.stale_link)
287 buffer[0] = '!';
288 else
289 buffer[0] = '@';
291 else if (S_ISCHR (fe->st.st_mode))
292 buffer[0] = '-';
293 else if (S_ISSOCK (fe->st.st_mode))
294 buffer[0] = '=';
295 else if (S_ISDOOR (fe->st.st_mode))
296 buffer[0] = '>';
297 else if (S_ISBLK (fe->st.st_mode))
298 buffer[0] = '+';
299 else if (S_ISFIFO (fe->st.st_mode))
300 buffer[0] = '|';
301 else if (S_ISNAM (fe->st.st_mode))
302 buffer[0] = '#';
303 else if (!S_ISREG (fe->st.st_mode))
304 buffer[0] = '?'; /* non-regular of unknown kind */
305 else if (is_exe (fe->st.st_mode))
306 buffer[0] = '*';
307 else
308 buffer[0] = ' ';
309 buffer[1] = '\0';
310 return buffer;
313 /* mtime */
314 static const char *
315 string_file_mtime (file_entry * fe, int len)
317 (void) len;
318 return file_date (fe->st.st_mtime);
321 /* atime */
322 static const char *
323 string_file_atime (file_entry * fe, int len)
325 (void) len;
326 return file_date (fe->st.st_atime);
329 /* ctime */
330 static const char *
331 string_file_ctime (file_entry * fe, int len)
333 (void) len;
334 return file_date (fe->st.st_ctime);
337 /* perm */
338 static const char *
339 string_file_permission (file_entry * fe, int len)
341 (void) len;
342 return string_perm (fe->st.st_mode);
345 /* mode */
346 static const char *
347 string_file_perm_octal (file_entry * fe, int len)
349 static char buffer[10];
351 (void) len;
352 g_snprintf (buffer, sizeof (buffer), "0%06lo", (unsigned long) fe->st.st_mode);
353 return buffer;
356 /* nlink */
357 static const char *
358 string_file_nlinks (file_entry * fe, int len)
360 static char buffer[BUF_TINY];
362 (void) len;
363 g_snprintf (buffer, sizeof (buffer), "%16d", (int) fe->st.st_nlink);
364 return buffer;
367 /* inode */
368 static const char *
369 string_inode (file_entry * fe, int len)
371 static char buffer[10];
373 (void) len;
374 g_snprintf (buffer, sizeof (buffer), "%lu", (unsigned long) fe->st.st_ino);
375 return buffer;
378 /* nuid */
379 static const char *
380 string_file_nuid (file_entry * fe, int len)
382 static char buffer[10];
384 (void) len;
385 g_snprintf (buffer, sizeof (buffer), "%lu", (unsigned long) fe->st.st_uid);
386 return buffer;
389 /* ngid */
390 static const char *
391 string_file_ngid (file_entry * fe, int len)
393 static char buffer[10];
395 (void) len;
396 g_snprintf (buffer, sizeof (buffer), "%lu", (unsigned long) fe->st.st_gid);
397 return buffer;
400 /* owner */
401 static const char *
402 string_file_owner (file_entry * fe, int len)
404 (void) len;
405 return get_owner (fe->st.st_uid);
408 /* group */
409 static const char *
410 string_file_group (file_entry * fe, int len)
412 (void) len;
413 return get_group (fe->st.st_gid);
416 /* mark */
417 static const char *
418 string_marked (file_entry * fe, int len)
420 (void) len;
421 return fe->f.marked ? "*" : " ";
424 /* space */
425 static const char *
426 string_space (file_entry * fe, int len)
428 (void) fe;
429 (void) len;
430 return " ";
433 /* dot */
434 static const char *
435 string_dot (file_entry * fe, int len)
437 (void) fe;
438 (void) len;
439 return ".";
442 #define GT 1
444 /* *INDENT-OFF* */
445 panel_field_t panel_fields[] = {
447 "unsorted", 12, 1, J_LEFT_FIT,
448 /* TRANSLATORS: one single character to represent 'unsorted' sort mode */
449 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
450 N_("sort|u"),
451 N_("&Unsorted"), TRUE, FALSE,
452 string_file_name,
453 (sortfn *) unsorted
457 "name", 12, 1, J_LEFT_FIT,
458 /* TRANSLATORS: one single character to represent 'name' sort mode */
459 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
460 N_("sort|n"),
461 N_("&Name"), TRUE, TRUE,
462 string_file_name,
463 (sortfn *) sort_name
467 "version", 12, 1, J_LEFT_FIT,
468 /* TRANSLATORS: one single character to represent 'version' sort mode */
469 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
470 N_("sort|v"),
471 N_("&Version"), TRUE, FALSE,
472 string_file_name,
473 (sortfn *) sort_vers
477 "extension", 12, 1, J_LEFT_FIT,
478 /* TRANSLATORS: one single character to represent 'extension' sort mode */
479 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
480 N_("sort|e"),
481 N_("&Extension"), TRUE, FALSE,
482 string_file_name, /* TODO: string_file_ext */
483 (sortfn *) sort_ext
487 "size", 7, 0, J_RIGHT,
488 /* TRANSLATORS: one single character to represent 'size' sort mode */
489 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
490 N_("sort|s"),
491 N_("&Size"), TRUE, TRUE,
492 string_file_size,
493 (sortfn *) sort_size
497 "bsize", 7, 0, J_RIGHT,
499 N_("Block Size"), FALSE, FALSE,
500 string_file_size_brief,
501 (sortfn *) sort_size
505 "type", GT, 0, J_LEFT,
507 "", FALSE, TRUE,
508 string_file_type,
509 NULL
513 "mtime", 12, 0, J_RIGHT,
514 /* TRANSLATORS: one single character to represent 'Modify time' sort mode */
515 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
516 N_("sort|m"),
517 N_("&Modify time"), TRUE, TRUE,
518 string_file_mtime,
519 (sortfn *) sort_time
523 "atime", 12, 0, J_RIGHT,
524 /* TRANSLATORS: one single character to represent 'Access time' sort mode */
525 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
526 N_("sort|a"),
527 N_("&Access time"), TRUE, TRUE,
528 string_file_atime,
529 (sortfn *) sort_atime
533 "ctime", 12, 0, J_RIGHT,
534 /* TRANSLATORS: one single character to represent 'Change time' sort mode */
535 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
536 N_("sort|h"),
537 N_("C&hange time"), TRUE, TRUE,
538 string_file_ctime,
539 (sortfn *) sort_ctime
543 "perm", 10, 0, J_LEFT,
545 N_("Permission"), FALSE, TRUE,
546 string_file_permission,
547 NULL
551 "mode", 6, 0, J_RIGHT,
553 N_("Perm"), FALSE, TRUE,
554 string_file_perm_octal,
555 NULL
559 "nlink", 2, 0, J_RIGHT,
561 N_("Nl"), FALSE, TRUE,
562 string_file_nlinks, NULL
566 "inode", 5, 0, J_RIGHT,
567 /* TRANSLATORS: one single character to represent 'inode' sort mode */
568 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
569 N_("sort|i"),
570 N_("&Inode"), TRUE, TRUE,
571 string_inode,
572 (sortfn *) sort_inode
576 "nuid", 5, 0, J_RIGHT,
578 N_("UID"), FALSE, FALSE,
579 string_file_nuid,
580 NULL
584 "ngid", 5, 0, J_RIGHT,
586 N_("GID"), FALSE, FALSE,
587 string_file_ngid,
588 NULL
592 "owner", 8, 0, J_LEFT_FIT,
594 N_("Owner"), FALSE, TRUE,
595 string_file_owner,
596 NULL
600 "group", 8, 0, J_LEFT_FIT,
602 N_("Group"), FALSE, TRUE,
603 string_file_group,
604 NULL
608 "mark", 1, 0, J_RIGHT,
610 " ", FALSE, TRUE,
611 string_marked,
612 NULL
616 "|", 1, 0, J_RIGHT,
618 " ", FALSE, TRUE,
619 NULL,
620 NULL
624 "space", 1, 0, J_RIGHT,
626 " ", FALSE, TRUE,
627 string_space,
628 NULL
632 "dot", 1, 0, J_RIGHT,
634 " ", FALSE, FALSE,
635 string_dot,
636 NULL
640 NULL, 0, 0, J_RIGHT, NULL, NULL, FALSE, FALSE, NULL, NULL
643 /* *INDENT-ON* */
645 static int
646 file_compute_color (int attr, file_entry * fe)
648 switch (attr)
650 case SELECTED:
651 return (SELECTED_COLOR);
652 case MARKED:
653 return (MARKED_COLOR);
654 case MARKED_SELECTED:
655 return (MARKED_SELECTED_COLOR);
656 case STATUS:
657 return (NORMAL_COLOR);
658 case NORMAL:
659 default:
660 if (!panels_options.filetype_mode)
661 return (NORMAL_COLOR);
664 return mc_fhl_get_color (mc_filehighlight, fe);
667 /* Formats the file number file_index of panel in the buffer dest */
668 static void
669 format_file (char *dest, int limit, WPanel * panel, int file_index, int width, int attr,
670 int isstatus)
672 int color, length, empty_line;
673 const char *txt;
674 format_e *format, *home;
675 file_entry *fe;
677 (void) dest;
678 (void) limit;
679 length = 0;
680 empty_line = (file_index >= panel->count);
681 home = (isstatus) ? panel->status_format : panel->format;
682 fe = &panel->dir.list[file_index];
684 if (!empty_line)
685 color = file_compute_color (attr, fe);
686 else
687 color = NORMAL_COLOR;
689 for (format = home; format; format = format->next)
691 if (length == width)
692 break;
694 if (format->string_fn)
696 int len, perm;
697 char *preperad_text;
699 if (empty_line)
700 txt = " ";
701 else
702 txt = (*format->string_fn) (fe, format->field_len);
704 len = format->field_len;
705 if (len + length > width)
706 len = width - length;
707 if (len <= 0)
708 break;
710 perm = 0;
711 if (panels_options.permission_mode)
713 if (!strcmp (format->id, "perm"))
714 perm = 1;
715 else if (!strcmp (format->id, "mode"))
716 perm = 2;
719 if (color >= 0)
720 tty_setcolor (color);
721 else
722 tty_lowlevel_setcolor (-color);
724 preperad_text = (char *) str_fit_to_term (txt, len, format->just_mode);
725 if (perm)
726 add_permission_string (preperad_text, format->field_len, fe, attr, color, perm - 1);
727 else
728 tty_print_string (preperad_text);
730 length += len;
732 else
734 if (attr == SELECTED || attr == MARKED_SELECTED)
735 tty_setcolor (SELECTED_COLOR);
736 else
737 tty_setcolor (NORMAL_COLOR);
738 tty_print_one_vline (TRUE);
739 length++;
743 if (length < width)
744 tty_draw_hline (-1, -1, ' ', width - length);
747 static void
748 repaint_file (WPanel * panel, int file_index, int mv, int attr, int isstatus)
750 int second_column = 0;
751 int width;
752 int offset = 0;
753 char buffer[BUF_MEDIUM];
755 gboolean panel_is_split = !isstatus && panel->split;
757 width = panel->widget.cols - 2;
759 if (panel_is_split)
761 second_column = (file_index - panel->top_file) / llines (panel);
762 width = width / 2 - 1;
764 if (second_column != 0)
766 offset = 1 + width;
767 /*width = (panel->widget.cols-2) - (panel->widget.cols-2)/2 - 1; */
768 width = panel->widget.cols - offset - 2;
772 /* Nothing to paint */
773 if (width <= 0)
774 return;
776 if (mv)
778 if (panel_is_split)
779 widget_move (&panel->widget,
780 (file_index - panel->top_file) % llines (panel) + 2, offset + 1);
781 else
782 widget_move (&panel->widget, file_index - panel->top_file + 2, 1);
785 format_file (buffer, sizeof (buffer), panel, file_index, width, attr, isstatus);
787 if (panel_is_split)
789 if (second_column)
790 tty_print_char (' ');
791 else
793 tty_setcolor (NORMAL_COLOR);
794 tty_print_one_vline (TRUE);
799 static void
800 display_mini_info (WPanel * panel)
802 if (!show_mini_info)
803 return;
805 widget_move (&panel->widget, llines (panel) + 3, 1);
807 if (panel->searching)
809 tty_setcolor (INPUT_COLOR);
810 tty_print_char ('/');
811 tty_print_string (str_fit_to_term (panel->search_buffer, panel->widget.cols - 3, J_LEFT));
812 return;
815 /* Status resolves links and show them */
816 set_colors (panel);
818 if (S_ISLNK (panel->dir.list[panel->selected].st.st_mode))
820 char *lc_link, link_target[MC_MAXPATHLEN];
821 int len;
823 lc_link = concat_dir_and_file (panel->cwd, panel->dir.list[panel->selected].fname);
824 len = mc_readlink (lc_link, link_target, MC_MAXPATHLEN - 1);
825 g_free (lc_link);
826 if (len > 0)
828 link_target[len] = 0;
829 tty_print_string ("-> ");
830 tty_print_string (str_fit_to_term (link_target, panel->widget.cols - 5, J_LEFT_FIT));
832 else
833 tty_print_string (str_fit_to_term (_("<readlink failed>"),
834 panel->widget.cols - 2, J_LEFT));
836 else if (strcmp (panel->dir.list[panel->selected].fname, "..") == 0)
838 /* FIXME:
839 * while loading directory (do_load_dir() and do_reload_dir()),
840 * the actual stat info about ".." directory isn't got;
841 * so just don't display incorrect info about ".." directory */
842 tty_print_string (str_fit_to_term (_("UP--DIR"), panel->widget.cols - 2, J_LEFT));
844 else
845 /* Default behavior */
846 repaint_file (panel, panel->selected, 0, STATUS, 1);
849 static void
850 paint_dir (WPanel * panel)
852 int i;
853 int color; /* Color value of the line */
854 int items; /* Number of items */
856 items = llines (panel) * (panel->split ? 2 : 1);
858 for (i = 0; i < items; i++)
860 if (i + panel->top_file >= panel->count)
861 color = 0;
862 else
864 color = 2 * (panel->dir.list[i + panel->top_file].f.marked);
865 color += (panel->selected == i + panel->top_file && panel->active);
867 repaint_file (panel, i + panel->top_file, 1, color, 0);
869 tty_set_normal_attrs ();
872 static void
873 display_total_marked_size (WPanel * panel, int y, int x, gboolean size_only)
875 char buffer[BUF_SMALL], b_bytes[BUF_SMALL], *buf;
876 int cols;
878 if (panel->marked <= 0)
879 return;
881 buf = size_only ? b_bytes : buffer;
882 cols = panel->widget.cols - 2;
885 * This is a trick to use two ngettext() calls in one sentence.
886 * First make "N bytes", then insert it into "X in M files".
888 g_snprintf (b_bytes, sizeof (b_bytes),
889 ngettext ("%s byte", "%s bytes", (unsigned long) panel->total),
890 size_trunc_sep (panel->total, panels_options.kilobyte_si));
891 if (!size_only)
892 g_snprintf (buffer, sizeof (buffer),
893 ngettext ("%s in %d file", "%s in %d files", panel->marked),
894 b_bytes, panel->marked);
896 /* don't forget spaces around buffer content */
897 buf = (char *) str_trunc (buf, cols - 4);
899 if (x < 0)
900 /* center in panel */
901 x = (panel->widget.cols - str_term_width1 (buf)) / 2 - 1;
904 * y == llines (panel) + 2 for mini_info_separator
905 * y == panel->widget.lines - 1 for panel bottom frame
907 widget_move (&panel->widget, y, x);
908 tty_setcolor (MARKED_COLOR);
909 tty_printf (" %s ", buf);
912 static void
913 mini_info_separator (WPanel * panel)
915 if (show_mini_info)
917 const int y = llines (panel) + 2;
919 tty_setcolor (NORMAL_COLOR);
920 tty_draw_hline (panel->widget.y + y, panel->widget.x + 1,
921 ACS_HLINE, panel->widget.cols - 2);
922 /* Status displays total marked size.
923 * Centered in panel, full format. */
924 display_total_marked_size (panel, y, -1, FALSE);
928 static void
929 show_free_space (WPanel * panel)
931 /* Used to figure out how many free space we have */
932 static struct my_statfs myfs_stats;
933 /* Old current working directory for displaying free space */
934 static char *old_cwd = NULL;
936 /* Don't try to stat non-local fs */
937 if (!vfs_file_is_local (panel->cwd) || !free_space)
938 return;
940 if (old_cwd == NULL || strcmp (old_cwd, panel->cwd) != 0)
942 char rpath[PATH_MAX];
944 init_my_statfs ();
945 g_free (old_cwd);
946 old_cwd = g_strdup (panel->cwd);
948 if (mc_realpath (panel->cwd, rpath) == NULL)
949 return;
951 my_statfs (&myfs_stats, rpath);
954 if (myfs_stats.avail > 0 || myfs_stats.total > 0)
956 char buffer1[6], buffer2[6], tmp[BUF_SMALL];
957 size_trunc_len (buffer1, sizeof (buffer1) - 1, myfs_stats.avail, 1, panels_options.kilobyte_si);
958 size_trunc_len (buffer2, sizeof (buffer2) - 1, myfs_stats.total, 1, panels_options.kilobyte_si);
959 g_snprintf (tmp, sizeof (tmp), " %s/%s (%d%%) ", buffer1, buffer2,
960 myfs_stats.total > 0 ?
961 (int) (100 * (double) myfs_stats.avail / myfs_stats.total) : 0);
962 widget_move (&panel->widget, panel->widget.lines - 1,
963 panel->widget.cols - 2 - (int) strlen (tmp));
964 tty_setcolor (NORMAL_COLOR);
965 tty_print_string (tmp);
969 static void
970 show_dir (WPanel * panel)
972 gchar *tmp;
973 set_colors (panel);
974 draw_box (panel->widget.owner,
975 panel->widget.y, panel->widget.x, panel->widget.lines, panel->widget.cols, FALSE);
977 if (show_mini_info)
979 widget_move (&panel->widget, llines (panel) + 2, 0);
980 tty_print_alt_char (ACS_LTEE, FALSE);
981 widget_move (&panel->widget, llines (panel) + 2, panel->widget.cols - 1);
982 tty_print_alt_char (ACS_RTEE, FALSE);
985 widget_move (&panel->widget, 0, 1);
986 tty_print_string (panel_history_prev_item_sign);
988 tmp = panels_options.show_dot_files ? panel_hiddenfiles_sign_show : panel_hiddenfiles_sign_hide;
989 tmp = g_strdup_printf ("%s[%s]%s", tmp, panel_history_show_list_sign,
990 panel_history_next_item_sign);
992 widget_move (&panel->widget, 0, panel->widget.cols - 6);
993 tty_print_string (tmp);
995 g_free (tmp);
997 if (panel->active)
998 tty_setcolor (REVERSE_COLOR);
1000 widget_move (&panel->widget, 0, 3);
1002 tty_printf (" %s ",
1003 str_term_trim (strip_home_and_password (panel->cwd),
1004 min (max (panel->widget.cols - 12, 0), panel->widget.cols)));
1006 if (!show_mini_info)
1008 if (panel->marked == 0)
1010 /* Show size of curret file in the bottom of panel */
1011 if (S_ISREG (panel->dir.list[panel->selected].st.st_mode))
1013 char buffer[BUF_SMALL];
1015 g_snprintf (buffer, sizeof (buffer), " %s ",
1016 size_trunc_sep (panel->dir.list[panel->selected].st.st_size,
1017 panels_options.kilobyte_si));
1018 tty_setcolor (NORMAL_COLOR);
1019 widget_move (&panel->widget, panel->widget.lines - 1, 4);
1020 tty_print_string (buffer);
1023 else
1025 /* Show total size of marked files
1026 * In the bottom of panel, display size only. */
1027 display_total_marked_size (panel, panel->widget.lines - 1, 2, TRUE);
1031 show_free_space (panel);
1033 if (panel->active)
1034 tty_set_normal_attrs ();
1037 /* To be used only by long_frame and full_frame to adjust top_file */
1038 static void
1039 adjust_top_file (WPanel * panel)
1041 int old_top = panel->top_file;
1043 if (panel->selected - old_top > llines (panel))
1044 panel->top_file = panel->selected;
1045 if (old_top - panel->count > llines (panel))
1046 panel->top_file = panel->count - llines (panel);
1049 /* Repaint everything, including frame and separator */
1050 static void
1051 paint_panel (WPanel * panel)
1053 paint_frame (panel); /* including show_dir */
1054 paint_dir (panel);
1055 mini_info_separator (panel);
1056 display_mini_info (panel);
1057 panel->dirty = 0;
1060 /* add "#enc:encodning" to end of path */
1061 /* if path end width a previous #enc:, only encoding is changed no additional
1062 * #enc: is appended
1063 * retun new string
1065 static char *
1066 add_encoding_to_path (const char *path, const char *encoding)
1068 char *result;
1069 char *semi;
1070 char *slash;
1072 semi = g_strrstr (path, VFS_ENCODING_PREFIX);
1074 if (semi != NULL)
1076 slash = strchr (semi, PATH_SEP);
1077 if (slash != NULL)
1079 result = g_strconcat (path, PATH_SEP_STR VFS_ENCODING_PREFIX, encoding, (char *) NULL);
1081 else
1083 *semi = '\0';
1084 result = g_strconcat (path, PATH_SEP_STR VFS_ENCODING_PREFIX, encoding, (char *) NULL);
1085 *semi = '#';
1088 else
1090 result = g_strconcat (path, PATH_SEP_STR VFS_ENCODING_PREFIX, encoding, (char *) NULL);
1093 return result;
1096 char *
1097 remove_encoding_from_path (const char *path)
1099 GString *ret;
1100 GString *tmp_path, *tmp_conv;
1101 char *tmp, *tmp2;
1102 const char *enc;
1103 GIConv converter;
1105 ret = g_string_new ("");
1106 tmp_conv = g_string_new ("");
1108 tmp_path = g_string_new (path);
1110 while ((tmp = g_strrstr (tmp_path->str, PATH_SEP_STR VFS_ENCODING_PREFIX)) != NULL)
1112 enc = vfs_get_encoding ((const char *) tmp);
1113 converter = enc ? str_crt_conv_to (enc) : str_cnv_to_term;
1114 if (converter == INVALID_CONV)
1115 converter = str_cnv_to_term;
1117 tmp2 = tmp + 1;
1118 while (*tmp2 && *tmp2 != PATH_SEP)
1119 tmp2++;
1121 if (*tmp2)
1123 str_vfs_convert_from (converter, tmp2, tmp_conv);
1124 g_string_prepend (ret, tmp_conv->str);
1125 g_string_set_size (tmp_conv, 0);
1127 g_string_set_size (tmp_path, tmp - tmp_path->str);
1128 str_close_conv (converter);
1130 g_string_prepend (ret, tmp_path->str);
1131 g_string_free (tmp_path, TRUE);
1132 g_string_free (tmp_conv, TRUE);
1134 tmp = ret->str;
1135 g_string_free (ret, FALSE);
1136 return tmp;
1140 * Repaint the contents of the panels without frames. To schedule panel
1141 * for repainting, set panel->dirty to 1. There are many reasons why
1142 * the panels need to be repainted, and this is a costly operation, so
1143 * it's done once per event.
1145 void
1146 update_dirty_panels (void)
1148 if (current_panel->dirty)
1149 paint_panel (current_panel);
1151 if ((get_other_type () == view_listing) && other_panel->dirty)
1152 paint_panel (other_panel);
1155 static void
1156 do_select (WPanel * panel, int i)
1158 if (i != panel->selected)
1160 panel->dirty = 1;
1161 panel->selected = i;
1162 panel->top_file = panel->selected - (panel->widget.lines - 2) / 2;
1163 if (panel->top_file < 0)
1164 panel->top_file = 0;
1168 static void
1169 do_try_to_select (WPanel * panel, const char *name)
1171 int i;
1172 char *subdir;
1174 if (!name)
1176 do_select (panel, 0);
1177 return;
1180 /* We only want the last component of the directory,
1181 * and from this only the name without suffix. */
1182 subdir = vfs_strip_suffix_from_filename (x_basename (name));
1184 /* Search that subdirectory, if found select it */
1185 for (i = 0; i < panel->count; i++)
1187 if (strcmp (subdir, panel->dir.list[i].fname) == 0)
1189 do_select (panel, i);
1190 g_free (subdir);
1191 return;
1195 /* Try to select a file near the file that is missing */
1196 if (panel->selected >= panel->count)
1197 do_select (panel, panel->count - 1);
1198 g_free (subdir);
1201 void
1202 try_to_select (WPanel * panel, const char *name)
1204 do_try_to_select (panel, name);
1205 select_item (panel);
1208 void
1209 panel_update_cols (Widget * widget, int frame_size)
1211 int cols, origin;
1213 if (horizontal_split)
1215 widget->cols = COLS;
1216 return;
1219 if (frame_size == frame_full)
1221 cols = COLS;
1222 origin = 0;
1224 else
1226 if (widget == get_panel_widget (0))
1228 cols = first_panel_size;
1229 origin = 0;
1231 else
1233 cols = COLS - first_panel_size;
1234 origin = first_panel_size;
1238 widget->cols = cols;
1239 widget->x = origin;
1242 extern int saving_setup;
1243 static char *
1244 panel_save_name (WPanel * panel)
1246 /* If the program is shuting down */
1247 if ((midnight_shutdown && auto_save_setup) || saving_setup)
1248 return g_strdup (panel->panel_name);
1249 else
1250 return g_strconcat ("Temporal:", panel->panel_name, (char *) NULL);
1253 void
1254 panel_clean_dir (WPanel * panel)
1256 int count = panel->count;
1258 panel->count = 0;
1259 panel->top_file = 0;
1260 panel->selected = 0;
1261 panel->marked = 0;
1262 panel->dirs_marked = 0;
1263 panel->total = 0;
1264 panel->searching = FALSE;
1265 panel->is_panelized = 0;
1266 panel->dirty = 1;
1268 clean_dir (&panel->dir, count);
1271 static void
1272 panel_destroy (WPanel * p)
1274 size_t i;
1276 char *name = panel_save_name (p);
1278 panel_save_setup (p, name);
1279 panel_clean_dir (p);
1281 /* save and clean history */
1282 if (p->dir_history)
1284 history_put (p->hist_name, p->dir_history);
1286 p->dir_history = g_list_first (p->dir_history);
1287 g_list_foreach (p->dir_history, (GFunc) g_free, NULL);
1288 g_list_free (p->dir_history);
1291 g_free (p->hist_name);
1293 delete_format (p->format);
1294 delete_format (p->status_format);
1296 g_free (p->user_format);
1297 for (i = 0; i < LIST_TYPES; i++)
1298 g_free (p->user_status_format[i]);
1299 g_free (p->dir.list);
1300 g_free (p->panel_name);
1301 g_free (name);
1304 static void
1305 panel_format_modified (WPanel * panel)
1307 panel->format_modified = 1;
1310 /** Panel creation.
1311 * @param panel_name the name of the panel for setup retieving
1312 * @returns new instance of WPanel
1314 WPanel *
1315 panel_new (const char *panel_name)
1317 return panel_new_with_dir (panel_name, NULL);
1320 /** Panel creation for specified directory.
1321 * @param panel_name specifies the name of the panel for setup retieving
1322 * @param the path of working panel directory. If path is NULL then panel will be created for current directory
1323 * @returns new instance of WPanel
1325 WPanel *
1326 panel_new_with_dir (const char *panel_name, const char *wpath)
1328 WPanel *panel;
1329 char *section;
1330 int i, err;
1331 char curdir[MC_MAXPATHLEN] = "\0";
1333 panel = g_new0 (WPanel, 1);
1335 /* No know sizes of the panel at startup */
1336 init_widget (&panel->widget, 0, 0, 0, 0, panel_callback, panel_event);
1338 /* We do not want the cursor */
1339 widget_want_cursor (panel->widget, 0);
1341 if (wpath != NULL)
1343 g_strlcpy (panel->cwd, wpath, sizeof (panel->cwd));
1344 mc_get_current_wd (curdir, sizeof (curdir) - 2);
1346 else
1347 mc_get_current_wd (panel->cwd, sizeof (panel->cwd) - 2);
1349 strcpy (panel->lwd, ".");
1351 panel->hist_name = g_strconcat ("Dir Hist ", panel_name, (char *) NULL);
1352 panel->dir_history = history_get (panel->hist_name);
1353 directory_history_add (panel, panel->cwd);
1355 panel->dir.list = g_new (file_entry, MIN_FILES);
1356 panel->dir.size = MIN_FILES;
1357 panel->active = 0;
1358 panel->filter = 0;
1359 panel->split = 0;
1360 panel->top_file = 0;
1361 panel->selected = 0;
1362 panel->marked = 0;
1363 panel->total = 0;
1364 panel->reverse = 0;
1365 panel->dirty = 1;
1366 panel->searching = FALSE;
1367 panel->dirs_marked = 0;
1368 panel->is_panelized = 0;
1369 panel->format = 0;
1370 panel->status_format = 0;
1371 panel->format_modified = 1;
1373 panel->panel_name = g_strdup (panel_name);
1374 panel->user_format = g_strdup (DEFAULT_USER_FORMAT);
1376 panel->codepage = SELECT_CHARSET_NO_TRANSLATE;
1378 for (i = 0; i < LIST_TYPES; i++)
1379 panel->user_status_format[i] = g_strdup (DEFAULT_USER_FORMAT);
1381 panel->search_buffer[0] = '\0';
1382 panel->prev_search_buffer[0] = '\0';
1383 panel->frame_size = frame_half;
1385 section = g_strconcat ("Temporal:", panel->panel_name, (char *) NULL);
1386 if (!mc_config_has_group (mc_main_config, section))
1388 g_free (section);
1389 section = g_strdup (panel->panel_name);
1391 panel_load_setup (panel, section);
1392 g_free (section);
1394 /* Load format strings */
1395 err = set_panel_formats (panel);
1396 if (err != 0)
1397 set_panel_formats (panel);
1399 #ifdef HAVE_CHARSET
1401 const char *enc = vfs_get_encoding (panel->cwd);
1402 if (enc != NULL)
1403 panel->codepage = get_codepage_index (enc);
1405 #endif
1407 if (mc_chdir (panel->cwd) != 0)
1409 panel->codepage = SELECT_CHARSET_NO_TRANSLATE;
1410 mc_get_current_wd (panel->cwd, sizeof (panel->cwd) - 2);
1413 /* Load the default format */
1414 panel->count =
1415 do_load_dir (panel->cwd, &panel->dir, panel->current_sort_field->sort_routine,
1416 panel->reverse, panel->case_sensitive, panel->exec_first, panel->filter);
1418 /* Restore old right path */
1419 if (curdir[0] != '\0')
1420 err = mc_chdir (curdir);
1422 return panel;
1425 void
1426 panel_reload (WPanel * panel)
1428 struct stat current_stat;
1430 if (panels_options.fast_reload && !stat (panel->cwd, &current_stat)
1431 && current_stat.st_ctime == panel->dir_stat.st_ctime
1432 && current_stat.st_mtime == panel->dir_stat.st_mtime)
1433 return;
1435 while (mc_chdir (panel->cwd) == -1)
1437 char *last_slash;
1439 if (panel->cwd[0] == PATH_SEP && panel->cwd[1] == 0)
1441 panel_clean_dir (panel);
1442 panel->count = set_zero_dir (&panel->dir) ? 1 : 0;
1443 return;
1445 last_slash = strrchr (panel->cwd, PATH_SEP);
1446 if (!last_slash || last_slash == panel->cwd)
1447 strcpy (panel->cwd, PATH_SEP_STR);
1448 else
1449 *last_slash = 0;
1450 memset (&(panel->dir_stat), 0, sizeof (panel->dir_stat));
1451 show_dir (panel);
1454 panel->count =
1455 do_reload_dir (panel->cwd, &panel->dir, panel->current_sort_field->sort_routine,
1456 panel->count, panel->reverse, panel->case_sensitive,
1457 panel->exec_first, panel->filter);
1459 panel->dirty = 1;
1460 if (panel->selected >= panel->count)
1461 do_select (panel, panel->count - 1);
1463 recalculate_panel_summary (panel);
1466 static void
1467 panel_paint_sort_info (WPanel * panel)
1469 const char *sort_sign = (panel->reverse) ? panel_sort_down_sign : panel_sort_up_sign;
1470 char *str;
1472 if (*panel->current_sort_field->hotkey == '\0')
1473 return;
1475 str = g_strdup_printf ("%s%s", sort_sign, Q_ (panel->current_sort_field->hotkey));
1477 widget_move (&panel->widget, 1, 1);
1478 tty_print_string (str);
1479 g_free (str);
1482 static gchar *
1483 panel_get_title_without_hotkey (const char *title)
1485 char *translated_title;
1486 char *hkey;
1488 if (title == NULL)
1489 return NULL;
1490 if (title[0] == '\0')
1491 return g_strdup ("");
1493 translated_title = g_strdup (_(title));
1495 hkey = strchr (translated_title, '&');
1496 if ((hkey != NULL) && (hkey[1] != '\0'))
1497 memmove ((void *) hkey, (void *) hkey + 1, strlen (hkey));
1499 return translated_title;
1502 static void
1503 paint_frame (WPanel * panel)
1505 int side, width;
1506 GString *format_txt;
1508 if (!panel->split)
1509 adjust_top_file (panel);
1511 widget_erase (&panel->widget);
1512 show_dir (panel);
1514 widget_move (&panel->widget, 1, 1);
1516 for (side = 0; side <= panel->split; side++)
1518 format_e *format;
1520 if (side)
1522 tty_setcolor (NORMAL_COLOR);
1523 tty_print_one_vline (TRUE);
1524 width = panel->widget.cols - panel->widget.cols / 2 - 1;
1526 else if (panel->split)
1527 width = panel->widget.cols / 2 - 3;
1528 else
1529 width = panel->widget.cols - 2;
1531 format_txt = g_string_new ("");
1532 for (format = panel->format; format; format = format->next)
1534 if (format->string_fn)
1536 g_string_set_size (format_txt, 0);
1538 if (panel->list_type == list_long
1539 && strcmp (format->id, panel->current_sort_field->id) == 0)
1540 g_string_append (format_txt,
1541 (panel->reverse) ? panel_sort_down_sign : panel_sort_up_sign);
1543 g_string_append (format_txt, format->title);
1544 if (strcmp (format->id, "name") == 0 && panel->filter && *panel->filter)
1546 g_string_append (format_txt, " [");
1547 g_string_append (format_txt, panel->filter);
1548 g_string_append (format_txt, "]");
1551 tty_setcolor (HEADER_COLOR);
1552 tty_print_string (str_fit_to_term (format_txt->str, format->field_len,
1553 J_CENTER_LEFT));
1554 width -= format->field_len;
1556 else
1558 tty_setcolor (NORMAL_COLOR);
1559 tty_print_one_vline (TRUE);
1560 width--;
1563 g_string_free (format_txt, TRUE);
1565 if (width > 0)
1566 tty_draw_hline (-1, -1, ' ', width);
1569 if (panel->list_type != list_long)
1570 panel_paint_sort_info (panel);
1573 static const char *
1574 parse_panel_size (WPanel * panel, const char *format, int isstatus)
1576 int frame = frame_half;
1577 format = skip_separators (format);
1579 if (!strncmp (format, "full", 4))
1581 frame = frame_full;
1582 format += 4;
1584 else if (!strncmp (format, "half", 4))
1586 frame = frame_half;
1587 format += 4;
1590 if (!isstatus)
1592 panel->frame_size = frame;
1593 panel->split = 0;
1596 /* Now, the optional column specifier */
1597 format = skip_separators (format);
1599 if (*format == '1' || *format == '2')
1601 if (!isstatus)
1602 panel->split = *format == '2';
1603 format++;
1606 if (!isstatus)
1607 panel_update_cols (&(panel->widget), panel->frame_size);
1609 return skip_separators (format);
1612 /* Format is:
1614 all := panel_format? format
1615 panel_format := [full|half] [1|2]
1616 format := one_format_e
1617 | format , one_format_e
1619 one_format_e := just format.id [opt_size]
1620 just := [<=>]
1621 opt_size := : size [opt_expand]
1622 size := [0-9]+
1623 opt_expand := +
1627 static format_e *
1628 parse_display_format (WPanel * panel, const char *format, char **error, int isstatus,
1629 int *res_total_cols)
1631 format_e *darr, *old = 0, *home = 0; /* The formats we return */
1632 int total_cols = 0; /* Used columns by the format */
1633 int set_justify; /* flag: set justification mode? */
1634 align_crt_t justify = J_LEFT; /* Which mode. */
1635 size_t i;
1637 static size_t i18n_timelength = 0; /* flag: check ?Time length at startup */
1639 *error = 0;
1641 if (i18n_timelength == 0)
1643 i18n_timelength = i18n_checktimelength (); /* Musn't be 0 */
1645 for (i = 0; panel_fields[i].id != NULL; i++)
1646 if (strcmp ("time", panel_fields[i].id + 1) == 0)
1647 panel_fields[i].min_size = i18n_timelength;
1651 * This makes sure that the panel and mini status full/half mode
1652 * setting is equal
1654 format = parse_panel_size (panel, format, isstatus);
1656 while (*format)
1657 { /* format can be an empty string */
1658 int found = 0;
1660 darr = g_new0 (format_e, 1);
1662 /* I'm so ugly, don't look at me :-) */
1663 if (!home)
1664 home = old = darr;
1666 old->next = darr;
1667 darr->next = 0;
1668 old = darr;
1670 format = skip_separators (format);
1672 if (strchr ("<=>", *format))
1674 set_justify = 1;
1675 switch (*format)
1677 case '<':
1678 justify = J_LEFT;
1679 break;
1680 case '=':
1681 justify = J_CENTER;
1682 break;
1683 case '>':
1684 default:
1685 justify = J_RIGHT;
1686 break;
1688 format = skip_separators (format + 1);
1690 else
1691 set_justify = 0;
1693 for (i = 0; panel_fields[i].id != NULL; i++)
1695 size_t klen = strlen (panel_fields[i].id);
1697 if (strncmp (format, panel_fields[i].id, klen) != 0)
1698 continue;
1700 format += klen;
1702 darr->requested_field_len = panel_fields[i].min_size;
1703 darr->string_fn = panel_fields[i].string_fn;
1704 darr->title = panel_get_title_without_hotkey (panel_fields[i].title_hotkey);
1706 darr->id = panel_fields[i].id;
1707 darr->expand = panel_fields[i].expands;
1708 darr->just_mode = panel_fields[i].default_just;
1710 if (set_justify)
1712 if (IS_FIT (darr->just_mode))
1713 darr->just_mode = MAKE_FIT (justify);
1714 else
1715 darr->just_mode = justify;
1717 found = 1;
1719 format = skip_separators (format);
1721 /* If we have a size specifier */
1722 if (*format == ':')
1724 int req_length;
1726 /* If the size was specified, we don't want
1727 * auto-expansion by default
1729 darr->expand = 0;
1730 format++;
1731 req_length = atoi (format);
1732 darr->requested_field_len = req_length;
1734 format = skip_numbers (format);
1736 /* Now, if they insist on expansion */
1737 if (*format == '+')
1739 darr->expand = 1;
1740 format++;
1745 break;
1747 if (!found)
1749 char *tmp_format = g_strdup (format);
1751 int pos = min (8, strlen (format));
1752 delete_format (home);
1753 tmp_format[pos] = 0;
1754 *error = g_strconcat (_("Unknown tag on display format:"), " ", tmp_format, (char *) NULL);
1755 g_free (tmp_format);
1756 return 0;
1758 total_cols += darr->requested_field_len;
1761 *res_total_cols = total_cols;
1762 return home;
1765 static format_e *
1766 use_display_format (WPanel * panel, const char *format, char **error, int isstatus)
1768 #define MAX_EXPAND 4
1769 int expand_top = 0; /* Max used element in expand */
1770 int usable_columns; /* Usable columns in the panel */
1771 int total_cols = 0;
1772 int i;
1773 format_e *darr, *home;
1775 if (!format)
1776 format = DEFAULT_USER_FORMAT;
1778 home = parse_display_format (panel, format, error, isstatus, &total_cols);
1780 if (*error)
1781 return 0;
1783 panel->dirty = 1;
1785 /* Status needn't to be split */
1786 usable_columns = ((panel->widget.cols - 2) / ((isstatus)
1788 : (panel->split + 1))) - (!isstatus
1789 && panel->split);
1791 /* Look for the expandable fields and set field_len based on the requested field len */
1792 for (darr = home; darr && expand_top < MAX_EXPAND; darr = darr->next)
1794 darr->field_len = darr->requested_field_len;
1795 if (darr->expand)
1796 expand_top++;
1799 /* If we used more columns than the available columns, adjust that */
1800 if (total_cols > usable_columns)
1802 int pdif, dif = total_cols - usable_columns;
1804 while (dif)
1806 pdif = dif;
1807 for (darr = home; darr; darr = darr->next)
1809 if (dif && darr->field_len - 1)
1811 darr->field_len--;
1812 dif--;
1816 /* avoid endless loop if num fields > 40 */
1817 if (pdif == dif)
1818 break;
1820 total_cols = usable_columns; /* give up, the rest should be truncated */
1823 /* Expand the available space */
1824 if ((usable_columns > total_cols) && expand_top)
1826 int spaces = (usable_columns - total_cols) / expand_top;
1827 int extra = (usable_columns - total_cols) % expand_top;
1829 for (i = 0, darr = home; darr && (i < expand_top); darr = darr->next)
1830 if (darr->expand)
1832 darr->field_len += (spaces + ((i == 0) ? extra : 0));
1833 i++;
1836 return home;
1839 /* Switches the panel to the mode specified in the format */
1840 /* Seting up both format and status string. Return: 0 - on success; */
1841 /* 1 - format error; 2 - status error; 3 - errors in both formats. */
1843 set_panel_formats (WPanel * p)
1845 format_e *form;
1846 char *err = NULL;
1847 int retcode = 0;
1849 form = use_display_format (p, panel_format (p), &err, 0);
1851 if (err != NULL)
1853 g_free (err);
1854 retcode = 1;
1856 else
1858 delete_format (p->format);
1859 p->format = form;
1862 if (show_mini_info)
1864 form = use_display_format (p, mini_status_format (p), &err, 1);
1866 if (err != NULL)
1868 g_free (err);
1869 retcode += 2;
1871 else
1873 delete_format (p->status_format);
1874 p->status_format = form;
1878 panel_format_modified (p);
1879 panel_update_cols (&(p->widget), p->frame_size);
1881 if (retcode)
1882 message (D_ERROR, _("Warning"),
1883 _("User supplied format looks invalid, reverting to default."));
1884 if (retcode & 0x01)
1886 g_free (p->user_format);
1887 p->user_format = g_strdup (DEFAULT_USER_FORMAT);
1889 if (retcode & 0x02)
1891 g_free (p->user_status_format[p->list_type]);
1892 p->user_status_format[p->list_type] = g_strdup (DEFAULT_USER_FORMAT);
1895 return retcode;
1898 /* Given the panel->view_type returns the format string to be parsed */
1899 static const char *
1900 panel_format (WPanel * panel)
1902 switch (panel->list_type)
1905 case list_long:
1906 return "full perm space nlink space owner space group space size space mtime space name";
1908 case list_brief:
1909 return "half 2 type name";
1911 case list_user:
1912 return panel->user_format;
1914 default:
1915 case list_full:
1916 return "half type name | size | mtime";
1920 static const char *
1921 mini_status_format (WPanel * panel)
1923 if (panel->user_mini_status)
1924 return panel->user_status_format[panel->list_type];
1926 switch (panel->list_type)
1929 case list_long:
1930 return "full perm space nlink space owner space group space size space mtime space name";
1932 case list_brief:
1933 return "half type name space bsize space perm space";
1935 case list_full:
1936 return "half type name";
1938 default:
1939 case list_user:
1940 return panel->user_format;
1944 /* */
1945 /* Panel operation commands */
1946 /* */
1948 /* Used to emulate Lynx's entering leaving a directory with the arrow keys */
1949 static cb_ret_t
1950 maybe_cd (int move_up_dir)
1952 if (panels_options.navigate_with_arrows && (cmdline->buffer[0] == '\0'))
1954 if (move_up_dir)
1956 do_cd ("..", cd_exact);
1957 return MSG_HANDLED;
1960 if (S_ISDIR (selection (current_panel)->st.st_mode)
1961 || link_isdir (selection (current_panel)))
1963 do_cd (selection (current_panel)->fname, cd_exact);
1964 return MSG_HANDLED;
1967 return MSG_NOT_HANDLED;
1970 /* Returns the number of items in the given panel */
1971 static int
1972 ITEMS (WPanel * p)
1974 if (p->split)
1975 return llines (p) * 2;
1976 else
1977 return llines (p);
1980 /* Select current item and readjust the panel */
1981 void
1982 select_item (WPanel * panel)
1984 int items = ITEMS (panel);
1986 /* Although currently all over the code we set the selection and
1987 top file to decent values before calling select_item, I could
1988 forget it someday, so it's better to do the actual fitting here */
1990 if (panel->top_file < 0)
1991 panel->top_file = 0;
1993 if (panel->selected < 0)
1994 panel->selected = 0;
1996 if (panel->selected > panel->count - 1)
1997 panel->selected = panel->count - 1;
1999 if (panel->top_file > panel->count - 1)
2000 panel->top_file = panel->count - 1;
2002 if ((panel->count - panel->top_file) < items)
2004 panel->top_file = panel->count - items;
2005 if (panel->top_file < 0)
2006 panel->top_file = 0;
2009 if (panel->selected < panel->top_file)
2010 panel->top_file = panel->selected;
2012 if ((panel->selected - panel->top_file) >= items)
2013 panel->top_file = panel->selected - items + 1;
2015 panel->dirty = 1;
2017 execute_hooks (select_file_hook);
2020 /* Clears all files in the panel, used only when one file was marked */
2021 void
2022 unmark_files (WPanel * panel)
2024 int i;
2026 if (!panel->marked)
2027 return;
2028 for (i = 0; i < panel->count; i++)
2029 file_mark (panel, i, 0);
2031 panel->dirs_marked = 0;
2032 panel->marked = 0;
2033 panel->total = 0;
2036 static void
2037 unselect_item (WPanel * panel)
2039 repaint_file (panel, panel->selected, 1, 2 * selection (panel)->f.marked, 0);
2042 static void
2043 move_down (WPanel * panel)
2045 if (panel->selected + 1 == panel->count)
2046 return;
2048 unselect_item (panel);
2049 panel->selected++;
2050 if (panels_options.scroll_pages && panel->selected - panel->top_file == ITEMS (panel))
2052 /* Scroll window half screen */
2053 panel->top_file += ITEMS (panel) / 2;
2054 if (panel->top_file > panel->count - ITEMS (panel))
2055 panel->top_file = panel->count - ITEMS (panel);
2056 paint_dir (panel);
2058 select_item (panel);
2061 static void
2062 move_up (WPanel * panel)
2064 if (panel->selected == 0)
2065 return;
2067 unselect_item (panel);
2068 panel->selected--;
2069 if (panels_options.scroll_pages && panel->selected < panel->top_file)
2071 /* Scroll window half screen */
2072 panel->top_file -= ITEMS (panel) / 2;
2073 if (panel->top_file < 0)
2074 panel->top_file = 0;
2075 paint_dir (panel);
2077 select_item (panel);
2080 /* Changes the selection by lines (may be negative) */
2081 static void
2082 move_selection (WPanel * panel, int lines)
2084 int new_pos;
2085 int adjust = 0;
2087 new_pos = panel->selected + lines;
2088 if (new_pos >= panel->count)
2089 new_pos = panel->count - 1;
2091 if (new_pos < 0)
2092 new_pos = 0;
2094 unselect_item (panel);
2095 panel->selected = new_pos;
2097 if (panel->selected - panel->top_file >= ITEMS (panel))
2099 panel->top_file += lines;
2100 adjust = 1;
2103 if (panel->selected - panel->top_file < 0)
2105 panel->top_file += lines;
2106 adjust = 1;
2109 if (adjust)
2111 if (panel->top_file > panel->selected)
2112 panel->top_file = panel->selected;
2113 if (panel->top_file < 0)
2114 panel->top_file = 0;
2115 paint_dir (panel);
2117 select_item (panel);
2120 static cb_ret_t
2121 move_left (WPanel * panel)
2123 if (panel->split)
2125 move_selection (panel, -llines (panel));
2126 return MSG_HANDLED;
2128 else
2129 return maybe_cd (1); /* cd .. */
2132 static int
2133 move_right (WPanel * panel)
2135 if (panel->split)
2137 move_selection (panel, llines (panel));
2138 return MSG_HANDLED;
2140 else
2141 return maybe_cd (0); /* cd (selection) */
2144 static void
2145 prev_page (WPanel * panel)
2147 int items;
2149 if (!panel->selected && !panel->top_file)
2150 return;
2151 unselect_item (panel);
2152 items = ITEMS (panel);
2153 if (panel->top_file < items)
2154 items = panel->top_file;
2155 if (!items)
2156 panel->selected = 0;
2157 else
2158 panel->selected -= items;
2159 panel->top_file -= items;
2161 select_item (panel);
2162 paint_dir (panel);
2165 static void
2166 ctrl_prev_page (WPanel * panel)
2168 (void) panel;
2169 do_cd ("..", cd_exact);
2172 static void
2173 next_page (WPanel * panel)
2175 int items;
2177 if (panel->selected == panel->count - 1)
2178 return;
2179 unselect_item (panel);
2180 items = ITEMS (panel);
2181 if (panel->top_file > panel->count - 2 * items)
2182 items = panel->count - items - panel->top_file;
2183 if (panel->top_file + items < 0)
2184 items = -panel->top_file;
2185 if (!items)
2186 panel->selected = panel->count - 1;
2187 else
2188 panel->selected += items;
2189 panel->top_file += items;
2191 select_item (panel);
2192 paint_dir (panel);
2195 static void
2196 ctrl_next_page (WPanel * panel)
2198 if ((S_ISDIR (selection (panel)->st.st_mode) || link_isdir (selection (panel))))
2200 do_cd (selection (panel)->fname, cd_exact);
2204 static void
2205 goto_top_file (WPanel * panel)
2207 unselect_item (panel);
2208 panel->selected = panel->top_file;
2209 select_item (panel);
2212 static void
2213 goto_middle_file (WPanel * panel)
2215 unselect_item (panel);
2216 panel->selected = panel->top_file + (ITEMS (panel) / 2);
2217 select_item (panel);
2220 static void
2221 goto_bottom_file (WPanel * panel)
2223 unselect_item (panel);
2224 panel->selected = panel->top_file + ITEMS (panel) - 1;
2225 select_item (panel);
2228 static void
2229 move_home (WPanel * panel)
2231 if (panel->selected == 0)
2232 return;
2233 unselect_item (panel);
2235 if (torben_fj_mode)
2237 int middle_pos = panel->top_file + (ITEMS (panel) / 2);
2239 if (panel->selected > middle_pos)
2241 goto_middle_file (panel);
2242 return;
2244 if (panel->selected != panel->top_file)
2246 goto_top_file (panel);
2247 return;
2251 panel->top_file = 0;
2252 panel->selected = 0;
2254 paint_dir (panel);
2255 select_item (panel);
2258 static void
2259 move_end (WPanel * panel)
2261 if (panel->selected == panel->count - 1)
2262 return;
2263 unselect_item (panel);
2264 if (torben_fj_mode)
2266 int middle_pos = panel->top_file + (ITEMS (panel) / 2);
2268 if (panel->selected < middle_pos)
2270 goto_middle_file (panel);
2271 return;
2273 if (panel->selected != (panel->top_file + ITEMS (panel) - 1))
2275 goto_bottom_file (panel);
2276 return;
2280 panel->selected = panel->count - 1;
2281 paint_dir (panel);
2282 select_item (panel);
2285 /* Recalculate the panels summary information, used e.g. when marked
2286 files might have been removed by an external command */
2287 void
2288 recalculate_panel_summary (WPanel * panel)
2290 int i;
2292 panel->marked = 0;
2293 panel->dirs_marked = 0;
2294 panel->total = 0;
2296 for (i = 0; i < panel->count; i++)
2297 if (panel->dir.list[i].f.marked)
2299 /* do_file_mark will return immediately if newmark == oldmark.
2300 So we have to first unmark it to get panel's summary information
2301 updated. (Norbert) */
2302 panel->dir.list[i].f.marked = 0;
2303 do_file_mark (panel, i, 1);
2307 /* This routine marks a file or a directory */
2308 void
2309 do_file_mark (WPanel * panel, int idx, int mark)
2311 if (panel->dir.list[idx].f.marked == mark)
2312 return;
2314 /* Only '..' can't be marked, '.' isn't visible */
2315 if (!strcmp (panel->dir.list[idx].fname, ".."))
2316 return;
2318 file_mark (panel, idx, mark);
2319 if (panel->dir.list[idx].f.marked)
2321 panel->marked++;
2322 if (S_ISDIR (panel->dir.list[idx].st.st_mode))
2324 if (panel->dir.list[idx].f.dir_size_computed)
2325 panel->total += panel->dir.list[idx].st.st_size;
2326 panel->dirs_marked++;
2328 else
2329 panel->total += panel->dir.list[idx].st.st_size;
2330 set_colors (panel);
2332 else
2334 if (S_ISDIR (panel->dir.list[idx].st.st_mode))
2336 if (panel->dir.list[idx].f.dir_size_computed)
2337 panel->total -= panel->dir.list[idx].st.st_size;
2338 panel->dirs_marked--;
2340 else
2341 panel->total -= panel->dir.list[idx].st.st_size;
2342 panel->marked--;
2346 static void
2347 do_mark_file (WPanel * panel, mark_act_t do_move)
2349 do_file_mark (panel, panel->selected, selection (panel)->f.marked ? 0 : 1);
2350 if ((panels_options.mark_moves_down && do_move == MARK_DOWN) || do_move == MARK_FORCE_DOWN)
2351 move_down (panel);
2352 else if (do_move == MARK_FORCE_UP)
2353 move_up (panel);
2356 static void
2357 mark_file (WPanel * panel)
2359 do_mark_file (panel, MARK_DOWN);
2362 static void
2363 mark_file_up (WPanel * panel)
2365 do_mark_file (panel, MARK_FORCE_UP);
2368 static void
2369 mark_file_down (WPanel * panel)
2371 do_mark_file (panel, MARK_FORCE_DOWN);
2374 /** Incremental search of a file name in the panel.
2375 * @param panel instance of WPanel structure
2376 * @param c_code key code
2378 static void
2379 do_search (WPanel * panel, int c_code)
2381 size_t l;
2382 int i, sel;
2383 gboolean wrapped = FALSE;
2384 char *act;
2385 mc_search_t *search;
2386 char *reg_exp, *esc_str;
2387 gboolean is_found = FALSE;
2389 l = strlen (panel->search_buffer);
2390 if (c_code == KEY_BACKSPACE)
2392 if (l != 0)
2394 act = panel->search_buffer + l;
2395 str_prev_noncomb_char (&act, panel->search_buffer);
2396 act[0] = '\0';
2398 panel->search_chpoint = 0;
2400 else
2402 if (c_code != 0 && (gsize) panel->search_chpoint < sizeof (panel->search_char))
2404 panel->search_char[panel->search_chpoint] = c_code;
2405 panel->search_chpoint++;
2408 if (panel->search_chpoint > 0)
2410 switch (str_is_valid_char (panel->search_char, panel->search_chpoint))
2412 case -2:
2413 return;
2414 case -1:
2415 panel->search_chpoint = 0;
2416 return;
2417 default:
2418 if (l + panel->search_chpoint < sizeof (panel->search_buffer))
2420 memcpy (panel->search_buffer + l, panel->search_char, panel->search_chpoint);
2421 l += panel->search_chpoint;
2422 *(panel->search_buffer + l) = '\0';
2423 panel->search_chpoint = 0;
2429 reg_exp = g_strdup_printf ("%s*", panel->search_buffer);
2430 esc_str = strutils_escape (reg_exp, -1, ",|\\{}[]", TRUE);
2431 search = mc_search_new (esc_str, -1);
2432 search->search_type = MC_SEARCH_T_GLOB;
2433 search->is_entire_line = TRUE;
2434 switch (panels_options.qsearch_mode)
2436 case QSEARCH_CASE_SENSITIVE:
2437 search->is_case_sensitive = TRUE;
2438 break;
2439 case QSEARCH_CASE_INSENSITIVE:
2440 search->is_case_sensitive = FALSE;
2441 break;
2442 default:
2443 search->is_case_sensitive = panel->case_sensitive;
2444 break;
2446 sel = panel->selected;
2447 for (i = panel->selected; !wrapped || i != panel->selected; i++)
2449 if (i >= panel->count)
2451 i = 0;
2452 if (wrapped)
2453 break;
2454 wrapped = TRUE;
2456 if (mc_search_run (search, panel->dir.list[i].fname, 0, panel->dir.list[i].fnamelen, NULL))
2458 sel = i;
2459 is_found = TRUE;
2460 break;
2463 if (is_found)
2465 unselect_item (panel);
2466 panel->selected = sel;
2467 select_item (panel);
2468 paint_panel (panel);
2470 else if (c_code != KEY_BACKSPACE)
2472 act = panel->search_buffer + l;
2473 str_prev_noncomb_char (&act, panel->search_buffer);
2474 act[0] = '\0';
2476 mc_search_free (search);
2477 g_free (reg_exp);
2478 g_free (esc_str);
2481 /** Start new search.
2482 * @param panel instance of WPanel structure
2484 static void
2485 start_search (WPanel * panel)
2487 if (panel->searching)
2489 if (panel->selected + 1 == panel->count)
2490 panel->selected = 0;
2491 else
2492 move_down (panel);
2494 /* in case if there was no search string we need to recall
2495 previous string, with which we ended previous searching */
2496 if (panel->search_buffer[0] == '\0')
2497 g_strlcpy (panel->search_buffer, panel->prev_search_buffer,
2498 sizeof (panel->search_buffer));
2500 do_search (panel, 0);
2502 else
2504 panel->searching = TRUE;
2505 panel->search_buffer[0] = '\0';
2506 panel->search_char[0] = '\0';
2507 panel->search_chpoint = 0;
2508 display_mini_info (panel);
2509 mc_refresh ();
2513 static void
2514 stop_search (WPanel * panel)
2516 panel->searching = FALSE;
2518 /* if user had overrdied search string, we need to store it
2519 to the previous_search_buffer */
2520 if (panel->search_buffer[0] != '\0')
2521 g_strlcpy (panel->prev_search_buffer, panel->search_buffer,
2522 sizeof (panel->prev_search_buffer));
2524 display_mini_info (panel);
2527 /* Return 1 if the Enter key has been processed, 0 otherwise */
2528 static int
2529 do_enter_on_file_entry (file_entry * fe)
2531 char *full_name;
2534 * Directory or link to directory - change directory.
2535 * Try the same for the entries on which mc_lstat() has failed.
2537 if (S_ISDIR (fe->st.st_mode) || link_isdir (fe) || (fe->st.st_mode == 0))
2539 if (!do_cd (fe->fname, cd_exact))
2540 message (D_ERROR, MSG_ERROR, _("Cannot change directory"));
2541 return 1;
2544 /* Try associated command */
2545 if (regex_command (fe->fname, "Open", NULL) != 0)
2546 return 1;
2548 /* Check if the file is executable */
2549 full_name = concat_dir_and_file (current_panel->cwd, fe->fname);
2550 if (!is_exe (fe->st.st_mode) || !if_link_is_exe (full_name, fe))
2552 g_free (full_name);
2553 return 0;
2555 g_free (full_name);
2557 if (confirm_execute)
2559 if (query_dialog
2560 (_("The Midnight Commander"),
2561 _("Do you really want to execute?"), D_NORMAL, 2, _("&Yes"), _("&No")) != 0)
2562 return 1;
2565 if (!vfs_current_is_local ())
2567 char *tmp;
2568 int ret;
2570 tmp = concat_dir_and_file (vfs_get_current_dir (), fe->fname);
2571 ret = mc_setctl (tmp, VFS_SETCTL_RUN, NULL);
2572 g_free (tmp);
2573 /* We took action only if the dialog was shown or the execution
2574 * was successful */
2575 return confirm_execute || (ret == 0);
2579 char *tmp = name_quote (fe->fname, 0);
2580 char *cmd = g_strconcat (".", PATH_SEP_STR, tmp, (char *) NULL);
2581 g_free (tmp);
2582 shell_execute (cmd, 0);
2583 g_free (cmd);
2586 #if HAVE_CHARSET
2587 source_codepage = default_source_codepage;
2588 #endif
2590 return 1;
2593 static int
2594 do_enter (WPanel * panel)
2596 return do_enter_on_file_entry (selection (panel));
2599 static void
2600 chdir_other_panel (WPanel * panel)
2602 char *new_dir;
2603 char *sel_entry = NULL;
2605 if (get_other_type () != view_listing)
2607 set_display_type (get_other_index (), view_listing);
2610 if (!S_ISDIR (panel->dir.list[panel->selected].st.st_mode))
2612 new_dir = concat_dir_and_file (panel->cwd, "..");
2613 sel_entry = strrchr (panel->cwd, PATH_SEP);
2615 else
2616 new_dir = concat_dir_and_file (panel->cwd, panel->dir.list[panel->selected].fname);
2618 change_panel ();
2619 do_cd (new_dir, cd_exact);
2620 if (sel_entry)
2621 try_to_select (current_panel, sel_entry);
2622 change_panel ();
2624 move_down (panel);
2626 g_free (new_dir);
2630 * Make the current directory of the current panel also the current
2631 * directory of the other panel. Put the other panel to the listing
2632 * mode if needed. If the current panel is panelized, the other panel
2633 * doesn't become panelized.
2635 static void
2636 sync_other_panel (WPanel * panel)
2638 if (get_other_type () != view_listing)
2640 set_display_type (get_other_index (), view_listing);
2643 do_panel_cd (other_panel, current_panel->cwd, cd_exact);
2645 /* try to select current filename on the other panel */
2646 if (!panel->is_panelized)
2648 try_to_select (other_panel, selection (panel)->fname);
2652 static void
2653 chdir_to_readlink (WPanel * panel)
2655 char *new_dir;
2657 if (get_other_type () != view_listing)
2658 return;
2660 if (S_ISLNK (panel->dir.list[panel->selected].st.st_mode))
2662 char buffer[MC_MAXPATHLEN], *p;
2663 int i;
2664 struct stat st;
2666 i = readlink (selection (panel)->fname, buffer, MC_MAXPATHLEN - 1);
2667 if (i < 0)
2668 return;
2669 if (mc_stat (selection (panel)->fname, &st) < 0)
2670 return;
2671 buffer[i] = 0;
2672 if (!S_ISDIR (st.st_mode))
2674 p = strrchr (buffer, PATH_SEP);
2675 if (p && !p[1])
2677 *p = 0;
2678 p = strrchr (buffer, PATH_SEP);
2680 if (!p)
2681 return;
2682 p[1] = 0;
2684 if (*buffer == PATH_SEP)
2685 new_dir = g_strdup (buffer);
2686 else
2687 new_dir = concat_dir_and_file (panel->cwd, buffer);
2689 change_panel ();
2690 do_cd (new_dir, cd_exact);
2691 change_panel ();
2693 move_down (panel);
2695 g_free (new_dir);
2699 static gsize
2700 panel_get_format_field_count (WPanel * panel)
2702 format_e *format;
2703 gsize lc_index;
2704 for (lc_index = 0, format = panel->format; format != NULL; format = format->next, lc_index++);
2705 return lc_index;
2709 function return 0 if not found and REAL_INDEX+1 if found
2711 static gsize
2712 panel_get_format_field_index_by_name (WPanel * panel, const char *name)
2714 format_e *format;
2715 gsize lc_index;
2717 for (lc_index = 1, format = panel->format;
2718 !(format == NULL || strcmp (format->title, name) == 0); format = format->next, lc_index++);
2719 if (format == NULL)
2720 lc_index = 0;
2722 return lc_index;
2725 static format_e *
2726 panel_get_format_field_by_index (WPanel * panel, gsize lc_index)
2728 format_e *format;
2729 for (format = panel->format;
2730 !(format == NULL || lc_index == 0); format = format->next, lc_index--);
2731 return format;
2734 static const panel_field_t *
2735 panel_get_sortable_field_by_format (WPanel * panel, gsize lc_index)
2737 const panel_field_t *pfield;
2738 format_e *format;
2740 format = panel_get_format_field_by_index (panel, lc_index);
2741 if (format == NULL)
2742 return NULL;
2743 pfield = panel_get_field_by_title (format->title);
2744 if (pfield == NULL)
2745 return NULL;
2746 if (pfield->sort_routine == NULL)
2747 return NULL;
2748 return pfield;
2751 static void
2752 panel_toggle_sort_order_prev (WPanel * panel)
2754 gsize lc_index, i;
2755 gchar *title;
2757 const panel_field_t *pfield = NULL;
2759 title = panel_get_title_without_hotkey (panel->current_sort_field->title_hotkey);
2760 lc_index = panel_get_format_field_index_by_name (panel, title);
2761 g_free (title);
2763 if (lc_index > 1)
2765 /* search for prev sortable column in panel format */
2766 for (i = lc_index - 1;
2767 i != 0 && (pfield = panel_get_sortable_field_by_format (panel, i - 1)) == NULL; i--);
2770 if (pfield == NULL)
2772 /* Sortable field not found. Try to search in each array */
2773 for (i = panel_get_format_field_count (panel);
2774 i != 0 && (pfield = panel_get_sortable_field_by_format (panel, i - 1)) == NULL; i--);
2776 if (pfield == NULL)
2777 return;
2778 panel->current_sort_field = pfield;
2779 panel_set_sort_order (panel, panel->current_sort_field);
2783 static void
2784 panel_toggle_sort_order_next (WPanel * panel)
2786 gsize lc_index, i;
2787 const panel_field_t *pfield = NULL;
2788 gsize format_field_count = panel_get_format_field_count (panel);
2789 gchar *title;
2791 title = panel_get_title_without_hotkey (panel->current_sort_field->title_hotkey);
2792 lc_index = panel_get_format_field_index_by_name (panel, title);
2793 g_free (title);
2795 if (lc_index != 0 && lc_index != format_field_count)
2797 /* search for prev sortable column in panel format */
2798 for (i = lc_index;
2799 i != format_field_count
2800 && (pfield = panel_get_sortable_field_by_format (panel, i)) == NULL; i++);
2803 if (pfield == NULL)
2805 /* Sortable field not found. Try to search in each array */
2806 for (i = 0;
2807 i != format_field_count
2808 && (pfield = panel_get_sortable_field_by_format (panel, i)) == NULL; i++);
2810 if (pfield == NULL)
2811 return;
2812 panel->current_sort_field = pfield;
2813 panel_set_sort_order (panel, panel->current_sort_field);
2816 static void
2817 panel_select_sort_order (WPanel * panel)
2819 const panel_field_t *sort_order;
2820 sort_order = sort_box (panel->current_sort_field, &panel->reverse,
2821 &panel->case_sensitive, &panel->exec_first);
2822 if (sort_order == NULL)
2823 return;
2824 panel->current_sort_field = sort_order;
2825 panel_set_sort_order (panel, panel->current_sort_field);
2829 static void
2830 panel_set_sort_type_by_id (WPanel * panel, const char *name)
2832 const panel_field_t *sort_order;
2834 if (strcmp (panel->current_sort_field->id, name) != 0)
2836 sort_order = panel_get_field_by_id (name);
2837 if (sort_order == NULL)
2838 return;
2839 panel->current_sort_field = sort_order;
2841 else
2843 panel->reverse = !panel->reverse;
2845 panel_set_sort_order (panel, panel->current_sort_field);
2849 * If we moved to the parent directory move the selection pointer to
2850 * the old directory name; If we leave VFS dir, remove FS specificator.
2852 * You do _NOT_ want to add any vfs aware code here. <pavel@ucw.cz>
2854 static const char *
2855 get_parent_dir_name (const char *cwd, const char *lwd)
2857 size_t llen, clen;
2859 llen = strlen (lwd);
2860 clen = strlen (cwd);
2862 if (llen > clen)
2864 const char *p;
2866 p = strrchr (lwd, PATH_SEP);
2868 if ((p != NULL)
2869 && (strncmp (cwd, lwd, (size_t) (p - lwd)) == 0)
2870 && (clen == (size_t) (p - lwd)
2871 || ((p == lwd) && (cwd[0] == PATH_SEP) && (cwd[1] == '\0'))))
2872 return (p + 1);
2875 return NULL;
2879 * Changes the current directory of the panel.
2880 * Don't record change in the directory history.
2882 static gboolean
2883 _do_panel_cd (WPanel * panel, const char *new_dir, enum cd_enum cd_type)
2885 const char *directory;
2886 char *olddir;
2887 char temp[MC_MAXPATHLEN];
2888 char *translated_url;
2890 if (cd_type == cd_parse_command)
2892 while (*new_dir == ' ')
2893 new_dir++;
2896 olddir = g_strdup (panel->cwd);
2897 new_dir = translated_url = vfs_translate_url (new_dir);
2899 /* Convert *new_path to a suitable pathname, handle ~user */
2901 if (cd_type == cd_parse_command)
2903 if (!strcmp (new_dir, "-"))
2905 strcpy (temp, panel->lwd);
2906 new_dir = temp;
2909 directory = *new_dir ? new_dir : home_dir;
2911 if (mc_chdir (directory) == -1)
2913 strcpy (panel->cwd, olddir);
2914 g_free (olddir);
2915 g_free (translated_url);
2916 return FALSE;
2918 g_free (translated_url);
2920 /* Success: save previous directory, shutdown status of previous dir */
2921 strcpy (panel->lwd, olddir);
2922 free_completions (cmdline);
2924 mc_get_current_wd (panel->cwd, sizeof (panel->cwd) - 2);
2926 vfs_release_path (olddir);
2928 subshell_chdir (panel->cwd);
2930 /* Reload current panel */
2931 panel_clean_dir (panel);
2932 panel->count =
2933 do_load_dir (panel->cwd, &panel->dir, panel->current_sort_field->sort_routine,
2934 panel->reverse, panel->case_sensitive, panel->exec_first, panel->filter);
2935 try_to_select (panel, get_parent_dir_name (panel->cwd, olddir));
2936 load_hint (0);
2937 panel->dirty = 1;
2938 update_xterm_title_path ();
2940 g_free (olddir);
2942 return TRUE;
2946 * Changes the current directory of the panel.
2947 * Record change in the directory history.
2949 gboolean
2950 do_panel_cd (struct WPanel *panel, const char *new_dir, enum cd_enum cd_type)
2952 gboolean r;
2954 r = _do_panel_cd (panel, new_dir, cd_type);
2955 if (r)
2956 directory_history_add (panel, panel->cwd);
2957 return r;
2960 static void
2961 directory_history_next (WPanel * panel)
2963 GList *nextdir;
2965 nextdir = g_list_next (panel->dir_history);
2967 if ((nextdir != NULL) && (_do_panel_cd (panel, (char *) nextdir->data, cd_exact)))
2968 panel->dir_history = nextdir;
2971 static void
2972 directory_history_prev (WPanel * panel)
2974 GList *prevdir;
2976 prevdir = g_list_previous (panel->dir_history);
2978 if ((prevdir != NULL) && (_do_panel_cd (panel, (char *) prevdir->data, cd_exact)))
2979 panel->dir_history = prevdir;
2982 static void
2983 directory_history_list (WPanel * panel)
2985 char *s;
2987 s = show_hist (&panel->dir_history, &panel->widget);
2989 if (s != NULL)
2991 if (_do_panel_cd (panel, s, cd_exact))
2992 directory_history_add (panel, panel->cwd);
2993 else
2994 message (D_ERROR, MSG_ERROR, _("Cannot change directory"));
2995 g_free (s);
2999 static cb_ret_t
3000 panel_execute_cmd (WPanel * panel, unsigned long command)
3002 int res = MSG_HANDLED;
3004 if (command != CK_PanelStartSearch)
3005 stop_search (panel);
3007 switch (command)
3009 case CK_PanelChdirOtherPanel:
3010 chdir_other_panel (panel);
3011 break;
3012 case CK_PanelChdirToReadlink:
3013 chdir_to_readlink (panel);
3014 break;
3015 case CK_PanelCmdCopyLocal:
3016 copy_cmd_local ();
3017 break;
3018 case CK_PanelCmdDeleteLocal:
3019 delete_cmd_local ();
3020 break;
3021 case CK_PanelCmdDoEnter:
3022 do_enter (panel);
3023 break;
3024 case CK_PanelCmdViewSimple:
3025 view_simple_cmd ();
3026 break;
3027 case CK_PanelCmdEditNew:
3028 edit_cmd_new ();
3029 break;
3030 case CK_PanelCmdRenameLocal:
3031 rename_cmd_local ();
3032 break;
3033 case CK_PanelCmdReverseSelection:
3034 reverse_selection_cmd ();
3035 break;
3036 case CK_PanelCmdSelect:
3037 select_cmd ();
3038 break;
3039 case CK_PanelCmdUnselect:
3040 unselect_cmd ();
3041 break;
3042 case CK_PanelNextPage:
3043 next_page (panel);
3044 break;
3045 case CK_PanelPrevPage:
3046 prev_page (panel);
3047 break;
3048 case CK_PanelCtrlNextPage:
3049 ctrl_next_page (panel);
3050 break;
3051 case CK_PanelCtrlPrevPage:
3052 ctrl_prev_page (panel);
3053 break;
3054 case CK_PanelDirectoryHistoryList:
3055 directory_history_list (panel);
3056 break;
3057 case CK_PanelDirectoryHistoryNext:
3058 directory_history_next (panel);
3059 break;
3060 case CK_PanelDirectoryHistoryPrev:
3061 directory_history_prev (panel);
3062 break;
3063 case CK_PanelGotoBottomFile:
3064 goto_bottom_file (panel);
3065 break;
3066 case CK_PanelGotoMiddleFile:
3067 goto_middle_file (panel);
3068 break;
3069 case CK_PanelGotoTopFile:
3070 goto_top_file (panel);
3071 break;
3072 case CK_PanelMarkFile:
3073 mark_file (panel);
3074 break;
3075 case CK_PanelMarkFileUp:
3076 mark_file_up (panel);
3077 break;
3078 case CK_PanelMarkFileDown:
3079 mark_file_down (panel);
3080 break;
3081 case CK_PanelMoveUp:
3082 move_up (panel);
3083 break;
3084 case CK_PanelMoveDown:
3085 move_down (panel);
3086 break;
3087 case CK_PanelMoveLeft:
3088 res = move_left (panel);
3089 break;
3090 case CK_PanelMoveRight:
3091 res = move_right (panel);
3092 break;
3093 case CK_PanelMoveEnd:
3094 move_end (panel);
3095 break;
3096 case CK_PanelMoveHome:
3097 move_home (panel);
3098 break;
3099 case CK_PanelSetPanelEncoding:
3100 panel_change_encoding (panel);
3101 break;
3102 case CK_PanelStartSearch:
3103 start_search (panel);
3104 break;
3105 case CK_PanelStopSearch:
3106 break;
3107 case CK_PanelSyncOtherPanel:
3108 sync_other_panel (panel);
3109 break;
3110 case CK_PanelSelectSortOrder:
3111 panel_select_sort_order (panel);
3112 break;
3113 case CK_PanelToggleSortOrderPrev:
3114 panel_toggle_sort_order_prev (panel);
3115 break;
3116 case CK_PanelToggleSortOrderNext:
3117 panel_toggle_sort_order_next (panel);
3118 break;
3119 case CK_PanelReverseSort:
3120 panel->reverse = !panel->reverse;
3121 panel_set_sort_order (panel, panel->current_sort_field);
3122 break;
3123 case CK_PanelSortOrderByName:
3124 panel_set_sort_type_by_id (panel, "name");
3125 break;
3126 case CK_PanelSortOrderByExt:
3127 panel_set_sort_type_by_id (panel, "extension");
3128 break;
3129 case CK_PanelSortOrderBySize:
3130 panel_set_sort_type_by_id (panel, "size");
3131 break;
3132 case CK_PanelSortOrderByMTime:
3133 panel_set_sort_type_by_id (panel, "mtime");
3134 break;
3136 return res;
3139 static cb_ret_t
3140 panel_key (WPanel * panel, int key)
3142 size_t i;
3144 for (i = 0; panel_map[i].key != 0; i++)
3145 if (key == panel_map[i].key)
3146 return panel_execute_cmd (panel, panel_map[i].command);
3148 if (torben_fj_mode && key == ALT ('h'))
3150 goto_middle_file (panel);
3151 return MSG_HANDLED;
3154 if (is_abort_char (key))
3156 stop_search (panel);
3157 return MSG_HANDLED;
3160 /* Do not eat characters not meant for the panel below ' ' (e.g. C-l). */
3161 if ((key >= ' ' && key <= 255) || key == KEY_BACKSPACE)
3163 if (panel->searching)
3165 do_search (panel, key);
3166 return MSG_HANDLED;
3169 if (!command_prompt)
3171 start_search (panel);
3172 do_search (panel, key);
3173 return MSG_HANDLED;
3177 return MSG_NOT_HANDLED;
3180 static cb_ret_t
3181 panel_callback (Widget * w, widget_msg_t msg, int parm)
3183 WPanel *panel = (WPanel *) w;
3184 WButtonBar *bb;
3186 switch (msg)
3188 case WIDGET_DRAW:
3189 paint_panel (panel);
3190 return MSG_HANDLED;
3192 case WIDGET_FOCUS:
3193 current_panel = panel;
3194 panel->active = 1;
3195 if (mc_chdir (panel->cwd) != 0)
3197 char *cwd = strip_password (g_strdup (panel->cwd), 1);
3198 message (D_ERROR, MSG_ERROR, _("Cannot chdir to \"%s\"\n%s"),
3199 cwd, unix_error_string (errno));
3200 g_free (cwd);
3202 else
3203 subshell_chdir (panel->cwd);
3205 update_xterm_title_path ();
3206 select_item (panel);
3207 show_dir (panel);
3208 paint_dir (panel);
3209 panel->dirty = 0;
3211 bb = find_buttonbar (panel->widget.owner);
3212 midnight_set_buttonbar (bb);
3213 buttonbar_redraw (bb);
3214 return MSG_HANDLED;
3216 case WIDGET_UNFOCUS:
3217 /* Janne: look at this for the multiple panel options */
3218 stop_search (panel);
3219 panel->active = 0;
3220 show_dir (panel);
3221 unselect_item (panel);
3222 return MSG_HANDLED;
3224 case WIDGET_KEY:
3225 return panel_key (panel, parm);
3227 case WIDGET_COMMAND:
3228 return panel_execute_cmd (panel, parm);
3230 case WIDGET_DESTROY:
3231 panel_destroy (panel);
3232 free_my_statfs ();
3233 return MSG_HANDLED;
3235 default:
3236 return default_proc (msg, parm);
3240 void
3241 file_mark (WPanel * panel, int lc_index, int val)
3243 if (panel->dir.list[lc_index].f.marked != val)
3245 panel->dir.list[lc_index].f.marked = val;
3246 panel->dirty = 1;
3250 /* */
3251 /* Panel mouse events support routines */
3252 /* */
3253 static int mouse_marking = 0;
3255 static void
3256 mouse_toggle_mark (WPanel * panel)
3258 do_mark_file (panel, MARK_DONT_MOVE);
3259 mouse_marking = selection (panel)->f.marked;
3262 static void
3263 mouse_set_mark (WPanel * panel)
3265 if (mouse_marking && !(selection (panel)->f.marked))
3266 do_mark_file (panel, MARK_DONT_MOVE);
3267 else if (!mouse_marking && (selection (panel)->f.marked))
3268 do_mark_file (panel, MARK_DONT_MOVE);
3271 static int
3272 mark_if_marking (WPanel * panel, Gpm_Event * event)
3274 if (event->buttons & GPM_B_RIGHT)
3276 if (event->type & GPM_DOWN)
3277 mouse_toggle_mark (panel);
3278 else
3279 mouse_set_mark (panel);
3280 return 1;
3282 return 0;
3285 /* Determine which column was clicked, and sort the panel on
3286 * that column, or reverse sort on that column if already
3287 * sorted on that column.
3289 static void
3290 mouse_sort_col (Gpm_Event * event, WPanel * panel)
3292 int i;
3293 const char *lc_sort_name = NULL;
3294 panel_field_t *col_sort_format = NULL;
3295 format_e *format;
3296 gchar *title;
3298 for (i = 0, format = panel->format; format != NULL; format = format->next)
3300 i += format->field_len;
3301 if (event->x < i + 1)
3303 /* found column */
3304 lc_sort_name = format->title;
3305 break;
3309 if (lc_sort_name == NULL)
3310 return;
3312 for (i = 0; panel_fields[i].id != NULL; i++)
3314 title = panel_get_title_without_hotkey (panel_fields[i].title_hotkey);
3315 if (!strcmp (lc_sort_name, title) && panel_fields[i].sort_routine)
3317 col_sort_format = &panel_fields[i];
3318 g_free (title);
3319 break;
3321 g_free (title);
3324 if (!col_sort_format)
3325 return;
3327 if (panel->current_sort_field == col_sort_format)
3329 /* reverse the sort if clicked column is already the sorted column */
3330 panel->reverse = !panel->reverse;
3332 else
3334 /* new sort is forced to be ascending */
3335 panel->reverse = 0;
3337 panel_set_sort_order (panel, col_sort_format);
3342 * Mouse callback of the panel minus repainting.
3343 * If the event is redirected to the menu, *redir is set to TRUE.
3345 static int
3346 do_panel_event (Gpm_Event * event, WPanel * panel, gboolean * redir)
3348 const int lines = llines (panel);
3349 const gboolean is_active = dlg_widget_active (panel);
3350 const gboolean mouse_down = (event->type & GPM_DOWN) != 0;
3352 *redir = FALSE;
3354 /* 1st line */
3355 if (mouse_down && event->y == 1)
3357 /* "<" button */
3358 if (event->x == 2)
3360 directory_history_prev (panel);
3361 return MOU_NORMAL;
3364 /* "." button show/hide hidden files */
3365 if (event->x == panel->widget.cols - 5)
3367 toggle_show_hidden ();
3368 repaint_screen ();
3369 return MOU_NORMAL;
3372 /* ">" button */
3373 if (event->x == panel->widget.cols - 1)
3375 directory_history_next (panel);
3376 return MOU_NORMAL;
3379 /* "^" button */
3380 if (event->x >= panel->widget.cols - 4 && event->x <= panel->widget.cols - 2)
3382 directory_history_list (panel);
3383 return MOU_NORMAL;
3386 /* rest of the upper frame, the menu is invisible - call menu */
3387 if (!menubar_visible)
3389 *redir = TRUE;
3390 event->x += panel->widget.x;
3391 return the_menubar->widget.mouse (event, the_menubar);
3394 /* no other events on 1st line */
3395 return MOU_NORMAL;
3398 /* sort on clicked column; don't handle wheel events */
3399 if (mouse_down && (event->buttons & (GPM_B_UP | GPM_B_DOWN)) == 0 && event->y == 2)
3401 mouse_sort_col (event, panel);
3402 return MOU_NORMAL;
3405 /* Mouse wheel events */
3406 if (mouse_down && (event->buttons & GPM_B_UP))
3408 if (is_active)
3410 if (panels_options.mouse_move_pages && (panel->top_file > 0))
3411 prev_page (panel);
3412 else /* We are in first page */
3413 move_up (panel);
3415 return MOU_NORMAL;
3418 if (mouse_down && (event->buttons & GPM_B_DOWN))
3420 if (is_active)
3422 if (panels_options.mouse_move_pages
3423 && (panel->top_file + ITEMS (panel) < panel->count))
3424 next_page (panel);
3425 else /* We are in last page */
3426 move_down (panel);
3428 return MOU_NORMAL;
3431 event->y -= 2;
3432 if ((event->type & (GPM_DOWN | GPM_DRAG)))
3434 int my_index;
3436 if (!is_active)
3437 change_panel ();
3439 if (panel->top_file + event->y > panel->count)
3440 my_index = panel->count - 1;
3441 else
3443 my_index = panel->top_file + event->y - 1;
3444 if (panel->split && (event->x > ((panel->widget.cols - 2) / 2)))
3445 my_index += llines (panel);
3447 if (my_index >= panel->count)
3448 my_index = panel->count - 1;
3451 if (my_index != panel->selected)
3453 unselect_item (panel);
3454 panel->selected = my_index;
3455 select_item (panel);
3458 /* This one is new */
3459 mark_if_marking (panel, event);
3461 else if ((event->type & (GPM_UP | GPM_DOUBLE)) == (GPM_UP | GPM_DOUBLE))
3463 if (event->y > 0 && event->y <= lines)
3464 do_enter (panel);
3466 return MOU_NORMAL;
3469 /* Mouse callback of the panel */
3470 static int
3471 panel_event (Gpm_Event * event, void *data)
3473 WPanel *panel = data;
3474 int ret;
3475 gboolean redir;
3477 ret = do_panel_event (event, panel, &redir);
3478 if (!redir)
3479 paint_panel (panel);
3481 return ret;
3484 void
3485 panel_re_sort (WPanel * panel)
3487 char *filename;
3488 int i;
3490 if (panel == NULL)
3491 return;
3493 filename = g_strdup (selection (panel)->fname);
3494 unselect_item (panel);
3495 do_sort (&panel->dir, panel->current_sort_field->sort_routine, panel->count - 1, panel->reverse,
3496 panel->case_sensitive, panel->exec_first);
3497 panel->selected = -1;
3498 for (i = panel->count; i; i--)
3500 if (!strcmp (panel->dir.list[i - 1].fname, filename))
3502 panel->selected = i - 1;
3503 break;
3506 g_free (filename);
3507 panel->top_file = panel->selected - ITEMS (panel) / 2;
3508 select_item (panel);
3509 panel->dirty = 1;
3512 void
3513 panel_set_sort_order (WPanel * panel, const panel_field_t * sort_order)
3515 if (sort_order == 0)
3516 return;
3518 panel->current_sort_field = sort_order;
3520 /* The directory is already sorted, we have to load the unsorted stuff */
3521 if (sort_order->sort_routine == (sortfn *) unsorted)
3523 char *current_file;
3525 current_file = g_strdup (panel->dir.list[panel->selected].fname);
3526 panel_reload (panel);
3527 try_to_select (panel, current_file);
3528 g_free (current_file);
3530 panel_re_sort (panel);
3534 * Change panel encoding.
3535 * @param panel WPanel object
3537 void
3538 panel_change_encoding (WPanel * panel)
3540 const char *encoding = NULL;
3541 char *cd_path;
3542 #ifdef HAVE_CHARSET
3543 char *errmsg;
3544 int r;
3546 r = select_charset (-1, -1, panel->codepage, FALSE);
3548 if (r == SELECT_CHARSET_CANCEL)
3549 return; /* Cancel */
3551 panel->codepage = r;
3553 if (panel->codepage == SELECT_CHARSET_NO_TRANSLATE)
3555 /* No translation */
3556 g_free (init_translation_table (display_codepage, display_codepage));
3557 cd_path = remove_encoding_from_path (panel->cwd);
3558 do_panel_cd (panel, cd_path, cd_parse_command);
3559 g_free (cd_path);
3560 return;
3563 errmsg = init_translation_table (panel->codepage, display_codepage);
3564 if (errmsg != NULL)
3566 message (D_ERROR, MSG_ERROR, "%s", errmsg);
3567 g_free (errmsg);
3568 return;
3571 encoding = get_codepage_id (panel->codepage);
3572 #endif
3573 if (encoding != NULL)
3575 const char *enc;
3577 enc = vfs_get_encoding (panel->cwd);
3579 /* don't add current encoding */
3580 if ((enc == NULL) || (strcmp (encoding, enc) != 0))
3582 cd_path = add_encoding_to_path (panel->cwd, encoding);
3583 if (!do_panel_cd (panel, cd_path, cd_parse_command))
3584 message (D_ERROR, MSG_ERROR, _("Cannot chdir to \"%s\""), cd_path);
3585 g_free (cd_path);
3590 static void
3591 reload_panelized (WPanel * panel)
3593 int i, j;
3594 dir_list *list = &panel->dir;
3596 if (panel != current_panel)
3598 int ret;
3599 ret = mc_chdir (panel->cwd);
3602 for (i = 0, j = 0; i < panel->count; i++)
3604 if (list->list[i].f.marked)
3606 /* Unmark the file in advance. In case the following mc_lstat
3607 * fails we are done, else we have to mark the file again
3608 * (Note: do_file_mark depends on a valid "list->list [i].buf").
3609 * IMO that's the best way to update the panel's summary status
3610 * -- Norbert
3612 do_file_mark (panel, i, 0);
3614 if (mc_lstat (list->list[i].fname, &list->list[i].st))
3616 g_free (list->list[i].fname);
3617 continue;
3619 if (list->list[i].f.marked)
3620 do_file_mark (panel, i, 1);
3621 if (j != i)
3622 list->list[j] = list->list[i];
3623 j++;
3625 if (j == 0)
3626 panel->count = set_zero_dir (list) ? 1 : 0;
3627 else
3628 panel->count = j;
3630 if (panel != current_panel)
3632 int ret;
3633 ret = mc_chdir (current_panel->cwd);
3637 static void
3638 update_one_panel_widget (WPanel * panel, int force_update, const char *current_file)
3640 int free_pointer;
3641 char *my_current_file = NULL;
3643 if (force_update & UP_RELOAD)
3645 panel->is_panelized = 0;
3646 mc_setctl (panel->cwd, VFS_SETCTL_FLUSH, 0);
3647 memset (&(panel->dir_stat), 0, sizeof (panel->dir_stat));
3650 /* If current_file == -1 (an invalid pointer) then preserve selection */
3651 if (current_file == UP_KEEPSEL)
3653 free_pointer = 1;
3654 my_current_file = g_strdup (panel->dir.list[panel->selected].fname);
3655 current_file = my_current_file;
3657 else
3658 free_pointer = 0;
3660 if (panel->is_panelized)
3661 reload_panelized (panel);
3662 else
3663 panel_reload (panel);
3665 try_to_select (panel, current_file);
3666 panel->dirty = 1;
3668 if (free_pointer)
3669 g_free (my_current_file);
3672 static void
3673 update_one_panel (int which, int force_update, const char *current_file)
3675 if (get_display_type (which) == view_listing)
3677 WPanel *panel;
3678 panel = (WPanel *) get_panel_widget (which);
3679 update_one_panel_widget (panel, force_update, current_file);
3683 /* This routine reloads the directory in both panels. It tries to
3684 * select current_file in current_panel and other_file in other_panel.
3685 * If current_file == -1 then it automatically sets current_file and
3686 * other_file to the currently selected files in the panels.
3688 * if force_update has the UP_ONLY_CURRENT bit toggled on, then it
3689 * will not reload the other panel.
3691 void
3692 update_panels (int force_update, const char *current_file)
3694 int reload_other = !(force_update & UP_ONLY_CURRENT);
3695 WPanel *panel;
3696 int ret;
3698 update_one_panel (get_current_index (), force_update, current_file);
3699 if (reload_other)
3700 update_one_panel (get_other_index (), force_update, UP_KEEPSEL);
3702 if (get_current_type () == view_listing)
3703 panel = (WPanel *) get_panel_widget (get_current_index ());
3704 else
3705 panel = (WPanel *) get_panel_widget (get_other_index ());
3707 ret = mc_chdir (panel->cwd);
3710 void
3711 directory_history_add (struct WPanel *panel, const char *dir)
3713 char *tmp;
3715 tmp = g_strdup (dir);
3716 strip_password (tmp, 1);
3718 panel->dir_history = list_append_unique (panel->dir_history, tmp);
3721 gsize
3722 panel_get_num_of_sortable_fields (void)
3724 gsize ret = 0, lc_index;
3726 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
3727 if (panel_fields[lc_index].is_user_choice)
3728 ret++;
3729 return ret;
3732 const char **
3733 panel_get_sortable_fields (gsize * array_size)
3735 char **ret;
3736 gsize lc_index, i;
3738 lc_index = panel_get_num_of_sortable_fields ();
3740 ret = g_try_new0 (char *, lc_index + 1);
3741 if (ret == NULL)
3742 return NULL;
3744 if (array_size != NULL)
3745 *array_size = lc_index;
3747 lc_index = 0;
3749 for (i = 0; panel_fields[i].id != NULL; i++)
3750 if (panel_fields[i].is_user_choice)
3751 ret[lc_index++] = g_strdup (_(panel_fields[i].title_hotkey));
3752 return (const char **) ret;
3755 const panel_field_t *
3756 panel_get_field_by_id (const char *name)
3758 gsize lc_index;
3759 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
3760 if (panel_fields[lc_index].id != NULL && strcmp (name, panel_fields[lc_index].id) == 0)
3761 return &panel_fields[lc_index];
3762 return NULL;
3765 const panel_field_t *
3766 panel_get_field_by_title_hotkey (const char *name)
3768 gsize lc_index;
3769 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
3770 if (panel_fields[lc_index].title_hotkey != NULL &&
3771 strcmp (name, _(panel_fields[lc_index].title_hotkey)) == 0)
3772 return &panel_fields[lc_index];
3773 return NULL;
3776 const panel_field_t *
3777 panel_get_field_by_title (const char *name)
3779 gsize lc_index;
3780 gchar *title = NULL;
3782 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
3784 title = panel_get_title_without_hotkey (panel_fields[lc_index].title_hotkey);
3785 if (panel_fields[lc_index].title_hotkey != NULL && strcmp (name, title) == 0)
3787 g_free (title);
3788 return &panel_fields[lc_index];
3791 g_free (title);
3792 return NULL;
3795 gsize
3796 panel_get_num_of_user_possible_fields (void)
3798 gsize ret = 0, lc_index;
3800 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
3801 if (panel_fields[lc_index].use_in_user_format)
3802 ret++;
3803 return ret;
3806 const char **
3807 panel_get_user_possible_fields (gsize * array_size)
3809 char **ret;
3810 gsize lc_index, i;
3812 lc_index = panel_get_num_of_user_possible_fields ();
3814 ret = g_try_new0 (char *, lc_index + 1);
3815 if (ret == NULL)
3816 return NULL;
3818 if (array_size != NULL)
3819 *array_size = lc_index;
3821 lc_index = 0;
3823 for (i = 0; panel_fields[i].id != NULL; i++)
3824 if (panel_fields[i].use_in_user_format)
3825 ret[lc_index++] = g_strdup (_(panel_fields[i].title_hotkey));
3826 return (const char **) ret;
3829 void
3830 panel_init (void)
3832 panel_sort_up_sign = mc_skin_get ("widget-common", "sort-sign-up", "'");
3833 panel_sort_down_sign = mc_skin_get ("widget-common", "sort-sign-down", ",");
3835 panel_hiddenfiles_sign_show = mc_skin_get ("widget-panel", "hiddenfiles-sign-show", ".");
3836 panel_hiddenfiles_sign_hide = mc_skin_get ("widget-panel", "hiddenfiles-sign-hide", ".");
3837 panel_history_prev_item_sign = mc_skin_get ("widget-panel", "history-prev-item-sign", "<");
3838 panel_history_next_item_sign = mc_skin_get ("widget-panel", "history-next-item-sign", ">");
3839 panel_history_show_list_sign = mc_skin_get ("widget-panel", "history-show-list-sign", "^");
3843 void
3844 panel_deinit (void)
3846 g_free (panel_sort_up_sign);
3847 g_free (panel_sort_down_sign);
3849 g_free (panel_hiddenfiles_sign_show);
3850 g_free (panel_hiddenfiles_sign_hide);
3851 g_free (panel_history_prev_item_sign);
3852 g_free (panel_history_next_item_sign);
3853 g_free (panel_history_show_list_sign);