Added handlers for panel sort keybind actions.
[midnight-commander.git] / src / screen.c
blob3d3923030d73f46fb8f05910b0ccecbe8bc167a3
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 "tree.h"
45 #include "ext.h" /* regexp_command */
46 #include "layout.h" /* Most layout variables are here */
47 #include "wtools.h" /* for message (...) */
48 #include "cmd.h"
49 #include "command.h" /* cmdline */
50 #include "setup.h" /* For loading/saving panel options */
51 #include "user.h"
52 #include "../src/mcconfig/mcconfig.h"
53 #include "execute.h"
54 #include "widget.h"
55 #include "menu.h" /* menubar_visible */
56 #include "main-widgets.h"
57 #include "main.h"
58 #include "unixcompat.h"
59 #include "mountlist.h" /* my_statfs */
60 #include "selcodepage.h" /* select_charset () */
61 #include "charsets.h" /* get_codepage_id () */
62 #include "cmddef.h" /* CK_ cmd name const */
63 #include "keybind.h" /* global_key_map_t */
65 #define ELEMENTS(arr) ( sizeof(arr) / sizeof((arr)[0]) )
67 #define NORMAL 0
68 #define SELECTED 1
69 #define MARKED 2
70 #define MARKED_SELECTED 3
71 #define STATUS 5
74 * This describes a format item. The parse_display_format routine parses
75 * the user specified format and creates a linked list of format_e structures.
77 typedef struct format_e {
78 struct format_e *next;
79 int requested_field_len;
80 int field_len;
81 align_crt_t just_mode;
82 int expand;
83 const char *(*string_fn)(file_entry *, int len);
84 const char *title;
85 const char *id;
86 } format_e;
88 /* If true, show the mini-info on the panel */
89 int show_mini_info = 1;
91 /* If true, then use stat() on the cwd to determine directory changes */
92 int fast_reload = 0;
94 /* If true, use some usability hacks by Torben */
95 int torben_fj_mode = 0;
97 /* If true, up/down keys scroll the pane listing by pages */
98 int panel_scroll_pages = 1;
100 /* If 1, we use permission hilighting */
101 int permission_mode = 0;
103 /* If 1 - then add per file type hilighting */
104 int filetype_mode = 1;
106 /* The hook list for the select file function */
107 Hook *select_file_hook = 0;
109 const global_key_map_t *panel_map;
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 /* This macro extracts the number of available lines in a panel */
118 #define llines(p) (p->widget.lines-3 - (show_mini_info ? 2 : 0))
120 static void
121 set_colors (WPanel *panel)
123 (void) panel;
124 tty_set_normal_attrs ();
125 tty_setcolor (NORMAL_COLOR);
128 /* Delete format string, it is a linked list */
129 static void
130 delete_format (format_e *format)
132 format_e *next;
134 while (format){
135 next = format->next;
136 g_free (format);
137 format = next;
141 /* This code relies on the default justification!!! */
142 static void
143 add_permission_string (char *dest, int width, file_entry *fe, int attr, int color, int is_octal)
145 int i, r, l;
147 l = get_user_permissions (&fe->st);
149 if (is_octal){
150 /* Place of the access bit in octal mode */
151 l = width + l - 3;
152 r = l + 1;
153 } else {
154 /* The same to the triplet in string mode */
155 l = l * 3 + 1;
156 r = l + 3;
159 for(i = 0; i < width; i++){
160 if (i >= l && i < r){
161 if (attr == SELECTED || attr == MARKED_SELECTED)
162 tty_setcolor (MARKED_SELECTED_COLOR);
163 else
164 tty_setcolor (MARKED_COLOR);
165 } else if (color >= 0)
166 tty_setcolor (color);
167 else
168 tty_lowlevel_setcolor (-color);
170 tty_print_char (dest[i]);
174 /* String representations of various file attributes */
175 /* name */
176 static const char *
177 string_file_name (file_entry *fe, int len)
179 static char buffer [MC_MAXPATHLEN * MB_LEN_MAX + 1];
181 (void) len;
182 g_strlcpy (buffer, fe->fname, sizeof(buffer));
183 return buffer;
186 static unsigned int ilog10(dev_t n)
188 unsigned int digits = 0;
189 do {
190 digits++, n /= 10;
191 } while (n != 0);
192 return digits;
195 static void format_device_number (char *buf, size_t bufsize, dev_t dev)
197 dev_t major_dev = major(dev);
198 dev_t minor_dev = minor(dev);
199 unsigned int major_digits = ilog10(major_dev);
200 unsigned int minor_digits = ilog10(minor_dev);
202 g_assert(bufsize >= 1);
203 if (major_digits + 1 + minor_digits + 1 <= bufsize) {
204 g_snprintf(buf, bufsize, "%lu,%lu", (unsigned long) major_dev,
205 (unsigned long) minor_dev);
206 } else {
207 g_strlcpy(buf, _("[dev]"), bufsize);
211 /* size */
212 static const char *
213 string_file_size (file_entry *fe, int len)
215 static char buffer [BUF_TINY];
217 /* Don't ever show size of ".." since we don't calculate it */
218 if (!strcmp (fe->fname, "..")) {
219 return _("UP--DIR");
222 #ifdef HAVE_STRUCT_STAT_ST_RDEV
223 if (S_ISBLK (fe->st.st_mode) || S_ISCHR (fe->st.st_mode))
224 format_device_number (buffer, len + 1, fe->st.st_rdev);
225 else
226 #endif
228 size_trunc_len (buffer, (unsigned int) len, fe->st.st_size, 0);
230 return buffer;
233 /* bsize */
234 static const char *
235 string_file_size_brief (file_entry *fe, int len)
237 if (S_ISLNK (fe->st.st_mode) && !fe->f.link_to_dir) {
238 return _("SYMLINK");
241 if ((S_ISDIR (fe->st.st_mode) || fe->f.link_to_dir) && strcmp (fe->fname, "..")) {
242 return _("SUB-DIR");
245 return string_file_size (fe, len);
248 /* This functions return a string representation of a file entry */
249 /* type */
250 static const char *
251 string_file_type (file_entry *fe, int len)
253 static char buffer[2];
255 (void) len;
256 if (S_ISDIR (fe->st.st_mode))
257 buffer[0] = PATH_SEP;
258 else if (S_ISLNK (fe->st.st_mode)) {
259 if (fe->f.link_to_dir)
260 buffer[0] = '~';
261 else if (fe->f.stale_link)
262 buffer[0] = '!';
263 else
264 buffer[0] = '@';
265 } else if (S_ISCHR (fe->st.st_mode))
266 buffer[0] = '-';
267 else if (S_ISSOCK (fe->st.st_mode))
268 buffer[0] = '=';
269 else if (S_ISDOOR (fe->st.st_mode))
270 buffer[0] = '>';
271 else if (S_ISBLK (fe->st.st_mode))
272 buffer[0] = '+';
273 else if (S_ISFIFO (fe->st.st_mode))
274 buffer[0] = '|';
275 else if (S_ISNAM (fe->st.st_mode))
276 buffer[0] = '#';
277 else if (!S_ISREG (fe->st.st_mode))
278 buffer[0] = '?'; /* non-regular of unknown kind */
279 else if (is_exe (fe->st.st_mode))
280 buffer[0] = '*';
281 else
282 buffer[0] = ' ';
283 buffer[1] = '\0';
284 return buffer;
287 /* mtime */
288 static const char *
289 string_file_mtime (file_entry *fe, int len)
291 (void) len;
292 if (!strcmp (fe->fname, "..")) {
293 return "";
295 return file_date (fe->st.st_mtime);
298 /* atime */
299 static const char *
300 string_file_atime (file_entry *fe, int len)
302 (void) len;
303 if (!strcmp (fe->fname, "..")) {
304 return "";
306 return file_date (fe->st.st_atime);
309 /* ctime */
310 static const char *
311 string_file_ctime (file_entry *fe, int len)
313 (void) len;
314 if (!strcmp (fe->fname, "..")) {
315 return "";
317 return file_date (fe->st.st_ctime);
320 /* perm */
321 static const char *
322 string_file_permission (file_entry *fe, int len)
324 (void) len;
325 return string_perm (fe->st.st_mode);
328 /* mode */
329 static const char *
330 string_file_perm_octal (file_entry *fe, int len)
332 static char buffer [10];
334 (void) len;
335 g_snprintf (buffer, sizeof (buffer), "0%06lo", (unsigned long) fe->st.st_mode);
336 return buffer;
339 /* nlink */
340 static const char *
341 string_file_nlinks (file_entry *fe, int len)
343 static char buffer[BUF_TINY];
345 (void) len;
346 g_snprintf (buffer, sizeof (buffer), "%16d", (int) fe->st.st_nlink);
347 return buffer;
350 /* inode */
351 static const char *
352 string_inode (file_entry *fe, int len)
354 static char buffer [10];
356 (void) len;
357 g_snprintf (buffer, sizeof (buffer), "%lu",
358 (unsigned long) fe->st.st_ino);
359 return buffer;
362 /* nuid */
363 static const char *
364 string_file_nuid (file_entry *fe, int len)
366 static char buffer [10];
368 (void) len;
369 g_snprintf (buffer, sizeof (buffer), "%lu",
370 (unsigned long) fe->st.st_uid);
371 return buffer;
374 /* ngid */
375 static const char *
376 string_file_ngid (file_entry *fe, int len)
378 static char buffer [10];
380 (void) len;
381 g_snprintf (buffer, sizeof (buffer), "%lu",
382 (unsigned long) fe->st.st_gid);
383 return buffer;
386 /* owner */
387 static const char *
388 string_file_owner (file_entry *fe, int len)
390 (void) len;
391 return get_owner (fe->st.st_uid);
394 /* group */
395 static const char *
396 string_file_group (file_entry *fe, int len)
398 (void) len;
399 return get_group (fe->st.st_gid);
402 /* mark */
403 static const char *
404 string_marked (file_entry *fe, int len)
406 (void) len;
407 return fe->f.marked ? "*" : " ";
410 /* space */
411 static const char *
412 string_space (file_entry *fe, int len)
414 (void) fe;
415 (void) len;
416 return " ";
419 /* dot */
420 static const char *
421 string_dot (file_entry *fe, int len)
423 (void) fe;
424 (void) len;
425 return ".";
428 #define GT 1
430 panel_field_t panel_fields [] = {
432 "unsorted", 12, 1, J_LEFT_FIT,
433 N_("Unsorted"), N_("&Unsorted"), 0, FALSE,
434 string_file_name,
435 (sortfn *) unsorted
438 "name", 12, 1, J_LEFT_FIT,
439 N_("Name"), N_("&Name"), 1, TRUE,
440 string_file_name,
441 (sortfn *) sort_name
444 "extension", 12, 1, J_LEFT_FIT,
445 N_("Extension"), N_("&Extension"), 1, FALSE,
446 string_file_name, /* TODO: string_file_ext*/
447 (sortfn *) sort_ext
450 "size", 7, 0, J_RIGHT,
451 N_("Size"), N_("&Size"), 1, TRUE,
452 string_file_size,
453 (sortfn *) sort_size
456 "bsize", 7, 0, J_RIGHT,
457 N_("Block Size"), NULL, 1, FALSE,
458 string_file_size_brief,
459 (sortfn *) sort_size
462 "type", GT, 0, J_LEFT,
463 "", NULL, 2, TRUE,
464 string_file_type,
465 NULL
468 "mtime", 12, 0, J_RIGHT,
469 N_("MTime"), N_("&Modify time"), 1, TRUE,
470 string_file_mtime,
471 (sortfn *) sort_time
474 "atime", 12, 0, J_RIGHT,
475 N_("ATime"), N_("&Access time"), 1, TRUE,
476 string_file_atime,
477 (sortfn *) sort_atime
480 "ctime", 12, 0, J_RIGHT,
481 N_("CTime"), N_("C&Hange time"), 1, TRUE,
482 string_file_ctime,
483 (sortfn *) sort_ctime
486 "perm", 10, 0, J_LEFT,
487 N_("Permission"), NULL, 1, TRUE,
488 string_file_permission,
489 NULL
492 "mode", 6, 0, J_RIGHT,
493 N_("Perm"), NULL, 1, TRUE,
494 string_file_perm_octal,
495 NULL
498 "nlink", 2, 0, J_RIGHT,
499 N_("Nl"), NULL, 1, TRUE,
500 string_file_nlinks, NULL
503 "inode", 5, 0, J_RIGHT,
504 N_("Inode"), N_("&Inode"), 1, TRUE,
505 string_inode,
506 (sortfn *) sort_inode
509 "nuid", 5, 0, J_RIGHT,
510 N_("UID"), NULL, 1, FALSE,
511 string_file_nuid,
512 NULL
515 "ngid", 5, 0, J_RIGHT,
516 N_("GID"), NULL, 1, FALSE,
517 string_file_ngid,
518 NULL
521 "owner", 8, 0, J_LEFT_FIT,
522 N_("Owner"), NULL, 1, TRUE,
523 string_file_owner,
524 NULL
527 "group", 8,0, J_LEFT_FIT,
528 N_("Group"), NULL, 1, TRUE,
529 string_file_group,
530 NULL
533 "mark", 1, 0, J_RIGHT,
534 " ", NULL, 1, TRUE,
535 string_marked,
536 NULL
539 "|", 1, 0, J_RIGHT,
540 " ", NULL, 0, TRUE,
541 NULL,
542 NULL
545 "space", 1, 0, J_RIGHT,
546 " ", NULL, 0, TRUE,
547 string_space,
548 NULL
551 "dot", 1, 0, J_RIGHT,
552 " ", NULL, 0, FALSE,
553 string_dot,
554 NULL
557 NULL, 0, 0, J_RIGHT, NULL, NULL, 0, FALSE, NULL, NULL
561 static int
562 file_compute_color (int attr, file_entry *fe)
564 switch (attr) {
565 case SELECTED:
566 return (SELECTED_COLOR);
567 case MARKED:
568 return (MARKED_COLOR);
569 case MARKED_SELECTED:
570 return (MARKED_SELECTED_COLOR);
571 case STATUS:
572 return (NORMAL_COLOR);
573 case NORMAL:
574 default:
575 if (!filetype_mode)
576 return (NORMAL_COLOR);
579 return mc_fhl_get_color (mc_filehighlight, fe);
582 /* Formats the file number file_index of panel in the buffer dest */
583 static void
584 format_file (char *dest, int limit, WPanel *panel, int file_index, int width, int attr, int isstatus)
586 int color, length, empty_line;
587 const char *txt;
588 format_e *format, *home;
589 file_entry *fe;
591 (void) dest;
592 (void) limit;
593 length = 0;
594 empty_line = (file_index >= panel->count);
595 home = (isstatus) ? panel->status_format : panel->format;
596 fe = &panel->dir.list [file_index];
598 if (!empty_line)
599 color = file_compute_color (attr, fe);
600 else
601 color = NORMAL_COLOR;
603 for (format = home; format; format = format->next){
604 if (length == width)
605 break;
607 if (format->string_fn){
608 int len, perm;
609 char *preperad_text;
611 if (empty_line)
612 txt = " ";
613 else
614 txt = (*format->string_fn)(fe, format->field_len);
616 len = format->field_len;
617 if (len + length > width)
618 len = width - length;
619 if (len <= 0)
620 break;
622 perm = 0;
623 if (permission_mode) {
624 if (!strcmp(format->id, "perm"))
625 perm = 1;
626 else if (!strcmp(format->id, "mode"))
627 perm = 2;
630 if (color >= 0)
631 tty_setcolor (color);
632 else
633 tty_lowlevel_setcolor (-color);
635 preperad_text = (char*) str_fit_to_term(txt, len, format->just_mode);
636 if (perm)
637 add_permission_string (preperad_text, format->field_len, fe,
638 attr, color, perm - 1);
639 else
640 tty_print_string (preperad_text);
642 length+= len;
643 } else {
644 if (attr == SELECTED || attr == MARKED_SELECTED)
645 tty_setcolor (SELECTED_COLOR);
646 else
647 tty_setcolor (NORMAL_COLOR);
648 tty_print_one_vline ();
649 length++;
653 if (length < width)
654 tty_draw_hline (-1, -1, ' ', width - length);
657 static void
658 repaint_file (WPanel *panel, int file_index, int mv, int attr, int isstatus)
660 int second_column = 0;
661 int width;
662 int offset = 0;
663 char buffer [BUF_MEDIUM];
665 gboolean panel_is_split = !isstatus && panel->split;
667 width = panel->widget.cols - 2;
669 if (panel_is_split) {
670 second_column = (file_index - panel->top_file) / llines (panel);
671 width = width/2 - 1;
673 if (second_column != 0) {
674 offset = 1 + width;
675 /*width = (panel->widget.cols-2) - (panel->widget.cols-2)/2 - 1;*/
676 width = panel->widget.cols - offset - 2;
680 /* Nothing to paint */
681 if (width <= 0)
682 return;
684 if (mv){
685 if (panel_is_split)
686 widget_move (&panel->widget,
687 (file_index - panel->top_file) % llines (panel) + 2,
688 offset + 1);
689 else
690 widget_move (&panel->widget, file_index - panel->top_file + 2, 1);
693 format_file (buffer, sizeof(buffer), panel, file_index, width, attr, isstatus);
695 if (panel_is_split) {
696 if (second_column)
697 tty_print_char (' ');
698 else {
699 tty_setcolor (NORMAL_COLOR);
700 tty_print_one_vline ();
705 static void
706 display_mini_info (WPanel *panel)
708 widget_move (&panel->widget, llines (panel)+3, 1);
710 if (panel->searching){
711 tty_setcolor (INPUT_COLOR);
712 tty_print_char ('/');
713 tty_print_string (str_fit_to_term (panel->search_buffer,
714 panel->widget.cols - 3, J_LEFT));
715 return;
718 /* Status resolves links and show them */
719 set_colors (panel);
721 if (S_ISLNK (panel->dir.list [panel->selected].st.st_mode)){
722 char *link, link_target [MC_MAXPATHLEN];
723 int len;
725 link = concat_dir_and_file (panel->cwd, panel->dir.list [panel->selected].fname);
726 len = mc_readlink (link, link_target, MC_MAXPATHLEN - 1);
727 g_free (link);
728 if (len > 0){
729 link_target[len] = 0;
730 tty_print_string ("-> ");
731 tty_print_string (str_fit_to_term (link_target, panel->widget.cols - 5,
732 J_LEFT_FIT));
733 } else
734 tty_print_string (str_fit_to_term (_("<readlink failed>"),
735 panel->widget.cols - 2, J_LEFT));
736 } else if (strcmp (panel->dir.list [panel->selected].fname, "..") == 0) {
737 /* FIXME:
738 * while loading directory (do_load_dir() and do_reload_dir()),
739 * the actual stat info about ".." directory isn't got;
740 * so just don't display incorrect info about ".." directory */
741 tty_print_string (str_fit_to_term (_("UP--DIR"), panel->widget.cols - 2, J_LEFT));
742 } else
743 /* Default behavior */
744 repaint_file (panel, panel->selected, 0, STATUS, 1);
747 static void
748 paint_dir (WPanel *panel)
750 int i;
751 int color; /* Color value of the line */
752 int items; /* Number of items */
754 items = llines (panel) * (panel->split ? 2 : 1);
756 for (i = 0; i < items; i++){
757 if (i+panel->top_file >= panel->count)
758 color = 0;
759 else {
760 color = 2 * (panel->dir.list [i+panel->top_file].f.marked);
761 color += (panel->selected==i+panel->top_file && panel->active);
763 repaint_file (panel, i+panel->top_file, 1, color, 0);
765 tty_set_normal_attrs ();
768 static void
769 display_total_marked_size (WPanel *panel, int y, int x, gboolean size_only)
771 char buffer[BUF_SMALL], b_bytes[BUF_SMALL], *buf;
772 int cols;
774 if (panel->marked <= 0)
775 return;
777 buf = size_only ? b_bytes : buffer;
778 cols = panel->widget.cols - 2;
781 * This is a trick to use two ngettext() calls in one sentence.
782 * First make "N bytes", then insert it into "X in M files".
784 g_snprintf (b_bytes, sizeof (b_bytes),
785 ngettext("%s byte", "%s bytes", (unsigned long) panel->total),
786 size_trunc_sep (panel->total));
787 if (!size_only)
788 g_snprintf (buffer, sizeof (buffer),
789 ngettext("%s in %d file", "%s in %d files", panel->marked),
790 b_bytes, panel->marked);
792 /* don't forget spaces around buffer content */
793 buf = (char *) str_trunc (buf, cols - 4);
795 if (x < 0)
796 /* center in panel */
797 x = (panel->widget.cols - str_term_width1 (buf)) / 2 - 1;
800 * y == llines (panel) + 2 for mini_info_separator
801 * y == panel->widget.lines - 1 for panel bottom frame
803 widget_move (&panel->widget, y, x);
804 tty_setcolor (MARKED_COLOR);
805 tty_printf (" %s ", buf);
808 static void
809 mini_info_separator (WPanel *panel)
811 const int y = llines (panel) + 2;
813 tty_setcolor (NORMAL_COLOR);
814 tty_draw_hline (panel->widget.y + y, panel->widget.x + 1,
815 ACS_HLINE, panel->widget.cols - 2);
816 /* Status displays total marked size.
817 * Centered in panel, full format. */
818 display_total_marked_size (panel, y, -1, FALSE);
821 static void
822 show_free_space (WPanel *panel)
824 /* Used to figure out how many free space we have */
825 static struct my_statfs myfs_stats;
826 /* Old current working directory for displaying free space */
827 static char *old_cwd = NULL;
829 /* Don't try to stat non-local fs */
830 if (!vfs_file_is_local (panel->cwd) || !free_space)
831 return;
833 if (old_cwd == NULL || strcmp (old_cwd, panel->cwd) != 0) {
834 char rpath[PATH_MAX];
836 init_my_statfs ();
837 g_free (old_cwd);
838 old_cwd = g_strdup (panel->cwd);
840 if (mc_realpath (panel->cwd, rpath) == NULL)
841 return;
843 my_statfs (&myfs_stats, rpath);
846 if (myfs_stats.avail > 0 || myfs_stats.total > 0) {
847 char buffer1[6], buffer2[6], tmp[BUF_SMALL];
848 size_trunc_len (buffer1, sizeof(buffer1) - 1, myfs_stats.avail, 1);
849 size_trunc_len (buffer2, sizeof(buffer2) - 1, myfs_stats.total, 1);
850 g_snprintf (tmp, sizeof(tmp), " %s/%s (%d%%) ", buffer1, buffer2,
851 myfs_stats.total > 0 ?
852 (int)(100 * (double)myfs_stats.avail / myfs_stats.total) : 0);
853 widget_move (&panel->widget, panel->widget.lines - 1,
854 panel->widget.cols - 2 - (int) strlen (tmp));
855 tty_setcolor (NORMAL_COLOR);
856 tty_print_string (tmp);
860 static void
861 show_dir (WPanel *panel)
863 set_colors (panel);
864 draw_box (panel->widget.parent,
865 panel->widget.y, panel->widget.x,
866 panel->widget.lines, panel->widget.cols);
868 if (show_mini_info) {
869 widget_move (&panel->widget, llines (panel) + 2, 0);
870 tty_print_alt_char (ACS_LTEE);
871 widget_move (&panel->widget, llines (panel) + 2,
872 panel->widget.cols - 1);
873 tty_print_alt_char (ACS_RTEE);
876 if (panel->active)
877 tty_setcolor (REVERSE_COLOR);
879 widget_move (&panel->widget, 0, 3);
881 tty_printf (" %s ",
882 str_term_trim (strip_home_and_password (panel->cwd),
883 min (max (panel->widget.cols - 10, 0),
884 panel->widget.cols)));
886 widget_move (&panel->widget, 0, 1);
887 tty_print_char ('<');
888 widget_move (&panel->widget, 0, panel->widget.cols - 4);
889 tty_print_string (".v>");
891 if (!show_mini_info) {
892 if (panel->marked == 0) {
893 /* Show size of curret file in the bottom of panel */
894 if (S_ISREG (panel->dir.list [panel->selected].st.st_mode)) {
895 char buffer[BUF_SMALL];
897 g_snprintf (buffer, sizeof (buffer), " %s ",
898 size_trunc_sep (panel->dir.list [panel->selected].st.st_size));
899 tty_setcolor (NORMAL_COLOR);
900 widget_move (&panel->widget, panel->widget.lines - 1, 2);
901 tty_print_string (buffer);
903 } else {
904 /* Show total size of marked files
905 * In the bottom of panel, display size only. */
906 display_total_marked_size (panel, panel->widget.lines - 1, 2, TRUE);
910 show_free_space (panel);
912 if (panel->active)
913 tty_set_normal_attrs ();
916 /* To be used only by long_frame and full_frame to adjust top_file */
917 static void
918 adjust_top_file (WPanel *panel)
920 int old_top = panel->top_file;
922 if (panel->selected - old_top > llines (panel))
923 panel->top_file = panel->selected;
924 if (old_top - panel->count > llines (panel))
925 panel->top_file = panel->count - llines (panel);
928 /* Repaint everything, including frame and separator */
929 static void
930 paint_panel (WPanel *panel)
932 paint_frame (panel); /* including show_dir */
933 paint_dir (panel);
935 if (show_mini_info) {
936 mini_info_separator (panel);
937 display_mini_info (panel);
940 panel->dirty = 0;
943 /* add "#enc:encodning" to end of path */
944 /* if path end width a previous #enc:, only encoding is changed no additional
945 * #enc: is appended
946 * retun new string
948 static char
949 *add_encoding_to_path (const char *path, const char *encoding)
951 char *result;
952 char *semi;
953 char *slash;
955 semi = g_strrstr (path, "#enc:");
957 if (semi != NULL) {
958 slash = strchr (semi, PATH_SEP);
959 if (slash != NULL) {
960 result = g_strconcat (path, "/#enc:", encoding, NULL);
961 } else {
962 *semi = 0;
963 result = g_strconcat (path, "/#enc:", encoding, NULL);
964 *semi = '#';
966 } else {
967 result = g_strconcat (path, "/#enc:", encoding, NULL);
970 return result;
973 char *
974 remove_encoding_from_path (const char *path)
976 GString *ret;
977 GString *tmp_path, *tmp_conv;
978 char *tmp, *tmp2;
979 const char *enc;
980 GIConv converter;
982 ret = g_string_new("");
983 tmp_conv = g_string_new("");
985 tmp_path = g_string_new(path);
987 while ((tmp = g_strrstr (tmp_path->str, "/#enc:")) != NULL){
988 enc = vfs_get_encoding ((const char *) tmp);
989 converter = enc ? str_crt_conv_to (enc): str_cnv_to_term;
990 if (converter == INVALID_CONV) converter = str_cnv_to_term;
992 tmp2=tmp+1;
993 while (*tmp2 && *tmp2 != '/')
994 tmp2++;
996 if (*tmp2){
997 str_vfs_convert_from (converter, tmp2, tmp_conv);
998 g_string_prepend(ret, tmp_conv->str);
999 g_string_set_size(tmp_conv,0);
1001 g_string_set_size(tmp_path,tmp - tmp_path->str);
1002 str_close_conv (converter);
1004 g_string_prepend(ret, tmp_path->str);
1005 g_string_free(tmp_path,TRUE);
1006 g_string_free(tmp_conv,TRUE);
1008 tmp = ret->str;
1009 g_string_free(ret, FALSE);
1010 return tmp;
1014 * Repaint the contents of the panels without frames. To schedule panel
1015 * for repainting, set panel->dirty to 1. There are many reasons why
1016 * the panels need to be repainted, and this is a costly operation, so
1017 * it's done once per event.
1019 void
1020 update_dirty_panels (void)
1022 if (current_panel->dirty)
1023 paint_panel (current_panel);
1025 if ((get_other_type () == view_listing) && other_panel->dirty)
1026 paint_panel (other_panel);
1029 static void
1030 do_select (WPanel *panel, int i)
1032 if (i != panel->selected) {
1033 panel->dirty = 1;
1034 panel->selected = i;
1035 panel->top_file = panel->selected - (panel->widget.lines - 2) / 2;
1036 if (panel->top_file < 0)
1037 panel->top_file = 0;
1041 static void
1042 do_try_to_select (WPanel *panel, const char *name)
1044 int i;
1045 char *subdir;
1047 if (!name) {
1048 do_select(panel, 0);
1049 return;
1052 /* We only want the last component of the directory,
1053 * and from this only the name without suffix. */
1054 subdir = vfs_strip_suffix_from_filename (x_basename(name));
1056 /* Search that subdirectory, if found select it */
1057 for (i = 0; i < panel->count; i++){
1058 if (strcmp (subdir, panel->dir.list [i].fname) == 0) {
1059 do_select (panel, i);
1060 g_free (subdir);
1061 return;
1065 /* Try to select a file near the file that is missing */
1066 if (panel->selected >= panel->count)
1067 do_select (panel, panel->count-1);
1068 g_free (subdir);
1071 void
1072 try_to_select (WPanel *panel, const char *name)
1074 do_try_to_select (panel, name);
1075 select_item (panel);
1078 void
1079 panel_update_cols (Widget *widget, int frame_size)
1081 int cols, origin;
1083 if (horizontal_split){
1084 widget->cols = COLS;
1085 return;
1088 if (frame_size == frame_full){
1089 cols = COLS;
1090 origin = 0;
1091 } else {
1092 if (widget == get_panel_widget (0)){
1093 cols = first_panel_size;
1094 origin = 0;
1095 } else {
1096 cols = COLS-first_panel_size;
1097 origin = first_panel_size;
1101 widget->cols = cols;
1102 widget->x = origin;
1105 static char *
1106 panel_save_name (WPanel *panel)
1108 extern int saving_setup;
1110 /* If the program is shuting down */
1111 if ((midnight_shutdown && auto_save_setup) || saving_setup)
1112 return g_strdup (panel->panel_name);
1113 else
1114 return g_strconcat ("Temporal:", panel->panel_name, (char *) NULL);
1117 void
1118 panel_clean_dir (WPanel *panel)
1120 int count = panel->count;
1122 panel->count = 0;
1123 panel->top_file = 0;
1124 panel->selected = 0;
1125 panel->marked = 0;
1126 panel->dirs_marked = 0;
1127 panel->total = 0;
1128 panel->searching = 0;
1129 panel->is_panelized = 0;
1130 panel->dirty = 1;
1132 clean_dir (&panel->dir, count);
1135 static void
1136 panel_destroy (WPanel *p)
1138 int i;
1140 char *name = panel_save_name (p);
1142 panel_save_setup (p, name);
1143 panel_clean_dir (p);
1145 /* save and clean history */
1146 if (p->dir_history) {
1147 history_put (p->hist_name, p->dir_history);
1149 p->dir_history = g_list_first (p->dir_history);
1150 g_list_foreach (p->dir_history, (GFunc) g_free, NULL);
1151 g_list_free (p->dir_history);
1154 g_free (p->hist_name);
1156 delete_format (p->format);
1157 delete_format (p->status_format);
1159 g_free (p->user_format);
1160 for (i = 0; i < LIST_TYPES; i++)
1161 g_free (p->user_status_format[i]);
1162 g_free (p->dir.list);
1163 g_free (p->panel_name);
1164 g_free (name);
1167 static void
1168 panel_format_modified (WPanel *panel)
1170 panel->format_modified = 1;
1173 /* Panel creation */
1174 /* The parameter specifies the name of the panel for setup retieving */
1175 WPanel *
1176 panel_new (const char *panel_name)
1178 return panel_new_with_dir(panel_name, NULL);
1181 /* Panel creation for specified directory */
1182 /* The parameter specifies the name of the panel for setup retieving */
1183 /* and the path of working panel directory. If path is NULL then */
1184 /* panel will be created for current directory */
1185 WPanel *
1186 panel_new_with_dir (const char *panel_name, const char *wpath)
1188 WPanel *panel;
1189 char *section;
1190 int i, err;
1191 char curdir[MC_MAXPATHLEN];
1193 panel = g_new0 (WPanel, 1);
1195 /* No know sizes of the panel at startup */
1196 init_widget (&panel->widget, 0, 0, 0, 0, panel_callback, panel_event);
1198 /* We do not want the cursor */
1199 widget_want_cursor (panel->widget, 0);
1201 if (wpath) {
1202 g_strlcpy(panel->cwd, wpath, sizeof (panel->cwd));
1203 mc_get_current_wd (curdir, sizeof (curdir) - 2);
1204 } else
1205 mc_get_current_wd (panel->cwd, sizeof (panel->cwd) - 2);
1207 strcpy (panel->lwd, ".");
1209 panel->hist_name = g_strconcat ("Dir Hist ", panel_name, (char *) NULL);
1210 panel->dir_history = history_get (panel->hist_name);
1211 directory_history_add (panel, panel->cwd);
1213 panel->dir.list = g_new (file_entry, MIN_FILES);
1214 panel->dir.size = MIN_FILES;
1215 panel->active = 0;
1216 panel->filter = 0;
1217 panel->split = 0;
1218 panel->top_file = 0;
1219 panel->selected = 0;
1220 panel->marked = 0;
1221 panel->total = 0;
1222 panel->reverse = 0;
1223 panel->dirty = 1;
1224 panel->searching = 0;
1225 panel->dirs_marked = 0;
1226 panel->is_panelized = 0;
1227 panel->format = 0;
1228 panel->status_format = 0;
1229 panel->format_modified = 1;
1231 panel->panel_name = g_strdup (panel_name);
1232 panel->user_format = g_strdup (DEFAULT_USER_FORMAT);
1234 for (i = 0; i < LIST_TYPES; i++)
1235 panel->user_status_format[i] = g_strdup (DEFAULT_USER_FORMAT);
1237 panel->search_buffer[0] = 0;
1238 panel->frame_size = frame_half;
1239 section = g_strconcat ("Temporal:", panel->panel_name, (char *) NULL);
1240 if (!mc_config_has_group (mc_main_config, section)) {
1241 g_free (section);
1242 section = g_strdup (panel->panel_name);
1244 panel_load_setup (panel, section);
1245 g_free (section);
1247 /* Load format strings */
1248 err = set_panel_formats (panel);
1249 if (err) {
1250 set_panel_formats (panel);
1254 /* Because do_load_dir lists files in current directory */
1255 if (wpath)
1256 mc_chdir(wpath);
1258 /* Load the default format */
1259 panel->count =
1260 do_load_dir (panel->cwd, &panel->dir, panel->current_sort_field->sort_routine,
1261 panel->reverse, panel->case_sensitive,
1262 panel->exec_first, panel->filter);
1264 /* Restore old right path */
1265 if (wpath)
1266 mc_chdir(curdir);
1268 return panel;
1271 void
1272 panel_reload (WPanel *panel)
1274 struct stat current_stat;
1276 if (fast_reload && !stat (panel->cwd, &current_stat)
1277 && current_stat.st_ctime == panel->dir_stat.st_ctime
1278 && current_stat.st_mtime == panel->dir_stat.st_mtime)
1279 return;
1281 while (mc_chdir (panel->cwd) == -1) {
1282 char *last_slash;
1284 if (panel->cwd[0] == PATH_SEP && panel->cwd[1] == 0) {
1285 panel_clean_dir (panel);
1286 panel->count = set_zero_dir (&panel->dir);
1287 return;
1289 last_slash = strrchr (panel->cwd, PATH_SEP);
1290 if (!last_slash || last_slash == panel->cwd)
1291 strcpy (panel->cwd, PATH_SEP_STR);
1292 else
1293 *last_slash = 0;
1294 memset (&(panel->dir_stat), 0, sizeof (panel->dir_stat));
1295 show_dir (panel);
1298 panel->count =
1299 do_reload_dir (panel->cwd, &panel->dir, panel->current_sort_field->sort_routine,
1300 panel->count, panel->reverse, panel->case_sensitive,
1301 panel->exec_first, panel->filter);
1303 panel->dirty = 1;
1304 if (panel->selected >= panel->count)
1305 do_select (panel, panel->count - 1);
1307 recalculate_panel_summary (panel);
1310 static void
1311 paint_frame (WPanel *panel)
1313 int side, width;
1314 char *txt = NULL;
1316 if (!panel->split)
1317 adjust_top_file (panel);
1319 widget_erase (&panel->widget);
1320 show_dir (panel);
1322 widget_move (&panel->widget, 1, 1);
1324 for (side = 0; side <= panel->split; side++){
1325 format_e *format;
1327 if (side){
1328 tty_setcolor (NORMAL_COLOR);
1329 tty_print_one_vline ();
1330 width = panel->widget.cols - panel->widget.cols/2 - 1;
1331 } else if (panel->split)
1332 width = panel->widget.cols/2 - 3;
1333 else
1334 width = panel->widget.cols - 2;
1336 for (format = panel->format; format; format = format->next){
1337 if (format->string_fn){
1338 if (panel->filter && !strcmp (format->id, "name")) {
1339 txt = g_strdup_printf ("%s [%s]", format->title, panel->filter);
1340 } else {
1341 txt = g_strdup (format->title);
1344 tty_setcolor (MARKED_COLOR);
1345 tty_print_string (str_fit_to_term (format->title, format->field_len,
1346 J_CENTER_LEFT));
1347 g_free(txt);
1348 width -= format->field_len;
1349 } else {
1350 tty_setcolor (NORMAL_COLOR);
1351 tty_print_one_vline ();
1352 width--;
1356 if (width > 0)
1357 tty_draw_hline (-1, -1, ' ', width);
1361 static const char *
1362 parse_panel_size (WPanel *panel, const char *format, int isstatus)
1364 int frame = frame_half;
1365 format = skip_separators (format);
1367 if (!strncmp (format, "full", 4)){
1368 frame = frame_full;
1369 format += 4;
1370 } else if (!strncmp (format, "half", 4)){
1371 frame = frame_half;
1372 format += 4;
1375 if (!isstatus){
1376 panel->frame_size = frame;
1377 panel->split = 0;
1380 /* Now, the optional column specifier */
1381 format = skip_separators (format);
1383 if (*format == '1' || *format == '2'){
1384 if (!isstatus)
1385 panel->split = *format == '2';
1386 format++;
1389 if (!isstatus)
1390 panel_update_cols (&(panel->widget), panel->frame_size);
1392 return skip_separators (format);
1395 /* Format is:
1397 all := panel_format? format
1398 panel_format := [full|half] [1|2]
1399 format := one_format_e
1400 | format , one_format_e
1402 one_format_e := just format.id [opt_size]
1403 just := [<=>]
1404 opt_size := : size [opt_expand]
1405 size := [0-9]+
1406 opt_expand := +
1410 static format_e *
1411 parse_display_format (WPanel *panel, const char *format, char **error, int isstatus, int *res_total_cols)
1413 format_e *darr, *old = 0, *home = 0; /* The formats we return */
1414 int total_cols = 0; /* Used columns by the format */
1415 int set_justify; /* flag: set justification mode? */
1416 align_crt_t justify = J_LEFT; /* Which mode. */
1417 int items = 0; /* Number of items in the format */
1418 size_t i;
1420 static size_t i18n_timelength = 0; /* flag: check ?Time length at startup */
1422 *error = 0;
1424 if (i18n_timelength == 0) {
1425 i18n_timelength = i18n_checktimelength (); /* Musn't be 0 */
1427 for (i = 0; panel_fields[i].id != NULL; i++)
1428 if (strcmp ("time", panel_fields[i].id + 1) == 0)
1429 panel_fields [i].min_size = i18n_timelength;
1433 * This makes sure that the panel and mini status full/half mode
1434 * setting is equal
1436 format = parse_panel_size (panel, format, isstatus);
1438 while (*format){ /* format can be an empty string */
1439 int found = 0;
1441 darr = g_new (format_e, 1);
1443 /* I'm so ugly, don't look at me :-) */
1444 if (!home)
1445 home = old = darr;
1447 old->next = darr;
1448 darr->next = 0;
1449 old = darr;
1451 format = skip_separators (format);
1453 if (strchr ("<=>", *format)){
1454 set_justify = 1;
1455 switch (*format)
1457 case '<':
1458 justify = J_LEFT;
1459 break;
1460 case '=':
1461 justify = J_CENTER;
1462 break;
1463 case '>':
1464 default:
1465 justify = J_RIGHT;
1466 break;
1468 format = skip_separators (format+1);
1469 } else
1470 set_justify = 0;
1472 for (i = 0; panel_fields[i].id != NULL; i++) {
1473 size_t klen = strlen (panel_fields [i].id);
1475 if (strncmp (format, panel_fields [i].id, klen) != 0)
1476 continue;
1478 format += klen;
1480 if (panel_fields [i].use_in_gui)
1481 items++;
1483 darr->requested_field_len = panel_fields [i].min_size;
1484 darr->string_fn = panel_fields [i].string_fn;
1485 if (panel_fields [i].title [0])
1486 darr->title = _(panel_fields [i].title);
1487 else
1488 darr->title = "";
1489 darr->id = panel_fields [i].id;
1490 darr->expand = panel_fields [i].expands;
1491 darr->just_mode = panel_fields [i].default_just;
1493 if (set_justify) {
1494 if (IS_FIT(darr->just_mode))
1495 darr->just_mode = MAKE_FIT(justify);
1496 else
1497 darr->just_mode = justify;
1499 found = 1;
1501 format = skip_separators (format);
1503 /* If we have a size specifier */
1504 if (*format == ':'){
1505 int req_length;
1507 /* If the size was specified, we don't want
1508 * auto-expansion by default
1510 darr->expand = 0;
1511 format++;
1512 req_length = atoi (format);
1513 darr->requested_field_len = req_length;
1515 format = skip_numbers (format);
1517 /* Now, if they insist on expansion */
1518 if (*format == '+'){
1519 darr->expand = 1;
1520 format++;
1525 break;
1527 if (!found){
1528 char *tmp_format = g_strdup (format);
1530 int pos = min (8, strlen (format));
1531 delete_format (home);
1532 tmp_format [pos] = 0;
1533 *error = g_strconcat (_("Unknown tag on display format: "), tmp_format, (char *) NULL);
1534 g_free (tmp_format);
1535 return 0;
1537 total_cols += darr->requested_field_len;
1540 *res_total_cols = total_cols;
1541 return home;
1544 static format_e *
1545 use_display_format (WPanel *panel, const char *format, char **error, int isstatus)
1547 #define MAX_EXPAND 4
1548 int expand_top = 0; /* Max used element in expand */
1549 int usable_columns; /* Usable columns in the panel */
1550 int total_cols = 0;
1551 int i;
1552 format_e *darr, *home;
1554 if (!format)
1555 format = DEFAULT_USER_FORMAT;
1557 home = parse_display_format (panel, format, error, isstatus, &total_cols);
1559 if (*error)
1560 return 0;
1562 panel->dirty = 1;
1564 /* Status needn't to be split */
1565 usable_columns = ((panel->widget.cols-2)/((isstatus)
1567 : (panel->split+1))) - (!isstatus && panel->split);
1569 /* Look for the expandable fields and set field_len based on the requested field len */
1570 for (darr = home; darr && expand_top < MAX_EXPAND; darr = darr->next){
1571 darr->field_len = darr->requested_field_len;
1572 if (darr->expand)
1573 expand_top++;
1576 /* If we used more columns than the available columns, adjust that */
1577 if (total_cols > usable_columns){
1578 int pdif, dif = total_cols - usable_columns;
1580 while (dif){
1581 pdif = dif;
1582 for (darr = home; darr; darr = darr->next){
1583 if (dif && darr->field_len - 1){
1584 darr->field_len--;
1585 dif--;
1589 /* avoid endless loop if num fields > 40 */
1590 if (pdif == dif)
1591 break;
1593 total_cols = usable_columns; /* give up, the rest should be truncated */
1596 /* Expand the available space */
1597 if ((usable_columns > total_cols) && expand_top){
1598 int spaces = (usable_columns - total_cols) / expand_top;
1599 int extra = (usable_columns - total_cols) % expand_top;
1601 for (i = 0, darr = home; darr && (i < expand_top); darr = darr->next)
1602 if (darr->expand){
1603 darr->field_len += (spaces + ((i == 0) ? extra : 0));
1604 i++;
1607 return home;
1610 /* Switches the panel to the mode specified in the format */
1611 /* Seting up both format and status string. Return: 0 - on success; */
1612 /* 1 - format error; 2 - status error; 3 - errors in both formats. */
1614 set_panel_formats (WPanel *p)
1616 format_e *form;
1617 char *err;
1618 int retcode = 0;
1620 form = use_display_format (p, panel_format (p), &err, 0);
1622 if (err){
1623 g_free (err);
1624 retcode = 1;
1626 else {
1627 if (p->format)
1628 delete_format (p->format);
1630 p->format = form;
1633 if (show_mini_info){
1635 form = use_display_format (p, mini_status_format (p), &err, 1);
1637 if (err){
1638 g_free (err);
1639 retcode += 2;
1641 else {
1642 if (p->status_format)
1643 delete_format (p->status_format);
1645 p->status_format = form;
1649 panel_format_modified (p);
1650 panel_update_cols (&(p->widget), p->frame_size);
1652 if (retcode)
1653 message (D_ERROR, _("Warning" ), _( "User supplied format looks invalid, reverting to default." ) );
1654 if (retcode & 0x01){
1655 g_free (p->user_format);
1656 p->user_format = g_strdup (DEFAULT_USER_FORMAT);
1658 if (retcode & 0x02){
1659 g_free (p->user_status_format [p->list_type]);
1660 p->user_status_format [p->list_type] = g_strdup (DEFAULT_USER_FORMAT);
1663 return retcode;
1666 /* Given the panel->view_type returns the format string to be parsed */
1667 static const char *
1668 panel_format (WPanel *panel)
1670 switch (panel->list_type){
1672 case list_long:
1673 return "full perm space nlink space owner space group space size space mtime space name";
1675 case list_brief:
1676 return "half 2 type name";
1678 case list_user:
1679 return panel->user_format;
1681 default:
1682 case list_full:
1683 return "half type name | size | mtime";
1687 static const char *
1688 mini_status_format (WPanel *panel)
1690 if (panel->user_mini_status)
1691 return panel->user_status_format [panel->list_type];
1693 switch (panel->list_type){
1695 case list_long:
1696 return "full perm space nlink space owner space group space size space mtime space name";
1698 case list_brief:
1699 return "half type name space bsize space perm space";
1701 case list_full:
1702 return "half type name";
1704 default:
1705 case list_user:
1706 return panel->user_format;
1710 /* */
1711 /* Panel operation commands */
1712 /* */
1714 /* Used to emulate Lynx's entering leaving a directory with the arrow keys */
1715 static cb_ret_t
1716 maybe_cd (int move_up_dir)
1718 if (navigate_with_arrows) {
1719 if (!cmdline->buffer[0]) {
1720 if (move_up_dir) {
1721 do_cd ("..", cd_exact);
1722 return MSG_HANDLED;
1724 if (S_ISDIR (selection (current_panel)->st.st_mode)
1725 || link_isdir (selection (current_panel))) {
1726 do_cd (selection (current_panel)->fname, cd_exact);
1727 return MSG_HANDLED;
1731 return MSG_NOT_HANDLED;
1734 /* Returns the number of items in the given panel */
1735 static int
1736 ITEMS (WPanel *p)
1738 if (p->split)
1739 return llines (p) * 2;
1740 else
1741 return llines (p);
1744 /* Select current item and readjust the panel */
1745 void
1746 select_item (WPanel *panel)
1748 int items = ITEMS (panel);
1750 /* Although currently all over the code we set the selection and
1751 top file to decent values before calling select_item, I could
1752 forget it someday, so it's better to do the actual fitting here */
1754 if (panel->top_file < 0)
1755 panel->top_file = 0;
1757 if (panel->selected < 0)
1758 panel->selected = 0;
1760 if (panel->selected > panel->count - 1)
1761 panel->selected = panel->count - 1;
1763 if (panel->top_file > panel->count - 1)
1764 panel->top_file = panel->count - 1;
1766 if ((panel->count - panel->top_file) < items) {
1767 panel->top_file = panel->count - items;
1768 if (panel->top_file < 0)
1769 panel->top_file = 0;
1772 if (panel->selected < panel->top_file)
1773 panel->top_file = panel->selected;
1775 if ((panel->selected - panel->top_file) >= items)
1776 panel->top_file = panel->selected - items + 1;
1778 panel->dirty = 1;
1780 execute_hooks (select_file_hook);
1783 /* Clears all files in the panel, used only when one file was marked */
1784 void
1785 unmark_files (WPanel *panel)
1787 int i;
1789 if (!panel->marked)
1790 return;
1791 for (i = 0; i < panel->count; i++)
1792 file_mark (panel, i, 0);
1794 panel->dirs_marked = 0;
1795 panel->marked = 0;
1796 panel->total = 0;
1799 static void
1800 unselect_item (WPanel *panel)
1802 repaint_file (panel, panel->selected, 1, 2*selection (panel)->f.marked, 0);
1805 static void
1806 move_down (WPanel *panel)
1808 if (panel->selected+1 == panel->count)
1809 return;
1811 unselect_item (panel);
1812 panel->selected++;
1813 if (panel->selected - panel->top_file == ITEMS (panel) &&
1814 panel_scroll_pages) {
1815 /* Scroll window half screen */
1816 panel->top_file += ITEMS (panel)/2;
1817 if (panel->top_file > panel->count - ITEMS (panel))
1818 panel->top_file = panel->count - ITEMS (panel);
1819 paint_dir (panel);
1821 select_item (panel);
1824 static void
1825 move_up (WPanel *panel)
1827 if (panel->selected == 0)
1828 return;
1830 unselect_item (panel);
1831 panel->selected--;
1832 if (panel->selected < panel->top_file && panel_scroll_pages) {
1833 /* Scroll window half screen */
1834 panel->top_file -= ITEMS (panel)/2;
1835 if (panel->top_file < 0)
1836 panel->top_file = 0;
1837 paint_dir (panel);
1839 select_item (panel);
1842 /* Changes the selection by lines (may be negative) */
1843 static void
1844 move_selection (WPanel *panel, int lines)
1846 int new_pos;
1847 int adjust = 0;
1849 new_pos = panel->selected + lines;
1850 if (new_pos >= panel->count)
1851 new_pos = panel->count-1;
1853 if (new_pos < 0)
1854 new_pos = 0;
1856 unselect_item (panel);
1857 panel->selected = new_pos;
1859 if (panel->selected - panel->top_file >= ITEMS (panel)){
1860 panel->top_file += lines;
1861 adjust = 1;
1864 if (panel->selected - panel->top_file < 0){
1865 panel->top_file += lines;
1866 adjust = 1;
1869 if (adjust){
1870 if (panel->top_file > panel->selected)
1871 panel->top_file = panel->selected;
1872 if (panel->top_file < 0)
1873 panel->top_file = 0;
1874 paint_dir (panel);
1876 select_item (panel);
1879 static cb_ret_t
1880 move_left (WPanel *panel)
1882 if (panel->split) {
1883 move_selection (panel, -llines (panel));
1884 return MSG_HANDLED;
1885 } else
1886 return maybe_cd (1); /* cd .. */
1889 static int
1890 move_right (WPanel *panel)
1892 if (panel->split) {
1893 move_selection (panel, llines (panel));
1894 return MSG_HANDLED;
1895 } else
1896 return maybe_cd (0); /* cd (selection) */
1899 static void
1900 prev_page (WPanel *panel)
1902 int items;
1904 if (!panel->selected && !panel->top_file)
1905 return;
1906 unselect_item (panel);
1907 items = ITEMS (panel);
1908 if (panel->top_file < items)
1909 items = panel->top_file;
1910 if (!items)
1911 panel->selected = 0;
1912 else
1913 panel->selected -= items;
1914 panel->top_file -= items;
1916 /* This keeps the selection in a reasonable place */
1917 if (panel->selected < 0)
1918 panel->selected = 0;
1919 if (panel->top_file < 0)
1920 panel->top_file = 0;
1921 select_item (panel);
1922 paint_dir (panel);
1925 static void
1926 ctrl_prev_page (WPanel *panel)
1928 (void) panel;
1929 do_cd ("..", cd_exact);
1932 static void
1933 next_page (WPanel *panel)
1935 int items;
1937 if (panel->selected == panel->count - 1)
1938 return;
1939 unselect_item (panel);
1940 items = ITEMS (panel);
1941 if (panel->top_file > panel->count - 2 * items)
1942 items = panel->count - items - panel->top_file;
1943 if (panel->top_file + items < 0)
1944 items = -panel->top_file;
1945 if (!items)
1946 panel->selected = panel->count - 1;
1947 else
1948 panel->selected += items;
1949 panel->top_file += items;
1951 /* This keeps the selection in it's relative position */
1952 if (panel->selected >= panel->count)
1953 panel->selected = panel->count - 1;
1954 if (panel->top_file >= panel->count)
1955 panel->top_file = panel->count - 1;
1956 select_item (panel);
1957 paint_dir (panel);
1960 static void
1961 ctrl_next_page (WPanel *panel)
1963 if ((S_ISDIR (selection (panel)->st.st_mode)
1964 || link_isdir (selection (panel)))) {
1965 do_cd (selection (panel)->fname, cd_exact);
1969 static void
1970 goto_top_file (WPanel *panel)
1972 unselect_item (panel);
1973 panel->selected = panel->top_file;
1974 select_item (panel);
1977 static void
1978 goto_middle_file (WPanel *panel)
1980 unselect_item (panel);
1981 panel->selected = panel->top_file + (ITEMS (panel)/2);
1982 if (panel->selected >= panel->count)
1983 panel->selected = panel->count - 1;
1984 select_item (panel);
1987 static void
1988 goto_bottom_file (WPanel *panel)
1990 unselect_item (panel);
1991 panel->selected = panel->top_file + ITEMS (panel)-1;
1992 if (panel->selected >= panel->count)
1993 panel->selected = panel->count - 1;
1994 select_item (panel);
1997 static void
1998 move_home (WPanel *panel)
2000 if (panel->selected == 0)
2001 return;
2002 unselect_item (panel);
2004 if (torben_fj_mode){
2005 int middle_pos = panel->top_file + (ITEMS (panel)/2);
2007 if (panel->selected > middle_pos){
2008 goto_middle_file (panel);
2009 return;
2011 if (panel->selected != panel->top_file){
2012 goto_top_file (panel);
2013 return;
2017 panel->top_file = 0;
2018 panel->selected = 0;
2020 paint_dir (panel);
2021 select_item (panel);
2024 static void
2025 move_end (WPanel *panel)
2027 if (panel->selected == panel->count-1)
2028 return;
2029 unselect_item (panel);
2030 if (torben_fj_mode){
2031 int middle_pos = panel->top_file + (ITEMS (panel)/2);
2033 if (panel->selected < middle_pos){
2034 goto_middle_file (panel);
2035 return;
2037 if (panel->selected != (panel->top_file + ITEMS(panel)-1)){
2038 goto_bottom_file (panel);
2039 return;
2043 panel->selected = panel->count-1;
2044 paint_dir (panel);
2045 select_item (panel);
2048 /* Recalculate the panels summary information, used e.g. when marked
2049 files might have been removed by an external command */
2050 void
2051 recalculate_panel_summary (WPanel *panel)
2053 int i;
2055 panel->marked = 0;
2056 panel->dirs_marked = 0;
2057 panel->total = 0;
2059 for (i = 0; i < panel->count; i++)
2060 if (panel->dir.list [i].f.marked){
2061 /* do_file_mark will return immediately if newmark == oldmark.
2062 So we have to first unmark it to get panel's summary information
2063 updated. (Norbert) */
2064 panel->dir.list [i].f.marked = 0;
2065 do_file_mark (panel, i, 1);
2069 /* This routine marks a file or a directory */
2070 void
2071 do_file_mark (WPanel *panel, int idx, int mark)
2073 if (panel->dir.list[idx].f.marked == mark)
2074 return;
2076 /* Only '..' can't be marked, '.' isn't visible */
2077 if (!strcmp (panel->dir.list[idx].fname, ".."))
2078 return;
2080 file_mark (panel, idx, mark);
2081 if (panel->dir.list[idx].f.marked) {
2082 panel->marked++;
2083 if (S_ISDIR (panel->dir.list[idx].st.st_mode)) {
2084 if (panel->dir.list[idx].f.dir_size_computed)
2085 panel->total += panel->dir.list[idx].st.st_size;
2086 panel->dirs_marked++;
2087 } else
2088 panel->total += panel->dir.list[idx].st.st_size;
2089 set_colors (panel);
2090 } else {
2091 if (S_ISDIR (panel->dir.list[idx].st.st_mode)) {
2092 if (panel->dir.list[idx].f.dir_size_computed)
2093 panel->total -= panel->dir.list[idx].st.st_size;
2094 panel->dirs_marked--;
2095 } else
2096 panel->total -= panel->dir.list[idx].st.st_size;
2097 panel->marked--;
2101 static void
2102 do_mark_file (WPanel *panel, int do_move)
2104 do_file_mark (panel, panel->selected,
2105 selection (panel)->f.marked ? 0 : 1);
2106 if (mark_moves_down && do_move)
2107 move_down (panel);
2110 static void
2111 mark_file (WPanel *panel)
2113 do_mark_file (panel, 1);
2116 /* Incremental search of a file name in the panel */
2117 static void
2118 do_search (WPanel *panel, int c_code)
2120 size_t l, max, buf_max;
2121 int i, sel;
2122 int wrapped = 0;
2123 char *act;
2125 l = strlen (panel->search_buffer);
2126 if (c_code == KEY_BACKSPACE) {
2127 if (l != 0) {
2128 act = panel->search_buffer + l;
2129 str_prev_noncomb_char (&act, panel->search_buffer);
2130 act[0] = '\0';
2132 panel->search_chpoint = 0;
2133 } else {
2134 if (c_code && (gsize) panel->search_chpoint < sizeof (panel->search_char)) {
2135 panel->search_char[panel->search_chpoint] = c_code;
2136 panel->search_chpoint++;
2139 if (panel->search_chpoint > 0) {
2140 switch (str_is_valid_char (panel->search_char, panel->search_chpoint)) {
2141 case -2:
2142 return;
2143 case -1:
2144 panel->search_chpoint = 0;
2145 return;
2146 default:
2147 if (l + panel->search_chpoint < sizeof (panel->search_buffer)) {
2148 memcpy (panel->search_buffer + l, panel->search_char,
2149 panel->search_chpoint);
2150 l+= panel->search_chpoint;
2151 (panel->search_buffer + l)[0] = '\0';
2152 panel->search_chpoint = 0;
2158 buf_max = panel->case_sensitive ?
2159 str_prefix (panel->search_buffer, panel->search_buffer) :
2160 str_caseprefix (panel->search_buffer, panel->search_buffer);
2161 max = 0;
2162 sel = panel->selected;
2163 for (i = panel->selected; !wrapped || i != panel->selected; i++) {
2164 if (i >= panel->count) {
2165 i = 0;
2166 if (wrapped)
2167 break;
2168 wrapped = 1;
2170 l = panel->case_sensitive ?
2171 str_prefix (panel->dir.list[i].fname, panel->search_buffer) :
2172 str_caseprefix (panel->dir.list[i].fname, panel->search_buffer);
2173 if (l > max) {
2174 max = l;
2175 sel = i;
2176 if (max == buf_max) break;
2180 unselect_item (panel);
2181 panel->selected = sel;
2182 select_item (panel);
2184 act = panel->search_buffer + strlen (panel->search_buffer);
2185 while (max < buf_max) {
2186 str_prev_char_safe (&act);
2187 act[0] = '\0';
2188 buf_max = panel->case_sensitive ?
2189 str_prefix (panel->search_buffer, panel->search_buffer) :
2190 str_caseprefix (panel->search_buffer, panel->search_buffer);
2193 paint_panel (panel);
2196 static void
2197 start_search (WPanel *panel)
2199 if (panel->searching){
2200 if (panel->selected+1 == panel->count)
2201 panel->selected = 0;
2202 else
2203 move_down (panel);
2204 do_search (panel, 0);
2205 } else {
2206 panel->searching = 1;
2207 panel->search_buffer[0] = '\0';
2208 panel->search_char[0] = '\0';
2209 panel->search_chpoint = 0;
2210 display_mini_info (panel);
2211 mc_refresh ();
2215 /* Return 1 if the Enter key has been processed, 0 otherwise */
2216 static int
2217 do_enter_on_file_entry (file_entry *fe)
2219 char *full_name;
2222 * Directory or link to directory - change directory.
2223 * Try the same for the entries on which mc_lstat() has failed.
2225 if (S_ISDIR (fe->st.st_mode) || link_isdir (fe)
2226 || (fe->st.st_mode == 0)) {
2227 if (!do_cd (fe->fname, cd_exact))
2228 message (D_ERROR, MSG_ERROR, _("Cannot change directory"));
2229 return 1;
2232 /* Try associated command */
2233 if (regex_command (fe->fname, "Open", 0) != 0)
2234 return 1;
2236 /* Check if the file is executable */
2237 full_name = concat_dir_and_file (current_panel->cwd, fe->fname);
2238 if (!is_exe (fe->st.st_mode) || !if_link_is_exe (full_name, fe)) {
2239 g_free (full_name);
2240 return 0;
2242 g_free (full_name);
2244 if (confirm_execute) {
2245 if (query_dialog
2246 (_(" The Midnight Commander "),
2247 _(" Do you really want to execute? "), D_NORMAL, 2, _("&Yes"),
2248 _("&No")) != 0)
2249 return 1;
2251 #ifdef USE_VFS
2252 if (!vfs_current_is_local ()) {
2253 char *tmp;
2254 int ret;
2256 tmp = concat_dir_and_file (vfs_get_current_dir (), fe->fname);
2257 ret = mc_setctl (tmp, VFS_SETCTL_RUN, NULL);
2258 g_free (tmp);
2259 /* We took action only if the dialog was shown or the execution
2260 * was successful */
2261 return confirm_execute || (ret == 0);
2263 #endif
2266 char *tmp = name_quote (fe->fname, 0);
2267 char *cmd = g_strconcat (".", PATH_SEP_STR, tmp, (char *) NULL);
2268 g_free (tmp);
2269 shell_execute (cmd, 0);
2270 g_free (cmd);
2273 return 1;
2276 static int
2277 do_enter (WPanel *panel)
2279 return do_enter_on_file_entry (selection (panel));
2282 static void
2283 chdir_other_panel (WPanel *panel)
2285 char *new_dir;
2286 char *sel_entry = NULL;
2288 if (get_other_type () != view_listing) {
2289 set_display_type (get_other_index (), view_listing);
2292 if (!S_ISDIR (panel->dir.list [panel->selected].st.st_mode)) {
2293 new_dir = concat_dir_and_file (panel->cwd, "..");
2294 sel_entry = strrchr(panel->cwd, PATH_SEP);
2295 } else
2296 new_dir = concat_dir_and_file (panel->cwd, panel->dir.list [panel->selected].fname);
2298 change_panel ();
2299 do_cd (new_dir, cd_exact);
2300 if (sel_entry)
2301 try_to_select (current_panel, sel_entry);
2302 change_panel ();
2304 move_down (panel);
2306 g_free (new_dir);
2310 * Make the current directory of the current panel also the current
2311 * directory of the other panel. Put the other panel to the listing
2312 * mode if needed. If the current panel is panelized, the other panel
2313 * doesn't become panelized.
2315 static void
2316 sync_other_panel (WPanel *panel)
2318 if (get_other_type () != view_listing) {
2319 set_display_type (get_other_index (), view_listing);
2322 do_panel_cd (other_panel, current_panel->cwd, cd_exact);
2324 /* try to select current filename on the other panel */
2325 if (!panel->is_panelized) {
2326 try_to_select (other_panel, selection (panel)->fname);
2330 static void
2331 chdir_to_readlink (WPanel *panel)
2333 char *new_dir;
2335 if (get_other_type () != view_listing)
2336 return;
2338 if (S_ISLNK (panel->dir.list [panel->selected].st.st_mode)) {
2339 char buffer [MC_MAXPATHLEN], *p;
2340 int i;
2341 struct stat st;
2343 i = readlink (selection (panel)->fname, buffer, MC_MAXPATHLEN - 1);
2344 if (i < 0)
2345 return;
2346 if (mc_stat (selection (panel)->fname, &st) < 0)
2347 return;
2348 buffer [i] = 0;
2349 if (!S_ISDIR (st.st_mode)) {
2350 p = strrchr (buffer, PATH_SEP);
2351 if (p && !p[1]) {
2352 *p = 0;
2353 p = strrchr (buffer, PATH_SEP);
2355 if (!p)
2356 return;
2357 p[1] = 0;
2359 if (*buffer == PATH_SEP)
2360 new_dir = g_strdup (buffer);
2361 else
2362 new_dir = concat_dir_and_file (panel->cwd, buffer);
2364 change_panel ();
2365 do_cd (new_dir, cd_exact);
2366 change_panel ();
2368 move_down (panel);
2370 g_free (new_dir);
2374 static gsize
2375 panel_get_format_field_count(WPanel *panel)
2377 format_e *format;
2378 gsize index;
2379 for (
2380 index=0, format = panel->format;
2381 format != NULL;
2382 format = format->next, index++
2384 return index;
2388 function return 0 if not found and REAL_INDEX+1 if found
2390 static gsize
2391 panel_get_format_field_index_by_name(WPanel *panel, const char *name)
2393 format_e *format;
2394 gsize index;
2396 for (
2397 index=1, format = panel->format;
2398 ! ( format == NULL || strcmp(format->title, _(name)) == 0 );
2399 format = format->next, index++
2401 if (format == NULL)
2402 index = 0;
2404 return index;
2407 format_e *
2408 panel_get_format_field_by_index(WPanel *panel, gsize index)
2410 format_e *format;
2411 for (
2412 format = panel->format;
2413 ! ( format == NULL || index == 0 );
2414 format = format->next, index--
2416 return format;
2419 static const panel_field_t *
2420 panel_get_sortable_field_by_format(WPanel *panel, gsize index)
2422 const panel_field_t *pfield;
2423 format_e *format;
2425 format = panel_get_format_field_by_index(panel, index);
2426 if (format == NULL)
2427 return NULL;
2428 pfield = panel_get_field_by_title(format->title);
2429 if (pfield == NULL)
2430 return NULL;
2431 if (pfield->sort_routine == NULL)
2432 return NULL;
2433 return pfield;
2436 static void
2437 panel_toggle_sort_order_prev(WPanel *panel)
2439 gsize index, i;
2441 const panel_field_t *pfield = NULL;
2443 /* If reverse try to direct sort */
2444 if (panel->reverse) {
2445 panel->reverse = ! panel->reverse;
2446 panel_set_sort_order(panel, panel->current_sort_field);
2447 return;
2450 index = panel_get_format_field_index_by_name(panel, panel->current_sort_field->title);
2452 if (index > 1){
2453 /* search for prev sortable column in panel format */
2454 for (
2455 i = index-1 ;
2456 i != 0 && (pfield = panel_get_sortable_field_by_format(panel, i-1)) == NULL ;
2461 if ( pfield == NULL) {
2462 /* Sortable field not found. Try to search in each array */
2463 for (
2464 i = panel_get_format_field_count(panel) ;
2465 i != 0 && (pfield = panel_get_sortable_field_by_format(panel, i-1)) == NULL ;
2469 if ( pfield == NULL)
2470 return;
2471 panel->reverse = 1;
2472 panel->current_sort_field = pfield;
2473 panel_set_sort_order(panel, panel->current_sort_field);
2477 static void
2478 panel_toggle_sort_order_next(WPanel *panel)
2480 gsize index, i;
2481 const panel_field_t *pfield = NULL;
2482 gsize format_field_count = panel_get_format_field_count(panel);
2484 /* If reverse try to direct sort */
2485 if (! panel->reverse) {
2486 panel->reverse = ! panel->reverse;
2487 panel_set_sort_order(panel, panel->current_sort_field);
2488 return;
2491 index = panel_get_format_field_index_by_name(panel, panel->current_sort_field->title);
2493 if (index != 0 && index != format_field_count){
2494 /* search for prev sortable column in panel format */
2495 for (
2496 i = index;
2497 i != format_field_count && (pfield = panel_get_sortable_field_by_format(panel, i)) == NULL ;
2502 if ( pfield == NULL) {
2503 /* Sortable field not found. Try to search in each array */
2504 for (
2505 i = 0 ;
2506 i != format_field_count && (pfield = panel_get_sortable_field_by_format(panel, i)) == NULL ;
2510 if ( pfield == NULL)
2511 return;
2512 panel->reverse = 0;
2513 panel->current_sort_field = pfield;
2514 panel_set_sort_order(panel, panel->current_sort_field);
2517 typedef void (*panel_key_callback) (WPanel *);
2519 static void cmd_do_enter(WPanel *wp) { (void) do_enter(wp); }
2520 static void cmd_view_simple(WPanel *wp) { (void) wp; view_simple_cmd(); }
2521 static void cmd_edit_new(WPanel *wp) { (void) wp; edit_cmd_new(); }
2522 static void cmd_copy_local(WPanel *wp) { (void) wp;copy_cmd_local(); }
2523 static void cmd_rename_local(WPanel *wp) { (void) wp;ren_cmd_local(); }
2524 static void cmd_delete_local(WPanel *wp) { (void) wp;delete_cmd_local(); }
2525 static void cmd_select(WPanel *wp) { (void) wp;select_cmd(); }
2526 static void cmd_unselect(WPanel *wp) { (void) wp;unselect_cmd(); }
2527 static void cmd_reverse_selection(WPanel *wp) { (void) wp;reverse_selection_cmd(); }
2529 static cb_ret_t
2530 panel_execute_cmd (WPanel *panel, int command)
2532 int res = MSG_HANDLED;
2534 switch (command) {
2535 case CK_PanelChdirOtherPanel:
2536 chdir_other_panel (panel);
2537 break;
2538 case CK_PanelChdirToReadlink:
2539 chdir_to_readlink (panel);
2540 break;
2541 case CK_PanelCmdCopyLocal:
2542 cmd_copy_local (panel);
2543 break;
2544 case CK_PanelCmdDeleteLocal:
2545 cmd_delete_local (panel);
2546 break;
2547 case CK_PanelCmdDoEnter:
2548 cmd_do_enter (panel);
2549 break;
2550 case CK_PanelCmdViewSimple:
2551 cmd_view_simple (panel);
2552 break;
2553 case CK_PanelCmdEditNew:
2554 cmd_edit_new (panel);
2555 break;
2556 case CK_PanelCmdRenameLocal:
2557 cmd_rename_local (panel);
2558 break;
2559 case CK_PanelCmdReverseSelection:
2560 cmd_reverse_selection (panel);
2561 break;
2562 case CK_PanelCmdSelect:
2563 cmd_select (panel);
2564 break;
2565 case CK_PanelCmdUnselect:
2566 cmd_unselect (panel);
2567 break;
2568 case CK_PanelNextPage:
2569 next_page (panel);
2570 break;
2571 case CK_PanelPrevPage:
2572 prev_page (panel);
2573 break;
2574 case CK_PanelCtrlNextPage:
2575 ctrl_next_page (panel);
2576 break;
2577 case CK_PanelCtrlPrevPage:
2578 ctrl_prev_page (panel);
2579 break;
2580 case CK_PanelDirectoryHistoryList:
2581 directory_history_list (panel);
2582 break;
2583 case CK_PanelDirectoryHistoryNext:
2584 directory_history_next (panel);
2585 break;
2586 case CK_PanelDirectoryHistoryPrev:
2587 directory_history_prev (panel);
2588 break;
2589 case CK_PanelGotoBottomFile:
2590 goto_bottom_file (panel);
2591 break;
2592 case CK_PanelGotoMiddleFile:
2593 goto_middle_file (panel);
2594 break;
2595 case CK_PanelGotoTopFile:
2596 goto_top_file (panel);
2597 break;
2598 case CK_PanelMarkFile:
2599 mark_file (panel);
2600 break;
2601 case CK_PanelMoveUp:
2602 move_up (panel);
2603 break;
2604 case CK_PanelMoveDown:
2605 move_down (panel);
2606 break;
2607 case CK_PanelMoveLeft:
2608 res = move_left (panel);
2609 break;
2610 case CK_PanelMoveRight:
2611 res = move_right (panel);
2612 break;
2613 case CK_PanelMoveEnd:
2614 move_end (panel);
2615 break;
2616 case CK_PanelMoveHome:
2617 move_home (panel);
2618 break;
2619 case CK_PanelSetPanelEncoding:
2620 set_panel_encoding (panel);
2621 break;
2622 case CK_PanelStartSearch:
2623 start_search (panel);
2624 break;
2625 case CK_PanelSyncOtherPanel:
2626 sync_other_panel (panel);
2627 break;
2628 case CK_PanelSelectSortOrder:
2629 sort_cmd ();
2630 break;
2631 case CK_PanelToggleSortOrderPrev:
2632 panel_toggle_sort_order_prev(panel);
2633 break;
2634 case CK_PanelToggleSortOrderNext:
2635 panel_toggle_sort_order_next(panel);
2636 break;
2638 return res;
2641 static cb_ret_t
2642 panel_key (WPanel *panel, int key)
2644 int i;
2645 int res, command;
2647 for (i = 0; panel_map[i].key; i++) {
2648 if (key == panel_map[i].key) {
2649 int old_searching = panel->searching;
2651 if (panel_map[i].command != CK_PanelStartSearch)
2652 panel->searching = 0;
2654 command = panel_map[i].command;
2655 res = panel_execute_cmd (panel, command);
2657 if (res == MSG_NOT_HANDLED)
2658 return res;
2660 if (panel->searching != old_searching)
2661 display_mini_info (panel);
2662 return MSG_HANDLED;
2666 if (torben_fj_mode && key == ALT ('h')) {
2667 goto_middle_file (panel);
2668 return MSG_HANDLED;
2671 if (is_abort_char (key)) {
2672 panel->searching = 0;
2673 display_mini_info (panel);
2674 return MSG_HANDLED;
2677 /* Do not eat characters not meant for the panel below ' ' (e.g. C-l). */
2678 if ((key >= ' ' && key <= 255) || key == KEY_BACKSPACE) {
2679 if (panel->searching) {
2680 do_search (panel, key);
2681 return MSG_HANDLED;
2684 if (!command_prompt) {
2685 start_search (panel);
2686 do_search (panel, key);
2687 return MSG_HANDLED;
2691 return MSG_NOT_HANDLED;
2694 static cb_ret_t
2695 panel_callback (Widget *w, widget_msg_t msg, int parm)
2697 WPanel *panel = (WPanel *) w;
2698 Dlg_head *h = panel->widget.parent;
2700 switch (msg) {
2701 case WIDGET_DRAW:
2702 paint_panel (panel);
2703 return MSG_HANDLED;
2705 case WIDGET_FOCUS:
2706 current_panel = panel;
2707 panel->active = 1;
2708 if (mc_chdir (panel->cwd) != 0) {
2709 char *cwd = strip_password (g_strdup (panel->cwd), 1);
2710 message (D_ERROR, MSG_ERROR, _(" Cannot chdir to \"%s\" \n %s "),
2711 cwd, unix_error_string (errno));
2712 g_free(cwd);
2713 } else
2714 subshell_chdir (panel->cwd);
2716 update_xterm_title_path ();
2717 select_item (panel);
2718 show_dir (panel);
2719 paint_dir (panel);
2720 panel->dirty = 0;
2722 buttonbar_set_label (h, 1, _("Help"), help_cmd);
2723 buttonbar_set_label (h, 2, _("Menu"), user_file_menu_cmd);
2724 buttonbar_set_label (h, 3, _("View"), view_cmd);
2725 buttonbar_set_label (h, 4, _("Edit"), edit_cmd);
2726 buttonbar_set_label (h, 5, _("Copy"), copy_cmd);
2727 buttonbar_set_label (h, 6, _("RenMov"), ren_cmd);
2728 buttonbar_set_label (h, 7, _("Mkdir"), mkdir_cmd);
2729 buttonbar_set_label (h, 8, _("Delete"), delete_cmd);
2730 buttonbar_redraw (h);
2731 return MSG_HANDLED;
2733 case WIDGET_UNFOCUS:
2734 /* Janne: look at this for the multiple panel options */
2735 if (panel->searching){
2736 panel->searching = 0;
2737 display_mini_info (panel);
2739 panel->active = 0;
2740 show_dir (panel);
2741 unselect_item (panel);
2742 return MSG_HANDLED;
2744 case WIDGET_KEY:
2745 return panel_key (panel, parm);
2747 case WIDGET_DESTROY:
2748 panel_destroy (panel);
2749 return MSG_HANDLED;
2751 default:
2752 return default_proc (msg, parm);
2756 void
2757 file_mark (WPanel *panel, int index, int val)
2759 if (panel->dir.list[index].f.marked != val) {
2760 panel->dir.list[index].f.marked = val;
2761 panel->dirty = 1;
2765 /* */
2766 /* Panel mouse events support routines */
2767 /* */
2768 static int mouse_marking = 0;
2770 static void
2771 mouse_toggle_mark (WPanel *panel)
2773 do_mark_file (panel, 0);
2774 mouse_marking = selection (panel)->f.marked;
2777 static void
2778 mouse_set_mark (WPanel *panel)
2780 if (mouse_marking && !(selection (panel)->f.marked))
2781 do_mark_file (panel, 0);
2782 else if (!mouse_marking && (selection (panel)->f.marked))
2783 do_mark_file (panel, 0);
2786 static int
2787 mark_if_marking (WPanel *panel, Gpm_Event *event)
2789 if (event->buttons & GPM_B_RIGHT){
2790 if (event->type & GPM_DOWN)
2791 mouse_toggle_mark (panel);
2792 else
2793 mouse_set_mark (panel);
2794 return 1;
2796 return 0;
2799 /* Determine which column was clicked, and sort the panel on
2800 * that column, or reverse sort on that column if already
2801 * sorted on that column.
2803 static void
2804 mouse_sort_col(Gpm_Event *event, WPanel *panel)
2806 int i;
2807 const char *sort_name = NULL;
2808 panel_field_t *col_sort_format = NULL;
2809 format_e *format;
2812 for (i=0, format = panel->format; format != NULL; format = format->next, i += format->field_len){
2813 if ( event->x < i + 1) {
2814 /* found column */
2815 sort_name = format->title;
2816 break;
2820 if (sort_name == NULL)
2821 return;
2823 for(i=0; panel_fields[i].id != NULL; i ++) {
2824 if ( !strcmp(sort_name,_(panel_fields[i].title)) && panel_fields[i].sort_routine ) {
2825 col_sort_format = &panel_fields[i];
2826 break;
2830 if (! col_sort_format)
2831 return;
2833 if (panel->current_sort_field == col_sort_format) {
2834 /* reverse the sort if clicked column is already the sorted column */
2835 panel->reverse = ! panel->reverse;
2836 } else {
2837 panel->reverse = 0; /* new sort is forced to be ascending */
2839 panel_set_sort_order(panel, col_sort_format);
2844 * Mouse callback of the panel minus repainting.
2845 * If the event is redirected to the menu, *redir is set to 1.
2847 static int
2848 do_panel_event (Gpm_Event *event, WPanel *panel, int *redir)
2850 const int lines = llines (panel);
2851 const gboolean is_active = dlg_widget_active (panel);
2852 const gboolean mouse_down = (event->type & GPM_DOWN) != 0;
2854 /* "." button show/hide hidden files */
2855 if (event->type & GPM_DOWN && event->x == panel->widget.cols - 3 && event->y == 1) {
2856 toggle_show_hidden();
2857 return MOU_NORMAL;
2860 /* sort on clicked column */
2861 if (event->type & GPM_DOWN && event->y == 2) {
2862 mouse_sort_col(event,panel);
2863 return MOU_NORMAL;
2866 /* rest of the upper frame, the menu is invisible - call menu */
2867 if (mouse_down && event->y == 1 && !menubar_visible) {
2868 *redir = 1;
2869 event->x += panel->widget.x;
2870 return (*(the_menubar->widget.mouse)) (event, the_menubar);
2873 /* "<" button */
2874 if (mouse_down && event->y == 1 && event->x == 2) {
2875 directory_history_prev (panel);
2876 return MOU_NORMAL;
2879 /* ">" button */
2880 if (mouse_down && event->y == 1 && event->x == panel->widget.cols - 1) {
2881 directory_history_next (panel);
2882 return MOU_NORMAL;
2885 /* "v" button */
2886 if (mouse_down && event->y == 1 && event->x == panel->widget.cols - 2) {
2887 directory_history_list (panel);
2888 return MOU_NORMAL;
2891 /* rest of the upper frame, the menu is invisible - call menu */
2892 if (mouse_down && event->y == 1 && !menubar_visible) {
2893 *redir = 1;
2894 event->x += panel->widget.x;
2895 return the_menubar->widget.mouse (event, the_menubar);
2898 /* Mouse wheel events */
2899 if (mouse_down && (event->buttons & GPM_B_UP)) {
2900 if (is_active) {
2901 if (panel->top_file > 0)
2902 prev_page (panel);
2903 else /* We are in first page */
2904 move_up (panel);
2906 return MOU_NORMAL;
2909 if (mouse_down && (event->buttons & GPM_B_DOWN)) {
2910 if (is_active) {
2911 if (panel->top_file + ITEMS (panel) < panel->count)
2912 next_page (panel);
2913 else /* We are in last page */
2914 move_down (panel);
2916 return MOU_NORMAL;
2919 event->y -= 2;
2920 if ((event->type & (GPM_DOWN | GPM_DRAG))) {
2921 int my_index;
2923 if (!is_active)
2924 change_panel ();
2926 if (event->y <= 0) {
2927 mark_if_marking (panel, event);
2928 if (mouse_move_pages)
2929 prev_page (panel);
2930 else
2931 move_up (panel);
2932 return MOU_REPEAT;
2935 if (!((panel->top_file + event->y <= panel->count) && event->y <= lines)) {
2936 mark_if_marking (panel, event);
2937 if (mouse_move_pages)
2938 next_page (panel);
2939 else
2940 move_down (panel);
2941 return MOU_REPEAT;
2944 my_index = panel->top_file + event->y - 1;
2945 if (panel->split && (event->x > ((panel->widget.cols - 2) / 2)))
2946 my_index += llines (panel);
2948 if (my_index >= panel->count)
2949 my_index = panel->count - 1;
2951 if (my_index != panel->selected) {
2952 unselect_item (panel);
2953 panel->selected = my_index;
2954 select_item (panel);
2957 /* This one is new */
2958 mark_if_marking (panel, event);
2959 } else if ((event->type & (GPM_UP | GPM_DOUBLE)) == (GPM_UP | GPM_DOUBLE)) {
2960 if (event->y > 0 && event->y <= lines)
2961 do_enter (panel);
2963 return MOU_NORMAL;
2966 /* Mouse callback of the panel */
2967 static int
2968 panel_event (Gpm_Event *event, void *data)
2970 WPanel *panel = data;
2971 int ret;
2972 int redir = MOU_NORMAL;
2974 ret = do_panel_event (event, panel, &redir);
2975 if (!redir)
2976 paint_panel (panel);
2978 return ret;
2981 void
2982 panel_re_sort (WPanel *panel)
2984 char *filename;
2985 int i;
2987 if (panel == NULL)
2988 return;
2990 filename = g_strdup (selection (panel)->fname);
2991 unselect_item (panel);
2992 do_sort (&panel->dir, panel->current_sort_field->sort_routine, panel->count-1, panel->reverse,
2993 panel->case_sensitive, panel->exec_first);
2994 panel->selected = -1;
2995 for (i = panel->count; i; i--){
2996 if (!strcmp (panel->dir.list [i-1].fname, filename)){
2997 panel->selected = i-1;
2998 break;
3001 g_free (filename);
3002 panel->top_file = panel->selected - ITEMS (panel)/2;
3003 if (panel->top_file < 0)
3004 panel->top_file = 0;
3005 select_item (panel);
3006 panel->dirty = 1;
3009 void
3010 panel_set_sort_order (WPanel *panel, const panel_field_t *sort_order)
3012 if (sort_order == 0)
3013 return;
3015 panel->current_sort_field = sort_order;
3017 /* The directory is already sorted, we have to load the unsorted stuff */
3018 if (sort_order->sort_routine == (sortfn *) unsorted){
3019 char *current_file;
3021 current_file = g_strdup (panel->dir.list [panel->selected].fname);
3022 panel_reload (panel);
3023 try_to_select (panel, current_file);
3024 g_free (current_file);
3026 panel_re_sort (panel);
3029 void
3030 set_panel_encoding (WPanel *panel)
3032 const char *encoding = NULL;
3033 char *cd_path;
3034 #ifdef HAVE_CHARSET
3035 const char *errmsg;
3036 int offset;
3037 int r;
3039 if (horizontal_split) {
3040 offset = (get_current_index () != 0) ? panel->widget.lines : -panel->widget.lines;
3041 r = select_charset (0, offset, source_codepage, FALSE);
3042 } else {
3043 offset = (panel->widget.cols == COLS) ? 0
3044 : (get_current_index () != 0) ? panel->widget.cols
3045 : -panel->widget.cols;
3046 r = select_charset (offset, 0, source_codepage, FALSE);
3049 if (r == SELECT_CHARSET_CANCEL)
3050 return; /* Cancel */
3052 if (r == SELECT_CHARSET_NO_TRANSLATE) {
3053 /* No translation */
3054 errmsg = init_translation_table (display_codepage, display_codepage);
3055 cd_path = remove_encoding_from_path (panel->cwd);
3056 do_panel_cd (panel, cd_path, 0);
3057 g_free (cd_path);
3058 return;
3061 source_codepage = r;
3063 errmsg = init_translation_table (source_codepage, display_codepage);
3064 if (errmsg) {
3065 message (D_ERROR, MSG_ERROR, "%s", errmsg);
3066 return;
3069 encoding = get_codepage_id (source_codepage);
3070 #endif
3071 if (encoding != NULL) {
3072 cd_path = add_encoding_to_path (panel->cwd, encoding);
3073 if (!do_panel_cd (panel, cd_path, 0))
3074 message (D_ERROR, MSG_ERROR, _(" Cannot chdir to %s "), cd_path);
3075 g_free (cd_path);
3079 static void
3080 reload_panelized (WPanel *panel)
3082 int i, j;
3083 dir_list *list = &panel->dir;
3085 if (panel != current_panel)
3086 mc_chdir (panel->cwd);
3088 for (i = 0, j = 0; i < panel->count; i++) {
3089 if (list->list[i].f.marked) {
3090 /* Unmark the file in advance. In case the following mc_lstat
3091 * fails we are done, else we have to mark the file again
3092 * (Note: do_file_mark depends on a valid "list->list [i].buf").
3093 * IMO that's the best way to update the panel's summary status
3094 * -- Norbert
3096 do_file_mark (panel, i, 0);
3098 if (mc_lstat (list->list[i].fname, &list->list[i].st)) {
3099 g_free (list->list[i].fname);
3100 continue;
3102 if (list->list[i].f.marked)
3103 do_file_mark (panel, i, 1);
3104 if (j != i)
3105 list->list[j] = list->list[i];
3106 j++;
3108 if (j == 0)
3109 panel->count = set_zero_dir (list);
3110 else
3111 panel->count = j;
3113 if (panel != current_panel)
3114 mc_chdir (current_panel->cwd);
3117 static void
3118 update_one_panel_widget (WPanel *panel, int force_update,
3119 const char *current_file)
3121 int free_pointer;
3122 char *my_current_file = NULL;
3124 if (force_update & UP_RELOAD) {
3125 panel->is_panelized = 0;
3126 mc_setctl (panel->cwd, VFS_SETCTL_FLUSH, 0);
3127 memset (&(panel->dir_stat), 0, sizeof (panel->dir_stat));
3130 /* If current_file == -1 (an invalid pointer) then preserve selection */
3131 if (current_file == UP_KEEPSEL) {
3132 free_pointer = 1;
3133 my_current_file = g_strdup (panel->dir.list[panel->selected].fname);
3134 current_file = my_current_file;
3135 } else
3136 free_pointer = 0;
3138 if (panel->is_panelized)
3139 reload_panelized (panel);
3140 else
3141 panel_reload (panel);
3143 try_to_select (panel, current_file);
3144 panel->dirty = 1;
3146 if (free_pointer)
3147 g_free (my_current_file);
3150 static void
3151 update_one_panel (int which, int force_update, const char *current_file)
3153 WPanel *panel;
3155 if (get_display_type (which) != view_listing)
3156 return;
3158 panel = (WPanel *) get_panel_widget (which);
3159 update_one_panel_widget (panel, force_update, current_file);
3162 /* This routine reloads the directory in both panels. It tries to
3163 * select current_file in current_panel and other_file in other_panel.
3164 * If current_file == -1 then it automatically sets current_file and
3165 * other_file to the currently selected files in the panels.
3167 * if force_update has the UP_ONLY_CURRENT bit toggled on, then it
3168 * will not reload the other panel.
3170 void
3171 update_panels (int force_update, const char *current_file)
3173 int reload_other = !(force_update & UP_ONLY_CURRENT);
3174 WPanel *panel;
3176 update_one_panel (get_current_index (), force_update, current_file);
3177 if (reload_other)
3178 update_one_panel (get_other_index (), force_update, UP_KEEPSEL);
3180 if (get_current_type () == view_listing)
3181 panel = (WPanel *) get_panel_widget (get_current_index ());
3182 else
3183 panel = (WPanel *) get_panel_widget (get_other_index ());
3185 mc_chdir (panel->cwd);
3188 gsize
3189 panel_get_num_of_sortable_fields(void)
3191 gsize ret = 0, index;
3193 for(index=0; panel_fields[index].id != NULL; index ++)
3194 if (panel_fields[index].title_hotkey != NULL)
3195 ret++;
3196 return ret;
3200 const char **
3201 panel_get_sortable_fields(gsize *array_size)
3203 char **ret;
3204 gsize index, i;
3206 index = panel_get_num_of_sortable_fields();
3208 ret = g_new0 (char *, index + 1);
3209 if (ret == NULL)
3210 return NULL;
3212 if (array_size != NULL)
3213 *array_size = index;
3215 index=0;
3217 for(i=0; panel_fields[i].id != NULL; i ++)
3218 if (panel_fields[i].title_hotkey != NULL)
3219 ret[index++] = g_strdup(_(panel_fields[i].title_hotkey));
3220 return (const char**) ret;
3223 const panel_field_t *
3224 panel_get_field_by_id(const char *name)
3226 gsize index;
3227 for(index=0; panel_fields[index].id != NULL; index ++)
3228 if (
3229 panel_fields[index].id != NULL &&
3230 strcmp(name, panel_fields[index].id) == 0
3232 return &panel_fields[index];
3233 return NULL;
3236 const panel_field_t *
3237 panel_get_field_by_title_hotkey(const char *name)
3239 gsize index;
3240 for(index=0; panel_fields[index].id != NULL; index ++)
3241 if (
3242 panel_fields[index].title_hotkey != NULL &&
3243 strcmp(name, _(panel_fields[index].title_hotkey)) == 0
3245 return &panel_fields[index];
3246 return NULL;
3249 const panel_field_t *
3250 panel_get_field_by_title(const char *name)
3252 gsize index;
3253 for(index=0; panel_fields[index].id != NULL; index ++)
3254 if (
3255 panel_fields[index].title_hotkey != NULL &&
3256 strcmp(name, _(panel_fields[index].title)) == 0
3258 return &panel_fields[index];
3259 return NULL;
3262 gsize
3263 panel_get_num_of_user_possible_fields(void)
3265 gsize ret = 0, index;
3267 for(index=0; panel_fields[index].id != NULL; index ++)
3268 if (panel_fields[index].use_in_user_format)
3269 ret++;
3270 return ret;
3273 const char **
3274 panel_get_user_possible_fields(gsize *array_size)
3276 char **ret;
3277 gsize index, i;
3279 index = panel_get_num_of_user_possible_fields();
3281 ret = g_new0 (char *, index + 1);
3282 if (ret == NULL)
3283 return NULL;
3285 if (array_size != NULL)
3286 *array_size = index;
3288 index=0;
3290 for(i=0; panel_fields[i].id != NULL; i ++)
3291 if (panel_fields[i].use_in_user_format)
3292 ret[index++] = g_strdup(_(panel_fields[i].title_hotkey));
3293 return (const char**) ret;