Cleanup of code. Remove unused panel_field_t::use_in_gui variable
[midnight-commander.git] / src / screen.c
blobcc5908249ead36022e8db4ec97a4921854bc7eb1
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 N_("Unsorted"), N_("&Unsorted"), FALSE,
438 string_file_name,
439 (sortfn *) unsorted
442 "name", 12, 1, J_LEFT_FIT,
443 N_("Name"), N_("&Name"), TRUE,
444 string_file_name,
445 (sortfn *) sort_name
448 "extension", 12, 1, J_LEFT_FIT,
449 N_("Extension"), N_("&Extension"), FALSE,
450 string_file_name, /* TODO: string_file_ext*/
451 (sortfn *) sort_ext
454 "size", 7, 0, J_RIGHT,
455 N_("Size"), N_("&Size"), TRUE,
456 string_file_size,
457 (sortfn *) sort_size
460 "bsize", 7, 0, J_RIGHT,
461 N_("Block Size"), NULL, FALSE,
462 string_file_size_brief,
463 (sortfn *) sort_size
466 "type", GT, 0, J_LEFT,
467 "", NULL, TRUE,
468 string_file_type,
469 NULL
472 "mtime", 12, 0, J_RIGHT,
473 N_("MTime"), N_("&Modify time"), TRUE,
474 string_file_mtime,
475 (sortfn *) sort_time
478 "atime", 12, 0, J_RIGHT,
479 N_("ATime"), N_("&Access time"), TRUE,
480 string_file_atime,
481 (sortfn *) sort_atime
484 "ctime", 12, 0, J_RIGHT,
485 N_("CTime"), N_("C&Hange time"), TRUE,
486 string_file_ctime,
487 (sortfn *) sort_ctime
490 "perm", 10, 0, J_LEFT,
491 N_("Permission"), NULL, TRUE,
492 string_file_permission,
493 NULL
496 "mode", 6, 0, J_RIGHT,
497 N_("Perm"), NULL, TRUE,
498 string_file_perm_octal,
499 NULL
502 "nlink", 2, 0, J_RIGHT,
503 N_("Nl"), NULL, TRUE,
504 string_file_nlinks, NULL
507 "inode", 5, 0, J_RIGHT,
508 N_("Inode"), N_("&Inode"), TRUE,
509 string_inode,
510 (sortfn *) sort_inode
513 "nuid", 5, 0, J_RIGHT,
514 N_("UID"), NULL, FALSE,
515 string_file_nuid,
516 NULL
519 "ngid", 5, 0, J_RIGHT,
520 N_("GID"), NULL, FALSE,
521 string_file_ngid,
522 NULL
525 "owner", 8, 0, J_LEFT_FIT,
526 N_("Owner"), NULL, TRUE,
527 string_file_owner,
528 NULL
531 "group", 8,0, J_LEFT_FIT,
532 N_("Group"), NULL, TRUE,
533 string_file_group,
534 NULL
537 "mark", 1, 0, J_RIGHT,
538 " ", NULL, TRUE,
539 string_marked,
540 NULL
543 "|", 1, 0, J_RIGHT,
544 " ", NULL, TRUE,
545 NULL,
546 NULL
549 "space", 1, 0, J_RIGHT,
550 " ", NULL, TRUE,
551 string_space,
552 NULL
555 "dot", 1, 0, J_RIGHT,
556 " ", NULL, FALSE,
557 string_dot,
558 NULL
561 NULL, 0, 0, J_RIGHT, NULL, NULL, FALSE, NULL, NULL
565 static int
566 file_compute_color (int attr, file_entry *fe)
568 switch (attr) {
569 case SELECTED:
570 return (SELECTED_COLOR);
571 case MARKED:
572 return (MARKED_COLOR);
573 case MARKED_SELECTED:
574 return (MARKED_SELECTED_COLOR);
575 case STATUS:
576 return (NORMAL_COLOR);
577 case NORMAL:
578 default:
579 if (!filetype_mode)
580 return (NORMAL_COLOR);
583 return mc_fhl_get_color (mc_filehighlight, fe);
586 /* Formats the file number file_index of panel in the buffer dest */
587 static void
588 format_file (char *dest, int limit, WPanel *panel, int file_index, int width, int attr, int isstatus)
590 int color, length, empty_line;
591 const char *txt;
592 format_e *format, *home;
593 file_entry *fe;
595 (void) dest;
596 (void) limit;
597 length = 0;
598 empty_line = (file_index >= panel->count);
599 home = (isstatus) ? panel->status_format : panel->format;
600 fe = &panel->dir.list [file_index];
602 if (!empty_line)
603 color = file_compute_color (attr, fe);
604 else
605 color = NORMAL_COLOR;
607 for (format = home; format; format = format->next){
608 if (length == width)
609 break;
611 if (format->string_fn){
612 int len, perm;
613 char *preperad_text;
615 if (empty_line)
616 txt = " ";
617 else
618 txt = (*format->string_fn)(fe, format->field_len);
620 len = format->field_len;
621 if (len + length > width)
622 len = width - length;
623 if (len <= 0)
624 break;
626 perm = 0;
627 if (permission_mode) {
628 if (!strcmp(format->id, "perm"))
629 perm = 1;
630 else if (!strcmp(format->id, "mode"))
631 perm = 2;
634 if (color >= 0)
635 tty_setcolor (color);
636 else
637 tty_lowlevel_setcolor (-color);
639 preperad_text = (char*) str_fit_to_term(txt, len, format->just_mode);
640 if (perm)
641 add_permission_string (preperad_text, format->field_len, fe,
642 attr, color, perm - 1);
643 else
644 tty_print_string (preperad_text);
646 length+= len;
647 } else {
648 if (attr == SELECTED || attr == MARKED_SELECTED)
649 tty_setcolor (SELECTED_COLOR);
650 else
651 tty_setcolor (NORMAL_COLOR);
652 tty_print_one_vline ();
653 length++;
657 if (length < width)
658 tty_draw_hline (-1, -1, ' ', width - length);
661 static void
662 repaint_file (WPanel *panel, int file_index, int mv, int attr, int isstatus)
664 int second_column = 0;
665 int width;
666 int offset = 0;
667 char buffer [BUF_MEDIUM];
669 gboolean panel_is_split = !isstatus && panel->split;
671 width = panel->widget.cols - 2;
673 if (panel_is_split) {
674 second_column = (file_index - panel->top_file) / llines (panel);
675 width = width/2 - 1;
677 if (second_column != 0) {
678 offset = 1 + width;
679 /*width = (panel->widget.cols-2) - (panel->widget.cols-2)/2 - 1;*/
680 width = panel->widget.cols - offset - 2;
684 /* Nothing to paint */
685 if (width <= 0)
686 return;
688 if (mv){
689 if (panel_is_split)
690 widget_move (&panel->widget,
691 (file_index - panel->top_file) % llines (panel) + 2,
692 offset + 1);
693 else
694 widget_move (&panel->widget, file_index - panel->top_file + 2, 1);
697 format_file (buffer, sizeof(buffer), panel, file_index, width, attr, isstatus);
699 if (panel_is_split) {
700 if (second_column)
701 tty_print_char (' ');
702 else {
703 tty_setcolor (NORMAL_COLOR);
704 tty_print_one_vline ();
709 static void
710 display_mini_info (WPanel *panel)
712 widget_move (&panel->widget, llines (panel)+3, 1);
714 if (panel->searching){
715 tty_setcolor (INPUT_COLOR);
716 tty_print_char ('/');
717 tty_print_string (str_fit_to_term (panel->search_buffer,
718 panel->widget.cols - 3, J_LEFT));
719 return;
722 /* Status resolves links and show them */
723 set_colors (panel);
725 if (S_ISLNK (panel->dir.list [panel->selected].st.st_mode)){
726 char *link, link_target [MC_MAXPATHLEN];
727 int len;
729 link = concat_dir_and_file (panel->cwd, panel->dir.list [panel->selected].fname);
730 len = mc_readlink (link, link_target, MC_MAXPATHLEN - 1);
731 g_free (link);
732 if (len > 0){
733 link_target[len] = 0;
734 tty_print_string ("-> ");
735 tty_print_string (str_fit_to_term (link_target, panel->widget.cols - 5,
736 J_LEFT_FIT));
737 } else
738 tty_print_string (str_fit_to_term (_("<readlink failed>"),
739 panel->widget.cols - 2, J_LEFT));
740 } else if (strcmp (panel->dir.list [panel->selected].fname, "..") == 0) {
741 /* FIXME:
742 * while loading directory (do_load_dir() and do_reload_dir()),
743 * the actual stat info about ".." directory isn't got;
744 * so just don't display incorrect info about ".." directory */
745 tty_print_string (str_fit_to_term (_("UP--DIR"), panel->widget.cols - 2, J_LEFT));
746 } else
747 /* Default behavior */
748 repaint_file (panel, panel->selected, 0, STATUS, 1);
751 static void
752 paint_dir (WPanel *panel)
754 int i;
755 int color; /* Color value of the line */
756 int items; /* Number of items */
758 items = llines (panel) * (panel->split ? 2 : 1);
760 for (i = 0; i < items; i++){
761 if (i+panel->top_file >= panel->count)
762 color = 0;
763 else {
764 color = 2 * (panel->dir.list [i+panel->top_file].f.marked);
765 color += (panel->selected==i+panel->top_file && panel->active);
767 repaint_file (panel, i+panel->top_file, 1, color, 0);
769 tty_set_normal_attrs ();
772 static void
773 display_total_marked_size (WPanel *panel, int y, int x, gboolean size_only)
775 char buffer[BUF_SMALL], b_bytes[BUF_SMALL], *buf;
776 int cols;
778 if (panel->marked <= 0)
779 return;
781 buf = size_only ? b_bytes : buffer;
782 cols = panel->widget.cols - 2;
785 * This is a trick to use two ngettext() calls in one sentence.
786 * First make "N bytes", then insert it into "X in M files".
788 g_snprintf (b_bytes, sizeof (b_bytes),
789 ngettext("%s byte", "%s bytes", (unsigned long) panel->total),
790 size_trunc_sep (panel->total));
791 if (!size_only)
792 g_snprintf (buffer, sizeof (buffer),
793 ngettext("%s in %d file", "%s in %d files", panel->marked),
794 b_bytes, panel->marked);
796 /* don't forget spaces around buffer content */
797 buf = (char *) str_trunc (buf, cols - 4);
799 if (x < 0)
800 /* center in panel */
801 x = (panel->widget.cols - str_term_width1 (buf)) / 2 - 1;
804 * y == llines (panel) + 2 for mini_info_separator
805 * y == panel->widget.lines - 1 for panel bottom frame
807 widget_move (&panel->widget, y, x);
808 tty_setcolor (MARKED_COLOR);
809 tty_printf (" %s ", buf);
812 static void
813 mini_info_separator (WPanel *panel)
815 const int y = llines (panel) + 2;
817 tty_setcolor (NORMAL_COLOR);
818 tty_draw_hline (panel->widget.y + y, panel->widget.x + 1,
819 ACS_HLINE, panel->widget.cols - 2);
820 /* Status displays total marked size.
821 * Centered in panel, full format. */
822 display_total_marked_size (panel, y, -1, FALSE);
825 static void
826 show_free_space (WPanel *panel)
828 /* Used to figure out how many free space we have */
829 static struct my_statfs myfs_stats;
830 /* Old current working directory for displaying free space */
831 static char *old_cwd = NULL;
833 /* Don't try to stat non-local fs */
834 if (!vfs_file_is_local (panel->cwd) || !free_space)
835 return;
837 if (old_cwd == NULL || strcmp (old_cwd, panel->cwd) != 0) {
838 char rpath[PATH_MAX];
840 init_my_statfs ();
841 g_free (old_cwd);
842 old_cwd = g_strdup (panel->cwd);
844 if (mc_realpath (panel->cwd, rpath) == NULL)
845 return;
847 my_statfs (&myfs_stats, rpath);
850 if (myfs_stats.avail > 0 || myfs_stats.total > 0) {
851 char buffer1[6], buffer2[6], tmp[BUF_SMALL];
852 size_trunc_len (buffer1, sizeof(buffer1) - 1, myfs_stats.avail, 1);
853 size_trunc_len (buffer2, sizeof(buffer2) - 1, myfs_stats.total, 1);
854 g_snprintf (tmp, sizeof(tmp), " %s/%s (%d%%) ", buffer1, buffer2,
855 myfs_stats.total > 0 ?
856 (int)(100 * (double)myfs_stats.avail / myfs_stats.total) : 0);
857 widget_move (&panel->widget, panel->widget.lines - 1,
858 panel->widget.cols - 2 - (int) strlen (tmp));
859 tty_setcolor (NORMAL_COLOR);
860 tty_print_string (tmp);
864 static void
865 show_dir (WPanel *panel)
867 set_colors (panel);
868 draw_box (panel->widget.parent,
869 panel->widget.y, panel->widget.x,
870 panel->widget.lines, panel->widget.cols);
872 if (show_mini_info) {
873 widget_move (&panel->widget, llines (panel) + 2, 0);
874 tty_print_alt_char (ACS_LTEE);
875 widget_move (&panel->widget, llines (panel) + 2,
876 panel->widget.cols - 1);
877 tty_print_alt_char (ACS_RTEE);
880 if (panel->active)
881 tty_setcolor (REVERSE_COLOR);
883 widget_move (&panel->widget, 0, 3);
885 tty_printf (" %s ",
886 str_term_trim (strip_home_and_password (panel->cwd),
887 min (max (panel->widget.cols - 10, 0),
888 panel->widget.cols)));
890 widget_move (&panel->widget, 0, 1);
891 tty_print_char ('<');
892 widget_move (&panel->widget, 0, panel->widget.cols - 4);
893 tty_print_string (".v>");
895 if (!show_mini_info) {
896 if (panel->marked == 0) {
897 /* Show size of curret file in the bottom of panel */
898 if (S_ISREG (panel->dir.list [panel->selected].st.st_mode)) {
899 char buffer[BUF_SMALL];
901 g_snprintf (buffer, sizeof (buffer), " %s ",
902 size_trunc_sep (panel->dir.list [panel->selected].st.st_size));
903 tty_setcolor (NORMAL_COLOR);
904 widget_move (&panel->widget, panel->widget.lines - 1, 2);
905 tty_print_string (buffer);
907 } else {
908 /* Show total size of marked files
909 * In the bottom of panel, display size only. */
910 display_total_marked_size (panel, panel->widget.lines - 1, 2, TRUE);
914 show_free_space (panel);
916 if (panel->active)
917 tty_set_normal_attrs ();
920 /* To be used only by long_frame and full_frame to adjust top_file */
921 static void
922 adjust_top_file (WPanel *panel)
924 int old_top = panel->top_file;
926 if (panel->selected - old_top > llines (panel))
927 panel->top_file = panel->selected;
928 if (old_top - panel->count > llines (panel))
929 panel->top_file = panel->count - llines (panel);
932 /* Repaint everything, including frame and separator */
933 static void
934 paint_panel (WPanel *panel)
936 paint_frame (panel); /* including show_dir */
937 paint_dir (panel);
939 if (show_mini_info) {
940 mini_info_separator (panel);
941 display_mini_info (panel);
944 panel->dirty = 0;
947 /* add "#enc:encodning" to end of path */
948 /* if path end width a previous #enc:, only encoding is changed no additional
949 * #enc: is appended
950 * retun new string
952 static char
953 *add_encoding_to_path (const char *path, const char *encoding)
955 char *result;
956 char *semi;
957 char *slash;
959 semi = g_strrstr (path, "#enc:");
961 if (semi != NULL) {
962 slash = strchr (semi, PATH_SEP);
963 if (slash != NULL) {
964 result = g_strconcat (path, "/#enc:", encoding, NULL);
965 } else {
966 *semi = 0;
967 result = g_strconcat (path, "/#enc:", encoding, NULL);
968 *semi = '#';
970 } else {
971 result = g_strconcat (path, "/#enc:", encoding, NULL);
974 return result;
977 char *
978 remove_encoding_from_path (const char *path)
980 GString *ret;
981 GString *tmp_path, *tmp_conv;
982 char *tmp, *tmp2;
983 const char *enc;
984 GIConv converter;
986 ret = g_string_new("");
987 tmp_conv = g_string_new("");
989 tmp_path = g_string_new(path);
991 while ((tmp = g_strrstr (tmp_path->str, "/#enc:")) != NULL){
992 enc = vfs_get_encoding ((const char *) tmp);
993 converter = enc ? str_crt_conv_to (enc): str_cnv_to_term;
994 if (converter == INVALID_CONV) converter = str_cnv_to_term;
996 tmp2=tmp+1;
997 while (*tmp2 && *tmp2 != '/')
998 tmp2++;
1000 if (*tmp2){
1001 str_vfs_convert_from (converter, tmp2, tmp_conv);
1002 g_string_prepend(ret, tmp_conv->str);
1003 g_string_set_size(tmp_conv,0);
1005 g_string_set_size(tmp_path,tmp - tmp_path->str);
1006 str_close_conv (converter);
1008 g_string_prepend(ret, tmp_path->str);
1009 g_string_free(tmp_path,TRUE);
1010 g_string_free(tmp_conv,TRUE);
1012 tmp = ret->str;
1013 g_string_free(ret, FALSE);
1014 return tmp;
1018 * Repaint the contents of the panels without frames. To schedule panel
1019 * for repainting, set panel->dirty to 1. There are many reasons why
1020 * the panels need to be repainted, and this is a costly operation, so
1021 * it's done once per event.
1023 void
1024 update_dirty_panels (void)
1026 if (current_panel->dirty)
1027 paint_panel (current_panel);
1029 if ((get_other_type () == view_listing) && other_panel->dirty)
1030 paint_panel (other_panel);
1033 static void
1034 do_select (WPanel *panel, int i)
1036 if (i != panel->selected) {
1037 panel->dirty = 1;
1038 panel->selected = i;
1039 panel->top_file = panel->selected - (panel->widget.lines - 2) / 2;
1040 if (panel->top_file < 0)
1041 panel->top_file = 0;
1045 static void
1046 do_try_to_select (WPanel *panel, const char *name)
1048 int i;
1049 char *subdir;
1051 if (!name) {
1052 do_select(panel, 0);
1053 return;
1056 /* We only want the last component of the directory,
1057 * and from this only the name without suffix. */
1058 subdir = vfs_strip_suffix_from_filename (x_basename(name));
1060 /* Search that subdirectory, if found select it */
1061 for (i = 0; i < panel->count; i++){
1062 if (strcmp (subdir, panel->dir.list [i].fname) == 0) {
1063 do_select (panel, i);
1064 g_free (subdir);
1065 return;
1069 /* Try to select a file near the file that is missing */
1070 if (panel->selected >= panel->count)
1071 do_select (panel, panel->count-1);
1072 g_free (subdir);
1075 void
1076 try_to_select (WPanel *panel, const char *name)
1078 do_try_to_select (panel, name);
1079 select_item (panel);
1082 void
1083 panel_update_cols (Widget *widget, int frame_size)
1085 int cols, origin;
1087 if (horizontal_split){
1088 widget->cols = COLS;
1089 return;
1092 if (frame_size == frame_full){
1093 cols = COLS;
1094 origin = 0;
1095 } else {
1096 if (widget == get_panel_widget (0)){
1097 cols = first_panel_size;
1098 origin = 0;
1099 } else {
1100 cols = COLS-first_panel_size;
1101 origin = first_panel_size;
1105 widget->cols = cols;
1106 widget->x = origin;
1109 static char *
1110 panel_save_name (WPanel *panel)
1112 extern int saving_setup;
1114 /* If the program is shuting down */
1115 if ((midnight_shutdown && auto_save_setup) || saving_setup)
1116 return g_strdup (panel->panel_name);
1117 else
1118 return g_strconcat ("Temporal:", panel->panel_name, (char *) NULL);
1121 void
1122 panel_clean_dir (WPanel *panel)
1124 int count = panel->count;
1126 panel->count = 0;
1127 panel->top_file = 0;
1128 panel->selected = 0;
1129 panel->marked = 0;
1130 panel->dirs_marked = 0;
1131 panel->total = 0;
1132 panel->searching = 0;
1133 panel->is_panelized = 0;
1134 panel->dirty = 1;
1136 clean_dir (&panel->dir, count);
1139 static void
1140 panel_destroy (WPanel *p)
1142 int i;
1144 char *name = panel_save_name (p);
1146 panel_save_setup (p, name);
1147 panel_clean_dir (p);
1149 /* save and clean history */
1150 if (p->dir_history) {
1151 history_put (p->hist_name, p->dir_history);
1153 p->dir_history = g_list_first (p->dir_history);
1154 g_list_foreach (p->dir_history, (GFunc) g_free, NULL);
1155 g_list_free (p->dir_history);
1158 g_free (p->hist_name);
1160 delete_format (p->format);
1161 delete_format (p->status_format);
1163 g_free (p->user_format);
1164 for (i = 0; i < LIST_TYPES; i++)
1165 g_free (p->user_status_format[i]);
1166 g_free (p->dir.list);
1167 g_free (p->panel_name);
1168 g_free (name);
1171 static void
1172 panel_format_modified (WPanel *panel)
1174 panel->format_modified = 1;
1177 /* Panel creation */
1178 /* The parameter specifies the name of the panel for setup retieving */
1179 WPanel *
1180 panel_new (const char *panel_name)
1182 return panel_new_with_dir(panel_name, NULL);
1185 /* Panel creation for specified directory */
1186 /* The parameter specifies the name of the panel for setup retieving */
1187 /* and the path of working panel directory. If path is NULL then */
1188 /* panel will be created for current directory */
1189 WPanel *
1190 panel_new_with_dir (const char *panel_name, const char *wpath)
1192 WPanel *panel;
1193 char *section;
1194 int i, err;
1195 char curdir[MC_MAXPATHLEN];
1197 panel = g_new0 (WPanel, 1);
1199 /* No know sizes of the panel at startup */
1200 init_widget (&panel->widget, 0, 0, 0, 0, panel_callback, panel_event);
1202 /* We do not want the cursor */
1203 widget_want_cursor (panel->widget, 0);
1205 if (wpath) {
1206 g_strlcpy(panel->cwd, wpath, sizeof (panel->cwd));
1207 mc_get_current_wd (curdir, sizeof (curdir) - 2);
1208 } else
1209 mc_get_current_wd (panel->cwd, sizeof (panel->cwd) - 2);
1211 strcpy (panel->lwd, ".");
1213 panel->hist_name = g_strconcat ("Dir Hist ", panel_name, (char *) NULL);
1214 panel->dir_history = history_get (panel->hist_name);
1215 directory_history_add (panel, panel->cwd);
1217 panel->dir.list = g_new (file_entry, MIN_FILES);
1218 panel->dir.size = MIN_FILES;
1219 panel->active = 0;
1220 panel->filter = 0;
1221 panel->split = 0;
1222 panel->top_file = 0;
1223 panel->selected = 0;
1224 panel->marked = 0;
1225 panel->total = 0;
1226 panel->reverse = 0;
1227 panel->dirty = 1;
1228 panel->searching = 0;
1229 panel->dirs_marked = 0;
1230 panel->is_panelized = 0;
1231 panel->format = 0;
1232 panel->status_format = 0;
1233 panel->format_modified = 1;
1235 panel->panel_name = g_strdup (panel_name);
1236 panel->user_format = g_strdup (DEFAULT_USER_FORMAT);
1238 for (i = 0; i < LIST_TYPES; i++)
1239 panel->user_status_format[i] = g_strdup (DEFAULT_USER_FORMAT);
1241 panel->search_buffer[0] = 0;
1242 panel->frame_size = frame_half;
1243 section = g_strconcat ("Temporal:", panel->panel_name, (char *) NULL);
1244 if (!mc_config_has_group (mc_main_config, section)) {
1245 g_free (section);
1246 section = g_strdup (panel->panel_name);
1248 panel_load_setup (panel, section);
1249 g_free (section);
1251 /* Load format strings */
1252 err = set_panel_formats (panel);
1253 if (err) {
1254 set_panel_formats (panel);
1258 /* Because do_load_dir lists files in current directory */
1259 if (wpath)
1260 mc_chdir(wpath);
1262 /* Load the default format */
1263 panel->count =
1264 do_load_dir (panel->cwd, &panel->dir, panel->current_sort_field->sort_routine,
1265 panel->reverse, panel->case_sensitive,
1266 panel->exec_first, panel->filter);
1268 /* Restore old right path */
1269 if (wpath)
1270 mc_chdir(curdir);
1272 return panel;
1275 void
1276 panel_reload (WPanel *panel)
1278 struct stat current_stat;
1280 if (fast_reload && !stat (panel->cwd, &current_stat)
1281 && current_stat.st_ctime == panel->dir_stat.st_ctime
1282 && current_stat.st_mtime == panel->dir_stat.st_mtime)
1283 return;
1285 while (mc_chdir (panel->cwd) == -1) {
1286 char *last_slash;
1288 if (panel->cwd[0] == PATH_SEP && panel->cwd[1] == 0) {
1289 panel_clean_dir (panel);
1290 panel->count = set_zero_dir (&panel->dir);
1291 return;
1293 last_slash = strrchr (panel->cwd, PATH_SEP);
1294 if (!last_slash || last_slash == panel->cwd)
1295 strcpy (panel->cwd, PATH_SEP_STR);
1296 else
1297 *last_slash = 0;
1298 memset (&(panel->dir_stat), 0, sizeof (panel->dir_stat));
1299 show_dir (panel);
1302 panel->count =
1303 do_reload_dir (panel->cwd, &panel->dir, panel->current_sort_field->sort_routine,
1304 panel->count, panel->reverse, panel->case_sensitive,
1305 panel->exec_first, panel->filter);
1307 panel->dirty = 1;
1308 if (panel->selected >= panel->count)
1309 do_select (panel, panel->count - 1);
1311 recalculate_panel_summary (panel);
1314 static void
1315 panel_paint_sort_info(WPanel *panel)
1317 struct hotkey_t hk;
1318 const char *sort_sign = (panel->reverse) ? panel_sort_down_sign : panel_sort_up_sign;
1319 char *str, *hotkey;
1320 gsize len=6;
1322 /* get hotkey from field description */
1323 hk = parse_hotkey (_(panel->current_sort_field->title_hotkey));
1324 if (hk.hotkey) {
1325 hotkey = g_strdup(hk.hotkey);
1326 } else {
1327 /* if field don't have hotkey - use first char of field name */
1328 hotkey = g_strdup(panel->current_sort_field->id);
1329 hotkey[1] = '\0';
1331 release_hotkey (hk);
1333 /* transform to lower case */
1334 str = hotkey;
1335 str_tolower (hotkey, &str, &len);
1337 str = g_strdup_printf("%s%s",sort_sign, hotkey);
1338 g_free(hotkey);
1340 widget_move (&panel->widget, 1, 1);
1341 tty_print_string (str);
1343 g_free(str);
1346 static void
1347 paint_frame (WPanel *panel)
1349 int side, width;
1350 GString *format_txt;
1352 if (!panel->split)
1353 adjust_top_file (panel);
1355 widget_erase (&panel->widget);
1356 show_dir (panel);
1358 widget_move (&panel->widget, 1, 1);
1360 for (side = 0; side <= panel->split; side++){
1361 format_e *format;
1363 if (side){
1364 tty_setcolor (NORMAL_COLOR);
1365 tty_print_one_vline ();
1366 width = panel->widget.cols - panel->widget.cols/2 - 1;
1367 } else if (panel->split)
1368 width = panel->widget.cols/2 - 3;
1369 else
1370 width = panel->widget.cols - 2;
1372 format_txt = g_string_new("");
1373 for (format = panel->format; format; format = format->next){
1374 if (format->string_fn){
1375 g_string_set_size(format_txt, 0);
1377 if (panel->list_type == list_long && strcmp (format->id, panel->current_sort_field->id) == 0)
1378 g_string_append (format_txt, (panel->reverse) ? panel_sort_down_sign : panel_sort_up_sign);
1380 g_string_append (format_txt, format->title);
1381 if (strcmp (format->id, "name") == 0 && panel->filter && *panel->filter) {
1382 g_string_append (format_txt, " [");
1383 g_string_append (format_txt, panel->filter);
1384 g_string_append (format_txt, "]");
1387 tty_setcolor (MARKED_COLOR);
1388 tty_print_string (str_fit_to_term (format_txt->str, format->field_len,
1389 J_CENTER_LEFT));
1390 width -= format->field_len;
1391 } else {
1392 tty_setcolor (NORMAL_COLOR);
1393 tty_print_one_vline ();
1394 width--;
1397 g_string_free(format_txt, TRUE);
1399 if (width > 0)
1400 tty_draw_hline (-1, -1, ' ', width);
1403 if (panel->list_type != list_long)
1404 panel_paint_sort_info(panel);
1407 static const char *
1408 parse_panel_size (WPanel *panel, const char *format, int isstatus)
1410 int frame = frame_half;
1411 format = skip_separators (format);
1413 if (!strncmp (format, "full", 4)){
1414 frame = frame_full;
1415 format += 4;
1416 } else if (!strncmp (format, "half", 4)){
1417 frame = frame_half;
1418 format += 4;
1421 if (!isstatus){
1422 panel->frame_size = frame;
1423 panel->split = 0;
1426 /* Now, the optional column specifier */
1427 format = skip_separators (format);
1429 if (*format == '1' || *format == '2'){
1430 if (!isstatus)
1431 panel->split = *format == '2';
1432 format++;
1435 if (!isstatus)
1436 panel_update_cols (&(panel->widget), panel->frame_size);
1438 return skip_separators (format);
1441 /* Format is:
1443 all := panel_format? format
1444 panel_format := [full|half] [1|2]
1445 format := one_format_e
1446 | format , one_format_e
1448 one_format_e := just format.id [opt_size]
1449 just := [<=>]
1450 opt_size := : size [opt_expand]
1451 size := [0-9]+
1452 opt_expand := +
1456 static format_e *
1457 parse_display_format (WPanel *panel, const char *format, char **error, int isstatus, int *res_total_cols)
1459 format_e *darr, *old = 0, *home = 0; /* The formats we return */
1460 int total_cols = 0; /* Used columns by the format */
1461 int set_justify; /* flag: set justification mode? */
1462 align_crt_t justify = J_LEFT; /* Which mode. */
1463 size_t i;
1465 static size_t i18n_timelength = 0; /* flag: check ?Time length at startup */
1467 *error = 0;
1469 if (i18n_timelength == 0) {
1470 i18n_timelength = i18n_checktimelength (); /* Musn't be 0 */
1472 for (i = 0; panel_fields[i].id != NULL; i++)
1473 if (strcmp ("time", panel_fields[i].id + 1) == 0)
1474 panel_fields [i].min_size = i18n_timelength;
1478 * This makes sure that the panel and mini status full/half mode
1479 * setting is equal
1481 format = parse_panel_size (panel, format, isstatus);
1483 while (*format){ /* format can be an empty string */
1484 int found = 0;
1486 darr = g_new (format_e, 1);
1488 /* I'm so ugly, don't look at me :-) */
1489 if (!home)
1490 home = old = darr;
1492 old->next = darr;
1493 darr->next = 0;
1494 old = darr;
1496 format = skip_separators (format);
1498 if (strchr ("<=>", *format)){
1499 set_justify = 1;
1500 switch (*format)
1502 case '<':
1503 justify = J_LEFT;
1504 break;
1505 case '=':
1506 justify = J_CENTER;
1507 break;
1508 case '>':
1509 default:
1510 justify = J_RIGHT;
1511 break;
1513 format = skip_separators (format+1);
1514 } else
1515 set_justify = 0;
1517 for (i = 0; panel_fields[i].id != NULL; i++) {
1518 size_t klen = strlen (panel_fields [i].id);
1520 if (strncmp (format, panel_fields [i].id, klen) != 0)
1521 continue;
1523 format += klen;
1525 darr->requested_field_len = panel_fields [i].min_size;
1526 darr->string_fn = panel_fields [i].string_fn;
1527 if (panel_fields [i].title [0])
1528 darr->title = _(panel_fields [i].title);
1529 else
1530 darr->title = "";
1531 darr->id = panel_fields [i].id;
1532 darr->expand = panel_fields [i].expands;
1533 darr->just_mode = panel_fields [i].default_just;
1535 if (set_justify) {
1536 if (IS_FIT(darr->just_mode))
1537 darr->just_mode = MAKE_FIT(justify);
1538 else
1539 darr->just_mode = justify;
1541 found = 1;
1543 format = skip_separators (format);
1545 /* If we have a size specifier */
1546 if (*format == ':'){
1547 int req_length;
1549 /* If the size was specified, we don't want
1550 * auto-expansion by default
1552 darr->expand = 0;
1553 format++;
1554 req_length = atoi (format);
1555 darr->requested_field_len = req_length;
1557 format = skip_numbers (format);
1559 /* Now, if they insist on expansion */
1560 if (*format == '+'){
1561 darr->expand = 1;
1562 format++;
1567 break;
1569 if (!found){
1570 char *tmp_format = g_strdup (format);
1572 int pos = min (8, strlen (format));
1573 delete_format (home);
1574 tmp_format [pos] = 0;
1575 *error = g_strconcat (_("Unknown tag on display format: "), tmp_format, (char *) NULL);
1576 g_free (tmp_format);
1577 return 0;
1579 total_cols += darr->requested_field_len;
1582 *res_total_cols = total_cols;
1583 return home;
1586 static format_e *
1587 use_display_format (WPanel *panel, const char *format, char **error, int isstatus)
1589 #define MAX_EXPAND 4
1590 int expand_top = 0; /* Max used element in expand */
1591 int usable_columns; /* Usable columns in the panel */
1592 int total_cols = 0;
1593 int i;
1594 format_e *darr, *home;
1596 if (!format)
1597 format = DEFAULT_USER_FORMAT;
1599 home = parse_display_format (panel, format, error, isstatus, &total_cols);
1601 if (*error)
1602 return 0;
1604 panel->dirty = 1;
1606 /* Status needn't to be split */
1607 usable_columns = ((panel->widget.cols-2)/((isstatus)
1609 : (panel->split+1))) - (!isstatus && panel->split);
1611 /* Look for the expandable fields and set field_len based on the requested field len */
1612 for (darr = home; darr && expand_top < MAX_EXPAND; darr = darr->next){
1613 darr->field_len = darr->requested_field_len;
1614 if (darr->expand)
1615 expand_top++;
1618 /* If we used more columns than the available columns, adjust that */
1619 if (total_cols > usable_columns){
1620 int pdif, dif = total_cols - usable_columns;
1622 while (dif){
1623 pdif = dif;
1624 for (darr = home; darr; darr = darr->next){
1625 if (dif && darr->field_len - 1){
1626 darr->field_len--;
1627 dif--;
1631 /* avoid endless loop if num fields > 40 */
1632 if (pdif == dif)
1633 break;
1635 total_cols = usable_columns; /* give up, the rest should be truncated */
1638 /* Expand the available space */
1639 if ((usable_columns > total_cols) && expand_top){
1640 int spaces = (usable_columns - total_cols) / expand_top;
1641 int extra = (usable_columns - total_cols) % expand_top;
1643 for (i = 0, darr = home; darr && (i < expand_top); darr = darr->next)
1644 if (darr->expand){
1645 darr->field_len += (spaces + ((i == 0) ? extra : 0));
1646 i++;
1649 return home;
1652 /* Switches the panel to the mode specified in the format */
1653 /* Seting up both format and status string. Return: 0 - on success; */
1654 /* 1 - format error; 2 - status error; 3 - errors in both formats. */
1656 set_panel_formats (WPanel *p)
1658 format_e *form;
1659 char *err;
1660 int retcode = 0;
1662 form = use_display_format (p, panel_format (p), &err, 0);
1664 if (err){
1665 g_free (err);
1666 retcode = 1;
1668 else {
1669 if (p->format)
1670 delete_format (p->format);
1672 p->format = form;
1675 if (show_mini_info){
1677 form = use_display_format (p, mini_status_format (p), &err, 1);
1679 if (err){
1680 g_free (err);
1681 retcode += 2;
1683 else {
1684 if (p->status_format)
1685 delete_format (p->status_format);
1687 p->status_format = form;
1691 panel_format_modified (p);
1692 panel_update_cols (&(p->widget), p->frame_size);
1694 if (retcode)
1695 message (D_ERROR, _("Warning" ), _( "User supplied format looks invalid, reverting to default." ) );
1696 if (retcode & 0x01){
1697 g_free (p->user_format);
1698 p->user_format = g_strdup (DEFAULT_USER_FORMAT);
1700 if (retcode & 0x02){
1701 g_free (p->user_status_format [p->list_type]);
1702 p->user_status_format [p->list_type] = g_strdup (DEFAULT_USER_FORMAT);
1705 return retcode;
1708 /* Given the panel->view_type returns the format string to be parsed */
1709 static const char *
1710 panel_format (WPanel *panel)
1712 switch (panel->list_type){
1714 case list_long:
1715 return "full perm space nlink space owner space group space size space mtime space name";
1717 case list_brief:
1718 return "half 2 type name";
1720 case list_user:
1721 return panel->user_format;
1723 default:
1724 case list_full:
1725 return "half type name | size | mtime";
1729 static const char *
1730 mini_status_format (WPanel *panel)
1732 if (panel->user_mini_status)
1733 return panel->user_status_format [panel->list_type];
1735 switch (panel->list_type){
1737 case list_long:
1738 return "full perm space nlink space owner space group space size space mtime space name";
1740 case list_brief:
1741 return "half type name space bsize space perm space";
1743 case list_full:
1744 return "half type name";
1746 default:
1747 case list_user:
1748 return panel->user_format;
1752 /* */
1753 /* Panel operation commands */
1754 /* */
1756 /* Used to emulate Lynx's entering leaving a directory with the arrow keys */
1757 static cb_ret_t
1758 maybe_cd (int move_up_dir)
1760 if (navigate_with_arrows) {
1761 if (!cmdline->buffer[0]) {
1762 if (move_up_dir) {
1763 do_cd ("..", cd_exact);
1764 return MSG_HANDLED;
1766 if (S_ISDIR (selection (current_panel)->st.st_mode)
1767 || link_isdir (selection (current_panel))) {
1768 do_cd (selection (current_panel)->fname, cd_exact);
1769 return MSG_HANDLED;
1773 return MSG_NOT_HANDLED;
1776 /* Returns the number of items in the given panel */
1777 static int
1778 ITEMS (WPanel *p)
1780 if (p->split)
1781 return llines (p) * 2;
1782 else
1783 return llines (p);
1786 /* Select current item and readjust the panel */
1787 void
1788 select_item (WPanel *panel)
1790 int items = ITEMS (panel);
1792 /* Although currently all over the code we set the selection and
1793 top file to decent values before calling select_item, I could
1794 forget it someday, so it's better to do the actual fitting here */
1796 if (panel->top_file < 0)
1797 panel->top_file = 0;
1799 if (panel->selected < 0)
1800 panel->selected = 0;
1802 if (panel->selected > panel->count - 1)
1803 panel->selected = panel->count - 1;
1805 if (panel->top_file > panel->count - 1)
1806 panel->top_file = panel->count - 1;
1808 if ((panel->count - panel->top_file) < items) {
1809 panel->top_file = panel->count - items;
1810 if (panel->top_file < 0)
1811 panel->top_file = 0;
1814 if (panel->selected < panel->top_file)
1815 panel->top_file = panel->selected;
1817 if ((panel->selected - panel->top_file) >= items)
1818 panel->top_file = panel->selected - items + 1;
1820 panel->dirty = 1;
1822 execute_hooks (select_file_hook);
1825 /* Clears all files in the panel, used only when one file was marked */
1826 void
1827 unmark_files (WPanel *panel)
1829 int i;
1831 if (!panel->marked)
1832 return;
1833 for (i = 0; i < panel->count; i++)
1834 file_mark (panel, i, 0);
1836 panel->dirs_marked = 0;
1837 panel->marked = 0;
1838 panel->total = 0;
1841 static void
1842 unselect_item (WPanel *panel)
1844 repaint_file (panel, panel->selected, 1, 2*selection (panel)->f.marked, 0);
1847 static void
1848 move_down (WPanel *panel)
1850 if (panel->selected+1 == panel->count)
1851 return;
1853 unselect_item (panel);
1854 panel->selected++;
1855 if (panel->selected - panel->top_file == ITEMS (panel) &&
1856 panel_scroll_pages) {
1857 /* Scroll window half screen */
1858 panel->top_file += ITEMS (panel)/2;
1859 if (panel->top_file > panel->count - ITEMS (panel))
1860 panel->top_file = panel->count - ITEMS (panel);
1861 paint_dir (panel);
1863 select_item (panel);
1866 static void
1867 move_up (WPanel *panel)
1869 if (panel->selected == 0)
1870 return;
1872 unselect_item (panel);
1873 panel->selected--;
1874 if (panel->selected < panel->top_file && panel_scroll_pages) {
1875 /* Scroll window half screen */
1876 panel->top_file -= ITEMS (panel)/2;
1877 if (panel->top_file < 0)
1878 panel->top_file = 0;
1879 paint_dir (panel);
1881 select_item (panel);
1884 /* Changes the selection by lines (may be negative) */
1885 static void
1886 move_selection (WPanel *panel, int lines)
1888 int new_pos;
1889 int adjust = 0;
1891 new_pos = panel->selected + lines;
1892 if (new_pos >= panel->count)
1893 new_pos = panel->count-1;
1895 if (new_pos < 0)
1896 new_pos = 0;
1898 unselect_item (panel);
1899 panel->selected = new_pos;
1901 if (panel->selected - panel->top_file >= ITEMS (panel)){
1902 panel->top_file += lines;
1903 adjust = 1;
1906 if (panel->selected - panel->top_file < 0){
1907 panel->top_file += lines;
1908 adjust = 1;
1911 if (adjust){
1912 if (panel->top_file > panel->selected)
1913 panel->top_file = panel->selected;
1914 if (panel->top_file < 0)
1915 panel->top_file = 0;
1916 paint_dir (panel);
1918 select_item (panel);
1921 static cb_ret_t
1922 move_left (WPanel *panel)
1924 if (panel->split) {
1925 move_selection (panel, -llines (panel));
1926 return MSG_HANDLED;
1927 } else
1928 return maybe_cd (1); /* cd .. */
1931 static int
1932 move_right (WPanel *panel)
1934 if (panel->split) {
1935 move_selection (panel, llines (panel));
1936 return MSG_HANDLED;
1937 } else
1938 return maybe_cd (0); /* cd (selection) */
1941 static void
1942 prev_page (WPanel *panel)
1944 int items;
1946 if (!panel->selected && !panel->top_file)
1947 return;
1948 unselect_item (panel);
1949 items = ITEMS (panel);
1950 if (panel->top_file < items)
1951 items = panel->top_file;
1952 if (!items)
1953 panel->selected = 0;
1954 else
1955 panel->selected -= items;
1956 panel->top_file -= items;
1958 /* This keeps the selection in a reasonable place */
1959 if (panel->selected < 0)
1960 panel->selected = 0;
1961 if (panel->top_file < 0)
1962 panel->top_file = 0;
1963 select_item (panel);
1964 paint_dir (panel);
1967 static void
1968 ctrl_prev_page (WPanel *panel)
1970 (void) panel;
1971 do_cd ("..", cd_exact);
1974 static void
1975 next_page (WPanel *panel)
1977 int items;
1979 if (panel->selected == panel->count - 1)
1980 return;
1981 unselect_item (panel);
1982 items = ITEMS (panel);
1983 if (panel->top_file > panel->count - 2 * items)
1984 items = panel->count - items - panel->top_file;
1985 if (panel->top_file + items < 0)
1986 items = -panel->top_file;
1987 if (!items)
1988 panel->selected = panel->count - 1;
1989 else
1990 panel->selected += items;
1991 panel->top_file += items;
1993 /* This keeps the selection in it's relative position */
1994 if (panel->selected >= panel->count)
1995 panel->selected = panel->count - 1;
1996 if (panel->top_file >= panel->count)
1997 panel->top_file = panel->count - 1;
1998 select_item (panel);
1999 paint_dir (panel);
2002 static void
2003 ctrl_next_page (WPanel *panel)
2005 if ((S_ISDIR (selection (panel)->st.st_mode)
2006 || link_isdir (selection (panel)))) {
2007 do_cd (selection (panel)->fname, cd_exact);
2011 static void
2012 goto_top_file (WPanel *panel)
2014 unselect_item (panel);
2015 panel->selected = panel->top_file;
2016 select_item (panel);
2019 static void
2020 goto_middle_file (WPanel *panel)
2022 unselect_item (panel);
2023 panel->selected = panel->top_file + (ITEMS (panel)/2);
2024 if (panel->selected >= panel->count)
2025 panel->selected = panel->count - 1;
2026 select_item (panel);
2029 static void
2030 goto_bottom_file (WPanel *panel)
2032 unselect_item (panel);
2033 panel->selected = panel->top_file + ITEMS (panel)-1;
2034 if (panel->selected >= panel->count)
2035 panel->selected = panel->count - 1;
2036 select_item (panel);
2039 static void
2040 move_home (WPanel *panel)
2042 if (panel->selected == 0)
2043 return;
2044 unselect_item (panel);
2046 if (torben_fj_mode){
2047 int middle_pos = panel->top_file + (ITEMS (panel)/2);
2049 if (panel->selected > middle_pos){
2050 goto_middle_file (panel);
2051 return;
2053 if (panel->selected != panel->top_file){
2054 goto_top_file (panel);
2055 return;
2059 panel->top_file = 0;
2060 panel->selected = 0;
2062 paint_dir (panel);
2063 select_item (panel);
2066 static void
2067 move_end (WPanel *panel)
2069 if (panel->selected == panel->count-1)
2070 return;
2071 unselect_item (panel);
2072 if (torben_fj_mode){
2073 int middle_pos = panel->top_file + (ITEMS (panel)/2);
2075 if (panel->selected < middle_pos){
2076 goto_middle_file (panel);
2077 return;
2079 if (panel->selected != (panel->top_file + ITEMS(panel)-1)){
2080 goto_bottom_file (panel);
2081 return;
2085 panel->selected = panel->count-1;
2086 paint_dir (panel);
2087 select_item (panel);
2090 /* Recalculate the panels summary information, used e.g. when marked
2091 files might have been removed by an external command */
2092 void
2093 recalculate_panel_summary (WPanel *panel)
2095 int i;
2097 panel->marked = 0;
2098 panel->dirs_marked = 0;
2099 panel->total = 0;
2101 for (i = 0; i < panel->count; i++)
2102 if (panel->dir.list [i].f.marked){
2103 /* do_file_mark will return immediately if newmark == oldmark.
2104 So we have to first unmark it to get panel's summary information
2105 updated. (Norbert) */
2106 panel->dir.list [i].f.marked = 0;
2107 do_file_mark (panel, i, 1);
2111 /* This routine marks a file or a directory */
2112 void
2113 do_file_mark (WPanel *panel, int idx, int mark)
2115 if (panel->dir.list[idx].f.marked == mark)
2116 return;
2118 /* Only '..' can't be marked, '.' isn't visible */
2119 if (!strcmp (panel->dir.list[idx].fname, ".."))
2120 return;
2122 file_mark (panel, idx, mark);
2123 if (panel->dir.list[idx].f.marked) {
2124 panel->marked++;
2125 if (S_ISDIR (panel->dir.list[idx].st.st_mode)) {
2126 if (panel->dir.list[idx].f.dir_size_computed)
2127 panel->total += panel->dir.list[idx].st.st_size;
2128 panel->dirs_marked++;
2129 } else
2130 panel->total += panel->dir.list[idx].st.st_size;
2131 set_colors (panel);
2132 } else {
2133 if (S_ISDIR (panel->dir.list[idx].st.st_mode)) {
2134 if (panel->dir.list[idx].f.dir_size_computed)
2135 panel->total -= panel->dir.list[idx].st.st_size;
2136 panel->dirs_marked--;
2137 } else
2138 panel->total -= panel->dir.list[idx].st.st_size;
2139 panel->marked--;
2143 static void
2144 do_mark_file (WPanel *panel, int do_move)
2146 do_file_mark (panel, panel->selected,
2147 selection (panel)->f.marked ? 0 : 1);
2148 if (mark_moves_down && do_move)
2149 move_down (panel);
2152 static void
2153 mark_file (WPanel *panel)
2155 do_mark_file (panel, 1);
2158 /* Incremental search of a file name in the panel */
2159 static void
2160 do_search (WPanel *panel, int c_code)
2162 size_t l, max, buf_max;
2163 int i, sel;
2164 int wrapped = 0;
2165 char *act;
2167 l = strlen (panel->search_buffer);
2168 if (c_code == KEY_BACKSPACE) {
2169 if (l != 0) {
2170 act = panel->search_buffer + l;
2171 str_prev_noncomb_char (&act, panel->search_buffer);
2172 act[0] = '\0';
2174 panel->search_chpoint = 0;
2175 } else {
2176 if (c_code && (gsize) panel->search_chpoint < sizeof (panel->search_char)) {
2177 panel->search_char[panel->search_chpoint] = c_code;
2178 panel->search_chpoint++;
2181 if (panel->search_chpoint > 0) {
2182 switch (str_is_valid_char (panel->search_char, panel->search_chpoint)) {
2183 case -2:
2184 return;
2185 case -1:
2186 panel->search_chpoint = 0;
2187 return;
2188 default:
2189 if (l + panel->search_chpoint < sizeof (panel->search_buffer)) {
2190 memcpy (panel->search_buffer + l, panel->search_char,
2191 panel->search_chpoint);
2192 l+= panel->search_chpoint;
2193 (panel->search_buffer + l)[0] = '\0';
2194 panel->search_chpoint = 0;
2200 buf_max = panel->case_sensitive ?
2201 str_prefix (panel->search_buffer, panel->search_buffer) :
2202 str_caseprefix (panel->search_buffer, panel->search_buffer);
2203 max = 0;
2204 sel = panel->selected;
2205 for (i = panel->selected; !wrapped || i != panel->selected; i++) {
2206 if (i >= panel->count) {
2207 i = 0;
2208 if (wrapped)
2209 break;
2210 wrapped = 1;
2212 l = panel->case_sensitive ?
2213 str_prefix (panel->dir.list[i].fname, panel->search_buffer) :
2214 str_caseprefix (panel->dir.list[i].fname, panel->search_buffer);
2215 if (l > max) {
2216 max = l;
2217 sel = i;
2218 if (max == buf_max) break;
2222 unselect_item (panel);
2223 panel->selected = sel;
2224 select_item (panel);
2226 act = panel->search_buffer + strlen (panel->search_buffer);
2227 while (max < buf_max) {
2228 str_prev_char_safe (&act);
2229 act[0] = '\0';
2230 buf_max = panel->case_sensitive ?
2231 str_prefix (panel->search_buffer, panel->search_buffer) :
2232 str_caseprefix (panel->search_buffer, panel->search_buffer);
2235 paint_panel (panel);
2238 static void
2239 start_search (WPanel *panel)
2241 if (panel->searching){
2242 if (panel->selected+1 == panel->count)
2243 panel->selected = 0;
2244 else
2245 move_down (panel);
2246 do_search (panel, 0);
2247 } else {
2248 panel->searching = 1;
2249 panel->search_buffer[0] = '\0';
2250 panel->search_char[0] = '\0';
2251 panel->search_chpoint = 0;
2252 display_mini_info (panel);
2253 mc_refresh ();
2257 /* Return 1 if the Enter key has been processed, 0 otherwise */
2258 static int
2259 do_enter_on_file_entry (file_entry *fe)
2261 char *full_name;
2264 * Directory or link to directory - change directory.
2265 * Try the same for the entries on which mc_lstat() has failed.
2267 if (S_ISDIR (fe->st.st_mode) || link_isdir (fe)
2268 || (fe->st.st_mode == 0)) {
2269 if (!do_cd (fe->fname, cd_exact))
2270 message (D_ERROR, MSG_ERROR, _("Cannot change directory"));
2271 return 1;
2274 /* Try associated command */
2275 if (regex_command (fe->fname, "Open", 0) != 0)
2276 return 1;
2278 /* Check if the file is executable */
2279 full_name = concat_dir_and_file (current_panel->cwd, fe->fname);
2280 if (!is_exe (fe->st.st_mode) || !if_link_is_exe (full_name, fe)) {
2281 g_free (full_name);
2282 return 0;
2284 g_free (full_name);
2286 if (confirm_execute) {
2287 if (query_dialog
2288 (_(" The Midnight Commander "),
2289 _(" Do you really want to execute? "), D_NORMAL, 2, _("&Yes"),
2290 _("&No")) != 0)
2291 return 1;
2293 #ifdef USE_VFS
2294 if (!vfs_current_is_local ()) {
2295 char *tmp;
2296 int ret;
2298 tmp = concat_dir_and_file (vfs_get_current_dir (), fe->fname);
2299 ret = mc_setctl (tmp, VFS_SETCTL_RUN, NULL);
2300 g_free (tmp);
2301 /* We took action only if the dialog was shown or the execution
2302 * was successful */
2303 return confirm_execute || (ret == 0);
2305 #endif
2308 char *tmp = name_quote (fe->fname, 0);
2309 char *cmd = g_strconcat (".", PATH_SEP_STR, tmp, (char *) NULL);
2310 g_free (tmp);
2311 shell_execute (cmd, 0);
2312 g_free (cmd);
2315 return 1;
2318 static int
2319 do_enter (WPanel *panel)
2321 return do_enter_on_file_entry (selection (panel));
2324 static void
2325 chdir_other_panel (WPanel *panel)
2327 char *new_dir;
2328 char *sel_entry = NULL;
2330 if (get_other_type () != view_listing) {
2331 set_display_type (get_other_index (), view_listing);
2334 if (!S_ISDIR (panel->dir.list [panel->selected].st.st_mode)) {
2335 new_dir = concat_dir_and_file (panel->cwd, "..");
2336 sel_entry = strrchr(panel->cwd, PATH_SEP);
2337 } else
2338 new_dir = concat_dir_and_file (panel->cwd, panel->dir.list [panel->selected].fname);
2340 change_panel ();
2341 do_cd (new_dir, cd_exact);
2342 if (sel_entry)
2343 try_to_select (current_panel, sel_entry);
2344 change_panel ();
2346 move_down (panel);
2348 g_free (new_dir);
2352 * Make the current directory of the current panel also the current
2353 * directory of the other panel. Put the other panel to the listing
2354 * mode if needed. If the current panel is panelized, the other panel
2355 * doesn't become panelized.
2357 static void
2358 sync_other_panel (WPanel *panel)
2360 if (get_other_type () != view_listing) {
2361 set_display_type (get_other_index (), view_listing);
2364 do_panel_cd (other_panel, current_panel->cwd, cd_exact);
2366 /* try to select current filename on the other panel */
2367 if (!panel->is_panelized) {
2368 try_to_select (other_panel, selection (panel)->fname);
2372 static void
2373 chdir_to_readlink (WPanel *panel)
2375 char *new_dir;
2377 if (get_other_type () != view_listing)
2378 return;
2380 if (S_ISLNK (panel->dir.list [panel->selected].st.st_mode)) {
2381 char buffer [MC_MAXPATHLEN], *p;
2382 int i;
2383 struct stat st;
2385 i = readlink (selection (panel)->fname, buffer, MC_MAXPATHLEN - 1);
2386 if (i < 0)
2387 return;
2388 if (mc_stat (selection (panel)->fname, &st) < 0)
2389 return;
2390 buffer [i] = 0;
2391 if (!S_ISDIR (st.st_mode)) {
2392 p = strrchr (buffer, PATH_SEP);
2393 if (p && !p[1]) {
2394 *p = 0;
2395 p = strrchr (buffer, PATH_SEP);
2397 if (!p)
2398 return;
2399 p[1] = 0;
2401 if (*buffer == PATH_SEP)
2402 new_dir = g_strdup (buffer);
2403 else
2404 new_dir = concat_dir_and_file (panel->cwd, buffer);
2406 change_panel ();
2407 do_cd (new_dir, cd_exact);
2408 change_panel ();
2410 move_down (panel);
2412 g_free (new_dir);
2416 static gsize
2417 panel_get_format_field_count(WPanel *panel)
2419 format_e *format;
2420 gsize index;
2421 for (
2422 index=0, format = panel->format;
2423 format != NULL;
2424 format = format->next, index++
2426 return index;
2430 function return 0 if not found and REAL_INDEX+1 if found
2432 static gsize
2433 panel_get_format_field_index_by_name(WPanel *panel, const char *name)
2435 format_e *format;
2436 gsize index;
2438 for (
2439 index=1, format = panel->format;
2440 ! ( format == NULL || strcmp(format->title, _(name)) == 0 );
2441 format = format->next, index++
2443 if (format == NULL)
2444 index = 0;
2446 return index;
2449 format_e *
2450 panel_get_format_field_by_index(WPanel *panel, gsize index)
2452 format_e *format;
2453 for (
2454 format = panel->format;
2455 ! ( format == NULL || index == 0 );
2456 format = format->next, index--
2458 return format;
2461 static const panel_field_t *
2462 panel_get_sortable_field_by_format(WPanel *panel, gsize index)
2464 const panel_field_t *pfield;
2465 format_e *format;
2467 format = panel_get_format_field_by_index(panel, index);
2468 if (format == NULL)
2469 return NULL;
2470 pfield = panel_get_field_by_title(format->title);
2471 if (pfield == NULL)
2472 return NULL;
2473 if (pfield->sort_routine == NULL)
2474 return NULL;
2475 return pfield;
2478 static void
2479 panel_toggle_sort_order_prev(WPanel *panel)
2481 gsize index, i;
2483 const panel_field_t *pfield = NULL;
2485 index = panel_get_format_field_index_by_name(panel, panel->current_sort_field->title);
2487 if (index > 1){
2488 /* search for prev sortable column in panel format */
2489 for (
2490 i = index-1 ;
2491 i != 0 && (pfield = panel_get_sortable_field_by_format(panel, i-1)) == NULL ;
2496 if ( pfield == NULL) {
2497 /* Sortable field not found. Try to search in each array */
2498 for (
2499 i = panel_get_format_field_count(panel) ;
2500 i != 0 && (pfield = panel_get_sortable_field_by_format(panel, i-1)) == NULL ;
2504 if ( pfield == NULL)
2505 return;
2506 panel->current_sort_field = pfield;
2507 panel_set_sort_order(panel, panel->current_sort_field);
2511 static void
2512 panel_toggle_sort_order_next(WPanel *panel)
2514 gsize index, i;
2515 const panel_field_t *pfield = NULL;
2516 gsize format_field_count = panel_get_format_field_count(panel);
2518 index = panel_get_format_field_index_by_name(panel, panel->current_sort_field->title);
2520 if (index != 0 && index != format_field_count){
2521 /* search for prev sortable column in panel format */
2522 for (
2523 i = index;
2524 i != format_field_count && (pfield = panel_get_sortable_field_by_format(panel, i)) == NULL ;
2529 if ( pfield == NULL) {
2530 /* Sortable field not found. Try to search in each array */
2531 for (
2532 i = 0 ;
2533 i != format_field_count && (pfield = panel_get_sortable_field_by_format(panel, i)) == NULL ;
2537 if ( pfield == NULL)
2538 return;
2539 panel->current_sort_field = pfield;
2540 panel_set_sort_order(panel, panel->current_sort_field);
2543 static void
2544 panel_select_sort_order(WPanel *panel)
2546 const panel_field_t *sort_order;
2547 sort_order = sort_box (panel->current_sort_field, &panel->reverse,
2548 &panel->case_sensitive,
2549 &panel->exec_first);
2550 if (sort_order == NULL)
2551 return;
2552 panel->current_sort_field = sort_order;
2553 panel_set_sort_order (panel, panel->current_sort_field);
2557 static void
2558 panel_set_sort_type_by_id(WPanel *panel, const char *name)
2560 const panel_field_t *sort_order;
2562 if (strcmp(panel->current_sort_field->id, name) != 0) {
2563 sort_order = panel_get_field_by_id (name);
2564 if (sort_order == NULL)
2565 return;
2566 panel->current_sort_field = sort_order;
2567 } else {
2568 panel->reverse = ! panel->reverse;
2570 panel_set_sort_order (panel, panel->current_sort_field);
2573 typedef void (*panel_key_callback) (WPanel *);
2575 static void cmd_do_enter(WPanel *wp) { (void) do_enter(wp); }
2576 static void cmd_view_simple(WPanel *wp) { (void) wp; view_simple_cmd(); }
2577 static void cmd_edit_new(WPanel *wp) { (void) wp; edit_cmd_new(); }
2578 static void cmd_copy_local(WPanel *wp) { (void) wp;copy_cmd_local(); }
2579 static void cmd_rename_local(WPanel *wp) { (void) wp;ren_cmd_local(); }
2580 static void cmd_delete_local(WPanel *wp) { (void) wp;delete_cmd_local(); }
2581 static void cmd_select(WPanel *wp) { (void) wp;select_cmd(); }
2582 static void cmd_unselect(WPanel *wp) { (void) wp;unselect_cmd(); }
2583 static void cmd_reverse_selection(WPanel *wp) { (void) wp;reverse_selection_cmd(); }
2585 static cb_ret_t
2586 panel_execute_cmd (WPanel *panel, int command)
2588 int res = MSG_HANDLED;
2590 switch (command) {
2591 case CK_PanelChdirOtherPanel:
2592 chdir_other_panel (panel);
2593 break;
2594 case CK_PanelChdirToReadlink:
2595 chdir_to_readlink (panel);
2596 break;
2597 case CK_PanelCmdCopyLocal:
2598 cmd_copy_local (panel);
2599 break;
2600 case CK_PanelCmdDeleteLocal:
2601 cmd_delete_local (panel);
2602 break;
2603 case CK_PanelCmdDoEnter:
2604 cmd_do_enter (panel);
2605 break;
2606 case CK_PanelCmdViewSimple:
2607 cmd_view_simple (panel);
2608 break;
2609 case CK_PanelCmdEditNew:
2610 cmd_edit_new (panel);
2611 break;
2612 case CK_PanelCmdRenameLocal:
2613 cmd_rename_local (panel);
2614 break;
2615 case CK_PanelCmdReverseSelection:
2616 cmd_reverse_selection (panel);
2617 break;
2618 case CK_PanelCmdSelect:
2619 cmd_select (panel);
2620 break;
2621 case CK_PanelCmdUnselect:
2622 cmd_unselect (panel);
2623 break;
2624 case CK_PanelNextPage:
2625 next_page (panel);
2626 break;
2627 case CK_PanelPrevPage:
2628 prev_page (panel);
2629 break;
2630 case CK_PanelCtrlNextPage:
2631 ctrl_next_page (panel);
2632 break;
2633 case CK_PanelCtrlPrevPage:
2634 ctrl_prev_page (panel);
2635 break;
2636 case CK_PanelDirectoryHistoryList:
2637 directory_history_list (panel);
2638 break;
2639 case CK_PanelDirectoryHistoryNext:
2640 directory_history_next (panel);
2641 break;
2642 case CK_PanelDirectoryHistoryPrev:
2643 directory_history_prev (panel);
2644 break;
2645 case CK_PanelGotoBottomFile:
2646 goto_bottom_file (panel);
2647 break;
2648 case CK_PanelGotoMiddleFile:
2649 goto_middle_file (panel);
2650 break;
2651 case CK_PanelGotoTopFile:
2652 goto_top_file (panel);
2653 break;
2654 case CK_PanelMarkFile:
2655 mark_file (panel);
2656 break;
2657 case CK_PanelMoveUp:
2658 move_up (panel);
2659 break;
2660 case CK_PanelMoveDown:
2661 move_down (panel);
2662 break;
2663 case CK_PanelMoveLeft:
2664 res = move_left (panel);
2665 break;
2666 case CK_PanelMoveRight:
2667 res = move_right (panel);
2668 break;
2669 case CK_PanelMoveEnd:
2670 move_end (panel);
2671 break;
2672 case CK_PanelMoveHome:
2673 move_home (panel);
2674 break;
2675 case CK_PanelSetPanelEncoding:
2676 set_panel_encoding (panel);
2677 break;
2678 case CK_PanelStartSearch:
2679 start_search (panel);
2680 break;
2681 case CK_PanelSyncOtherPanel:
2682 sync_other_panel (panel);
2683 break;
2684 case CK_PanelSelectSortOrder:
2685 panel_select_sort_order(panel);
2686 break;
2687 case CK_PanelToggleSortOrderPrev:
2688 panel_toggle_sort_order_prev(panel);
2689 break;
2690 case CK_PanelToggleSortOrderNext:
2691 panel_toggle_sort_order_next(panel);
2692 break;
2693 case CK_PanelReverseSort:
2694 panel->reverse = ! panel->reverse;
2695 panel_set_sort_order (panel, panel->current_sort_field);
2696 break;
2697 case CK_PanelSortOrderByName:
2698 panel_set_sort_type_by_id(panel, "name");
2699 break;
2700 case CK_PanelSortOrderByExt:
2701 panel_set_sort_type_by_id(panel, "extension");
2702 break;
2703 case CK_PanelSortOrderBySize:
2704 panel_set_sort_type_by_id(panel, "size");
2705 break;
2706 case CK_PanelSortOrderByMTime:
2707 panel_set_sort_type_by_id(panel, "mtime");
2708 break;
2710 return res;
2713 static cb_ret_t
2714 panel_key (WPanel *panel, int key)
2716 int i;
2717 int res, command;
2719 for (i = 0; panel_map[i].key; i++) {
2720 if (key == panel_map[i].key) {
2721 int old_searching = panel->searching;
2723 if (panel_map[i].command != CK_PanelStartSearch)
2724 panel->searching = 0;
2726 command = panel_map[i].command;
2727 res = panel_execute_cmd (panel, command);
2729 if (res == MSG_NOT_HANDLED)
2730 return res;
2732 if (panel->searching != old_searching)
2733 display_mini_info (panel);
2734 return MSG_HANDLED;
2738 if (torben_fj_mode && key == ALT ('h')) {
2739 goto_middle_file (panel);
2740 return MSG_HANDLED;
2743 if (is_abort_char (key)) {
2744 panel->searching = 0;
2745 display_mini_info (panel);
2746 return MSG_HANDLED;
2749 /* Do not eat characters not meant for the panel below ' ' (e.g. C-l). */
2750 if ((key >= ' ' && key <= 255) || key == KEY_BACKSPACE) {
2751 if (panel->searching) {
2752 do_search (panel, key);
2753 return MSG_HANDLED;
2756 if (!command_prompt) {
2757 start_search (panel);
2758 do_search (panel, key);
2759 return MSG_HANDLED;
2763 return MSG_NOT_HANDLED;
2766 static cb_ret_t
2767 panel_callback (Widget *w, widget_msg_t msg, int parm)
2769 WPanel *panel = (WPanel *) w;
2770 Dlg_head *h = panel->widget.parent;
2772 switch (msg) {
2773 case WIDGET_DRAW:
2774 paint_panel (panel);
2775 return MSG_HANDLED;
2777 case WIDGET_FOCUS:
2778 current_panel = panel;
2779 panel->active = 1;
2780 if (mc_chdir (panel->cwd) != 0) {
2781 char *cwd = strip_password (g_strdup (panel->cwd), 1);
2782 message (D_ERROR, MSG_ERROR, _(" Cannot chdir to \"%s\" \n %s "),
2783 cwd, unix_error_string (errno));
2784 g_free(cwd);
2785 } else
2786 subshell_chdir (panel->cwd);
2788 update_xterm_title_path ();
2789 select_item (panel);
2790 show_dir (panel);
2791 paint_dir (panel);
2792 panel->dirty = 0;
2794 buttonbar_set_label (h, 1, _("Help"), help_cmd);
2795 buttonbar_set_label (h, 2, _("Menu"), user_file_menu_cmd);
2796 buttonbar_set_label (h, 3, _("View"), view_cmd);
2797 buttonbar_set_label (h, 4, _("Edit"), edit_cmd);
2798 buttonbar_set_label (h, 5, _("Copy"), copy_cmd);
2799 buttonbar_set_label (h, 6, _("RenMov"), ren_cmd);
2800 buttonbar_set_label (h, 7, _("Mkdir"), mkdir_cmd);
2801 buttonbar_set_label (h, 8, _("Delete"), delete_cmd);
2802 buttonbar_redraw (h);
2803 return MSG_HANDLED;
2805 case WIDGET_UNFOCUS:
2806 /* Janne: look at this for the multiple panel options */
2807 if (panel->searching){
2808 panel->searching = 0;
2809 display_mini_info (panel);
2811 panel->active = 0;
2812 show_dir (panel);
2813 unselect_item (panel);
2814 return MSG_HANDLED;
2816 case WIDGET_KEY:
2817 return panel_key (panel, parm);
2819 case WIDGET_DESTROY:
2820 panel_destroy (panel);
2821 return MSG_HANDLED;
2823 default:
2824 return default_proc (msg, parm);
2828 void
2829 file_mark (WPanel *panel, int index, int val)
2831 if (panel->dir.list[index].f.marked != val) {
2832 panel->dir.list[index].f.marked = val;
2833 panel->dirty = 1;
2837 /* */
2838 /* Panel mouse events support routines */
2839 /* */
2840 static int mouse_marking = 0;
2842 static void
2843 mouse_toggle_mark (WPanel *panel)
2845 do_mark_file (panel, 0);
2846 mouse_marking = selection (panel)->f.marked;
2849 static void
2850 mouse_set_mark (WPanel *panel)
2852 if (mouse_marking && !(selection (panel)->f.marked))
2853 do_mark_file (panel, 0);
2854 else if (!mouse_marking && (selection (panel)->f.marked))
2855 do_mark_file (panel, 0);
2858 static int
2859 mark_if_marking (WPanel *panel, Gpm_Event *event)
2861 if (event->buttons & GPM_B_RIGHT){
2862 if (event->type & GPM_DOWN)
2863 mouse_toggle_mark (panel);
2864 else
2865 mouse_set_mark (panel);
2866 return 1;
2868 return 0;
2871 /* Determine which column was clicked, and sort the panel on
2872 * that column, or reverse sort on that column if already
2873 * sorted on that column.
2875 static void
2876 mouse_sort_col(Gpm_Event *event, WPanel *panel)
2878 int i;
2879 const char *sort_name = NULL;
2880 panel_field_t *col_sort_format = NULL;
2881 format_e *format;
2883 for (i = 0, format = panel->format; format != NULL; format = format->next) {
2884 i += format->field_len;
2885 if (event->x < i + 1) {
2886 /* found column */
2887 sort_name = format->title;
2888 break;
2892 if (sort_name == NULL)
2893 return;
2895 for(i = 0; panel_fields[i].id != NULL; i++) {
2896 if (!strcmp (sort_name, _(panel_fields[i].title)) && panel_fields[i].sort_routine) {
2897 col_sort_format = &panel_fields[i];
2898 break;
2902 if (!col_sort_format)
2903 return;
2905 if (panel->current_sort_field == col_sort_format) {
2906 /* reverse the sort if clicked column is already the sorted column */
2907 panel->reverse = !panel->reverse;
2908 } else {
2909 /* new sort is forced to be ascending */
2910 panel->reverse = 0;
2912 panel_set_sort_order (panel, col_sort_format);
2917 * Mouse callback of the panel minus repainting.
2918 * If the event is redirected to the menu, *redir is set to 1.
2920 static int
2921 do_panel_event (Gpm_Event *event, WPanel *panel, int *redir)
2923 const int lines = llines (panel);
2924 const gboolean is_active = dlg_widget_active (panel);
2925 const gboolean mouse_down = (event->type & GPM_DOWN) != 0;
2927 /* "." button show/hide hidden files */
2928 if (event->type & GPM_DOWN && event->x == panel->widget.cols - 3 && event->y == 1) {
2929 toggle_show_hidden();
2930 return MOU_NORMAL;
2933 /* sort on clicked column */
2934 if (event->type & GPM_DOWN && event->y == 2) {
2935 mouse_sort_col(event,panel);
2936 return MOU_NORMAL;
2939 /* rest of the upper frame, the menu is invisible - call menu */
2940 if (mouse_down && event->y == 1 && !menubar_visible) {
2941 *redir = 1;
2942 event->x += panel->widget.x;
2943 return (*(the_menubar->widget.mouse)) (event, the_menubar);
2946 /* "<" button */
2947 if (mouse_down && event->y == 1 && event->x == 2) {
2948 directory_history_prev (panel);
2949 return MOU_NORMAL;
2952 /* ">" button */
2953 if (mouse_down && event->y == 1 && event->x == panel->widget.cols - 1) {
2954 directory_history_next (panel);
2955 return MOU_NORMAL;
2958 /* "v" button */
2959 if (mouse_down && event->y == 1 && event->x == panel->widget.cols - 2) {
2960 directory_history_list (panel);
2961 return MOU_NORMAL;
2964 /* rest of the upper frame, the menu is invisible - call menu */
2965 if (mouse_down && event->y == 1 && !menubar_visible) {
2966 *redir = 1;
2967 event->x += panel->widget.x;
2968 return the_menubar->widget.mouse (event, the_menubar);
2971 /* Mouse wheel events */
2972 if (mouse_down && (event->buttons & GPM_B_UP)) {
2973 if (is_active) {
2974 if (panel->top_file > 0)
2975 prev_page (panel);
2976 else /* We are in first page */
2977 move_up (panel);
2979 return MOU_NORMAL;
2982 if (mouse_down && (event->buttons & GPM_B_DOWN)) {
2983 if (is_active) {
2984 if (panel->top_file + ITEMS (panel) < panel->count)
2985 next_page (panel);
2986 else /* We are in last page */
2987 move_down (panel);
2989 return MOU_NORMAL;
2992 event->y -= 2;
2993 if ((event->type & (GPM_DOWN | GPM_DRAG))) {
2994 int my_index;
2996 if (!is_active)
2997 change_panel ();
2999 if (event->y <= 0) {
3000 mark_if_marking (panel, event);
3001 if (mouse_move_pages)
3002 prev_page (panel);
3003 else
3004 move_up (panel);
3005 return MOU_REPEAT;
3008 if (!((panel->top_file + event->y <= panel->count) && event->y <= lines)) {
3009 mark_if_marking (panel, event);
3010 if (mouse_move_pages)
3011 next_page (panel);
3012 else
3013 move_down (panel);
3014 return MOU_REPEAT;
3017 my_index = panel->top_file + event->y - 1;
3018 if (panel->split && (event->x > ((panel->widget.cols - 2) / 2)))
3019 my_index += llines (panel);
3021 if (my_index >= panel->count)
3022 my_index = panel->count - 1;
3024 if (my_index != panel->selected) {
3025 unselect_item (panel);
3026 panel->selected = my_index;
3027 select_item (panel);
3030 /* This one is new */
3031 mark_if_marking (panel, event);
3032 } else if ((event->type & (GPM_UP | GPM_DOUBLE)) == (GPM_UP | GPM_DOUBLE)) {
3033 if (event->y > 0 && event->y <= lines)
3034 do_enter (panel);
3036 return MOU_NORMAL;
3039 /* Mouse callback of the panel */
3040 static int
3041 panel_event (Gpm_Event *event, void *data)
3043 WPanel *panel = data;
3044 int ret;
3045 int redir = MOU_NORMAL;
3047 ret = do_panel_event (event, panel, &redir);
3048 if (!redir)
3049 paint_panel (panel);
3051 return ret;
3054 void
3055 panel_re_sort (WPanel *panel)
3057 char *filename;
3058 int i;
3060 if (panel == NULL)
3061 return;
3063 filename = g_strdup (selection (panel)->fname);
3064 unselect_item (panel);
3065 do_sort (&panel->dir, panel->current_sort_field->sort_routine, panel->count-1, panel->reverse,
3066 panel->case_sensitive, panel->exec_first);
3067 panel->selected = -1;
3068 for (i = panel->count; i; i--){
3069 if (!strcmp (panel->dir.list [i-1].fname, filename)){
3070 panel->selected = i-1;
3071 break;
3074 g_free (filename);
3075 panel->top_file = panel->selected - ITEMS (panel)/2;
3076 if (panel->top_file < 0)
3077 panel->top_file = 0;
3078 select_item (panel);
3079 panel->dirty = 1;
3082 void
3083 panel_set_sort_order (WPanel *panel, const panel_field_t *sort_order)
3085 if (sort_order == 0)
3086 return;
3088 panel->current_sort_field = sort_order;
3090 /* The directory is already sorted, we have to load the unsorted stuff */
3091 if (sort_order->sort_routine == (sortfn *) unsorted){
3092 char *current_file;
3094 current_file = g_strdup (panel->dir.list [panel->selected].fname);
3095 panel_reload (panel);
3096 try_to_select (panel, current_file);
3097 g_free (current_file);
3099 panel_re_sort (panel);
3102 void
3103 set_panel_encoding (WPanel *panel)
3105 const char *encoding = NULL;
3106 char *cd_path;
3107 #ifdef HAVE_CHARSET
3108 const char *errmsg;
3109 int offset;
3110 int r;
3112 if (horizontal_split) {
3113 offset = (get_current_index () != 0) ? panel->widget.lines : -panel->widget.lines;
3114 r = select_charset (0, offset, source_codepage, FALSE);
3115 } else {
3116 offset = (panel->widget.cols == COLS) ? 0
3117 : (get_current_index () != 0) ? panel->widget.cols
3118 : -panel->widget.cols;
3119 r = select_charset (offset, 0, source_codepage, FALSE);
3122 if (r == SELECT_CHARSET_CANCEL)
3123 return; /* Cancel */
3125 if (r == SELECT_CHARSET_NO_TRANSLATE) {
3126 /* No translation */
3127 errmsg = init_translation_table (display_codepage, display_codepage);
3128 cd_path = remove_encoding_from_path (panel->cwd);
3129 do_panel_cd (panel, cd_path, 0);
3130 g_free (cd_path);
3131 return;
3134 source_codepage = r;
3136 errmsg = init_translation_table (source_codepage, display_codepage);
3137 if (errmsg) {
3138 message (D_ERROR, MSG_ERROR, "%s", errmsg);
3139 return;
3142 encoding = get_codepage_id (source_codepage);
3143 #endif
3144 if (encoding != NULL) {
3145 cd_path = add_encoding_to_path (panel->cwd, encoding);
3146 if (!do_panel_cd (panel, cd_path, 0))
3147 message (D_ERROR, MSG_ERROR, _(" Cannot chdir to %s "), cd_path);
3148 g_free (cd_path);
3152 static void
3153 reload_panelized (WPanel *panel)
3155 int i, j;
3156 dir_list *list = &panel->dir;
3158 if (panel != current_panel)
3159 mc_chdir (panel->cwd);
3161 for (i = 0, j = 0; i < panel->count; i++) {
3162 if (list->list[i].f.marked) {
3163 /* Unmark the file in advance. In case the following mc_lstat
3164 * fails we are done, else we have to mark the file again
3165 * (Note: do_file_mark depends on a valid "list->list [i].buf").
3166 * IMO that's the best way to update the panel's summary status
3167 * -- Norbert
3169 do_file_mark (panel, i, 0);
3171 if (mc_lstat (list->list[i].fname, &list->list[i].st)) {
3172 g_free (list->list[i].fname);
3173 continue;
3175 if (list->list[i].f.marked)
3176 do_file_mark (panel, i, 1);
3177 if (j != i)
3178 list->list[j] = list->list[i];
3179 j++;
3181 if (j == 0)
3182 panel->count = set_zero_dir (list);
3183 else
3184 panel->count = j;
3186 if (panel != current_panel)
3187 mc_chdir (current_panel->cwd);
3190 static void
3191 update_one_panel_widget (WPanel *panel, int force_update,
3192 const char *current_file)
3194 int free_pointer;
3195 char *my_current_file = NULL;
3197 if (force_update & UP_RELOAD) {
3198 panel->is_panelized = 0;
3199 mc_setctl (panel->cwd, VFS_SETCTL_FLUSH, 0);
3200 memset (&(panel->dir_stat), 0, sizeof (panel->dir_stat));
3203 /* If current_file == -1 (an invalid pointer) then preserve selection */
3204 if (current_file == UP_KEEPSEL) {
3205 free_pointer = 1;
3206 my_current_file = g_strdup (panel->dir.list[panel->selected].fname);
3207 current_file = my_current_file;
3208 } else
3209 free_pointer = 0;
3211 if (panel->is_panelized)
3212 reload_panelized (panel);
3213 else
3214 panel_reload (panel);
3216 try_to_select (panel, current_file);
3217 panel->dirty = 1;
3219 if (free_pointer)
3220 g_free (my_current_file);
3223 static void
3224 update_one_panel (int which, int force_update, const char *current_file)
3226 WPanel *panel;
3228 if (get_display_type (which) != view_listing)
3229 return;
3231 panel = (WPanel *) get_panel_widget (which);
3232 update_one_panel_widget (panel, force_update, current_file);
3235 /* This routine reloads the directory in both panels. It tries to
3236 * select current_file in current_panel and other_file in other_panel.
3237 * If current_file == -1 then it automatically sets current_file and
3238 * other_file to the currently selected files in the panels.
3240 * if force_update has the UP_ONLY_CURRENT bit toggled on, then it
3241 * will not reload the other panel.
3243 void
3244 update_panels (int force_update, const char *current_file)
3246 int reload_other = !(force_update & UP_ONLY_CURRENT);
3247 WPanel *panel;
3249 update_one_panel (get_current_index (), force_update, current_file);
3250 if (reload_other)
3251 update_one_panel (get_other_index (), force_update, UP_KEEPSEL);
3253 if (get_current_type () == view_listing)
3254 panel = (WPanel *) get_panel_widget (get_current_index ());
3255 else
3256 panel = (WPanel *) get_panel_widget (get_other_index ());
3258 mc_chdir (panel->cwd);
3261 gsize
3262 panel_get_num_of_sortable_fields(void)
3264 gsize ret = 0, index;
3266 for(index=0; panel_fields[index].id != NULL; index ++)
3267 if (panel_fields[index].title_hotkey != NULL)
3268 ret++;
3269 return ret;
3273 const char **
3274 panel_get_sortable_fields(gsize *array_size)
3276 char **ret;
3277 gsize index, i;
3279 index = panel_get_num_of_sortable_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].title_hotkey != NULL)
3292 ret[index++] = g_strdup(_(panel_fields[i].title_hotkey));
3293 return (const char**) ret;
3296 const panel_field_t *
3297 panel_get_field_by_id(const char *name)
3299 gsize index;
3300 for(index=0; panel_fields[index].id != NULL; index ++)
3301 if (
3302 panel_fields[index].id != NULL &&
3303 strcmp(name, panel_fields[index].id) == 0
3305 return &panel_fields[index];
3306 return NULL;
3309 const panel_field_t *
3310 panel_get_field_by_title_hotkey(const char *name)
3312 gsize index;
3313 for(index=0; panel_fields[index].id != NULL; index ++)
3314 if (
3315 panel_fields[index].title_hotkey != NULL &&
3316 strcmp(name, _(panel_fields[index].title_hotkey)) == 0
3318 return &panel_fields[index];
3319 return NULL;
3322 const panel_field_t *
3323 panel_get_field_by_title(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)) == 0
3331 return &panel_fields[index];
3332 return NULL;
3335 gsize
3336 panel_get_num_of_user_possible_fields(void)
3338 gsize ret = 0, index;
3340 for(index=0; panel_fields[index].id != NULL; index ++)
3341 if (panel_fields[index].use_in_user_format)
3342 ret++;
3343 return ret;
3346 const char **
3347 panel_get_user_possible_fields(gsize *array_size)
3349 char **ret;
3350 gsize index, i;
3352 index = panel_get_num_of_user_possible_fields();
3354 ret = g_new0 (char *, index + 1);
3355 if (ret == NULL)
3356 return NULL;
3358 if (array_size != NULL)
3359 *array_size = index;
3361 index=0;
3363 for(i=0; panel_fields[i].id != NULL; i ++)
3364 if (panel_fields[i].use_in_user_format)
3365 ret[index++] = g_strdup(_(panel_fields[i].title_hotkey));
3366 return (const char**) ret;
3369 void
3370 panel_init(void)
3372 panel_sort_up_sign = mc_config_get_string(mc_skin__default.config,"widget-common","sort-sign-up","'");
3373 panel_sort_down_sign = mc_config_get_string(mc_skin__default.config,"widget-common","sort-sign-down",",");
3376 void
3377 panel_deinit(void)
3379 g_free(panel_sort_up_sign);
3380 g_free(panel_sort_down_sign);