Ticket #397: Fix initialization of structure.
[midnight-commander.git] / src / screen.c
blobe807780781012abe1c29febeb621a96c20751ba3
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 "global.h"
36 #include "../src/tty/tty.h"
37 #include "../src/skin/skin.h"
38 #include "../src/tty/mouse.h" /* For Gpm_Event */
39 #include "../src/tty/key.h" /* XCTRL and ALT macros */
40 #include "../src/filehighlight/fhl.h"
42 #include "dir.h"
43 #include "panel.h"
44 #include "boxes.h"
45 #include "tree.h"
46 #include "ext.h" /* regexp_command */
47 #include "layout.h" /* Most layout variables are here */
48 #include "wtools.h" /* for message (...) */
49 #include "cmd.h"
50 #include "command.h" /* cmdline */
51 #include "setup.h" /* For loading/saving panel options */
52 #include "user.h"
53 #include "../src/mcconfig/mcconfig.h"
54 #include "execute.h"
55 #include "widget.h"
56 #include "menu.h" /* menubar_visible */
57 #include "main-widgets.h"
58 #include "main.h"
59 #include "unixcompat.h"
60 #include "mountlist.h" /* my_statfs */
61 #include "selcodepage.h" /* select_charset () */
62 #include "charsets.h" /* get_codepage_id () */
63 #include "cmddef.h" /* CK_ cmd name const */
64 #include "keybind.h" /* global_key_map_t */
66 #define ELEMENTS(arr) ( sizeof(arr) / sizeof((arr)[0]) )
68 #define NORMAL 0
69 #define SELECTED 1
70 #define MARKED 2
71 #define MARKED_SELECTED 3
72 #define STATUS 5
75 * This describes a format item. The parse_display_format routine parses
76 * the user specified format and creates a linked list of format_e structures.
78 typedef struct format_e {
79 struct format_e *next;
80 int requested_field_len;
81 int field_len;
82 align_crt_t just_mode;
83 int expand;
84 const char *(*string_fn)(file_entry *, int len);
85 const char *title;
86 const char *id;
87 } format_e;
89 /* If true, show the mini-info on the panel */
90 int show_mini_info = 1;
92 /* If true, then use stat() on the cwd to determine directory changes */
93 int fast_reload = 0;
95 /* If true, use some usability hacks by Torben */
96 int torben_fj_mode = 0;
98 /* If true, up/down keys scroll the pane listing by pages */
99 int panel_scroll_pages = 1;
101 /* If 1, we use permission hilighting */
102 int permission_mode = 0;
104 /* If 1 - then add per file type hilighting */
105 int filetype_mode = 1;
107 /* The hook list for the select file function */
108 Hook *select_file_hook = 0;
110 const global_key_map_t *panel_map;
112 static cb_ret_t panel_callback (Widget *, widget_msg_t msg, int parm);
113 static int panel_event (Gpm_Event *event, void *);
114 static void paint_frame (WPanel *panel);
115 static const char *panel_format (WPanel *panel);
116 static const char *mini_status_format (WPanel *panel);
118 static char *panel_sort_up_sign = NULL;
119 static char *panel_sort_down_sign = NULL;
121 /* This macro extracts the number of available lines in a panel */
122 #define llines(p) (p->widget.lines-3 - (show_mini_info ? 2 : 0))
124 static void
125 set_colors (WPanel *panel)
127 (void) panel;
128 tty_set_normal_attrs ();
129 tty_setcolor (NORMAL_COLOR);
132 /* Delete format string, it is a linked list */
133 static void
134 delete_format (format_e *format)
136 format_e *next;
138 while (format){
139 next = format->next;
140 g_free (format);
141 format = next;
145 /* This code relies on the default justification!!! */
146 static void
147 add_permission_string (char *dest, int width, file_entry *fe, int attr, int color, int is_octal)
149 int i, r, l;
151 l = get_user_permissions (&fe->st);
153 if (is_octal){
154 /* Place of the access bit in octal mode */
155 l = width + l - 3;
156 r = l + 1;
157 } else {
158 /* The same to the triplet in string mode */
159 l = l * 3 + 1;
160 r = l + 3;
163 for(i = 0; i < width; i++){
164 if (i >= l && i < r){
165 if (attr == SELECTED || attr == MARKED_SELECTED)
166 tty_setcolor (MARKED_SELECTED_COLOR);
167 else
168 tty_setcolor (MARKED_COLOR);
169 } else if (color >= 0)
170 tty_setcolor (color);
171 else
172 tty_lowlevel_setcolor (-color);
174 tty_print_char (dest[i]);
178 /* String representations of various file attributes */
179 /* name */
180 static const char *
181 string_file_name (file_entry *fe, int len)
183 static char buffer [MC_MAXPATHLEN * MB_LEN_MAX + 1];
185 (void) len;
186 g_strlcpy (buffer, fe->fname, sizeof(buffer));
187 return buffer;
190 static unsigned int ilog10(dev_t n)
192 unsigned int digits = 0;
193 do {
194 digits++, n /= 10;
195 } while (n != 0);
196 return digits;
199 static void format_device_number (char *buf, size_t bufsize, dev_t dev)
201 dev_t major_dev = major(dev);
202 dev_t minor_dev = minor(dev);
203 unsigned int major_digits = ilog10(major_dev);
204 unsigned int minor_digits = ilog10(minor_dev);
206 g_assert(bufsize >= 1);
207 if (major_digits + 1 + minor_digits + 1 <= bufsize) {
208 g_snprintf(buf, bufsize, "%lu,%lu", (unsigned long) major_dev,
209 (unsigned long) minor_dev);
210 } else {
211 g_strlcpy(buf, _("[dev]"), bufsize);
215 /* size */
216 static const char *
217 string_file_size (file_entry *fe, int len)
219 static char buffer [BUF_TINY];
221 /* Don't ever show size of ".." since we don't calculate it */
222 if (!strcmp (fe->fname, "..")) {
223 return _("UP--DIR");
226 #ifdef HAVE_STRUCT_STAT_ST_RDEV
227 if (S_ISBLK (fe->st.st_mode) || S_ISCHR (fe->st.st_mode))
228 format_device_number (buffer, len + 1, fe->st.st_rdev);
229 else
230 #endif
232 size_trunc_len (buffer, (unsigned int) len, fe->st.st_size, 0);
234 return buffer;
237 /* bsize */
238 static const char *
239 string_file_size_brief (file_entry *fe, int len)
241 if (S_ISLNK (fe->st.st_mode) && !fe->f.link_to_dir) {
242 return _("SYMLINK");
245 if ((S_ISDIR (fe->st.st_mode) || fe->f.link_to_dir) && strcmp (fe->fname, "..")) {
246 return _("SUB-DIR");
249 return string_file_size (fe, len);
252 /* This functions return a string representation of a file entry */
253 /* type */
254 static const char *
255 string_file_type (file_entry *fe, int len)
257 static char buffer[2];
259 (void) len;
260 if (S_ISDIR (fe->st.st_mode))
261 buffer[0] = PATH_SEP;
262 else if (S_ISLNK (fe->st.st_mode)) {
263 if (fe->f.link_to_dir)
264 buffer[0] = '~';
265 else if (fe->f.stale_link)
266 buffer[0] = '!';
267 else
268 buffer[0] = '@';
269 } else if (S_ISCHR (fe->st.st_mode))
270 buffer[0] = '-';
271 else if (S_ISSOCK (fe->st.st_mode))
272 buffer[0] = '=';
273 else if (S_ISDOOR (fe->st.st_mode))
274 buffer[0] = '>';
275 else if (S_ISBLK (fe->st.st_mode))
276 buffer[0] = '+';
277 else if (S_ISFIFO (fe->st.st_mode))
278 buffer[0] = '|';
279 else if (S_ISNAM (fe->st.st_mode))
280 buffer[0] = '#';
281 else if (!S_ISREG (fe->st.st_mode))
282 buffer[0] = '?'; /* non-regular of unknown kind */
283 else if (is_exe (fe->st.st_mode))
284 buffer[0] = '*';
285 else
286 buffer[0] = ' ';
287 buffer[1] = '\0';
288 return buffer;
291 /* mtime */
292 static const char *
293 string_file_mtime (file_entry *fe, int len)
295 (void) len;
296 if (!strcmp (fe->fname, "..")) {
297 return "";
299 return file_date (fe->st.st_mtime);
302 /* atime */
303 static const char *
304 string_file_atime (file_entry *fe, int len)
306 (void) len;
307 if (!strcmp (fe->fname, "..")) {
308 return "";
310 return file_date (fe->st.st_atime);
313 /* ctime */
314 static const char *
315 string_file_ctime (file_entry *fe, int len)
317 (void) len;
318 if (!strcmp (fe->fname, "..")) {
319 return "";
321 return file_date (fe->st.st_ctime);
324 /* perm */
325 static const char *
326 string_file_permission (file_entry *fe, int len)
328 (void) len;
329 return string_perm (fe->st.st_mode);
332 /* mode */
333 static const char *
334 string_file_perm_octal (file_entry *fe, int len)
336 static char buffer [10];
338 (void) len;
339 g_snprintf (buffer, sizeof (buffer), "0%06lo", (unsigned long) fe->st.st_mode);
340 return buffer;
343 /* nlink */
344 static const char *
345 string_file_nlinks (file_entry *fe, int len)
347 static char buffer[BUF_TINY];
349 (void) len;
350 g_snprintf (buffer, sizeof (buffer), "%16d", (int) fe->st.st_nlink);
351 return buffer;
354 /* inode */
355 static const char *
356 string_inode (file_entry *fe, int len)
358 static char buffer [10];
360 (void) len;
361 g_snprintf (buffer, sizeof (buffer), "%lu",
362 (unsigned long) fe->st.st_ino);
363 return buffer;
366 /* nuid */
367 static const char *
368 string_file_nuid (file_entry *fe, int len)
370 static char buffer [10];
372 (void) len;
373 g_snprintf (buffer, sizeof (buffer), "%lu",
374 (unsigned long) fe->st.st_uid);
375 return buffer;
378 /* ngid */
379 static const char *
380 string_file_ngid (file_entry *fe, int len)
382 static char buffer [10];
384 (void) len;
385 g_snprintf (buffer, sizeof (buffer), "%lu",
386 (unsigned long) fe->st.st_gid);
387 return buffer;
390 /* owner */
391 static const char *
392 string_file_owner (file_entry *fe, int len)
394 (void) len;
395 return get_owner (fe->st.st_uid);
398 /* group */
399 static const char *
400 string_file_group (file_entry *fe, int len)
402 (void) len;
403 return get_group (fe->st.st_gid);
406 /* mark */
407 static const char *
408 string_marked (file_entry *fe, int len)
410 (void) len;
411 return fe->f.marked ? "*" : " ";
414 /* space */
415 static const char *
416 string_space (file_entry *fe, int len)
418 (void) fe;
419 (void) len;
420 return " ";
423 /* dot */
424 static const char *
425 string_dot (file_entry *fe, int len)
427 (void) fe;
428 (void) len;
429 return ".";
432 #define GT 1
434 panel_field_t panel_fields [] = {
436 "unsorted", 12, 1, J_LEFT_FIT,
437 /* TRANSLATORS: one single character to represent 'unsorted' sort mode */
438 N_("u"),
439 N_("Unsorted"), N_("&Unsorted"), FALSE,
440 string_file_name,
441 (sortfn *) unsorted
444 "name", 12, 1, J_LEFT_FIT,
445 /* TRANSLATORS: one single character to represent 'name' sort mode */
446 N_("n"),
447 N_("Name"), N_("&Name"), TRUE,
448 string_file_name,
449 (sortfn *) sort_name
452 "extension", 12, 1, J_LEFT_FIT,
453 /* TRANSLATORS: one single character to represent 'extension' sort mode */
454 N_("e"),
455 N_("Extension"), N_("&Extension"), FALSE,
456 string_file_name, /* TODO: string_file_ext*/
457 (sortfn *) sort_ext
460 "size", 7, 0, J_RIGHT,
461 /* TRANSLATORS: one single character to represent 'size' sort mode */
462 N_("s"),
463 N_("Size"), N_("&Size"), TRUE,
464 string_file_size,
465 (sortfn *) sort_size
468 "bsize", 7, 0, J_RIGHT,
470 N_("Block Size"), NULL, FALSE,
471 string_file_size_brief,
472 (sortfn *) sort_size
475 "type", GT, 0, J_LEFT,
477 "", NULL, TRUE,
478 string_file_type,
479 NULL
482 "mtime", 12, 0, J_RIGHT,
483 /* TRANSLATORS: one single character to represent 'Modify time' sort mode */
484 N_("m"),
485 N_("MTime"), N_("&Modify time"), TRUE,
486 string_file_mtime,
487 (sortfn *) sort_time
490 "atime", 12, 0, J_RIGHT,
491 /* TRANSLATORS: one single character to represent 'Access time' sort mode */
492 N_("a"),
493 N_("ATime"), N_("&Access time"), TRUE,
494 string_file_atime,
495 (sortfn *) sort_atime
498 "ctime", 12, 0, J_RIGHT,
499 /* TRANSLATORS: one single character to represent 'Change time' sort mode */
500 N_("h"),
501 N_("CTime"), N_("C&Hange time"), TRUE,
502 string_file_ctime,
503 (sortfn *) sort_ctime
506 "perm", 10, 0, J_LEFT,
508 N_("Permission"), NULL, TRUE,
509 string_file_permission,
510 NULL
513 "mode", 6, 0, J_RIGHT,
515 N_("Perm"), NULL, TRUE,
516 string_file_perm_octal,
517 NULL
520 "nlink", 2, 0, J_RIGHT,
522 N_("Nl"), NULL, TRUE,
523 string_file_nlinks, NULL
526 "inode", 5, 0, J_RIGHT,
527 /* TRANSLATORS: one single character to represent 'inode' sort mode */
528 N_("i"),
529 N_("Inode"), N_("&Inode"), TRUE,
530 string_inode,
531 (sortfn *) sort_inode
534 "nuid", 5, 0, J_RIGHT,
536 N_("UID"), NULL, FALSE,
537 string_file_nuid,
538 NULL
541 "ngid", 5, 0, J_RIGHT,
543 N_("GID"), NULL, FALSE,
544 string_file_ngid,
545 NULL
548 "owner", 8, 0, J_LEFT_FIT,
550 N_("Owner"), NULL, TRUE,
551 string_file_owner,
552 NULL
555 "group", 8,0, J_LEFT_FIT,
557 N_("Group"), NULL, TRUE,
558 string_file_group,
559 NULL
562 "mark", 1, 0, J_RIGHT,
564 " ", NULL, TRUE,
565 string_marked,
566 NULL
569 "|", 1, 0, J_RIGHT,
571 " ", NULL, TRUE,
572 NULL,
573 NULL
576 "space", 1, 0, J_RIGHT,
578 " ", NULL, TRUE,
579 string_space,
580 NULL
583 "dot", 1, 0, J_RIGHT,
585 " ", NULL, FALSE,
586 string_dot,
587 NULL
590 NULL, 0, 0, J_RIGHT, NULL, NULL, NULL, FALSE, NULL, NULL
594 static int
595 file_compute_color (int attr, file_entry *fe)
597 switch (attr) {
598 case SELECTED:
599 return (SELECTED_COLOR);
600 case MARKED:
601 return (MARKED_COLOR);
602 case MARKED_SELECTED:
603 return (MARKED_SELECTED_COLOR);
604 case STATUS:
605 return (NORMAL_COLOR);
606 case NORMAL:
607 default:
608 if (!filetype_mode)
609 return (NORMAL_COLOR);
612 return mc_fhl_get_color (mc_filehighlight, fe);
615 /* Formats the file number file_index of panel in the buffer dest */
616 static void
617 format_file (char *dest, int limit, WPanel *panel, int file_index, int width, int attr, int isstatus)
619 int color, length, empty_line;
620 const char *txt;
621 format_e *format, *home;
622 file_entry *fe;
624 (void) dest;
625 (void) limit;
626 length = 0;
627 empty_line = (file_index >= panel->count);
628 home = (isstatus) ? panel->status_format : panel->format;
629 fe = &panel->dir.list [file_index];
631 if (!empty_line)
632 color = file_compute_color (attr, fe);
633 else
634 color = NORMAL_COLOR;
636 for (format = home; format; format = format->next){
637 if (length == width)
638 break;
640 if (format->string_fn){
641 int len, perm;
642 char *preperad_text;
644 if (empty_line)
645 txt = " ";
646 else
647 txt = (*format->string_fn)(fe, format->field_len);
649 len = format->field_len;
650 if (len + length > width)
651 len = width - length;
652 if (len <= 0)
653 break;
655 perm = 0;
656 if (permission_mode) {
657 if (!strcmp(format->id, "perm"))
658 perm = 1;
659 else if (!strcmp(format->id, "mode"))
660 perm = 2;
663 if (color >= 0)
664 tty_setcolor (color);
665 else
666 tty_lowlevel_setcolor (-color);
668 preperad_text = (char*) str_fit_to_term(txt, len, format->just_mode);
669 if (perm)
670 add_permission_string (preperad_text, format->field_len, fe,
671 attr, color, perm - 1);
672 else
673 tty_print_string (preperad_text);
675 length+= len;
676 } else {
677 if (attr == SELECTED || attr == MARKED_SELECTED)
678 tty_setcolor (SELECTED_COLOR);
679 else
680 tty_setcolor (NORMAL_COLOR);
681 tty_print_one_vline ();
682 length++;
686 if (length < width)
687 tty_draw_hline (-1, -1, ' ', width - length);
690 static void
691 repaint_file (WPanel *panel, int file_index, int mv, int attr, int isstatus)
693 int second_column = 0;
694 int width;
695 int offset = 0;
696 char buffer [BUF_MEDIUM];
698 gboolean panel_is_split = !isstatus && panel->split;
700 width = panel->widget.cols - 2;
702 if (panel_is_split) {
703 second_column = (file_index - panel->top_file) / llines (panel);
704 width = width/2 - 1;
706 if (second_column != 0) {
707 offset = 1 + width;
708 /*width = (panel->widget.cols-2) - (panel->widget.cols-2)/2 - 1;*/
709 width = panel->widget.cols - offset - 2;
713 /* Nothing to paint */
714 if (width <= 0)
715 return;
717 if (mv){
718 if (panel_is_split)
719 widget_move (&panel->widget,
720 (file_index - panel->top_file) % llines (panel) + 2,
721 offset + 1);
722 else
723 widget_move (&panel->widget, file_index - panel->top_file + 2, 1);
726 format_file (buffer, sizeof(buffer), panel, file_index, width, attr, isstatus);
728 if (panel_is_split) {
729 if (second_column)
730 tty_print_char (' ');
731 else {
732 tty_setcolor (NORMAL_COLOR);
733 tty_print_one_vline ();
738 static void
739 display_mini_info (WPanel *panel)
741 widget_move (&panel->widget, llines (panel)+3, 1);
743 if (panel->searching){
744 tty_setcolor (INPUT_COLOR);
745 tty_print_char ('/');
746 tty_print_string (str_fit_to_term (panel->search_buffer,
747 panel->widget.cols - 3, J_LEFT));
748 return;
751 /* Status resolves links and show them */
752 set_colors (panel);
754 if (S_ISLNK (panel->dir.list [panel->selected].st.st_mode)){
755 char *link, link_target [MC_MAXPATHLEN];
756 int len;
758 link = concat_dir_and_file (panel->cwd, panel->dir.list [panel->selected].fname);
759 len = mc_readlink (link, link_target, MC_MAXPATHLEN - 1);
760 g_free (link);
761 if (len > 0){
762 link_target[len] = 0;
763 tty_print_string ("-> ");
764 tty_print_string (str_fit_to_term (link_target, panel->widget.cols - 5,
765 J_LEFT_FIT));
766 } else
767 tty_print_string (str_fit_to_term (_("<readlink failed>"),
768 panel->widget.cols - 2, J_LEFT));
769 } else if (strcmp (panel->dir.list [panel->selected].fname, "..") == 0) {
770 /* FIXME:
771 * while loading directory (do_load_dir() and do_reload_dir()),
772 * the actual stat info about ".." directory isn't got;
773 * so just don't display incorrect info about ".." directory */
774 tty_print_string (str_fit_to_term (_("UP--DIR"), panel->widget.cols - 2, J_LEFT));
775 } else
776 /* Default behavior */
777 repaint_file (panel, panel->selected, 0, STATUS, 1);
780 static void
781 paint_dir (WPanel *panel)
783 int i;
784 int color; /* Color value of the line */
785 int items; /* Number of items */
787 items = llines (panel) * (panel->split ? 2 : 1);
789 for (i = 0; i < items; i++){
790 if (i+panel->top_file >= panel->count)
791 color = 0;
792 else {
793 color = 2 * (panel->dir.list [i+panel->top_file].f.marked);
794 color += (panel->selected==i+panel->top_file && panel->active);
796 repaint_file (panel, i+panel->top_file, 1, color, 0);
798 tty_set_normal_attrs ();
801 static void
802 display_total_marked_size (WPanel *panel, int y, int x, gboolean size_only)
804 char buffer[BUF_SMALL], b_bytes[BUF_SMALL], *buf;
805 int cols;
807 if (panel->marked <= 0)
808 return;
810 buf = size_only ? b_bytes : buffer;
811 cols = panel->widget.cols - 2;
814 * This is a trick to use two ngettext() calls in one sentence.
815 * First make "N bytes", then insert it into "X in M files".
817 g_snprintf (b_bytes, sizeof (b_bytes),
818 ngettext("%s byte", "%s bytes", (unsigned long) panel->total),
819 size_trunc_sep (panel->total));
820 if (!size_only)
821 g_snprintf (buffer, sizeof (buffer),
822 ngettext("%s in %d file", "%s in %d files", panel->marked),
823 b_bytes, panel->marked);
825 /* don't forget spaces around buffer content */
826 buf = (char *) str_trunc (buf, cols - 4);
828 if (x < 0)
829 /* center in panel */
830 x = (panel->widget.cols - str_term_width1 (buf)) / 2 - 1;
833 * y == llines (panel) + 2 for mini_info_separator
834 * y == panel->widget.lines - 1 for panel bottom frame
836 widget_move (&panel->widget, y, x);
837 tty_setcolor (MARKED_COLOR);
838 tty_printf (" %s ", buf);
841 static void
842 mini_info_separator (WPanel *panel)
844 const int y = llines (panel) + 2;
846 tty_setcolor (NORMAL_COLOR);
847 tty_draw_hline (panel->widget.y + y, panel->widget.x + 1,
848 ACS_HLINE, panel->widget.cols - 2);
849 /* Status displays total marked size.
850 * Centered in panel, full format. */
851 display_total_marked_size (panel, y, -1, FALSE);
854 static void
855 show_free_space (WPanel *panel)
857 /* Used to figure out how many free space we have */
858 static struct my_statfs myfs_stats;
859 /* Old current working directory for displaying free space */
860 static char *old_cwd = NULL;
862 /* Don't try to stat non-local fs */
863 if (!vfs_file_is_local (panel->cwd) || !free_space)
864 return;
866 if (old_cwd == NULL || strcmp (old_cwd, panel->cwd) != 0) {
867 char rpath[PATH_MAX];
869 init_my_statfs ();
870 g_free (old_cwd);
871 old_cwd = g_strdup (panel->cwd);
873 if (mc_realpath (panel->cwd, rpath) == NULL)
874 return;
876 my_statfs (&myfs_stats, rpath);
879 if (myfs_stats.avail > 0 || myfs_stats.total > 0) {
880 char buffer1[6], buffer2[6], tmp[BUF_SMALL];
881 size_trunc_len (buffer1, sizeof(buffer1) - 1, myfs_stats.avail, 1);
882 size_trunc_len (buffer2, sizeof(buffer2) - 1, myfs_stats.total, 1);
883 g_snprintf (tmp, sizeof(tmp), " %s/%s (%d%%) ", buffer1, buffer2,
884 myfs_stats.total > 0 ?
885 (int)(100 * (double)myfs_stats.avail / myfs_stats.total) : 0);
886 widget_move (&panel->widget, panel->widget.lines - 1,
887 panel->widget.cols - 2 - (int) strlen (tmp));
888 tty_setcolor (NORMAL_COLOR);
889 tty_print_string (tmp);
893 static void
894 show_dir (WPanel *panel)
896 set_colors (panel);
897 draw_box (panel->widget.parent,
898 panel->widget.y, panel->widget.x,
899 panel->widget.lines, panel->widget.cols);
901 if (show_mini_info) {
902 widget_move (&panel->widget, llines (panel) + 2, 0);
903 tty_print_alt_char (ACS_LTEE);
904 widget_move (&panel->widget, llines (panel) + 2,
905 panel->widget.cols - 1);
906 tty_print_alt_char (ACS_RTEE);
909 if (panel->active)
910 tty_setcolor (REVERSE_COLOR);
912 widget_move (&panel->widget, 0, 3);
914 tty_printf (" %s ",
915 str_term_trim (strip_home_and_password (panel->cwd),
916 min (max (panel->widget.cols - 10, 0),
917 panel->widget.cols)));
919 widget_move (&panel->widget, 0, 1);
920 tty_print_char ('<');
921 widget_move (&panel->widget, 0, panel->widget.cols - 4);
922 tty_print_string (".v>");
924 if (!show_mini_info) {
925 if (panel->marked == 0) {
926 /* Show size of curret file in the bottom of panel */
927 if (S_ISREG (panel->dir.list [panel->selected].st.st_mode)) {
928 char buffer[BUF_SMALL];
930 g_snprintf (buffer, sizeof (buffer), " %s ",
931 size_trunc_sep (panel->dir.list [panel->selected].st.st_size));
932 tty_setcolor (NORMAL_COLOR);
933 widget_move (&panel->widget, panel->widget.lines - 1, 2);
934 tty_print_string (buffer);
936 } else {
937 /* Show total size of marked files
938 * In the bottom of panel, display size only. */
939 display_total_marked_size (panel, panel->widget.lines - 1, 2, TRUE);
943 show_free_space (panel);
945 if (panel->active)
946 tty_set_normal_attrs ();
949 /* To be used only by long_frame and full_frame to adjust top_file */
950 static void
951 adjust_top_file (WPanel *panel)
953 int old_top = panel->top_file;
955 if (panel->selected - old_top > llines (panel))
956 panel->top_file = panel->selected;
957 if (old_top - panel->count > llines (panel))
958 panel->top_file = panel->count - llines (panel);
961 /* Repaint everything, including frame and separator */
962 static void
963 paint_panel (WPanel *panel)
965 paint_frame (panel); /* including show_dir */
966 paint_dir (panel);
968 if (show_mini_info) {
969 mini_info_separator (panel);
970 display_mini_info (panel);
973 panel->dirty = 0;
976 /* add "#enc:encodning" to end of path */
977 /* if path end width a previous #enc:, only encoding is changed no additional
978 * #enc: is appended
979 * retun new string
981 static char
982 *add_encoding_to_path (const char *path, const char *encoding)
984 char *result;
985 char *semi;
986 char *slash;
988 semi = g_strrstr (path, "#enc:");
990 if (semi != NULL) {
991 slash = strchr (semi, PATH_SEP);
992 if (slash != NULL) {
993 result = g_strconcat (path, "/#enc:", encoding, NULL);
994 } else {
995 *semi = 0;
996 result = g_strconcat (path, "/#enc:", encoding, NULL);
997 *semi = '#';
999 } else {
1000 result = g_strconcat (path, "/#enc:", encoding, NULL);
1003 return result;
1006 char *
1007 remove_encoding_from_path (const char *path)
1009 GString *ret;
1010 GString *tmp_path, *tmp_conv;
1011 char *tmp, *tmp2;
1012 const char *enc;
1013 GIConv converter;
1015 ret = g_string_new("");
1016 tmp_conv = g_string_new("");
1018 tmp_path = g_string_new(path);
1020 while ((tmp = g_strrstr (tmp_path->str, "/#enc:")) != NULL){
1021 enc = vfs_get_encoding ((const char *) tmp);
1022 converter = enc ? str_crt_conv_to (enc): str_cnv_to_term;
1023 if (converter == INVALID_CONV) converter = str_cnv_to_term;
1025 tmp2=tmp+1;
1026 while (*tmp2 && *tmp2 != '/')
1027 tmp2++;
1029 if (*tmp2){
1030 str_vfs_convert_from (converter, tmp2, tmp_conv);
1031 g_string_prepend(ret, tmp_conv->str);
1032 g_string_set_size(tmp_conv,0);
1034 g_string_set_size(tmp_path,tmp - tmp_path->str);
1035 str_close_conv (converter);
1037 g_string_prepend(ret, tmp_path->str);
1038 g_string_free(tmp_path,TRUE);
1039 g_string_free(tmp_conv,TRUE);
1041 tmp = ret->str;
1042 g_string_free(ret, FALSE);
1043 return tmp;
1047 * Repaint the contents of the panels without frames. To schedule panel
1048 * for repainting, set panel->dirty to 1. There are many reasons why
1049 * the panels need to be repainted, and this is a costly operation, so
1050 * it's done once per event.
1052 void
1053 update_dirty_panels (void)
1055 if (current_panel->dirty)
1056 paint_panel (current_panel);
1058 if ((get_other_type () == view_listing) && other_panel->dirty)
1059 paint_panel (other_panel);
1062 static void
1063 do_select (WPanel *panel, int i)
1065 if (i != panel->selected) {
1066 panel->dirty = 1;
1067 panel->selected = i;
1068 panel->top_file = panel->selected - (panel->widget.lines - 2) / 2;
1069 if (panel->top_file < 0)
1070 panel->top_file = 0;
1074 static void
1075 do_try_to_select (WPanel *panel, const char *name)
1077 int i;
1078 char *subdir;
1080 if (!name) {
1081 do_select(panel, 0);
1082 return;
1085 /* We only want the last component of the directory,
1086 * and from this only the name without suffix. */
1087 subdir = vfs_strip_suffix_from_filename (x_basename(name));
1089 /* Search that subdirectory, if found select it */
1090 for (i = 0; i < panel->count; i++){
1091 if (strcmp (subdir, panel->dir.list [i].fname) == 0) {
1092 do_select (panel, i);
1093 g_free (subdir);
1094 return;
1098 /* Try to select a file near the file that is missing */
1099 if (panel->selected >= panel->count)
1100 do_select (panel, panel->count-1);
1101 g_free (subdir);
1104 void
1105 try_to_select (WPanel *panel, const char *name)
1107 do_try_to_select (panel, name);
1108 select_item (panel);
1111 void
1112 panel_update_cols (Widget *widget, int frame_size)
1114 int cols, origin;
1116 if (horizontal_split){
1117 widget->cols = COLS;
1118 return;
1121 if (frame_size == frame_full){
1122 cols = COLS;
1123 origin = 0;
1124 } else {
1125 if (widget == get_panel_widget (0)){
1126 cols = first_panel_size;
1127 origin = 0;
1128 } else {
1129 cols = COLS-first_panel_size;
1130 origin = first_panel_size;
1134 widget->cols = cols;
1135 widget->x = origin;
1138 static char *
1139 panel_save_name (WPanel *panel)
1141 extern int saving_setup;
1143 /* If the program is shuting down */
1144 if ((midnight_shutdown && auto_save_setup) || saving_setup)
1145 return g_strdup (panel->panel_name);
1146 else
1147 return g_strconcat ("Temporal:", panel->panel_name, (char *) NULL);
1150 void
1151 panel_clean_dir (WPanel *panel)
1153 int count = panel->count;
1155 panel->count = 0;
1156 panel->top_file = 0;
1157 panel->selected = 0;
1158 panel->marked = 0;
1159 panel->dirs_marked = 0;
1160 panel->total = 0;
1161 panel->searching = 0;
1162 panel->is_panelized = 0;
1163 panel->dirty = 1;
1165 clean_dir (&panel->dir, count);
1168 static void
1169 panel_destroy (WPanel *p)
1171 int i;
1173 char *name = panel_save_name (p);
1175 panel_save_setup (p, name);
1176 panel_clean_dir (p);
1178 /* save and clean history */
1179 if (p->dir_history) {
1180 history_put (p->hist_name, p->dir_history);
1182 p->dir_history = g_list_first (p->dir_history);
1183 g_list_foreach (p->dir_history, (GFunc) g_free, NULL);
1184 g_list_free (p->dir_history);
1187 g_free (p->hist_name);
1189 delete_format (p->format);
1190 delete_format (p->status_format);
1192 g_free (p->user_format);
1193 for (i = 0; i < LIST_TYPES; i++)
1194 g_free (p->user_status_format[i]);
1195 g_free (p->dir.list);
1196 g_free (p->panel_name);
1197 g_free (name);
1200 static void
1201 panel_format_modified (WPanel *panel)
1203 panel->format_modified = 1;
1206 /* Panel creation */
1207 /* The parameter specifies the name of the panel for setup retieving */
1208 WPanel *
1209 panel_new (const char *panel_name)
1211 return panel_new_with_dir(panel_name, NULL);
1214 /* Panel creation for specified directory */
1215 /* The parameter specifies the name of the panel for setup retieving */
1216 /* and the path of working panel directory. If path is NULL then */
1217 /* panel will be created for current directory */
1218 WPanel *
1219 panel_new_with_dir (const char *panel_name, const char *wpath)
1221 WPanel *panel;
1222 char *section;
1223 int i, err;
1224 char curdir[MC_MAXPATHLEN];
1226 panel = g_new0 (WPanel, 1);
1228 /* No know sizes of the panel at startup */
1229 init_widget (&panel->widget, 0, 0, 0, 0, panel_callback, panel_event);
1231 /* We do not want the cursor */
1232 widget_want_cursor (panel->widget, 0);
1234 if (wpath) {
1235 g_strlcpy(panel->cwd, wpath, sizeof (panel->cwd));
1236 mc_get_current_wd (curdir, sizeof (curdir) - 2);
1237 } else
1238 mc_get_current_wd (panel->cwd, sizeof (panel->cwd) - 2);
1240 strcpy (panel->lwd, ".");
1242 panel->hist_name = g_strconcat ("Dir Hist ", panel_name, (char *) NULL);
1243 panel->dir_history = history_get (panel->hist_name);
1244 directory_history_add (panel, panel->cwd);
1246 panel->dir.list = g_new (file_entry, MIN_FILES);
1247 panel->dir.size = MIN_FILES;
1248 panel->active = 0;
1249 panel->filter = 0;
1250 panel->split = 0;
1251 panel->top_file = 0;
1252 panel->selected = 0;
1253 panel->marked = 0;
1254 panel->total = 0;
1255 panel->reverse = 0;
1256 panel->dirty = 1;
1257 panel->searching = 0;
1258 panel->dirs_marked = 0;
1259 panel->is_panelized = 0;
1260 panel->format = 0;
1261 panel->status_format = 0;
1262 panel->format_modified = 1;
1264 panel->panel_name = g_strdup (panel_name);
1265 panel->user_format = g_strdup (DEFAULT_USER_FORMAT);
1267 for (i = 0; i < LIST_TYPES; i++)
1268 panel->user_status_format[i] = g_strdup (DEFAULT_USER_FORMAT);
1270 panel->search_buffer[0] = 0;
1271 panel->frame_size = frame_half;
1272 section = g_strconcat ("Temporal:", panel->panel_name, (char *) NULL);
1273 if (!mc_config_has_group (mc_main_config, section)) {
1274 g_free (section);
1275 section = g_strdup (panel->panel_name);
1277 panel_load_setup (panel, section);
1278 g_free (section);
1280 /* Load format strings */
1281 err = set_panel_formats (panel);
1282 if (err) {
1283 set_panel_formats (panel);
1287 /* Because do_load_dir lists files in current directory */
1288 if (wpath)
1289 mc_chdir(wpath);
1291 /* Load the default format */
1292 panel->count =
1293 do_load_dir (panel->cwd, &panel->dir, panel->current_sort_field->sort_routine,
1294 panel->reverse, panel->case_sensitive,
1295 panel->exec_first, panel->filter);
1297 /* Restore old right path */
1298 if (wpath)
1299 mc_chdir(curdir);
1301 return panel;
1304 void
1305 panel_reload (WPanel *panel)
1307 struct stat current_stat;
1309 if (fast_reload && !stat (panel->cwd, &current_stat)
1310 && current_stat.st_ctime == panel->dir_stat.st_ctime
1311 && current_stat.st_mtime == panel->dir_stat.st_mtime)
1312 return;
1314 while (mc_chdir (panel->cwd) == -1) {
1315 char *last_slash;
1317 if (panel->cwd[0] == PATH_SEP && panel->cwd[1] == 0) {
1318 panel_clean_dir (panel);
1319 panel->count = set_zero_dir (&panel->dir);
1320 return;
1322 last_slash = strrchr (panel->cwd, PATH_SEP);
1323 if (!last_slash || last_slash == panel->cwd)
1324 strcpy (panel->cwd, PATH_SEP_STR);
1325 else
1326 *last_slash = 0;
1327 memset (&(panel->dir_stat), 0, sizeof (panel->dir_stat));
1328 show_dir (panel);
1331 panel->count =
1332 do_reload_dir (panel->cwd, &panel->dir, panel->current_sort_field->sort_routine,
1333 panel->count, panel->reverse, panel->case_sensitive,
1334 panel->exec_first, panel->filter);
1336 panel->dirty = 1;
1337 if (panel->selected >= panel->count)
1338 do_select (panel, panel->count - 1);
1340 recalculate_panel_summary (panel);
1343 static void
1344 panel_paint_sort_info(WPanel *panel)
1346 const char *sort_sign = (panel->reverse) ? panel_sort_down_sign : panel_sort_up_sign;
1347 char *str;
1349 if (*panel->current_sort_field->hotkey == '\0')
1350 return;
1352 str = g_strdup_printf("%s%s",sort_sign, _(panel->current_sort_field->hotkey));
1354 widget_move (&panel->widget, 1, 1);
1355 tty_print_string (str);
1356 g_free(str);
1359 static void
1360 paint_frame (WPanel *panel)
1362 int side, width;
1363 GString *format_txt;
1365 if (!panel->split)
1366 adjust_top_file (panel);
1368 widget_erase (&panel->widget);
1369 show_dir (panel);
1371 widget_move (&panel->widget, 1, 1);
1373 for (side = 0; side <= panel->split; side++){
1374 format_e *format;
1376 if (side){
1377 tty_setcolor (NORMAL_COLOR);
1378 tty_print_one_vline ();
1379 width = panel->widget.cols - panel->widget.cols/2 - 1;
1380 } else if (panel->split)
1381 width = panel->widget.cols/2 - 3;
1382 else
1383 width = panel->widget.cols - 2;
1385 format_txt = g_string_new("");
1386 for (format = panel->format; format; format = format->next){
1387 if (format->string_fn){
1388 g_string_set_size(format_txt, 0);
1390 if (panel->list_type == list_long && strcmp (format->id, panel->current_sort_field->id) == 0)
1391 g_string_append (format_txt, (panel->reverse) ? panel_sort_down_sign : panel_sort_up_sign);
1393 g_string_append (format_txt, format->title);
1394 if (strcmp (format->id, "name") == 0 && panel->filter && *panel->filter) {
1395 g_string_append (format_txt, " [");
1396 g_string_append (format_txt, panel->filter);
1397 g_string_append (format_txt, "]");
1400 tty_setcolor (MARKED_COLOR);
1401 tty_print_string (str_fit_to_term (format_txt->str, format->field_len,
1402 J_CENTER_LEFT));
1403 width -= format->field_len;
1404 } else {
1405 tty_setcolor (NORMAL_COLOR);
1406 tty_print_one_vline ();
1407 width--;
1410 g_string_free(format_txt, TRUE);
1412 if (width > 0)
1413 tty_draw_hline (-1, -1, ' ', width);
1416 if (panel->list_type != list_long)
1417 panel_paint_sort_info(panel);
1420 static const char *
1421 parse_panel_size (WPanel *panel, const char *format, int isstatus)
1423 int frame = frame_half;
1424 format = skip_separators (format);
1426 if (!strncmp (format, "full", 4)){
1427 frame = frame_full;
1428 format += 4;
1429 } else if (!strncmp (format, "half", 4)){
1430 frame = frame_half;
1431 format += 4;
1434 if (!isstatus){
1435 panel->frame_size = frame;
1436 panel->split = 0;
1439 /* Now, the optional column specifier */
1440 format = skip_separators (format);
1442 if (*format == '1' || *format == '2'){
1443 if (!isstatus)
1444 panel->split = *format == '2';
1445 format++;
1448 if (!isstatus)
1449 panel_update_cols (&(panel->widget), panel->frame_size);
1451 return skip_separators (format);
1454 /* Format is:
1456 all := panel_format? format
1457 panel_format := [full|half] [1|2]
1458 format := one_format_e
1459 | format , one_format_e
1461 one_format_e := just format.id [opt_size]
1462 just := [<=>]
1463 opt_size := : size [opt_expand]
1464 size := [0-9]+
1465 opt_expand := +
1469 static format_e *
1470 parse_display_format (WPanel *panel, const char *format, char **error, int isstatus, int *res_total_cols)
1472 format_e *darr, *old = 0, *home = 0; /* The formats we return */
1473 int total_cols = 0; /* Used columns by the format */
1474 int set_justify; /* flag: set justification mode? */
1475 align_crt_t justify = J_LEFT; /* Which mode. */
1476 size_t i;
1478 static size_t i18n_timelength = 0; /* flag: check ?Time length at startup */
1480 *error = 0;
1482 if (i18n_timelength == 0) {
1483 i18n_timelength = i18n_checktimelength (); /* Musn't be 0 */
1485 for (i = 0; panel_fields[i].id != NULL; i++)
1486 if (strcmp ("time", panel_fields[i].id + 1) == 0)
1487 panel_fields [i].min_size = i18n_timelength;
1491 * This makes sure that the panel and mini status full/half mode
1492 * setting is equal
1494 format = parse_panel_size (panel, format, isstatus);
1496 while (*format){ /* format can be an empty string */
1497 int found = 0;
1499 darr = g_new (format_e, 1);
1501 /* I'm so ugly, don't look at me :-) */
1502 if (!home)
1503 home = old = darr;
1505 old->next = darr;
1506 darr->next = 0;
1507 old = darr;
1509 format = skip_separators (format);
1511 if (strchr ("<=>", *format)){
1512 set_justify = 1;
1513 switch (*format)
1515 case '<':
1516 justify = J_LEFT;
1517 break;
1518 case '=':
1519 justify = J_CENTER;
1520 break;
1521 case '>':
1522 default:
1523 justify = J_RIGHT;
1524 break;
1526 format = skip_separators (format+1);
1527 } else
1528 set_justify = 0;
1530 for (i = 0; panel_fields[i].id != NULL; i++) {
1531 size_t klen = strlen (panel_fields [i].id);
1533 if (strncmp (format, panel_fields [i].id, klen) != 0)
1534 continue;
1536 format += klen;
1538 darr->requested_field_len = panel_fields [i].min_size;
1539 darr->string_fn = panel_fields [i].string_fn;
1540 if (panel_fields [i].title [0])
1541 darr->title = _(panel_fields [i].title);
1542 else
1543 darr->title = "";
1544 darr->id = panel_fields [i].id;
1545 darr->expand = panel_fields [i].expands;
1546 darr->just_mode = panel_fields [i].default_just;
1548 if (set_justify) {
1549 if (IS_FIT(darr->just_mode))
1550 darr->just_mode = MAKE_FIT(justify);
1551 else
1552 darr->just_mode = justify;
1554 found = 1;
1556 format = skip_separators (format);
1558 /* If we have a size specifier */
1559 if (*format == ':'){
1560 int req_length;
1562 /* If the size was specified, we don't want
1563 * auto-expansion by default
1565 darr->expand = 0;
1566 format++;
1567 req_length = atoi (format);
1568 darr->requested_field_len = req_length;
1570 format = skip_numbers (format);
1572 /* Now, if they insist on expansion */
1573 if (*format == '+'){
1574 darr->expand = 1;
1575 format++;
1580 break;
1582 if (!found){
1583 char *tmp_format = g_strdup (format);
1585 int pos = min (8, strlen (format));
1586 delete_format (home);
1587 tmp_format [pos] = 0;
1588 *error = g_strconcat (_("Unknown tag on display format: "), tmp_format, (char *) NULL);
1589 g_free (tmp_format);
1590 return 0;
1592 total_cols += darr->requested_field_len;
1595 *res_total_cols = total_cols;
1596 return home;
1599 static format_e *
1600 use_display_format (WPanel *panel, const char *format, char **error, int isstatus)
1602 #define MAX_EXPAND 4
1603 int expand_top = 0; /* Max used element in expand */
1604 int usable_columns; /* Usable columns in the panel */
1605 int total_cols = 0;
1606 int i;
1607 format_e *darr, *home;
1609 if (!format)
1610 format = DEFAULT_USER_FORMAT;
1612 home = parse_display_format (panel, format, error, isstatus, &total_cols);
1614 if (*error)
1615 return 0;
1617 panel->dirty = 1;
1619 /* Status needn't to be split */
1620 usable_columns = ((panel->widget.cols-2)/((isstatus)
1622 : (panel->split+1))) - (!isstatus && panel->split);
1624 /* Look for the expandable fields and set field_len based on the requested field len */
1625 for (darr = home; darr && expand_top < MAX_EXPAND; darr = darr->next){
1626 darr->field_len = darr->requested_field_len;
1627 if (darr->expand)
1628 expand_top++;
1631 /* If we used more columns than the available columns, adjust that */
1632 if (total_cols > usable_columns){
1633 int pdif, dif = total_cols - usable_columns;
1635 while (dif){
1636 pdif = dif;
1637 for (darr = home; darr; darr = darr->next){
1638 if (dif && darr->field_len - 1){
1639 darr->field_len--;
1640 dif--;
1644 /* avoid endless loop if num fields > 40 */
1645 if (pdif == dif)
1646 break;
1648 total_cols = usable_columns; /* give up, the rest should be truncated */
1651 /* Expand the available space */
1652 if ((usable_columns > total_cols) && expand_top){
1653 int spaces = (usable_columns - total_cols) / expand_top;
1654 int extra = (usable_columns - total_cols) % expand_top;
1656 for (i = 0, darr = home; darr && (i < expand_top); darr = darr->next)
1657 if (darr->expand){
1658 darr->field_len += (spaces + ((i == 0) ? extra : 0));
1659 i++;
1662 return home;
1665 /* Switches the panel to the mode specified in the format */
1666 /* Seting up both format and status string. Return: 0 - on success; */
1667 /* 1 - format error; 2 - status error; 3 - errors in both formats. */
1669 set_panel_formats (WPanel *p)
1671 format_e *form;
1672 char *err;
1673 int retcode = 0;
1675 form = use_display_format (p, panel_format (p), &err, 0);
1677 if (err){
1678 g_free (err);
1679 retcode = 1;
1681 else {
1682 if (p->format)
1683 delete_format (p->format);
1685 p->format = form;
1688 if (show_mini_info){
1690 form = use_display_format (p, mini_status_format (p), &err, 1);
1692 if (err){
1693 g_free (err);
1694 retcode += 2;
1696 else {
1697 if (p->status_format)
1698 delete_format (p->status_format);
1700 p->status_format = form;
1704 panel_format_modified (p);
1705 panel_update_cols (&(p->widget), p->frame_size);
1707 if (retcode)
1708 message (D_ERROR, _("Warning" ), _( "User supplied format looks invalid, reverting to default." ) );
1709 if (retcode & 0x01){
1710 g_free (p->user_format);
1711 p->user_format = g_strdup (DEFAULT_USER_FORMAT);
1713 if (retcode & 0x02){
1714 g_free (p->user_status_format [p->list_type]);
1715 p->user_status_format [p->list_type] = g_strdup (DEFAULT_USER_FORMAT);
1718 return retcode;
1721 /* Given the panel->view_type returns the format string to be parsed */
1722 static const char *
1723 panel_format (WPanel *panel)
1725 switch (panel->list_type){
1727 case list_long:
1728 return "full perm space nlink space owner space group space size space mtime space name";
1730 case list_brief:
1731 return "half 2 type name";
1733 case list_user:
1734 return panel->user_format;
1736 default:
1737 case list_full:
1738 return "half type name | size | mtime";
1742 static const char *
1743 mini_status_format (WPanel *panel)
1745 if (panel->user_mini_status)
1746 return panel->user_status_format [panel->list_type];
1748 switch (panel->list_type){
1750 case list_long:
1751 return "full perm space nlink space owner space group space size space mtime space name";
1753 case list_brief:
1754 return "half type name space bsize space perm space";
1756 case list_full:
1757 return "half type name";
1759 default:
1760 case list_user:
1761 return panel->user_format;
1765 /* */
1766 /* Panel operation commands */
1767 /* */
1769 /* Used to emulate Lynx's entering leaving a directory with the arrow keys */
1770 static cb_ret_t
1771 maybe_cd (int move_up_dir)
1773 if (navigate_with_arrows) {
1774 if (!cmdline->buffer[0]) {
1775 if (move_up_dir) {
1776 do_cd ("..", cd_exact);
1777 return MSG_HANDLED;
1779 if (S_ISDIR (selection (current_panel)->st.st_mode)
1780 || link_isdir (selection (current_panel))) {
1781 do_cd (selection (current_panel)->fname, cd_exact);
1782 return MSG_HANDLED;
1786 return MSG_NOT_HANDLED;
1789 /* Returns the number of items in the given panel */
1790 static int
1791 ITEMS (WPanel *p)
1793 if (p->split)
1794 return llines (p) * 2;
1795 else
1796 return llines (p);
1799 /* Select current item and readjust the panel */
1800 void
1801 select_item (WPanel *panel)
1803 int items = ITEMS (panel);
1805 /* Although currently all over the code we set the selection and
1806 top file to decent values before calling select_item, I could
1807 forget it someday, so it's better to do the actual fitting here */
1809 if (panel->top_file < 0)
1810 panel->top_file = 0;
1812 if (panel->selected < 0)
1813 panel->selected = 0;
1815 if (panel->selected > panel->count - 1)
1816 panel->selected = panel->count - 1;
1818 if (panel->top_file > panel->count - 1)
1819 panel->top_file = panel->count - 1;
1821 if ((panel->count - panel->top_file) < items) {
1822 panel->top_file = panel->count - items;
1823 if (panel->top_file < 0)
1824 panel->top_file = 0;
1827 if (panel->selected < panel->top_file)
1828 panel->top_file = panel->selected;
1830 if ((panel->selected - panel->top_file) >= items)
1831 panel->top_file = panel->selected - items + 1;
1833 panel->dirty = 1;
1835 execute_hooks (select_file_hook);
1838 /* Clears all files in the panel, used only when one file was marked */
1839 void
1840 unmark_files (WPanel *panel)
1842 int i;
1844 if (!panel->marked)
1845 return;
1846 for (i = 0; i < panel->count; i++)
1847 file_mark (panel, i, 0);
1849 panel->dirs_marked = 0;
1850 panel->marked = 0;
1851 panel->total = 0;
1854 static void
1855 unselect_item (WPanel *panel)
1857 repaint_file (panel, panel->selected, 1, 2*selection (panel)->f.marked, 0);
1860 static void
1861 move_down (WPanel *panel)
1863 if (panel->selected+1 == panel->count)
1864 return;
1866 unselect_item (panel);
1867 panel->selected++;
1868 if (panel->selected - panel->top_file == ITEMS (panel) &&
1869 panel_scroll_pages) {
1870 /* Scroll window half screen */
1871 panel->top_file += ITEMS (panel)/2;
1872 if (panel->top_file > panel->count - ITEMS (panel))
1873 panel->top_file = panel->count - ITEMS (panel);
1874 paint_dir (panel);
1876 select_item (panel);
1879 static void
1880 move_up (WPanel *panel)
1882 if (panel->selected == 0)
1883 return;
1885 unselect_item (panel);
1886 panel->selected--;
1887 if (panel->selected < panel->top_file && panel_scroll_pages) {
1888 /* Scroll window half screen */
1889 panel->top_file -= ITEMS (panel)/2;
1890 if (panel->top_file < 0)
1891 panel->top_file = 0;
1892 paint_dir (panel);
1894 select_item (panel);
1897 /* Changes the selection by lines (may be negative) */
1898 static void
1899 move_selection (WPanel *panel, int lines)
1901 int new_pos;
1902 int adjust = 0;
1904 new_pos = panel->selected + lines;
1905 if (new_pos >= panel->count)
1906 new_pos = panel->count-1;
1908 if (new_pos < 0)
1909 new_pos = 0;
1911 unselect_item (panel);
1912 panel->selected = new_pos;
1914 if (panel->selected - panel->top_file >= ITEMS (panel)){
1915 panel->top_file += lines;
1916 adjust = 1;
1919 if (panel->selected - panel->top_file < 0){
1920 panel->top_file += lines;
1921 adjust = 1;
1924 if (adjust){
1925 if (panel->top_file > panel->selected)
1926 panel->top_file = panel->selected;
1927 if (panel->top_file < 0)
1928 panel->top_file = 0;
1929 paint_dir (panel);
1931 select_item (panel);
1934 static cb_ret_t
1935 move_left (WPanel *panel)
1937 if (panel->split) {
1938 move_selection (panel, -llines (panel));
1939 return MSG_HANDLED;
1940 } else
1941 return maybe_cd (1); /* cd .. */
1944 static int
1945 move_right (WPanel *panel)
1947 if (panel->split) {
1948 move_selection (panel, llines (panel));
1949 return MSG_HANDLED;
1950 } else
1951 return maybe_cd (0); /* cd (selection) */
1954 static void
1955 prev_page (WPanel *panel)
1957 int items;
1959 if (!panel->selected && !panel->top_file)
1960 return;
1961 unselect_item (panel);
1962 items = ITEMS (panel);
1963 if (panel->top_file < items)
1964 items = panel->top_file;
1965 if (!items)
1966 panel->selected = 0;
1967 else
1968 panel->selected -= items;
1969 panel->top_file -= items;
1971 /* This keeps the selection in a reasonable place */
1972 if (panel->selected < 0)
1973 panel->selected = 0;
1974 if (panel->top_file < 0)
1975 panel->top_file = 0;
1976 select_item (panel);
1977 paint_dir (panel);
1980 static void
1981 ctrl_prev_page (WPanel *panel)
1983 (void) panel;
1984 do_cd ("..", cd_exact);
1987 static void
1988 next_page (WPanel *panel)
1990 int items;
1992 if (panel->selected == panel->count - 1)
1993 return;
1994 unselect_item (panel);
1995 items = ITEMS (panel);
1996 if (panel->top_file > panel->count - 2 * items)
1997 items = panel->count - items - panel->top_file;
1998 if (panel->top_file + items < 0)
1999 items = -panel->top_file;
2000 if (!items)
2001 panel->selected = panel->count - 1;
2002 else
2003 panel->selected += items;
2004 panel->top_file += items;
2006 /* This keeps the selection in it's relative position */
2007 if (panel->selected >= panel->count)
2008 panel->selected = panel->count - 1;
2009 if (panel->top_file >= panel->count)
2010 panel->top_file = panel->count - 1;
2011 select_item (panel);
2012 paint_dir (panel);
2015 static void
2016 ctrl_next_page (WPanel *panel)
2018 if ((S_ISDIR (selection (panel)->st.st_mode)
2019 || link_isdir (selection (panel)))) {
2020 do_cd (selection (panel)->fname, cd_exact);
2024 static void
2025 goto_top_file (WPanel *panel)
2027 unselect_item (panel);
2028 panel->selected = panel->top_file;
2029 select_item (panel);
2032 static void
2033 goto_middle_file (WPanel *panel)
2035 unselect_item (panel);
2036 panel->selected = panel->top_file + (ITEMS (panel)/2);
2037 if (panel->selected >= panel->count)
2038 panel->selected = panel->count - 1;
2039 select_item (panel);
2042 static void
2043 goto_bottom_file (WPanel *panel)
2045 unselect_item (panel);
2046 panel->selected = panel->top_file + ITEMS (panel)-1;
2047 if (panel->selected >= panel->count)
2048 panel->selected = panel->count - 1;
2049 select_item (panel);
2052 static void
2053 move_home (WPanel *panel)
2055 if (panel->selected == 0)
2056 return;
2057 unselect_item (panel);
2059 if (torben_fj_mode){
2060 int middle_pos = panel->top_file + (ITEMS (panel)/2);
2062 if (panel->selected > middle_pos){
2063 goto_middle_file (panel);
2064 return;
2066 if (panel->selected != panel->top_file){
2067 goto_top_file (panel);
2068 return;
2072 panel->top_file = 0;
2073 panel->selected = 0;
2075 paint_dir (panel);
2076 select_item (panel);
2079 static void
2080 move_end (WPanel *panel)
2082 if (panel->selected == panel->count-1)
2083 return;
2084 unselect_item (panel);
2085 if (torben_fj_mode){
2086 int middle_pos = panel->top_file + (ITEMS (panel)/2);
2088 if (panel->selected < middle_pos){
2089 goto_middle_file (panel);
2090 return;
2092 if (panel->selected != (panel->top_file + ITEMS(panel)-1)){
2093 goto_bottom_file (panel);
2094 return;
2098 panel->selected = panel->count-1;
2099 paint_dir (panel);
2100 select_item (panel);
2103 /* Recalculate the panels summary information, used e.g. when marked
2104 files might have been removed by an external command */
2105 void
2106 recalculate_panel_summary (WPanel *panel)
2108 int i;
2110 panel->marked = 0;
2111 panel->dirs_marked = 0;
2112 panel->total = 0;
2114 for (i = 0; i < panel->count; i++)
2115 if (panel->dir.list [i].f.marked){
2116 /* do_file_mark will return immediately if newmark == oldmark.
2117 So we have to first unmark it to get panel's summary information
2118 updated. (Norbert) */
2119 panel->dir.list [i].f.marked = 0;
2120 do_file_mark (panel, i, 1);
2124 /* This routine marks a file or a directory */
2125 void
2126 do_file_mark (WPanel *panel, int idx, int mark)
2128 if (panel->dir.list[idx].f.marked == mark)
2129 return;
2131 /* Only '..' can't be marked, '.' isn't visible */
2132 if (!strcmp (panel->dir.list[idx].fname, ".."))
2133 return;
2135 file_mark (panel, idx, mark);
2136 if (panel->dir.list[idx].f.marked) {
2137 panel->marked++;
2138 if (S_ISDIR (panel->dir.list[idx].st.st_mode)) {
2139 if (panel->dir.list[idx].f.dir_size_computed)
2140 panel->total += panel->dir.list[idx].st.st_size;
2141 panel->dirs_marked++;
2142 } else
2143 panel->total += panel->dir.list[idx].st.st_size;
2144 set_colors (panel);
2145 } else {
2146 if (S_ISDIR (panel->dir.list[idx].st.st_mode)) {
2147 if (panel->dir.list[idx].f.dir_size_computed)
2148 panel->total -= panel->dir.list[idx].st.st_size;
2149 panel->dirs_marked--;
2150 } else
2151 panel->total -= panel->dir.list[idx].st.st_size;
2152 panel->marked--;
2156 static void
2157 do_mark_file (WPanel *panel, int do_move)
2159 do_file_mark (panel, panel->selected,
2160 selection (panel)->f.marked ? 0 : 1);
2161 if (mark_moves_down && do_move)
2162 move_down (panel);
2165 static void
2166 mark_file (WPanel *panel)
2168 do_mark_file (panel, 1);
2171 /* Incremental search of a file name in the panel */
2172 static void
2173 do_search (WPanel *panel, int c_code)
2175 size_t l, max, buf_max;
2176 int i, sel;
2177 int wrapped = 0;
2178 char *act;
2180 l = strlen (panel->search_buffer);
2181 if (c_code == KEY_BACKSPACE) {
2182 if (l != 0) {
2183 act = panel->search_buffer + l;
2184 str_prev_noncomb_char (&act, panel->search_buffer);
2185 act[0] = '\0';
2187 panel->search_chpoint = 0;
2188 } else {
2189 if (c_code && (gsize) panel->search_chpoint < sizeof (panel->search_char)) {
2190 panel->search_char[panel->search_chpoint] = c_code;
2191 panel->search_chpoint++;
2194 if (panel->search_chpoint > 0) {
2195 switch (str_is_valid_char (panel->search_char, panel->search_chpoint)) {
2196 case -2:
2197 return;
2198 case -1:
2199 panel->search_chpoint = 0;
2200 return;
2201 default:
2202 if (l + panel->search_chpoint < sizeof (panel->search_buffer)) {
2203 memcpy (panel->search_buffer + l, panel->search_char,
2204 panel->search_chpoint);
2205 l+= panel->search_chpoint;
2206 (panel->search_buffer + l)[0] = '\0';
2207 panel->search_chpoint = 0;
2213 buf_max = panel->case_sensitive ?
2214 str_prefix (panel->search_buffer, panel->search_buffer) :
2215 str_caseprefix (panel->search_buffer, panel->search_buffer);
2216 max = 0;
2217 sel = panel->selected;
2218 for (i = panel->selected; !wrapped || i != panel->selected; i++) {
2219 if (i >= panel->count) {
2220 i = 0;
2221 if (wrapped)
2222 break;
2223 wrapped = 1;
2225 l = panel->case_sensitive ?
2226 str_prefix (panel->dir.list[i].fname, panel->search_buffer) :
2227 str_caseprefix (panel->dir.list[i].fname, panel->search_buffer);
2228 if (l > max) {
2229 max = l;
2230 sel = i;
2231 if (max == buf_max) break;
2235 unselect_item (panel);
2236 panel->selected = sel;
2237 select_item (panel);
2239 act = panel->search_buffer + strlen (panel->search_buffer);
2240 while (max < buf_max) {
2241 str_prev_char_safe (&act);
2242 act[0] = '\0';
2243 buf_max = panel->case_sensitive ?
2244 str_prefix (panel->search_buffer, panel->search_buffer) :
2245 str_caseprefix (panel->search_buffer, panel->search_buffer);
2248 paint_panel (panel);
2251 static void
2252 start_search (WPanel *panel)
2254 if (panel->searching){
2255 if (panel->selected+1 == panel->count)
2256 panel->selected = 0;
2257 else
2258 move_down (panel);
2259 do_search (panel, 0);
2260 } else {
2261 panel->searching = 1;
2262 panel->search_buffer[0] = '\0';
2263 panel->search_char[0] = '\0';
2264 panel->search_chpoint = 0;
2265 display_mini_info (panel);
2266 mc_refresh ();
2270 /* Return 1 if the Enter key has been processed, 0 otherwise */
2271 static int
2272 do_enter_on_file_entry (file_entry *fe)
2274 char *full_name;
2277 * Directory or link to directory - change directory.
2278 * Try the same for the entries on which mc_lstat() has failed.
2280 if (S_ISDIR (fe->st.st_mode) || link_isdir (fe)
2281 || (fe->st.st_mode == 0)) {
2282 if (!do_cd (fe->fname, cd_exact))
2283 message (D_ERROR, MSG_ERROR, _("Cannot change directory"));
2284 return 1;
2287 /* Try associated command */
2288 if (regex_command (fe->fname, "Open", 0) != 0)
2289 return 1;
2291 /* Check if the file is executable */
2292 full_name = concat_dir_and_file (current_panel->cwd, fe->fname);
2293 if (!is_exe (fe->st.st_mode) || !if_link_is_exe (full_name, fe)) {
2294 g_free (full_name);
2295 return 0;
2297 g_free (full_name);
2299 if (confirm_execute) {
2300 if (query_dialog
2301 (_(" The Midnight Commander "),
2302 _(" Do you really want to execute? "), D_NORMAL, 2, _("&Yes"),
2303 _("&No")) != 0)
2304 return 1;
2306 #ifdef USE_VFS
2307 if (!vfs_current_is_local ()) {
2308 char *tmp;
2309 int ret;
2311 tmp = concat_dir_and_file (vfs_get_current_dir (), fe->fname);
2312 ret = mc_setctl (tmp, VFS_SETCTL_RUN, NULL);
2313 g_free (tmp);
2314 /* We took action only if the dialog was shown or the execution
2315 * was successful */
2316 return confirm_execute || (ret == 0);
2318 #endif
2321 char *tmp = name_quote (fe->fname, 0);
2322 char *cmd = g_strconcat (".", PATH_SEP_STR, tmp, (char *) NULL);
2323 g_free (tmp);
2324 shell_execute (cmd, 0);
2325 g_free (cmd);
2328 return 1;
2331 static int
2332 do_enter (WPanel *panel)
2334 return do_enter_on_file_entry (selection (panel));
2337 static void
2338 chdir_other_panel (WPanel *panel)
2340 char *new_dir;
2341 char *sel_entry = NULL;
2343 if (get_other_type () != view_listing) {
2344 set_display_type (get_other_index (), view_listing);
2347 if (!S_ISDIR (panel->dir.list [panel->selected].st.st_mode)) {
2348 new_dir = concat_dir_and_file (panel->cwd, "..");
2349 sel_entry = strrchr(panel->cwd, PATH_SEP);
2350 } else
2351 new_dir = concat_dir_and_file (panel->cwd, panel->dir.list [panel->selected].fname);
2353 change_panel ();
2354 do_cd (new_dir, cd_exact);
2355 if (sel_entry)
2356 try_to_select (current_panel, sel_entry);
2357 change_panel ();
2359 move_down (panel);
2361 g_free (new_dir);
2365 * Make the current directory of the current panel also the current
2366 * directory of the other panel. Put the other panel to the listing
2367 * mode if needed. If the current panel is panelized, the other panel
2368 * doesn't become panelized.
2370 static void
2371 sync_other_panel (WPanel *panel)
2373 if (get_other_type () != view_listing) {
2374 set_display_type (get_other_index (), view_listing);
2377 do_panel_cd (other_panel, current_panel->cwd, cd_exact);
2379 /* try to select current filename on the other panel */
2380 if (!panel->is_panelized) {
2381 try_to_select (other_panel, selection (panel)->fname);
2385 static void
2386 chdir_to_readlink (WPanel *panel)
2388 char *new_dir;
2390 if (get_other_type () != view_listing)
2391 return;
2393 if (S_ISLNK (panel->dir.list [panel->selected].st.st_mode)) {
2394 char buffer [MC_MAXPATHLEN], *p;
2395 int i;
2396 struct stat st;
2398 i = readlink (selection (panel)->fname, buffer, MC_MAXPATHLEN - 1);
2399 if (i < 0)
2400 return;
2401 if (mc_stat (selection (panel)->fname, &st) < 0)
2402 return;
2403 buffer [i] = 0;
2404 if (!S_ISDIR (st.st_mode)) {
2405 p = strrchr (buffer, PATH_SEP);
2406 if (p && !p[1]) {
2407 *p = 0;
2408 p = strrchr (buffer, PATH_SEP);
2410 if (!p)
2411 return;
2412 p[1] = 0;
2414 if (*buffer == PATH_SEP)
2415 new_dir = g_strdup (buffer);
2416 else
2417 new_dir = concat_dir_and_file (panel->cwd, buffer);
2419 change_panel ();
2420 do_cd (new_dir, cd_exact);
2421 change_panel ();
2423 move_down (panel);
2425 g_free (new_dir);
2429 static gsize
2430 panel_get_format_field_count(WPanel *panel)
2432 format_e *format;
2433 gsize index;
2434 for (
2435 index=0, format = panel->format;
2436 format != NULL;
2437 format = format->next, index++
2439 return index;
2443 function return 0 if not found and REAL_INDEX+1 if found
2445 static gsize
2446 panel_get_format_field_index_by_name(WPanel *panel, const char *name)
2448 format_e *format;
2449 gsize index;
2451 for (
2452 index=1, format = panel->format;
2453 ! ( format == NULL || strcmp(format->title, _(name)) == 0 );
2454 format = format->next, index++
2456 if (format == NULL)
2457 index = 0;
2459 return index;
2462 format_e *
2463 panel_get_format_field_by_index(WPanel *panel, gsize index)
2465 format_e *format;
2466 for (
2467 format = panel->format;
2468 ! ( format == NULL || index == 0 );
2469 format = format->next, index--
2471 return format;
2474 static const panel_field_t *
2475 panel_get_sortable_field_by_format(WPanel *panel, gsize index)
2477 const panel_field_t *pfield;
2478 format_e *format;
2480 format = panel_get_format_field_by_index(panel, index);
2481 if (format == NULL)
2482 return NULL;
2483 pfield = panel_get_field_by_title(format->title);
2484 if (pfield == NULL)
2485 return NULL;
2486 if (pfield->sort_routine == NULL)
2487 return NULL;
2488 return pfield;
2491 static void
2492 panel_toggle_sort_order_prev(WPanel *panel)
2494 gsize index, i;
2496 const panel_field_t *pfield = NULL;
2498 index = panel_get_format_field_index_by_name(panel, panel->current_sort_field->title);
2500 if (index > 1){
2501 /* search for prev sortable column in panel format */
2502 for (
2503 i = index-1 ;
2504 i != 0 && (pfield = panel_get_sortable_field_by_format(panel, i-1)) == NULL ;
2509 if ( pfield == NULL) {
2510 /* Sortable field not found. Try to search in each array */
2511 for (
2512 i = panel_get_format_field_count(panel) ;
2513 i != 0 && (pfield = panel_get_sortable_field_by_format(panel, i-1)) == NULL ;
2517 if ( pfield == NULL)
2518 return;
2519 panel->current_sort_field = pfield;
2520 panel_set_sort_order(panel, panel->current_sort_field);
2524 static void
2525 panel_toggle_sort_order_next(WPanel *panel)
2527 gsize index, i;
2528 const panel_field_t *pfield = NULL;
2529 gsize format_field_count = panel_get_format_field_count(panel);
2531 index = panel_get_format_field_index_by_name(panel, panel->current_sort_field->title);
2533 if (index != 0 && index != format_field_count){
2534 /* search for prev sortable column in panel format */
2535 for (
2536 i = index;
2537 i != format_field_count && (pfield = panel_get_sortable_field_by_format(panel, i)) == NULL ;
2542 if ( pfield == NULL) {
2543 /* Sortable field not found. Try to search in each array */
2544 for (
2545 i = 0 ;
2546 i != format_field_count && (pfield = panel_get_sortable_field_by_format(panel, i)) == NULL ;
2550 if ( pfield == NULL)
2551 return;
2552 panel->current_sort_field = pfield;
2553 panel_set_sort_order(panel, panel->current_sort_field);
2556 static void
2557 panel_select_sort_order(WPanel *panel)
2559 const panel_field_t *sort_order;
2560 sort_order = sort_box (panel->current_sort_field, &panel->reverse,
2561 &panel->case_sensitive,
2562 &panel->exec_first);
2563 if (sort_order == NULL)
2564 return;
2565 panel->current_sort_field = sort_order;
2566 panel_set_sort_order (panel, panel->current_sort_field);
2570 static void
2571 panel_set_sort_type_by_id(WPanel *panel, const char *name)
2573 const panel_field_t *sort_order;
2575 if (strcmp(panel->current_sort_field->id, name) != 0) {
2576 sort_order = panel_get_field_by_id (name);
2577 if (sort_order == NULL)
2578 return;
2579 panel->current_sort_field = sort_order;
2580 } else {
2581 panel->reverse = ! panel->reverse;
2583 panel_set_sort_order (panel, panel->current_sort_field);
2586 typedef void (*panel_key_callback) (WPanel *);
2588 static void cmd_do_enter(WPanel *wp) { (void) do_enter(wp); }
2589 static void cmd_view_simple(WPanel *wp) { (void) wp; view_simple_cmd(); }
2590 static void cmd_edit_new(WPanel *wp) { (void) wp; edit_cmd_new(); }
2591 static void cmd_copy_local(WPanel *wp) { (void) wp;copy_cmd_local(); }
2592 static void cmd_rename_local(WPanel *wp) { (void) wp;ren_cmd_local(); }
2593 static void cmd_delete_local(WPanel *wp) { (void) wp;delete_cmd_local(); }
2594 static void cmd_select(WPanel *wp) { (void) wp;select_cmd(); }
2595 static void cmd_unselect(WPanel *wp) { (void) wp;unselect_cmd(); }
2596 static void cmd_reverse_selection(WPanel *wp) { (void) wp;reverse_selection_cmd(); }
2598 static cb_ret_t
2599 panel_execute_cmd (WPanel *panel, int command)
2601 int res = MSG_HANDLED;
2603 switch (command) {
2604 case CK_PanelChdirOtherPanel:
2605 chdir_other_panel (panel);
2606 break;
2607 case CK_PanelChdirToReadlink:
2608 chdir_to_readlink (panel);
2609 break;
2610 case CK_PanelCmdCopyLocal:
2611 cmd_copy_local (panel);
2612 break;
2613 case CK_PanelCmdDeleteLocal:
2614 cmd_delete_local (panel);
2615 break;
2616 case CK_PanelCmdDoEnter:
2617 cmd_do_enter (panel);
2618 break;
2619 case CK_PanelCmdViewSimple:
2620 cmd_view_simple (panel);
2621 break;
2622 case CK_PanelCmdEditNew:
2623 cmd_edit_new (panel);
2624 break;
2625 case CK_PanelCmdRenameLocal:
2626 cmd_rename_local (panel);
2627 break;
2628 case CK_PanelCmdReverseSelection:
2629 cmd_reverse_selection (panel);
2630 break;
2631 case CK_PanelCmdSelect:
2632 cmd_select (panel);
2633 break;
2634 case CK_PanelCmdUnselect:
2635 cmd_unselect (panel);
2636 break;
2637 case CK_PanelNextPage:
2638 next_page (panel);
2639 break;
2640 case CK_PanelPrevPage:
2641 prev_page (panel);
2642 break;
2643 case CK_PanelCtrlNextPage:
2644 ctrl_next_page (panel);
2645 break;
2646 case CK_PanelCtrlPrevPage:
2647 ctrl_prev_page (panel);
2648 break;
2649 case CK_PanelDirectoryHistoryList:
2650 directory_history_list (panel);
2651 break;
2652 case CK_PanelDirectoryHistoryNext:
2653 directory_history_next (panel);
2654 break;
2655 case CK_PanelDirectoryHistoryPrev:
2656 directory_history_prev (panel);
2657 break;
2658 case CK_PanelGotoBottomFile:
2659 goto_bottom_file (panel);
2660 break;
2661 case CK_PanelGotoMiddleFile:
2662 goto_middle_file (panel);
2663 break;
2664 case CK_PanelGotoTopFile:
2665 goto_top_file (panel);
2666 break;
2667 case CK_PanelMarkFile:
2668 mark_file (panel);
2669 break;
2670 case CK_PanelMoveUp:
2671 move_up (panel);
2672 break;
2673 case CK_PanelMoveDown:
2674 move_down (panel);
2675 break;
2676 case CK_PanelMoveLeft:
2677 res = move_left (panel);
2678 break;
2679 case CK_PanelMoveRight:
2680 res = move_right (panel);
2681 break;
2682 case CK_PanelMoveEnd:
2683 move_end (panel);
2684 break;
2685 case CK_PanelMoveHome:
2686 move_home (panel);
2687 break;
2688 case CK_PanelSetPanelEncoding:
2689 set_panel_encoding (panel);
2690 break;
2691 case CK_PanelStartSearch:
2692 start_search (panel);
2693 break;
2694 case CK_PanelSyncOtherPanel:
2695 sync_other_panel (panel);
2696 break;
2697 case CK_PanelSelectSortOrder:
2698 panel_select_sort_order(panel);
2699 break;
2700 case CK_PanelToggleSortOrderPrev:
2701 panel_toggle_sort_order_prev(panel);
2702 break;
2703 case CK_PanelToggleSortOrderNext:
2704 panel_toggle_sort_order_next(panel);
2705 break;
2706 case CK_PanelReverseSort:
2707 panel->reverse = ! panel->reverse;
2708 panel_set_sort_order (panel, panel->current_sort_field);
2709 break;
2710 case CK_PanelSortOrderByName:
2711 panel_set_sort_type_by_id(panel, "name");
2712 break;
2713 case CK_PanelSortOrderByExt:
2714 panel_set_sort_type_by_id(panel, "extension");
2715 break;
2716 case CK_PanelSortOrderBySize:
2717 panel_set_sort_type_by_id(panel, "size");
2718 break;
2719 case CK_PanelSortOrderByMTime:
2720 panel_set_sort_type_by_id(panel, "mtime");
2721 break;
2723 return res;
2726 static cb_ret_t
2727 panel_key (WPanel *panel, int key)
2729 int i;
2730 int res, command;
2732 for (i = 0; panel_map[i].key; i++) {
2733 if (key == panel_map[i].key) {
2734 int old_searching = panel->searching;
2736 if (panel_map[i].command != CK_PanelStartSearch)
2737 panel->searching = 0;
2739 command = panel_map[i].command;
2740 res = panel_execute_cmd (panel, command);
2742 if (res == MSG_NOT_HANDLED)
2743 return res;
2745 if (panel->searching != old_searching)
2746 display_mini_info (panel);
2747 return MSG_HANDLED;
2751 if (torben_fj_mode && key == ALT ('h')) {
2752 goto_middle_file (panel);
2753 return MSG_HANDLED;
2756 if (is_abort_char (key)) {
2757 panel->searching = 0;
2758 display_mini_info (panel);
2759 return MSG_HANDLED;
2762 /* Do not eat characters not meant for the panel below ' ' (e.g. C-l). */
2763 if ((key >= ' ' && key <= 255) || key == KEY_BACKSPACE) {
2764 if (panel->searching) {
2765 do_search (panel, key);
2766 return MSG_HANDLED;
2769 if (!command_prompt) {
2770 start_search (panel);
2771 do_search (panel, key);
2772 return MSG_HANDLED;
2776 return MSG_NOT_HANDLED;
2779 static cb_ret_t
2780 panel_callback (Widget *w, widget_msg_t msg, int parm)
2782 WPanel *panel = (WPanel *) w;
2783 Dlg_head *h = panel->widget.parent;
2785 switch (msg) {
2786 case WIDGET_DRAW:
2787 paint_panel (panel);
2788 return MSG_HANDLED;
2790 case WIDGET_FOCUS:
2791 current_panel = panel;
2792 panel->active = 1;
2793 if (mc_chdir (panel->cwd) != 0) {
2794 char *cwd = strip_password (g_strdup (panel->cwd), 1);
2795 message (D_ERROR, MSG_ERROR, _(" Cannot chdir to \"%s\" \n %s "),
2796 cwd, unix_error_string (errno));
2797 g_free(cwd);
2798 } else
2799 subshell_chdir (panel->cwd);
2801 update_xterm_title_path ();
2802 select_item (panel);
2803 show_dir (panel);
2804 paint_dir (panel);
2805 panel->dirty = 0;
2807 buttonbar_set_label (h, 1, _("Help"), help_cmd);
2808 buttonbar_set_label (h, 2, _("Menu"), user_file_menu_cmd);
2809 buttonbar_set_label (h, 3, _("View"), view_cmd);
2810 buttonbar_set_label (h, 4, _("Edit"), edit_cmd);
2811 buttonbar_set_label (h, 5, _("Copy"), copy_cmd);
2812 buttonbar_set_label (h, 6, _("RenMov"), ren_cmd);
2813 buttonbar_set_label (h, 7, _("Mkdir"), mkdir_cmd);
2814 buttonbar_set_label (h, 8, _("Delete"), delete_cmd);
2815 buttonbar_redraw (h);
2816 return MSG_HANDLED;
2818 case WIDGET_UNFOCUS:
2819 /* Janne: look at this for the multiple panel options */
2820 if (panel->searching){
2821 panel->searching = 0;
2822 display_mini_info (panel);
2824 panel->active = 0;
2825 show_dir (panel);
2826 unselect_item (panel);
2827 return MSG_HANDLED;
2829 case WIDGET_KEY:
2830 return panel_key (panel, parm);
2832 case WIDGET_DESTROY:
2833 panel_destroy (panel);
2834 return MSG_HANDLED;
2836 default:
2837 return default_proc (msg, parm);
2841 void
2842 file_mark (WPanel *panel, int index, int val)
2844 if (panel->dir.list[index].f.marked != val) {
2845 panel->dir.list[index].f.marked = val;
2846 panel->dirty = 1;
2850 /* */
2851 /* Panel mouse events support routines */
2852 /* */
2853 static int mouse_marking = 0;
2855 static void
2856 mouse_toggle_mark (WPanel *panel)
2858 do_mark_file (panel, 0);
2859 mouse_marking = selection (panel)->f.marked;
2862 static void
2863 mouse_set_mark (WPanel *panel)
2865 if (mouse_marking && !(selection (panel)->f.marked))
2866 do_mark_file (panel, 0);
2867 else if (!mouse_marking && (selection (panel)->f.marked))
2868 do_mark_file (panel, 0);
2871 static int
2872 mark_if_marking (WPanel *panel, Gpm_Event *event)
2874 if (event->buttons & GPM_B_RIGHT){
2875 if (event->type & GPM_DOWN)
2876 mouse_toggle_mark (panel);
2877 else
2878 mouse_set_mark (panel);
2879 return 1;
2881 return 0;
2884 /* Determine which column was clicked, and sort the panel on
2885 * that column, or reverse sort on that column if already
2886 * sorted on that column.
2888 static void
2889 mouse_sort_col(Gpm_Event *event, WPanel *panel)
2891 int i;
2892 const char *sort_name = NULL;
2893 panel_field_t *col_sort_format = NULL;
2894 format_e *format;
2896 for (i = 0, format = panel->format; format != NULL; format = format->next) {
2897 i += format->field_len;
2898 if (event->x < i + 1) {
2899 /* found column */
2900 sort_name = format->title;
2901 break;
2905 if (sort_name == NULL)
2906 return;
2908 for(i = 0; panel_fields[i].id != NULL; i++) {
2909 if (!strcmp (sort_name, _(panel_fields[i].title)) && panel_fields[i].sort_routine) {
2910 col_sort_format = &panel_fields[i];
2911 break;
2915 if (!col_sort_format)
2916 return;
2918 if (panel->current_sort_field == col_sort_format) {
2919 /* reverse the sort if clicked column is already the sorted column */
2920 panel->reverse = !panel->reverse;
2921 } else {
2922 /* new sort is forced to be ascending */
2923 panel->reverse = 0;
2925 panel_set_sort_order (panel, col_sort_format);
2930 * Mouse callback of the panel minus repainting.
2931 * If the event is redirected to the menu, *redir is set to 1.
2933 static int
2934 do_panel_event (Gpm_Event *event, WPanel *panel, int *redir)
2936 const int lines = llines (panel);
2937 const gboolean is_active = dlg_widget_active (panel);
2938 const gboolean mouse_down = (event->type & GPM_DOWN) != 0;
2940 /* "." button show/hide hidden files */
2941 if (event->type & GPM_DOWN && event->x == panel->widget.cols - 3 && event->y == 1) {
2942 toggle_show_hidden();
2943 return MOU_NORMAL;
2946 /* sort on clicked column */
2947 if (event->type & GPM_DOWN && event->y == 2) {
2948 mouse_sort_col(event,panel);
2949 return MOU_NORMAL;
2952 /* rest of the upper frame, the menu is invisible - call menu */
2953 if (mouse_down && event->y == 1 && !menubar_visible) {
2954 *redir = 1;
2955 event->x += panel->widget.x;
2956 return (*(the_menubar->widget.mouse)) (event, the_menubar);
2959 /* "<" button */
2960 if (mouse_down && event->y == 1 && event->x == 2) {
2961 directory_history_prev (panel);
2962 return MOU_NORMAL;
2965 /* ">" button */
2966 if (mouse_down && event->y == 1 && event->x == panel->widget.cols - 1) {
2967 directory_history_next (panel);
2968 return MOU_NORMAL;
2971 /* "v" button */
2972 if (mouse_down && event->y == 1 && event->x == panel->widget.cols - 2) {
2973 directory_history_list (panel);
2974 return MOU_NORMAL;
2977 /* rest of the upper frame, the menu is invisible - call menu */
2978 if (mouse_down && event->y == 1 && !menubar_visible) {
2979 *redir = 1;
2980 event->x += panel->widget.x;
2981 return the_menubar->widget.mouse (event, the_menubar);
2984 /* Mouse wheel events */
2985 if (mouse_down && (event->buttons & GPM_B_UP)) {
2986 if (is_active) {
2987 if (panel->top_file > 0)
2988 prev_page (panel);
2989 else /* We are in first page */
2990 move_up (panel);
2992 return MOU_NORMAL;
2995 if (mouse_down && (event->buttons & GPM_B_DOWN)) {
2996 if (is_active) {
2997 if (panel->top_file + ITEMS (panel) < panel->count)
2998 next_page (panel);
2999 else /* We are in last page */
3000 move_down (panel);
3002 return MOU_NORMAL;
3005 event->y -= 2;
3006 if ((event->type & (GPM_DOWN | GPM_DRAG))) {
3007 int my_index;
3009 if (!is_active)
3010 change_panel ();
3012 if (event->y <= 0) {
3013 mark_if_marking (panel, event);
3014 if (mouse_move_pages)
3015 prev_page (panel);
3016 else
3017 move_up (panel);
3018 return MOU_REPEAT;
3021 if (!((panel->top_file + event->y <= panel->count) && event->y <= lines)) {
3022 mark_if_marking (panel, event);
3023 if (mouse_move_pages)
3024 next_page (panel);
3025 else
3026 move_down (panel);
3027 return MOU_REPEAT;
3030 my_index = panel->top_file + event->y - 1;
3031 if (panel->split && (event->x > ((panel->widget.cols - 2) / 2)))
3032 my_index += llines (panel);
3034 if (my_index >= panel->count)
3035 my_index = panel->count - 1;
3037 if (my_index != panel->selected) {
3038 unselect_item (panel);
3039 panel->selected = my_index;
3040 select_item (panel);
3043 /* This one is new */
3044 mark_if_marking (panel, event);
3045 } else if ((event->type & (GPM_UP | GPM_DOUBLE)) == (GPM_UP | GPM_DOUBLE)) {
3046 if (event->y > 0 && event->y <= lines)
3047 do_enter (panel);
3049 return MOU_NORMAL;
3052 /* Mouse callback of the panel */
3053 static int
3054 panel_event (Gpm_Event *event, void *data)
3056 WPanel *panel = data;
3057 int ret;
3058 int redir = MOU_NORMAL;
3060 ret = do_panel_event (event, panel, &redir);
3061 if (!redir)
3062 paint_panel (panel);
3064 return ret;
3067 void
3068 panel_re_sort (WPanel *panel)
3070 char *filename;
3071 int i;
3073 if (panel == NULL)
3074 return;
3076 filename = g_strdup (selection (panel)->fname);
3077 unselect_item (panel);
3078 do_sort (&panel->dir, panel->current_sort_field->sort_routine, panel->count-1, panel->reverse,
3079 panel->case_sensitive, panel->exec_first);
3080 panel->selected = -1;
3081 for (i = panel->count; i; i--){
3082 if (!strcmp (panel->dir.list [i-1].fname, filename)){
3083 panel->selected = i-1;
3084 break;
3087 g_free (filename);
3088 panel->top_file = panel->selected - ITEMS (panel)/2;
3089 if (panel->top_file < 0)
3090 panel->top_file = 0;
3091 select_item (panel);
3092 panel->dirty = 1;
3095 void
3096 panel_set_sort_order (WPanel *panel, const panel_field_t *sort_order)
3098 if (sort_order == 0)
3099 return;
3101 panel->current_sort_field = sort_order;
3103 /* The directory is already sorted, we have to load the unsorted stuff */
3104 if (sort_order->sort_routine == (sortfn *) unsorted){
3105 char *current_file;
3107 current_file = g_strdup (panel->dir.list [panel->selected].fname);
3108 panel_reload (panel);
3109 try_to_select (panel, current_file);
3110 g_free (current_file);
3112 panel_re_sort (panel);
3115 void
3116 set_panel_encoding (WPanel *panel)
3118 const char *encoding = NULL;
3119 char *cd_path;
3120 #ifdef HAVE_CHARSET
3121 const char *errmsg;
3122 int offset;
3123 int r;
3125 if (horizontal_split) {
3126 offset = (get_current_index () != 0) ? panel->widget.lines : -panel->widget.lines;
3127 r = select_charset (0, offset, source_codepage, FALSE);
3128 } else {
3129 offset = (panel->widget.cols == COLS) ? 0
3130 : (get_current_index () != 0) ? panel->widget.cols
3131 : -panel->widget.cols;
3132 r = select_charset (offset, 0, source_codepage, FALSE);
3135 if (r == SELECT_CHARSET_CANCEL)
3136 return; /* Cancel */
3138 if (r == SELECT_CHARSET_NO_TRANSLATE) {
3139 /* No translation */
3140 errmsg = init_translation_table (display_codepage, display_codepage);
3141 cd_path = remove_encoding_from_path (panel->cwd);
3142 do_panel_cd (panel, cd_path, 0);
3143 g_free (cd_path);
3144 return;
3147 source_codepage = r;
3149 errmsg = init_translation_table (source_codepage, display_codepage);
3150 if (errmsg) {
3151 message (D_ERROR, MSG_ERROR, "%s", errmsg);
3152 return;
3155 encoding = get_codepage_id (source_codepage);
3156 #endif
3157 if (encoding != NULL) {
3158 cd_path = add_encoding_to_path (panel->cwd, encoding);
3159 if (!do_panel_cd (panel, cd_path, 0))
3160 message (D_ERROR, MSG_ERROR, _(" Cannot chdir to %s "), cd_path);
3161 g_free (cd_path);
3165 static void
3166 reload_panelized (WPanel *panel)
3168 int i, j;
3169 dir_list *list = &panel->dir;
3171 if (panel != current_panel)
3172 mc_chdir (panel->cwd);
3174 for (i = 0, j = 0; i < panel->count; i++) {
3175 if (list->list[i].f.marked) {
3176 /* Unmark the file in advance. In case the following mc_lstat
3177 * fails we are done, else we have to mark the file again
3178 * (Note: do_file_mark depends on a valid "list->list [i].buf").
3179 * IMO that's the best way to update the panel's summary status
3180 * -- Norbert
3182 do_file_mark (panel, i, 0);
3184 if (mc_lstat (list->list[i].fname, &list->list[i].st)) {
3185 g_free (list->list[i].fname);
3186 continue;
3188 if (list->list[i].f.marked)
3189 do_file_mark (panel, i, 1);
3190 if (j != i)
3191 list->list[j] = list->list[i];
3192 j++;
3194 if (j == 0)
3195 panel->count = set_zero_dir (list);
3196 else
3197 panel->count = j;
3199 if (panel != current_panel)
3200 mc_chdir (current_panel->cwd);
3203 static void
3204 update_one_panel_widget (WPanel *panel, int force_update,
3205 const char *current_file)
3207 int free_pointer;
3208 char *my_current_file = NULL;
3210 if (force_update & UP_RELOAD) {
3211 panel->is_panelized = 0;
3212 mc_setctl (panel->cwd, VFS_SETCTL_FLUSH, 0);
3213 memset (&(panel->dir_stat), 0, sizeof (panel->dir_stat));
3216 /* If current_file == -1 (an invalid pointer) then preserve selection */
3217 if (current_file == UP_KEEPSEL) {
3218 free_pointer = 1;
3219 my_current_file = g_strdup (panel->dir.list[panel->selected].fname);
3220 current_file = my_current_file;
3221 } else
3222 free_pointer = 0;
3224 if (panel->is_panelized)
3225 reload_panelized (panel);
3226 else
3227 panel_reload (panel);
3229 try_to_select (panel, current_file);
3230 panel->dirty = 1;
3232 if (free_pointer)
3233 g_free (my_current_file);
3236 static void
3237 update_one_panel (int which, int force_update, const char *current_file)
3239 WPanel *panel;
3241 if (get_display_type (which) != view_listing)
3242 return;
3244 panel = (WPanel *) get_panel_widget (which);
3245 update_one_panel_widget (panel, force_update, current_file);
3248 /* This routine reloads the directory in both panels. It tries to
3249 * select current_file in current_panel and other_file in other_panel.
3250 * If current_file == -1 then it automatically sets current_file and
3251 * other_file to the currently selected files in the panels.
3253 * if force_update has the UP_ONLY_CURRENT bit toggled on, then it
3254 * will not reload the other panel.
3256 void
3257 update_panels (int force_update, const char *current_file)
3259 int reload_other = !(force_update & UP_ONLY_CURRENT);
3260 WPanel *panel;
3262 update_one_panel (get_current_index (), force_update, current_file);
3263 if (reload_other)
3264 update_one_panel (get_other_index (), force_update, UP_KEEPSEL);
3266 if (get_current_type () == view_listing)
3267 panel = (WPanel *) get_panel_widget (get_current_index ());
3268 else
3269 panel = (WPanel *) get_panel_widget (get_other_index ());
3271 mc_chdir (panel->cwd);
3274 gsize
3275 panel_get_num_of_sortable_fields(void)
3277 gsize ret = 0, index;
3279 for(index=0; panel_fields[index].id != NULL; index ++)
3280 if (panel_fields[index].title_hotkey != NULL)
3281 ret++;
3282 return ret;
3286 const char **
3287 panel_get_sortable_fields(gsize *array_size)
3289 char **ret;
3290 gsize index, i;
3292 index = panel_get_num_of_sortable_fields();
3294 ret = g_new0 (char *, index + 1);
3295 if (ret == NULL)
3296 return NULL;
3298 if (array_size != NULL)
3299 *array_size = index;
3301 index=0;
3303 for(i=0; panel_fields[i].id != NULL; i ++)
3304 if (panel_fields[i].title_hotkey != NULL)
3305 ret[index++] = g_strdup(_(panel_fields[i].title_hotkey));
3306 return (const char**) ret;
3309 const panel_field_t *
3310 panel_get_field_by_id(const char *name)
3312 gsize index;
3313 for(index=0; panel_fields[index].id != NULL; index ++)
3314 if (
3315 panel_fields[index].id != NULL &&
3316 strcmp(name, panel_fields[index].id) == 0
3318 return &panel_fields[index];
3319 return NULL;
3322 const panel_field_t *
3323 panel_get_field_by_title_hotkey(const char *name)
3325 gsize index;
3326 for(index=0; panel_fields[index].id != NULL; index ++)
3327 if (
3328 panel_fields[index].title_hotkey != NULL &&
3329 strcmp(name, _(panel_fields[index].title_hotkey)) == 0
3331 return &panel_fields[index];
3332 return NULL;
3335 const panel_field_t *
3336 panel_get_field_by_title(const char *name)
3338 gsize index;
3339 for(index=0; panel_fields[index].id != NULL; index ++)
3340 if (
3341 panel_fields[index].title_hotkey != NULL &&
3342 strcmp(name, _(panel_fields[index].title)) == 0
3344 return &panel_fields[index];
3345 return NULL;
3348 gsize
3349 panel_get_num_of_user_possible_fields(void)
3351 gsize ret = 0, index;
3353 for(index=0; panel_fields[index].id != NULL; index ++)
3354 if (panel_fields[index].use_in_user_format)
3355 ret++;
3356 return ret;
3359 const char **
3360 panel_get_user_possible_fields(gsize *array_size)
3362 char **ret;
3363 gsize index, i;
3365 index = panel_get_num_of_user_possible_fields();
3367 ret = g_new0 (char *, index + 1);
3368 if (ret == NULL)
3369 return NULL;
3371 if (array_size != NULL)
3372 *array_size = index;
3374 index=0;
3376 for(i=0; panel_fields[i].id != NULL; i ++)
3377 if (panel_fields[i].use_in_user_format)
3378 ret[index++] = g_strdup(_(panel_fields[i].title_hotkey));
3379 return (const char**) ret;
3382 void
3383 panel_init(void)
3385 panel_sort_up_sign = mc_config_get_string(mc_skin__default.config,"widget-common","sort-sign-up","'");
3386 panel_sort_down_sign = mc_config_get_string(mc_skin__default.config,"widget-common","sort-sign-down",",");
3389 void
3390 panel_deinit(void)
3392 g_free(panel_sort_up_sign);
3393 g_free(panel_sort_down_sign);