More usage of gettext context.
[pantumic.git] / src / screen.c
blob6f86599c701b421178fcec77fc35a9512473b96b
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 static char *panel_hiddenfiles_sign_show = NULL;
122 static char *panel_hiddenfiles_sign_hide = NULL;
123 static char *panel_history_prev_item_sign = NULL;
124 static char *panel_history_next_item_sign = NULL;
125 static char *panel_history_show_list_sign = NULL;
127 /* This macro extracts the number of available lines in a panel */
128 #define llines(p) (p->widget.lines-3 - (show_mini_info ? 2 : 0))
130 static void
131 set_colors (WPanel *panel)
133 (void) panel;
134 tty_set_normal_attrs ();
135 tty_setcolor (NORMAL_COLOR);
138 /* Delete format string, it is a linked list */
139 static void
140 delete_format (format_e *format)
142 format_e *next;
144 while (format){
145 next = format->next;
146 g_free (format);
147 format = next;
151 /* This code relies on the default justification!!! */
152 static void
153 add_permission_string (char *dest, int width, file_entry *fe, int attr, int color, int is_octal)
155 int i, r, l;
157 l = get_user_permissions (&fe->st);
159 if (is_octal){
160 /* Place of the access bit in octal mode */
161 l = width + l - 3;
162 r = l + 1;
163 } else {
164 /* The same to the triplet in string mode */
165 l = l * 3 + 1;
166 r = l + 3;
169 for(i = 0; i < width; i++){
170 if (i >= l && i < r){
171 if (attr == SELECTED || attr == MARKED_SELECTED)
172 tty_setcolor (MARKED_SELECTED_COLOR);
173 else
174 tty_setcolor (MARKED_COLOR);
175 } else if (color >= 0)
176 tty_setcolor (color);
177 else
178 tty_lowlevel_setcolor (-color);
180 tty_print_char (dest[i]);
184 /* String representations of various file attributes */
185 /* name */
186 static const char *
187 string_file_name (file_entry *fe, int len)
189 static char buffer [MC_MAXPATHLEN * MB_LEN_MAX + 1];
191 (void) len;
192 g_strlcpy (buffer, fe->fname, sizeof(buffer));
193 return buffer;
196 static unsigned int ilog10(dev_t n)
198 unsigned int digits = 0;
199 do {
200 digits++, n /= 10;
201 } while (n != 0);
202 return digits;
205 static void format_device_number (char *buf, size_t bufsize, dev_t dev)
207 dev_t major_dev = major(dev);
208 dev_t minor_dev = minor(dev);
209 unsigned int major_digits = ilog10(major_dev);
210 unsigned int minor_digits = ilog10(minor_dev);
212 g_assert(bufsize >= 1);
213 if (major_digits + 1 + minor_digits + 1 <= bufsize) {
214 g_snprintf(buf, bufsize, "%lu,%lu", (unsigned long) major_dev,
215 (unsigned long) minor_dev);
216 } else {
217 g_strlcpy(buf, _("[dev]"), bufsize);
221 /* size */
222 static const char *
223 string_file_size (file_entry *fe, int len)
225 static char buffer [BUF_TINY];
227 /* Don't ever show size of ".." since we don't calculate it */
228 if (!strcmp (fe->fname, "..")) {
229 return _("UP--DIR");
232 #ifdef HAVE_STRUCT_STAT_ST_RDEV
233 if (S_ISBLK (fe->st.st_mode) || S_ISCHR (fe->st.st_mode))
234 format_device_number (buffer, len + 1, fe->st.st_rdev);
235 else
236 #endif
238 size_trunc_len (buffer, (unsigned int) len, fe->st.st_size, 0);
240 return buffer;
243 /* bsize */
244 static const char *
245 string_file_size_brief (file_entry *fe, int len)
247 if (S_ISLNK (fe->st.st_mode) && !fe->f.link_to_dir) {
248 return _("SYMLINK");
251 if ((S_ISDIR (fe->st.st_mode) || fe->f.link_to_dir) && strcmp (fe->fname, "..")) {
252 return _("SUB-DIR");
255 return string_file_size (fe, len);
258 /* This functions return a string representation of a file entry */
259 /* type */
260 static const char *
261 string_file_type (file_entry *fe, int len)
263 static char buffer[2];
265 (void) len;
266 if (S_ISDIR (fe->st.st_mode))
267 buffer[0] = PATH_SEP;
268 else if (S_ISLNK (fe->st.st_mode)) {
269 if (fe->f.link_to_dir)
270 buffer[0] = '~';
271 else if (fe->f.stale_link)
272 buffer[0] = '!';
273 else
274 buffer[0] = '@';
275 } else if (S_ISCHR (fe->st.st_mode))
276 buffer[0] = '-';
277 else if (S_ISSOCK (fe->st.st_mode))
278 buffer[0] = '=';
279 else if (S_ISDOOR (fe->st.st_mode))
280 buffer[0] = '>';
281 else if (S_ISBLK (fe->st.st_mode))
282 buffer[0] = '+';
283 else if (S_ISFIFO (fe->st.st_mode))
284 buffer[0] = '|';
285 else if (S_ISNAM (fe->st.st_mode))
286 buffer[0] = '#';
287 else if (!S_ISREG (fe->st.st_mode))
288 buffer[0] = '?'; /* non-regular of unknown kind */
289 else if (is_exe (fe->st.st_mode))
290 buffer[0] = '*';
291 else
292 buffer[0] = ' ';
293 buffer[1] = '\0';
294 return buffer;
297 /* mtime */
298 static const char *
299 string_file_mtime (file_entry *fe, int len)
301 (void) len;
302 if (!strcmp (fe->fname, "..")) {
303 return "";
305 return file_date (fe->st.st_mtime);
308 /* atime */
309 static const char *
310 string_file_atime (file_entry *fe, int len)
312 (void) len;
313 if (!strcmp (fe->fname, "..")) {
314 return "";
316 return file_date (fe->st.st_atime);
319 /* ctime */
320 static const char *
321 string_file_ctime (file_entry *fe, int len)
323 (void) len;
324 if (!strcmp (fe->fname, "..")) {
325 return "";
327 return file_date (fe->st.st_ctime);
330 /* perm */
331 static const char *
332 string_file_permission (file_entry *fe, int len)
334 (void) len;
335 return string_perm (fe->st.st_mode);
338 /* mode */
339 static const char *
340 string_file_perm_octal (file_entry *fe, int len)
342 static char buffer [10];
344 (void) len;
345 g_snprintf (buffer, sizeof (buffer), "0%06lo", (unsigned long) fe->st.st_mode);
346 return buffer;
349 /* nlink */
350 static const char *
351 string_file_nlinks (file_entry *fe, int len)
353 static char buffer[BUF_TINY];
355 (void) len;
356 g_snprintf (buffer, sizeof (buffer), "%16d", (int) fe->st.st_nlink);
357 return buffer;
360 /* inode */
361 static const char *
362 string_inode (file_entry *fe, int len)
364 static char buffer [10];
366 (void) len;
367 g_snprintf (buffer, sizeof (buffer), "%lu",
368 (unsigned long) fe->st.st_ino);
369 return buffer;
372 /* nuid */
373 static const char *
374 string_file_nuid (file_entry *fe, int len)
376 static char buffer [10];
378 (void) len;
379 g_snprintf (buffer, sizeof (buffer), "%lu",
380 (unsigned long) fe->st.st_uid);
381 return buffer;
384 /* ngid */
385 static const char *
386 string_file_ngid (file_entry *fe, int len)
388 static char buffer [10];
390 (void) len;
391 g_snprintf (buffer, sizeof (buffer), "%lu",
392 (unsigned long) fe->st.st_gid);
393 return buffer;
396 /* owner */
397 static const char *
398 string_file_owner (file_entry *fe, int len)
400 (void) len;
401 return get_owner (fe->st.st_uid);
404 /* group */
405 static const char *
406 string_file_group (file_entry *fe, int len)
408 (void) len;
409 return get_group (fe->st.st_gid);
412 /* mark */
413 static const char *
414 string_marked (file_entry *fe, int len)
416 (void) len;
417 return fe->f.marked ? "*" : " ";
420 /* space */
421 static const char *
422 string_space (file_entry *fe, int len)
424 (void) fe;
425 (void) len;
426 return " ";
429 /* dot */
430 static const char *
431 string_dot (file_entry *fe, int len)
433 (void) fe;
434 (void) len;
435 return ".";
438 #define GT 1
440 panel_field_t panel_fields [] = {
442 "unsorted", 12, 1, J_LEFT_FIT,
443 /* TRANSLATORS: one single character to represent 'unsorted' sort mode */
444 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
445 N_("sort|u"),
446 N_("Unsorted"), N_("&Unsorted"), FALSE,
447 string_file_name,
448 (sortfn *) unsorted
451 "name", 12, 1, J_LEFT_FIT,
452 /* TRANSLATORS: one single character to represent 'name' sort mode */
453 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
454 N_("sort|n"),
455 N_("Name"), N_("&Name"), TRUE,
456 string_file_name,
457 (sortfn *) sort_name
460 "extension", 12, 1, J_LEFT_FIT,
461 /* TRANSLATORS: one single character to represent 'extension' sort mode */
462 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
463 N_("sort|e"),
464 N_("Extension"), N_("&Extension"), FALSE,
465 string_file_name, /* TODO: string_file_ext*/
466 (sortfn *) sort_ext
469 "size", 7, 0, J_RIGHT,
470 /* TRANSLATORS: one single character to represent 'size' sort mode */
471 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
472 N_("sort|s"),
473 N_("Size"), N_("&Size"), TRUE,
474 string_file_size,
475 (sortfn *) sort_size
478 "bsize", 7, 0, J_RIGHT,
480 N_("Block Size"), NULL, FALSE,
481 string_file_size_brief,
482 (sortfn *) sort_size
485 "type", GT, 0, J_LEFT,
487 "", NULL, TRUE,
488 string_file_type,
489 NULL
492 "mtime", 12, 0, J_RIGHT,
493 /* TRANSLATORS: one single character to represent 'Modify time' sort mode */
494 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
495 N_("sort|m"),
496 N_("MTime"), N_("&Modify time"), TRUE,
497 string_file_mtime,
498 (sortfn *) sort_time
501 "atime", 12, 0, J_RIGHT,
502 /* TRANSLATORS: one single character to represent 'Access time' sort mode */
503 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
504 N_("sort|a"),
505 N_("ATime"), N_("&Access time"), TRUE,
506 string_file_atime,
507 (sortfn *) sort_atime
510 "ctime", 12, 0, J_RIGHT,
511 /* TRANSLATORS: one single character to represent 'Change time' sort mode */
512 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
513 N_("sort|h"),
514 N_("CTime"), N_("C&Hange time"), TRUE,
515 string_file_ctime,
516 (sortfn *) sort_ctime
519 "perm", 10, 0, J_LEFT,
521 N_("Permission"), NULL, TRUE,
522 string_file_permission,
523 NULL
526 "mode", 6, 0, J_RIGHT,
528 N_("Perm"), NULL, TRUE,
529 string_file_perm_octal,
530 NULL
533 "nlink", 2, 0, J_RIGHT,
535 N_("Nl"), NULL, TRUE,
536 string_file_nlinks, NULL
539 "inode", 5, 0, J_RIGHT,
540 /* TRANSLATORS: one single character to represent 'inode' sort mode */
541 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
542 N_("sort|i"),
543 N_("Inode"), N_("&Inode"), TRUE,
544 string_inode,
545 (sortfn *) sort_inode
548 "nuid", 5, 0, J_RIGHT,
550 N_("UID"), NULL, FALSE,
551 string_file_nuid,
552 NULL
555 "ngid", 5, 0, J_RIGHT,
557 N_("GID"), NULL, FALSE,
558 string_file_ngid,
559 NULL
562 "owner", 8, 0, J_LEFT_FIT,
564 N_("Owner"), NULL, TRUE,
565 string_file_owner,
566 NULL
569 "group", 8,0, J_LEFT_FIT,
571 N_("Group"), NULL, TRUE,
572 string_file_group,
573 NULL
576 "mark", 1, 0, J_RIGHT,
578 " ", NULL, TRUE,
579 string_marked,
580 NULL
583 "|", 1, 0, J_RIGHT,
585 " ", NULL, TRUE,
586 NULL,
587 NULL
590 "space", 1, 0, J_RIGHT,
592 " ", NULL, TRUE,
593 string_space,
594 NULL
597 "dot", 1, 0, J_RIGHT,
599 " ", NULL, FALSE,
600 string_dot,
601 NULL
604 NULL, 0, 0, J_RIGHT, NULL, NULL, NULL, FALSE, NULL, NULL
608 static int
609 file_compute_color (int attr, file_entry *fe)
611 switch (attr) {
612 case SELECTED:
613 return (SELECTED_COLOR);
614 case MARKED:
615 return (MARKED_COLOR);
616 case MARKED_SELECTED:
617 return (MARKED_SELECTED_COLOR);
618 case STATUS:
619 return (NORMAL_COLOR);
620 case NORMAL:
621 default:
622 if (!filetype_mode)
623 return (NORMAL_COLOR);
626 return mc_fhl_get_color (mc_filehighlight, fe);
629 /* Formats the file number file_index of panel in the buffer dest */
630 static void
631 format_file (char *dest, int limit, WPanel *panel, int file_index, int width, int attr, int isstatus)
633 int color, length, empty_line;
634 const char *txt;
635 format_e *format, *home;
636 file_entry *fe;
638 (void) dest;
639 (void) limit;
640 length = 0;
641 empty_line = (file_index >= panel->count);
642 home = (isstatus) ? panel->status_format : panel->format;
643 fe = &panel->dir.list [file_index];
645 if (!empty_line)
646 color = file_compute_color (attr, fe);
647 else
648 color = NORMAL_COLOR;
650 for (format = home; format; format = format->next){
651 if (length == width)
652 break;
654 if (format->string_fn){
655 int len, perm;
656 char *preperad_text;
658 if (empty_line)
659 txt = " ";
660 else
661 txt = (*format->string_fn)(fe, format->field_len);
663 len = format->field_len;
664 if (len + length > width)
665 len = width - length;
666 if (len <= 0)
667 break;
669 perm = 0;
670 if (permission_mode) {
671 if (!strcmp(format->id, "perm"))
672 perm = 1;
673 else if (!strcmp(format->id, "mode"))
674 perm = 2;
677 if (color >= 0)
678 tty_setcolor (color);
679 else
680 tty_lowlevel_setcolor (-color);
682 preperad_text = (char*) str_fit_to_term(txt, len, format->just_mode);
683 if (perm)
684 add_permission_string (preperad_text, format->field_len, fe,
685 attr, color, perm - 1);
686 else
687 tty_print_string (preperad_text);
689 length+= len;
690 } else {
691 if (attr == SELECTED || attr == MARKED_SELECTED)
692 tty_setcolor (SELECTED_COLOR);
693 else
694 tty_setcolor (NORMAL_COLOR);
695 tty_print_one_vline ();
696 length++;
700 if (length < width)
701 tty_draw_hline (-1, -1, ' ', width - length);
704 static void
705 repaint_file (WPanel *panel, int file_index, int mv, int attr, int isstatus)
707 int second_column = 0;
708 int width;
709 int offset = 0;
710 char buffer [BUF_MEDIUM];
712 gboolean panel_is_split = !isstatus && panel->split;
714 width = panel->widget.cols - 2;
716 if (panel_is_split) {
717 second_column = (file_index - panel->top_file) / llines (panel);
718 width = width/2 - 1;
720 if (second_column != 0) {
721 offset = 1 + width;
722 /*width = (panel->widget.cols-2) - (panel->widget.cols-2)/2 - 1;*/
723 width = panel->widget.cols - offset - 2;
727 /* Nothing to paint */
728 if (width <= 0)
729 return;
731 if (mv){
732 if (panel_is_split)
733 widget_move (&panel->widget,
734 (file_index - panel->top_file) % llines (panel) + 2,
735 offset + 1);
736 else
737 widget_move (&panel->widget, file_index - panel->top_file + 2, 1);
740 format_file (buffer, sizeof(buffer), panel, file_index, width, attr, isstatus);
742 if (panel_is_split) {
743 if (second_column)
744 tty_print_char (' ');
745 else {
746 tty_setcolor (NORMAL_COLOR);
747 tty_print_one_vline ();
752 static void
753 display_mini_info (WPanel *panel)
755 if (!show_mini_info)
756 return;
758 widget_move (&panel->widget, llines (panel) + 3, 1);
760 if (panel->searching) {
761 tty_setcolor (INPUT_COLOR);
762 tty_print_char ('/');
763 tty_print_string (str_fit_to_term (panel->search_buffer,
764 panel->widget.cols - 3, J_LEFT));
765 return;
768 /* Status resolves links and show them */
769 set_colors (panel);
771 if (S_ISLNK (panel->dir.list [panel->selected].st.st_mode)){
772 char *link, link_target [MC_MAXPATHLEN];
773 int len;
775 link = concat_dir_and_file (panel->cwd, panel->dir.list [panel->selected].fname);
776 len = mc_readlink (link, link_target, MC_MAXPATHLEN - 1);
777 g_free (link);
778 if (len > 0){
779 link_target[len] = 0;
780 tty_print_string ("-> ");
781 tty_print_string (str_fit_to_term (link_target, panel->widget.cols - 5,
782 J_LEFT_FIT));
783 } else
784 tty_print_string (str_fit_to_term (_("<readlink failed>"),
785 panel->widget.cols - 2, J_LEFT));
786 } else if (strcmp (panel->dir.list [panel->selected].fname, "..") == 0) {
787 /* FIXME:
788 * while loading directory (do_load_dir() and do_reload_dir()),
789 * the actual stat info about ".." directory isn't got;
790 * so just don't display incorrect info about ".." directory */
791 tty_print_string (str_fit_to_term (_("UP--DIR"), panel->widget.cols - 2, J_LEFT));
792 } else
793 /* Default behavior */
794 repaint_file (panel, panel->selected, 0, STATUS, 1);
797 static void
798 paint_dir (WPanel *panel)
800 int i;
801 int color; /* Color value of the line */
802 int items; /* Number of items */
804 items = llines (panel) * (panel->split ? 2 : 1);
806 for (i = 0; i < items; i++){
807 if (i+panel->top_file >= panel->count)
808 color = 0;
809 else {
810 color = 2 * (panel->dir.list [i+panel->top_file].f.marked);
811 color += (panel->selected==i+panel->top_file && panel->active);
813 repaint_file (panel, i+panel->top_file, 1, color, 0);
815 tty_set_normal_attrs ();
818 static void
819 display_total_marked_size (WPanel *panel, int y, int x, gboolean size_only)
821 char buffer[BUF_SMALL], b_bytes[BUF_SMALL], *buf;
822 int cols;
824 if (panel->marked <= 0)
825 return;
827 buf = size_only ? b_bytes : buffer;
828 cols = panel->widget.cols - 2;
831 * This is a trick to use two ngettext() calls in one sentence.
832 * First make "N bytes", then insert it into "X in M files".
834 g_snprintf (b_bytes, sizeof (b_bytes),
835 ngettext("%s byte", "%s bytes", (unsigned long) panel->total),
836 size_trunc_sep (panel->total));
837 if (!size_only)
838 g_snprintf (buffer, sizeof (buffer),
839 ngettext("%s in %d file", "%s in %d files", panel->marked),
840 b_bytes, panel->marked);
842 /* don't forget spaces around buffer content */
843 buf = (char *) str_trunc (buf, cols - 4);
845 if (x < 0)
846 /* center in panel */
847 x = (panel->widget.cols - str_term_width1 (buf)) / 2 - 1;
850 * y == llines (panel) + 2 for mini_info_separator
851 * y == panel->widget.lines - 1 for panel bottom frame
853 widget_move (&panel->widget, y, x);
854 tty_setcolor (MARKED_COLOR);
855 tty_printf (" %s ", buf);
858 static void
859 mini_info_separator (WPanel *panel)
861 if (show_mini_info) {
862 const int y = llines (panel) + 2;
864 tty_setcolor (NORMAL_COLOR);
865 tty_draw_hline (panel->widget.y + y, panel->widget.x + 1,
866 ACS_HLINE, panel->widget.cols - 2);
867 /* Status displays total marked size.
868 * Centered in panel, full format. */
869 display_total_marked_size (panel, y, -1, FALSE);
873 static void
874 show_free_space (WPanel *panel)
876 /* Used to figure out how many free space we have */
877 static struct my_statfs myfs_stats;
878 /* Old current working directory for displaying free space */
879 static char *old_cwd = NULL;
881 /* Don't try to stat non-local fs */
882 if (!vfs_file_is_local (panel->cwd) || !free_space)
883 return;
885 if (old_cwd == NULL || strcmp (old_cwd, panel->cwd) != 0) {
886 char rpath[PATH_MAX];
888 init_my_statfs ();
889 g_free (old_cwd);
890 old_cwd = g_strdup (panel->cwd);
892 if (mc_realpath (panel->cwd, rpath) == NULL)
893 return;
895 my_statfs (&myfs_stats, rpath);
898 if (myfs_stats.avail > 0 || myfs_stats.total > 0) {
899 char buffer1[6], buffer2[6], tmp[BUF_SMALL];
900 size_trunc_len (buffer1, sizeof(buffer1) - 1, myfs_stats.avail, 1);
901 size_trunc_len (buffer2, sizeof(buffer2) - 1, myfs_stats.total, 1);
902 g_snprintf (tmp, sizeof(tmp), " %s/%s (%d%%) ", buffer1, buffer2,
903 myfs_stats.total > 0 ?
904 (int)(100 * (double)myfs_stats.avail / myfs_stats.total) : 0);
905 widget_move (&panel->widget, panel->widget.lines - 1,
906 panel->widget.cols - 2 - (int) strlen (tmp));
907 tty_setcolor (NORMAL_COLOR);
908 tty_print_string (tmp);
912 static void
913 show_dir (WPanel *panel)
915 gchar *tmp;
916 set_colors (panel);
917 draw_box (panel->widget.parent,
918 panel->widget.y, panel->widget.x,
919 panel->widget.lines, panel->widget.cols);
921 if (show_mini_info) {
922 widget_move (&panel->widget, llines (panel) + 2, 0);
923 tty_print_alt_char (ACS_LTEE);
924 widget_move (&panel->widget, llines (panel) + 2,
925 panel->widget.cols - 1);
926 tty_print_alt_char (ACS_RTEE);
929 widget_move (&panel->widget, 0, 1);
930 tty_print_string (panel_history_prev_item_sign);
932 tmp = (show_dot_files) ? panel_hiddenfiles_sign_show : panel_hiddenfiles_sign_hide;
933 tmp = g_strdup_printf("%s[%s]%s",tmp,panel_history_show_list_sign,panel_history_next_item_sign);
935 widget_move (&panel->widget, 0, panel->widget.cols - 6);
936 tty_print_string (tmp);
938 g_free(tmp);
940 if (panel->active)
941 tty_setcolor (REVERSE_COLOR);
943 widget_move (&panel->widget, 0, 3);
945 tty_printf (" %s ",
946 str_term_trim (strip_home_and_password (panel->cwd),
947 min (max (panel->widget.cols - 12, 0),
948 panel->widget.cols)));
950 if (!show_mini_info) {
951 if (panel->marked == 0) {
952 /* Show size of curret file in the bottom of panel */
953 if (S_ISREG (panel->dir.list [panel->selected].st.st_mode)) {
954 char buffer[BUF_SMALL];
956 g_snprintf (buffer, sizeof (buffer), " %s ",
957 size_trunc_sep (panel->dir.list [panel->selected].st.st_size));
958 tty_setcolor (NORMAL_COLOR);
959 widget_move (&panel->widget, panel->widget.lines - 1, 4);
960 tty_print_string (buffer);
962 } else {
963 /* Show total size of marked files
964 * In the bottom of panel, display size only. */
965 display_total_marked_size (panel, panel->widget.lines - 1, 2, TRUE);
969 show_free_space (panel);
971 if (panel->active)
972 tty_set_normal_attrs ();
975 /* To be used only by long_frame and full_frame to adjust top_file */
976 static void
977 adjust_top_file (WPanel *panel)
979 int old_top = panel->top_file;
981 if (panel->selected - old_top > llines (panel))
982 panel->top_file = panel->selected;
983 if (old_top - panel->count > llines (panel))
984 panel->top_file = panel->count - llines (panel);
987 /* Repaint everything, including frame and separator */
988 static void
989 paint_panel (WPanel *panel)
991 paint_frame (panel); /* including show_dir */
992 paint_dir (panel);
993 mini_info_separator (panel);
994 display_mini_info (panel);
995 panel->dirty = 0;
998 /* add "#enc:encodning" to end of path */
999 /* if path end width a previous #enc:, only encoding is changed no additional
1000 * #enc: is appended
1001 * retun new string
1003 static char
1004 *add_encoding_to_path (const char *path, const char *encoding)
1006 char *result;
1007 char *semi;
1008 char *slash;
1010 semi = g_strrstr (path, "#enc:");
1012 if (semi != NULL) {
1013 slash = strchr (semi, PATH_SEP);
1014 if (slash != NULL) {
1015 result = g_strconcat (path, "/#enc:", encoding, NULL);
1016 } else {
1017 *semi = 0;
1018 result = g_strconcat (path, "/#enc:", encoding, NULL);
1019 *semi = '#';
1021 } else {
1022 result = g_strconcat (path, "/#enc:", encoding, NULL);
1025 return result;
1028 char *
1029 remove_encoding_from_path (const char *path)
1031 GString *ret;
1032 GString *tmp_path, *tmp_conv;
1033 char *tmp, *tmp2;
1034 const char *enc;
1035 GIConv converter;
1037 ret = g_string_new("");
1038 tmp_conv = g_string_new("");
1040 tmp_path = g_string_new(path);
1042 while ((tmp = g_strrstr (tmp_path->str, "/#enc:")) != NULL){
1043 enc = vfs_get_encoding ((const char *) tmp);
1044 converter = enc ? str_crt_conv_to (enc): str_cnv_to_term;
1045 if (converter == INVALID_CONV) converter = str_cnv_to_term;
1047 tmp2=tmp+1;
1048 while (*tmp2 && *tmp2 != '/')
1049 tmp2++;
1051 if (*tmp2){
1052 str_vfs_convert_from (converter, tmp2, tmp_conv);
1053 g_string_prepend(ret, tmp_conv->str);
1054 g_string_set_size(tmp_conv,0);
1056 g_string_set_size(tmp_path,tmp - tmp_path->str);
1057 str_close_conv (converter);
1059 g_string_prepend(ret, tmp_path->str);
1060 g_string_free(tmp_path,TRUE);
1061 g_string_free(tmp_conv,TRUE);
1063 tmp = ret->str;
1064 g_string_free(ret, FALSE);
1065 return tmp;
1069 * Repaint the contents of the panels without frames. To schedule panel
1070 * for repainting, set panel->dirty to 1. There are many reasons why
1071 * the panels need to be repainted, and this is a costly operation, so
1072 * it's done once per event.
1074 void
1075 update_dirty_panels (void)
1077 if (current_panel->dirty)
1078 paint_panel (current_panel);
1080 if ((get_other_type () == view_listing) && other_panel->dirty)
1081 paint_panel (other_panel);
1084 static void
1085 do_select (WPanel *panel, int i)
1087 if (i != panel->selected) {
1088 panel->dirty = 1;
1089 panel->selected = i;
1090 panel->top_file = panel->selected - (panel->widget.lines - 2) / 2;
1091 if (panel->top_file < 0)
1092 panel->top_file = 0;
1096 static void
1097 do_try_to_select (WPanel *panel, const char *name)
1099 int i;
1100 char *subdir;
1102 if (!name) {
1103 do_select(panel, 0);
1104 return;
1107 /* We only want the last component of the directory,
1108 * and from this only the name without suffix. */
1109 subdir = vfs_strip_suffix_from_filename (x_basename(name));
1111 /* Search that subdirectory, if found select it */
1112 for (i = 0; i < panel->count; i++){
1113 if (strcmp (subdir, panel->dir.list [i].fname) == 0) {
1114 do_select (panel, i);
1115 g_free (subdir);
1116 return;
1120 /* Try to select a file near the file that is missing */
1121 if (panel->selected >= panel->count)
1122 do_select (panel, panel->count-1);
1123 g_free (subdir);
1126 void
1127 try_to_select (WPanel *panel, const char *name)
1129 do_try_to_select (panel, name);
1130 select_item (panel);
1133 void
1134 panel_update_cols (Widget *widget, int frame_size)
1136 int cols, origin;
1138 if (horizontal_split){
1139 widget->cols = COLS;
1140 return;
1143 if (frame_size == frame_full){
1144 cols = COLS;
1145 origin = 0;
1146 } else {
1147 if (widget == get_panel_widget (0)){
1148 cols = first_panel_size;
1149 origin = 0;
1150 } else {
1151 cols = COLS-first_panel_size;
1152 origin = first_panel_size;
1156 widget->cols = cols;
1157 widget->x = origin;
1160 static char *
1161 panel_save_name (WPanel *panel)
1163 extern int saving_setup;
1165 /* If the program is shuting down */
1166 if ((midnight_shutdown && auto_save_setup) || saving_setup)
1167 return g_strdup (panel->panel_name);
1168 else
1169 return g_strconcat ("Temporal:", panel->panel_name, (char *) NULL);
1172 void
1173 panel_clean_dir (WPanel *panel)
1175 int count = panel->count;
1177 panel->count = 0;
1178 panel->top_file = 0;
1179 panel->selected = 0;
1180 panel->marked = 0;
1181 panel->dirs_marked = 0;
1182 panel->total = 0;
1183 panel->searching = 0;
1184 panel->is_panelized = 0;
1185 panel->dirty = 1;
1187 clean_dir (&panel->dir, count);
1190 static void
1191 panel_destroy (WPanel *p)
1193 int i;
1195 char *name = panel_save_name (p);
1197 panel_save_setup (p, name);
1198 panel_clean_dir (p);
1200 /* save and clean history */
1201 if (p->dir_history) {
1202 history_put (p->hist_name, p->dir_history);
1204 p->dir_history = g_list_first (p->dir_history);
1205 g_list_foreach (p->dir_history, (GFunc) g_free, NULL);
1206 g_list_free (p->dir_history);
1209 g_free (p->hist_name);
1211 delete_format (p->format);
1212 delete_format (p->status_format);
1214 g_free (p->user_format);
1215 for (i = 0; i < LIST_TYPES; i++)
1216 g_free (p->user_status_format[i]);
1217 g_free (p->dir.list);
1218 g_free (p->panel_name);
1219 g_free (name);
1222 static void
1223 panel_format_modified (WPanel *panel)
1225 panel->format_modified = 1;
1228 /* Panel creation */
1229 /* The parameter specifies the name of the panel for setup retieving */
1230 WPanel *
1231 panel_new (const char *panel_name)
1233 return panel_new_with_dir(panel_name, NULL);
1236 /* Panel creation for specified directory */
1237 /* The parameter specifies the name of the panel for setup retieving */
1238 /* and the path of working panel directory. If path is NULL then */
1239 /* panel will be created for current directory */
1240 WPanel *
1241 panel_new_with_dir (const char *panel_name, const char *wpath)
1243 WPanel *panel;
1244 char *section;
1245 int i, err;
1246 char curdir[MC_MAXPATHLEN];
1248 panel = g_new0 (WPanel, 1);
1250 /* No know sizes of the panel at startup */
1251 init_widget (&panel->widget, 0, 0, 0, 0, panel_callback, panel_event);
1253 /* We do not want the cursor */
1254 widget_want_cursor (panel->widget, 0);
1256 if (wpath) {
1257 g_strlcpy(panel->cwd, wpath, sizeof (panel->cwd));
1258 mc_get_current_wd (curdir, sizeof (curdir) - 2);
1259 } else
1260 mc_get_current_wd (panel->cwd, sizeof (panel->cwd) - 2);
1262 strcpy (panel->lwd, ".");
1264 panel->hist_name = g_strconcat ("Dir Hist ", panel_name, (char *) NULL);
1265 panel->dir_history = history_get (panel->hist_name);
1266 directory_history_add (panel, panel->cwd);
1268 panel->dir.list = g_new (file_entry, MIN_FILES);
1269 panel->dir.size = MIN_FILES;
1270 panel->active = 0;
1271 panel->filter = 0;
1272 panel->split = 0;
1273 panel->top_file = 0;
1274 panel->selected = 0;
1275 panel->marked = 0;
1276 panel->total = 0;
1277 panel->reverse = 0;
1278 panel->dirty = 1;
1279 panel->searching = 0;
1280 panel->dirs_marked = 0;
1281 panel->is_panelized = 0;
1282 panel->format = 0;
1283 panel->status_format = 0;
1284 panel->format_modified = 1;
1286 panel->panel_name = g_strdup (panel_name);
1287 panel->user_format = g_strdup (DEFAULT_USER_FORMAT);
1289 for (i = 0; i < LIST_TYPES; i++)
1290 panel->user_status_format[i] = g_strdup (DEFAULT_USER_FORMAT);
1292 panel->search_buffer[0] = 0;
1293 panel->frame_size = frame_half;
1294 section = g_strconcat ("Temporal:", panel->panel_name, (char *) NULL);
1295 if (!mc_config_has_group (mc_main_config, section)) {
1296 g_free (section);
1297 section = g_strdup (panel->panel_name);
1299 panel_load_setup (panel, section);
1300 g_free (section);
1302 /* Load format strings */
1303 err = set_panel_formats (panel);
1304 if (err) {
1305 set_panel_formats (panel);
1309 /* Because do_load_dir lists files in current directory */
1310 if (wpath)
1311 mc_chdir(wpath);
1313 /* Load the default format */
1314 panel->count =
1315 do_load_dir (panel->cwd, &panel->dir, panel->current_sort_field->sort_routine,
1316 panel->reverse, panel->case_sensitive,
1317 panel->exec_first, panel->filter);
1319 /* Restore old right path */
1320 if (wpath)
1321 mc_chdir(curdir);
1323 return panel;
1326 void
1327 panel_reload (WPanel *panel)
1329 struct stat current_stat;
1331 if (fast_reload && !stat (panel->cwd, &current_stat)
1332 && current_stat.st_ctime == panel->dir_stat.st_ctime
1333 && current_stat.st_mtime == panel->dir_stat.st_mtime)
1334 return;
1336 while (mc_chdir (panel->cwd) == -1) {
1337 char *last_slash;
1339 if (panel->cwd[0] == PATH_SEP && panel->cwd[1] == 0) {
1340 panel_clean_dir (panel);
1341 panel->count = set_zero_dir (&panel->dir);
1342 return;
1344 last_slash = strrchr (panel->cwd, PATH_SEP);
1345 if (!last_slash || last_slash == panel->cwd)
1346 strcpy (panel->cwd, PATH_SEP_STR);
1347 else
1348 *last_slash = 0;
1349 memset (&(panel->dir_stat), 0, sizeof (panel->dir_stat));
1350 show_dir (panel);
1353 panel->count =
1354 do_reload_dir (panel->cwd, &panel->dir, panel->current_sort_field->sort_routine,
1355 panel->count, panel->reverse, panel->case_sensitive,
1356 panel->exec_first, panel->filter);
1358 panel->dirty = 1;
1359 if (panel->selected >= panel->count)
1360 do_select (panel, panel->count - 1);
1362 recalculate_panel_summary (panel);
1365 static void
1366 panel_paint_sort_info(WPanel *panel)
1368 const char *sort_sign = (panel->reverse) ? panel_sort_down_sign : panel_sort_up_sign;
1369 char *str;
1371 if (*panel->current_sort_field->hotkey == '\0')
1372 return;
1374 str = g_strdup_printf("%s%s",sort_sign, Q_(panel->current_sort_field->hotkey));
1376 widget_move (&panel->widget, 1, 1);
1377 tty_print_string (str);
1378 g_free(str);
1381 static void
1382 paint_frame (WPanel *panel)
1384 int side, width;
1385 GString *format_txt;
1387 if (!panel->split)
1388 adjust_top_file (panel);
1390 widget_erase (&panel->widget);
1391 show_dir (panel);
1393 widget_move (&panel->widget, 1, 1);
1395 for (side = 0; side <= panel->split; side++){
1396 format_e *format;
1398 if (side){
1399 tty_setcolor (NORMAL_COLOR);
1400 tty_print_one_vline ();
1401 width = panel->widget.cols - panel->widget.cols/2 - 1;
1402 } else if (panel->split)
1403 width = panel->widget.cols/2 - 3;
1404 else
1405 width = panel->widget.cols - 2;
1407 format_txt = g_string_new("");
1408 for (format = panel->format; format; format = format->next){
1409 if (format->string_fn){
1410 g_string_set_size(format_txt, 0);
1412 if (panel->list_type == list_long && strcmp (format->id, panel->current_sort_field->id) == 0)
1413 g_string_append (format_txt, (panel->reverse) ? panel_sort_down_sign : panel_sort_up_sign);
1415 g_string_append (format_txt, format->title);
1416 if (strcmp (format->id, "name") == 0 && panel->filter && *panel->filter) {
1417 g_string_append (format_txt, " [");
1418 g_string_append (format_txt, panel->filter);
1419 g_string_append (format_txt, "]");
1422 tty_setcolor (MARKED_COLOR);
1423 tty_print_string (str_fit_to_term (format_txt->str, format->field_len,
1424 J_CENTER_LEFT));
1425 width -= format->field_len;
1426 } else {
1427 tty_setcolor (NORMAL_COLOR);
1428 tty_print_one_vline ();
1429 width--;
1432 g_string_free(format_txt, TRUE);
1434 if (width > 0)
1435 tty_draw_hline (-1, -1, ' ', width);
1438 if (panel->list_type != list_long)
1439 panel_paint_sort_info(panel);
1442 static const char *
1443 parse_panel_size (WPanel *panel, const char *format, int isstatus)
1445 int frame = frame_half;
1446 format = skip_separators (format);
1448 if (!strncmp (format, "full", 4)){
1449 frame = frame_full;
1450 format += 4;
1451 } else if (!strncmp (format, "half", 4)){
1452 frame = frame_half;
1453 format += 4;
1456 if (!isstatus){
1457 panel->frame_size = frame;
1458 panel->split = 0;
1461 /* Now, the optional column specifier */
1462 format = skip_separators (format);
1464 if (*format == '1' || *format == '2'){
1465 if (!isstatus)
1466 panel->split = *format == '2';
1467 format++;
1470 if (!isstatus)
1471 panel_update_cols (&(panel->widget), panel->frame_size);
1473 return skip_separators (format);
1476 /* Format is:
1478 all := panel_format? format
1479 panel_format := [full|half] [1|2]
1480 format := one_format_e
1481 | format , one_format_e
1483 one_format_e := just format.id [opt_size]
1484 just := [<=>]
1485 opt_size := : size [opt_expand]
1486 size := [0-9]+
1487 opt_expand := +
1491 static format_e *
1492 parse_display_format (WPanel *panel, const char *format, char **error, int isstatus, int *res_total_cols)
1494 format_e *darr, *old = 0, *home = 0; /* The formats we return */
1495 int total_cols = 0; /* Used columns by the format */
1496 int set_justify; /* flag: set justification mode? */
1497 align_crt_t justify = J_LEFT; /* Which mode. */
1498 size_t i;
1500 static size_t i18n_timelength = 0; /* flag: check ?Time length at startup */
1502 *error = 0;
1504 if (i18n_timelength == 0) {
1505 i18n_timelength = i18n_checktimelength (); /* Musn't be 0 */
1507 for (i = 0; panel_fields[i].id != NULL; i++)
1508 if (strcmp ("time", panel_fields[i].id + 1) == 0)
1509 panel_fields [i].min_size = i18n_timelength;
1513 * This makes sure that the panel and mini status full/half mode
1514 * setting is equal
1516 format = parse_panel_size (panel, format, isstatus);
1518 while (*format){ /* format can be an empty string */
1519 int found = 0;
1521 darr = g_new (format_e, 1);
1523 /* I'm so ugly, don't look at me :-) */
1524 if (!home)
1525 home = old = darr;
1527 old->next = darr;
1528 darr->next = 0;
1529 old = darr;
1531 format = skip_separators (format);
1533 if (strchr ("<=>", *format)){
1534 set_justify = 1;
1535 switch (*format)
1537 case '<':
1538 justify = J_LEFT;
1539 break;
1540 case '=':
1541 justify = J_CENTER;
1542 break;
1543 case '>':
1544 default:
1545 justify = J_RIGHT;
1546 break;
1548 format = skip_separators (format+1);
1549 } else
1550 set_justify = 0;
1552 for (i = 0; panel_fields[i].id != NULL; i++) {
1553 size_t klen = strlen (panel_fields [i].id);
1555 if (strncmp (format, panel_fields [i].id, klen) != 0)
1556 continue;
1558 format += klen;
1560 darr->requested_field_len = panel_fields [i].min_size;
1561 darr->string_fn = panel_fields [i].string_fn;
1562 if (panel_fields [i].title [0])
1563 darr->title = _(panel_fields [i].title);
1564 else
1565 darr->title = "";
1566 darr->id = panel_fields [i].id;
1567 darr->expand = panel_fields [i].expands;
1568 darr->just_mode = panel_fields [i].default_just;
1570 if (set_justify) {
1571 if (IS_FIT(darr->just_mode))
1572 darr->just_mode = MAKE_FIT(justify);
1573 else
1574 darr->just_mode = justify;
1576 found = 1;
1578 format = skip_separators (format);
1580 /* If we have a size specifier */
1581 if (*format == ':'){
1582 int req_length;
1584 /* If the size was specified, we don't want
1585 * auto-expansion by default
1587 darr->expand = 0;
1588 format++;
1589 req_length = atoi (format);
1590 darr->requested_field_len = req_length;
1592 format = skip_numbers (format);
1594 /* Now, if they insist on expansion */
1595 if (*format == '+'){
1596 darr->expand = 1;
1597 format++;
1602 break;
1604 if (!found){
1605 char *tmp_format = g_strdup (format);
1607 int pos = min (8, strlen (format));
1608 delete_format (home);
1609 tmp_format [pos] = 0;
1610 *error = g_strconcat (_("Unknown tag on display format: "), tmp_format, (char *) NULL);
1611 g_free (tmp_format);
1612 return 0;
1614 total_cols += darr->requested_field_len;
1617 *res_total_cols = total_cols;
1618 return home;
1621 static format_e *
1622 use_display_format (WPanel *panel, const char *format, char **error, int isstatus)
1624 #define MAX_EXPAND 4
1625 int expand_top = 0; /* Max used element in expand */
1626 int usable_columns; /* Usable columns in the panel */
1627 int total_cols = 0;
1628 int i;
1629 format_e *darr, *home;
1631 if (!format)
1632 format = DEFAULT_USER_FORMAT;
1634 home = parse_display_format (panel, format, error, isstatus, &total_cols);
1636 if (*error)
1637 return 0;
1639 panel->dirty = 1;
1641 /* Status needn't to be split */
1642 usable_columns = ((panel->widget.cols-2)/((isstatus)
1644 : (panel->split+1))) - (!isstatus && panel->split);
1646 /* Look for the expandable fields and set field_len based on the requested field len */
1647 for (darr = home; darr && expand_top < MAX_EXPAND; darr = darr->next){
1648 darr->field_len = darr->requested_field_len;
1649 if (darr->expand)
1650 expand_top++;
1653 /* If we used more columns than the available columns, adjust that */
1654 if (total_cols > usable_columns){
1655 int pdif, dif = total_cols - usable_columns;
1657 while (dif){
1658 pdif = dif;
1659 for (darr = home; darr; darr = darr->next){
1660 if (dif && darr->field_len - 1){
1661 darr->field_len--;
1662 dif--;
1666 /* avoid endless loop if num fields > 40 */
1667 if (pdif == dif)
1668 break;
1670 total_cols = usable_columns; /* give up, the rest should be truncated */
1673 /* Expand the available space */
1674 if ((usable_columns > total_cols) && expand_top){
1675 int spaces = (usable_columns - total_cols) / expand_top;
1676 int extra = (usable_columns - total_cols) % expand_top;
1678 for (i = 0, darr = home; darr && (i < expand_top); darr = darr->next)
1679 if (darr->expand){
1680 darr->field_len += (spaces + ((i == 0) ? extra : 0));
1681 i++;
1684 return home;
1687 /* Switches the panel to the mode specified in the format */
1688 /* Seting up both format and status string. Return: 0 - on success; */
1689 /* 1 - format error; 2 - status error; 3 - errors in both formats. */
1691 set_panel_formats (WPanel *p)
1693 format_e *form;
1694 char *err;
1695 int retcode = 0;
1697 form = use_display_format (p, panel_format (p), &err, 0);
1699 if (err){
1700 g_free (err);
1701 retcode = 1;
1703 else {
1704 if (p->format)
1705 delete_format (p->format);
1707 p->format = form;
1710 if (show_mini_info){
1712 form = use_display_format (p, mini_status_format (p), &err, 1);
1714 if (err){
1715 g_free (err);
1716 retcode += 2;
1718 else {
1719 if (p->status_format)
1720 delete_format (p->status_format);
1722 p->status_format = form;
1726 panel_format_modified (p);
1727 panel_update_cols (&(p->widget), p->frame_size);
1729 if (retcode)
1730 message (D_ERROR, _("Warning" ), _( "User supplied format looks invalid, reverting to default." ) );
1731 if (retcode & 0x01){
1732 g_free (p->user_format);
1733 p->user_format = g_strdup (DEFAULT_USER_FORMAT);
1735 if (retcode & 0x02){
1736 g_free (p->user_status_format [p->list_type]);
1737 p->user_status_format [p->list_type] = g_strdup (DEFAULT_USER_FORMAT);
1740 return retcode;
1743 /* Given the panel->view_type returns the format string to be parsed */
1744 static const char *
1745 panel_format (WPanel *panel)
1747 switch (panel->list_type){
1749 case list_long:
1750 return "full perm space nlink space owner space group space size space mtime space name";
1752 case list_brief:
1753 return "half 2 type name";
1755 case list_user:
1756 return panel->user_format;
1758 default:
1759 case list_full:
1760 return "half type name | size | mtime";
1764 static const char *
1765 mini_status_format (WPanel *panel)
1767 if (panel->user_mini_status)
1768 return panel->user_status_format [panel->list_type];
1770 switch (panel->list_type){
1772 case list_long:
1773 return "full perm space nlink space owner space group space size space mtime space name";
1775 case list_brief:
1776 return "half type name space bsize space perm space";
1778 case list_full:
1779 return "half type name";
1781 default:
1782 case list_user:
1783 return panel->user_format;
1787 /* */
1788 /* Panel operation commands */
1789 /* */
1791 /* Used to emulate Lynx's entering leaving a directory with the arrow keys */
1792 static cb_ret_t
1793 maybe_cd (int move_up_dir)
1795 if (navigate_with_arrows) {
1796 if (!cmdline->buffer[0]) {
1797 if (move_up_dir) {
1798 do_cd ("..", cd_exact);
1799 return MSG_HANDLED;
1801 if (S_ISDIR (selection (current_panel)->st.st_mode)
1802 || link_isdir (selection (current_panel))) {
1803 do_cd (selection (current_panel)->fname, cd_exact);
1804 return MSG_HANDLED;
1808 return MSG_NOT_HANDLED;
1811 /* Returns the number of items in the given panel */
1812 static int
1813 ITEMS (WPanel *p)
1815 if (p->split)
1816 return llines (p) * 2;
1817 else
1818 return llines (p);
1821 /* Select current item and readjust the panel */
1822 void
1823 select_item (WPanel *panel)
1825 int items = ITEMS (panel);
1827 /* Although currently all over the code we set the selection and
1828 top file to decent values before calling select_item, I could
1829 forget it someday, so it's better to do the actual fitting here */
1831 if (panel->top_file < 0)
1832 panel->top_file = 0;
1834 if (panel->selected < 0)
1835 panel->selected = 0;
1837 if (panel->selected > panel->count - 1)
1838 panel->selected = panel->count - 1;
1840 if (panel->top_file > panel->count - 1)
1841 panel->top_file = panel->count - 1;
1843 if ((panel->count - panel->top_file) < items) {
1844 panel->top_file = panel->count - items;
1845 if (panel->top_file < 0)
1846 panel->top_file = 0;
1849 if (panel->selected < panel->top_file)
1850 panel->top_file = panel->selected;
1852 if ((panel->selected - panel->top_file) >= items)
1853 panel->top_file = panel->selected - items + 1;
1855 panel->dirty = 1;
1857 execute_hooks (select_file_hook);
1860 /* Clears all files in the panel, used only when one file was marked */
1861 void
1862 unmark_files (WPanel *panel)
1864 int i;
1866 if (!panel->marked)
1867 return;
1868 for (i = 0; i < panel->count; i++)
1869 file_mark (panel, i, 0);
1871 panel->dirs_marked = 0;
1872 panel->marked = 0;
1873 panel->total = 0;
1876 static void
1877 unselect_item (WPanel *panel)
1879 repaint_file (panel, panel->selected, 1, 2*selection (panel)->f.marked, 0);
1882 static void
1883 move_down (WPanel *panel)
1885 if (panel->selected+1 == panel->count)
1886 return;
1888 unselect_item (panel);
1889 panel->selected++;
1890 if (panel->selected - panel->top_file == ITEMS (panel) &&
1891 panel_scroll_pages) {
1892 /* Scroll window half screen */
1893 panel->top_file += ITEMS (panel)/2;
1894 if (panel->top_file > panel->count - ITEMS (panel))
1895 panel->top_file = panel->count - ITEMS (panel);
1896 paint_dir (panel);
1898 select_item (panel);
1901 static void
1902 move_up (WPanel *panel)
1904 if (panel->selected == 0)
1905 return;
1907 unselect_item (panel);
1908 panel->selected--;
1909 if (panel->selected < panel->top_file && panel_scroll_pages) {
1910 /* Scroll window half screen */
1911 panel->top_file -= ITEMS (panel)/2;
1912 if (panel->top_file < 0)
1913 panel->top_file = 0;
1914 paint_dir (panel);
1916 select_item (panel);
1919 /* Changes the selection by lines (may be negative) */
1920 static void
1921 move_selection (WPanel *panel, int lines)
1923 int new_pos;
1924 int adjust = 0;
1926 new_pos = panel->selected + lines;
1927 if (new_pos >= panel->count)
1928 new_pos = panel->count-1;
1930 if (new_pos < 0)
1931 new_pos = 0;
1933 unselect_item (panel);
1934 panel->selected = new_pos;
1936 if (panel->selected - panel->top_file >= ITEMS (panel)){
1937 panel->top_file += lines;
1938 adjust = 1;
1941 if (panel->selected - panel->top_file < 0){
1942 panel->top_file += lines;
1943 adjust = 1;
1946 if (adjust){
1947 if (panel->top_file > panel->selected)
1948 panel->top_file = panel->selected;
1949 if (panel->top_file < 0)
1950 panel->top_file = 0;
1951 paint_dir (panel);
1953 select_item (panel);
1956 static cb_ret_t
1957 move_left (WPanel *panel)
1959 if (panel->split) {
1960 move_selection (panel, -llines (panel));
1961 return MSG_HANDLED;
1962 } else
1963 return maybe_cd (1); /* cd .. */
1966 static int
1967 move_right (WPanel *panel)
1969 if (panel->split) {
1970 move_selection (panel, llines (panel));
1971 return MSG_HANDLED;
1972 } else
1973 return maybe_cd (0); /* cd (selection) */
1976 static void
1977 prev_page (WPanel *panel)
1979 int items;
1981 if (!panel->selected && !panel->top_file)
1982 return;
1983 unselect_item (panel);
1984 items = ITEMS (panel);
1985 if (panel->top_file < items)
1986 items = panel->top_file;
1987 if (!items)
1988 panel->selected = 0;
1989 else
1990 panel->selected -= items;
1991 panel->top_file -= items;
1993 /* This keeps the selection in a reasonable place */
1994 if (panel->selected < 0)
1995 panel->selected = 0;
1996 if (panel->top_file < 0)
1997 panel->top_file = 0;
1998 select_item (panel);
1999 paint_dir (panel);
2002 static void
2003 ctrl_prev_page (WPanel *panel)
2005 (void) panel;
2006 do_cd ("..", cd_exact);
2009 static void
2010 next_page (WPanel *panel)
2012 int items;
2014 if (panel->selected == panel->count - 1)
2015 return;
2016 unselect_item (panel);
2017 items = ITEMS (panel);
2018 if (panel->top_file > panel->count - 2 * items)
2019 items = panel->count - items - panel->top_file;
2020 if (panel->top_file + items < 0)
2021 items = -panel->top_file;
2022 if (!items)
2023 panel->selected = panel->count - 1;
2024 else
2025 panel->selected += items;
2026 panel->top_file += items;
2028 /* This keeps the selection in it's relative position */
2029 if (panel->selected >= panel->count)
2030 panel->selected = panel->count - 1;
2031 if (panel->top_file >= panel->count)
2032 panel->top_file = panel->count - 1;
2033 select_item (panel);
2034 paint_dir (panel);
2037 static void
2038 ctrl_next_page (WPanel *panel)
2040 if ((S_ISDIR (selection (panel)->st.st_mode)
2041 || link_isdir (selection (panel)))) {
2042 do_cd (selection (panel)->fname, cd_exact);
2046 static void
2047 goto_top_file (WPanel *panel)
2049 unselect_item (panel);
2050 panel->selected = panel->top_file;
2051 select_item (panel);
2054 static void
2055 goto_middle_file (WPanel *panel)
2057 unselect_item (panel);
2058 panel->selected = panel->top_file + (ITEMS (panel)/2);
2059 if (panel->selected >= panel->count)
2060 panel->selected = panel->count - 1;
2061 select_item (panel);
2064 static void
2065 goto_bottom_file (WPanel *panel)
2067 unselect_item (panel);
2068 panel->selected = panel->top_file + ITEMS (panel)-1;
2069 if (panel->selected >= panel->count)
2070 panel->selected = panel->count - 1;
2071 select_item (panel);
2074 static void
2075 move_home (WPanel *panel)
2077 if (panel->selected == 0)
2078 return;
2079 unselect_item (panel);
2081 if (torben_fj_mode){
2082 int middle_pos = panel->top_file + (ITEMS (panel)/2);
2084 if (panel->selected > middle_pos){
2085 goto_middle_file (panel);
2086 return;
2088 if (panel->selected != panel->top_file){
2089 goto_top_file (panel);
2090 return;
2094 panel->top_file = 0;
2095 panel->selected = 0;
2097 paint_dir (panel);
2098 select_item (panel);
2101 static void
2102 move_end (WPanel *panel)
2104 if (panel->selected == panel->count-1)
2105 return;
2106 unselect_item (panel);
2107 if (torben_fj_mode){
2108 int middle_pos = panel->top_file + (ITEMS (panel)/2);
2110 if (panel->selected < middle_pos){
2111 goto_middle_file (panel);
2112 return;
2114 if (panel->selected != (panel->top_file + ITEMS(panel)-1)){
2115 goto_bottom_file (panel);
2116 return;
2120 panel->selected = panel->count-1;
2121 paint_dir (panel);
2122 select_item (panel);
2125 /* Recalculate the panels summary information, used e.g. when marked
2126 files might have been removed by an external command */
2127 void
2128 recalculate_panel_summary (WPanel *panel)
2130 int i;
2132 panel->marked = 0;
2133 panel->dirs_marked = 0;
2134 panel->total = 0;
2136 for (i = 0; i < panel->count; i++)
2137 if (panel->dir.list [i].f.marked){
2138 /* do_file_mark will return immediately if newmark == oldmark.
2139 So we have to first unmark it to get panel's summary information
2140 updated. (Norbert) */
2141 panel->dir.list [i].f.marked = 0;
2142 do_file_mark (panel, i, 1);
2146 /* This routine marks a file or a directory */
2147 void
2148 do_file_mark (WPanel *panel, int idx, int mark)
2150 if (panel->dir.list[idx].f.marked == mark)
2151 return;
2153 /* Only '..' can't be marked, '.' isn't visible */
2154 if (!strcmp (panel->dir.list[idx].fname, ".."))
2155 return;
2157 file_mark (panel, idx, mark);
2158 if (panel->dir.list[idx].f.marked) {
2159 panel->marked++;
2160 if (S_ISDIR (panel->dir.list[idx].st.st_mode)) {
2161 if (panel->dir.list[idx].f.dir_size_computed)
2162 panel->total += panel->dir.list[idx].st.st_size;
2163 panel->dirs_marked++;
2164 } else
2165 panel->total += panel->dir.list[idx].st.st_size;
2166 set_colors (panel);
2167 } else {
2168 if (S_ISDIR (panel->dir.list[idx].st.st_mode)) {
2169 if (panel->dir.list[idx].f.dir_size_computed)
2170 panel->total -= panel->dir.list[idx].st.st_size;
2171 panel->dirs_marked--;
2172 } else
2173 panel->total -= panel->dir.list[idx].st.st_size;
2174 panel->marked--;
2178 static void
2179 do_mark_file (WPanel *panel, int do_move)
2181 do_file_mark (panel, panel->selected,
2182 selection (panel)->f.marked ? 0 : 1);
2183 if (mark_moves_down && do_move)
2184 move_down (panel);
2187 static void
2188 mark_file (WPanel *panel)
2190 do_mark_file (panel, 1);
2193 /* Incremental search of a file name in the panel */
2194 static void
2195 do_search (WPanel *panel, int c_code)
2197 size_t l, max, buf_max;
2198 int i, sel;
2199 int wrapped = 0;
2200 char *act;
2202 l = strlen (panel->search_buffer);
2203 if (c_code == KEY_BACKSPACE) {
2204 if (l != 0) {
2205 act = panel->search_buffer + l;
2206 str_prev_noncomb_char (&act, panel->search_buffer);
2207 act[0] = '\0';
2209 panel->search_chpoint = 0;
2210 } else {
2211 if (c_code && (gsize) panel->search_chpoint < sizeof (panel->search_char)) {
2212 panel->search_char[panel->search_chpoint] = c_code;
2213 panel->search_chpoint++;
2216 if (panel->search_chpoint > 0) {
2217 switch (str_is_valid_char (panel->search_char, panel->search_chpoint)) {
2218 case -2:
2219 return;
2220 case -1:
2221 panel->search_chpoint = 0;
2222 return;
2223 default:
2224 if (l + panel->search_chpoint < sizeof (panel->search_buffer)) {
2225 memcpy (panel->search_buffer + l, panel->search_char,
2226 panel->search_chpoint);
2227 l+= panel->search_chpoint;
2228 (panel->search_buffer + l)[0] = '\0';
2229 panel->search_chpoint = 0;
2235 buf_max = panel->case_sensitive ?
2236 str_prefix (panel->search_buffer, panel->search_buffer) :
2237 str_caseprefix (panel->search_buffer, panel->search_buffer);
2238 max = 0;
2239 sel = panel->selected;
2240 for (i = panel->selected; !wrapped || i != panel->selected; i++) {
2241 if (i >= panel->count) {
2242 i = 0;
2243 if (wrapped)
2244 break;
2245 wrapped = 1;
2247 l = panel->case_sensitive ?
2248 str_prefix (panel->dir.list[i].fname, panel->search_buffer) :
2249 str_caseprefix (panel->dir.list[i].fname, panel->search_buffer);
2250 if (l > max) {
2251 max = l;
2252 sel = i;
2253 if (max == buf_max) break;
2257 unselect_item (panel);
2258 panel->selected = sel;
2259 select_item (panel);
2261 act = panel->search_buffer + strlen (panel->search_buffer);
2262 while (max < buf_max) {
2263 str_prev_char_safe (&act);
2264 act[0] = '\0';
2265 buf_max = panel->case_sensitive ?
2266 str_prefix (panel->search_buffer, panel->search_buffer) :
2267 str_caseprefix (panel->search_buffer, panel->search_buffer);
2270 paint_panel (panel);
2273 static void
2274 start_search (WPanel *panel)
2276 if (panel->searching){
2277 if (panel->selected+1 == panel->count)
2278 panel->selected = 0;
2279 else
2280 move_down (panel);
2281 do_search (panel, 0);
2282 } else {
2283 panel->searching = 1;
2284 panel->search_buffer[0] = '\0';
2285 panel->search_char[0] = '\0';
2286 panel->search_chpoint = 0;
2287 display_mini_info (panel);
2288 mc_refresh ();
2292 /* Return 1 if the Enter key has been processed, 0 otherwise */
2293 static int
2294 do_enter_on_file_entry (file_entry *fe)
2296 char *full_name;
2299 * Directory or link to directory - change directory.
2300 * Try the same for the entries on which mc_lstat() has failed.
2302 if (S_ISDIR (fe->st.st_mode) || link_isdir (fe)
2303 || (fe->st.st_mode == 0)) {
2304 if (!do_cd (fe->fname, cd_exact))
2305 message (D_ERROR, MSG_ERROR, _("Cannot change directory"));
2306 return 1;
2309 /* Try associated command */
2310 if (regex_command (fe->fname, "Open", 0) != 0)
2311 return 1;
2313 /* Check if the file is executable */
2314 full_name = concat_dir_and_file (current_panel->cwd, fe->fname);
2315 if (!is_exe (fe->st.st_mode) || !if_link_is_exe (full_name, fe)) {
2316 g_free (full_name);
2317 return 0;
2319 g_free (full_name);
2321 if (confirm_execute) {
2322 if (query_dialog
2323 (_(" The Midnight Commander "),
2324 _(" Do you really want to execute? "), D_NORMAL, 2, _("&Yes"),
2325 _("&No")) != 0)
2326 return 1;
2328 #ifdef USE_VFS
2329 if (!vfs_current_is_local ()) {
2330 char *tmp;
2331 int ret;
2333 tmp = concat_dir_and_file (vfs_get_current_dir (), fe->fname);
2334 ret = mc_setctl (tmp, VFS_SETCTL_RUN, NULL);
2335 g_free (tmp);
2336 /* We took action only if the dialog was shown or the execution
2337 * was successful */
2338 return confirm_execute || (ret == 0);
2340 #endif
2343 char *tmp = name_quote (fe->fname, 0);
2344 char *cmd = g_strconcat (".", PATH_SEP_STR, tmp, (char *) NULL);
2345 g_free (tmp);
2346 shell_execute (cmd, 0);
2347 g_free (cmd);
2350 return 1;
2353 static int
2354 do_enter (WPanel *panel)
2356 return do_enter_on_file_entry (selection (panel));
2359 static void
2360 chdir_other_panel (WPanel *panel)
2362 char *new_dir;
2363 char *sel_entry = NULL;
2365 if (get_other_type () != view_listing) {
2366 set_display_type (get_other_index (), view_listing);
2369 if (!S_ISDIR (panel->dir.list [panel->selected].st.st_mode)) {
2370 new_dir = concat_dir_and_file (panel->cwd, "..");
2371 sel_entry = strrchr(panel->cwd, PATH_SEP);
2372 } else
2373 new_dir = concat_dir_and_file (panel->cwd, panel->dir.list [panel->selected].fname);
2375 change_panel ();
2376 do_cd (new_dir, cd_exact);
2377 if (sel_entry)
2378 try_to_select (current_panel, sel_entry);
2379 change_panel ();
2381 move_down (panel);
2383 g_free (new_dir);
2387 * Make the current directory of the current panel also the current
2388 * directory of the other panel. Put the other panel to the listing
2389 * mode if needed. If the current panel is panelized, the other panel
2390 * doesn't become panelized.
2392 static void
2393 sync_other_panel (WPanel *panel)
2395 if (get_other_type () != view_listing) {
2396 set_display_type (get_other_index (), view_listing);
2399 do_panel_cd (other_panel, current_panel->cwd, cd_exact);
2401 /* try to select current filename on the other panel */
2402 if (!panel->is_panelized) {
2403 try_to_select (other_panel, selection (panel)->fname);
2407 static void
2408 chdir_to_readlink (WPanel *panel)
2410 char *new_dir;
2412 if (get_other_type () != view_listing)
2413 return;
2415 if (S_ISLNK (panel->dir.list [panel->selected].st.st_mode)) {
2416 char buffer [MC_MAXPATHLEN], *p;
2417 int i;
2418 struct stat st;
2420 i = readlink (selection (panel)->fname, buffer, MC_MAXPATHLEN - 1);
2421 if (i < 0)
2422 return;
2423 if (mc_stat (selection (panel)->fname, &st) < 0)
2424 return;
2425 buffer [i] = 0;
2426 if (!S_ISDIR (st.st_mode)) {
2427 p = strrchr (buffer, PATH_SEP);
2428 if (p && !p[1]) {
2429 *p = 0;
2430 p = strrchr (buffer, PATH_SEP);
2432 if (!p)
2433 return;
2434 p[1] = 0;
2436 if (*buffer == PATH_SEP)
2437 new_dir = g_strdup (buffer);
2438 else
2439 new_dir = concat_dir_and_file (panel->cwd, buffer);
2441 change_panel ();
2442 do_cd (new_dir, cd_exact);
2443 change_panel ();
2445 move_down (panel);
2447 g_free (new_dir);
2451 static gsize
2452 panel_get_format_field_count(WPanel *panel)
2454 format_e *format;
2455 gsize index;
2456 for (
2457 index=0, format = panel->format;
2458 format != NULL;
2459 format = format->next, index++
2461 return index;
2465 function return 0 if not found and REAL_INDEX+1 if found
2467 static gsize
2468 panel_get_format_field_index_by_name(WPanel *panel, const char *name)
2470 format_e *format;
2471 gsize index;
2473 for (
2474 index=1, format = panel->format;
2475 ! ( format == NULL || strcmp(format->title, _(name)) == 0 );
2476 format = format->next, index++
2478 if (format == NULL)
2479 index = 0;
2481 return index;
2484 format_e *
2485 panel_get_format_field_by_index(WPanel *panel, gsize index)
2487 format_e *format;
2488 for (
2489 format = panel->format;
2490 ! ( format == NULL || index == 0 );
2491 format = format->next, index--
2493 return format;
2496 static const panel_field_t *
2497 panel_get_sortable_field_by_format(WPanel *panel, gsize index)
2499 const panel_field_t *pfield;
2500 format_e *format;
2502 format = panel_get_format_field_by_index(panel, index);
2503 if (format == NULL)
2504 return NULL;
2505 pfield = panel_get_field_by_title(format->title);
2506 if (pfield == NULL)
2507 return NULL;
2508 if (pfield->sort_routine == NULL)
2509 return NULL;
2510 return pfield;
2513 static void
2514 panel_toggle_sort_order_prev(WPanel *panel)
2516 gsize index, i;
2518 const panel_field_t *pfield = NULL;
2520 index = panel_get_format_field_index_by_name(panel, panel->current_sort_field->title);
2522 if (index > 1){
2523 /* search for prev sortable column in panel format */
2524 for (
2525 i = index-1 ;
2526 i != 0 && (pfield = panel_get_sortable_field_by_format(panel, i-1)) == NULL ;
2531 if ( pfield == NULL) {
2532 /* Sortable field not found. Try to search in each array */
2533 for (
2534 i = panel_get_format_field_count(panel) ;
2535 i != 0 && (pfield = panel_get_sortable_field_by_format(panel, i-1)) == NULL ;
2539 if ( pfield == NULL)
2540 return;
2541 panel->current_sort_field = pfield;
2542 panel_set_sort_order(panel, panel->current_sort_field);
2546 static void
2547 panel_toggle_sort_order_next(WPanel *panel)
2549 gsize index, i;
2550 const panel_field_t *pfield = NULL;
2551 gsize format_field_count = panel_get_format_field_count(panel);
2553 index = panel_get_format_field_index_by_name(panel, panel->current_sort_field->title);
2555 if (index != 0 && index != format_field_count){
2556 /* search for prev sortable column in panel format */
2557 for (
2558 i = index;
2559 i != format_field_count && (pfield = panel_get_sortable_field_by_format(panel, i)) == NULL ;
2564 if ( pfield == NULL) {
2565 /* Sortable field not found. Try to search in each array */
2566 for (
2567 i = 0 ;
2568 i != format_field_count && (pfield = panel_get_sortable_field_by_format(panel, i)) == NULL ;
2572 if ( pfield == NULL)
2573 return;
2574 panel->current_sort_field = pfield;
2575 panel_set_sort_order(panel, panel->current_sort_field);
2578 static void
2579 panel_select_sort_order(WPanel *panel)
2581 const panel_field_t *sort_order;
2582 sort_order = sort_box (panel->current_sort_field, &panel->reverse,
2583 &panel->case_sensitive,
2584 &panel->exec_first);
2585 if (sort_order == NULL)
2586 return;
2587 panel->current_sort_field = sort_order;
2588 panel_set_sort_order (panel, panel->current_sort_field);
2592 static void
2593 panel_set_sort_type_by_id(WPanel *panel, const char *name)
2595 const panel_field_t *sort_order;
2597 if (strcmp(panel->current_sort_field->id, name) != 0) {
2598 sort_order = panel_get_field_by_id (name);
2599 if (sort_order == NULL)
2600 return;
2601 panel->current_sort_field = sort_order;
2602 } else {
2603 panel->reverse = ! panel->reverse;
2605 panel_set_sort_order (panel, panel->current_sort_field);
2608 typedef void (*panel_key_callback) (WPanel *);
2610 static void cmd_do_enter(WPanel *wp) { (void) do_enter(wp); }
2611 static void cmd_view_simple(WPanel *wp) { (void) wp; view_simple_cmd(); }
2612 static void cmd_edit_new(WPanel *wp) { (void) wp; edit_cmd_new(); }
2613 static void cmd_copy_local(WPanel *wp) { (void) wp;copy_cmd_local(); }
2614 static void cmd_rename_local(WPanel *wp) { (void) wp;ren_cmd_local(); }
2615 static void cmd_delete_local(WPanel *wp) { (void) wp;delete_cmd_local(); }
2616 static void cmd_select(WPanel *wp) { (void) wp;select_cmd(); }
2617 static void cmd_unselect(WPanel *wp) { (void) wp;unselect_cmd(); }
2618 static void cmd_reverse_selection(WPanel *wp) { (void) wp;reverse_selection_cmd(); }
2620 static cb_ret_t
2621 panel_execute_cmd (WPanel *panel, int command)
2623 int res = MSG_HANDLED;
2625 switch (command) {
2626 case CK_PanelChdirOtherPanel:
2627 chdir_other_panel (panel);
2628 break;
2629 case CK_PanelChdirToReadlink:
2630 chdir_to_readlink (panel);
2631 break;
2632 case CK_PanelCmdCopyLocal:
2633 cmd_copy_local (panel);
2634 break;
2635 case CK_PanelCmdDeleteLocal:
2636 cmd_delete_local (panel);
2637 break;
2638 case CK_PanelCmdDoEnter:
2639 cmd_do_enter (panel);
2640 break;
2641 case CK_PanelCmdViewSimple:
2642 cmd_view_simple (panel);
2643 break;
2644 case CK_PanelCmdEditNew:
2645 cmd_edit_new (panel);
2646 break;
2647 case CK_PanelCmdRenameLocal:
2648 cmd_rename_local (panel);
2649 break;
2650 case CK_PanelCmdReverseSelection:
2651 cmd_reverse_selection (panel);
2652 break;
2653 case CK_PanelCmdSelect:
2654 cmd_select (panel);
2655 break;
2656 case CK_PanelCmdUnselect:
2657 cmd_unselect (panel);
2658 break;
2659 case CK_PanelNextPage:
2660 next_page (panel);
2661 break;
2662 case CK_PanelPrevPage:
2663 prev_page (panel);
2664 break;
2665 case CK_PanelCtrlNextPage:
2666 ctrl_next_page (panel);
2667 break;
2668 case CK_PanelCtrlPrevPage:
2669 ctrl_prev_page (panel);
2670 break;
2671 case CK_PanelDirectoryHistoryList:
2672 directory_history_list (panel);
2673 break;
2674 case CK_PanelDirectoryHistoryNext:
2675 directory_history_next (panel);
2676 break;
2677 case CK_PanelDirectoryHistoryPrev:
2678 directory_history_prev (panel);
2679 break;
2680 case CK_PanelGotoBottomFile:
2681 goto_bottom_file (panel);
2682 break;
2683 case CK_PanelGotoMiddleFile:
2684 goto_middle_file (panel);
2685 break;
2686 case CK_PanelGotoTopFile:
2687 goto_top_file (panel);
2688 break;
2689 case CK_PanelMarkFile:
2690 mark_file (panel);
2691 break;
2692 case CK_PanelMoveUp:
2693 move_up (panel);
2694 break;
2695 case CK_PanelMoveDown:
2696 move_down (panel);
2697 break;
2698 case CK_PanelMoveLeft:
2699 res = move_left (panel);
2700 break;
2701 case CK_PanelMoveRight:
2702 res = move_right (panel);
2703 break;
2704 case CK_PanelMoveEnd:
2705 move_end (panel);
2706 break;
2707 case CK_PanelMoveHome:
2708 move_home (panel);
2709 break;
2710 case CK_PanelSetPanelEncoding:
2711 set_panel_encoding (panel);
2712 break;
2713 case CK_PanelStartSearch:
2714 start_search (panel);
2715 break;
2716 case CK_PanelSyncOtherPanel:
2717 sync_other_panel (panel);
2718 break;
2719 case CK_PanelSelectSortOrder:
2720 panel_select_sort_order(panel);
2721 break;
2722 case CK_PanelToggleSortOrderPrev:
2723 panel_toggle_sort_order_prev(panel);
2724 break;
2725 case CK_PanelToggleSortOrderNext:
2726 panel_toggle_sort_order_next(panel);
2727 break;
2728 case CK_PanelReverseSort:
2729 panel->reverse = ! panel->reverse;
2730 panel_set_sort_order (panel, panel->current_sort_field);
2731 break;
2732 case CK_PanelSortOrderByName:
2733 panel_set_sort_type_by_id(panel, "name");
2734 break;
2735 case CK_PanelSortOrderByExt:
2736 panel_set_sort_type_by_id(panel, "extension");
2737 break;
2738 case CK_PanelSortOrderBySize:
2739 panel_set_sort_type_by_id(panel, "size");
2740 break;
2741 case CK_PanelSortOrderByMTime:
2742 panel_set_sort_type_by_id(panel, "mtime");
2743 break;
2745 return res;
2748 static cb_ret_t
2749 panel_key (WPanel *panel, int key)
2751 int i;
2752 int res, command;
2754 for (i = 0; panel_map[i].key; i++) {
2755 if (key == panel_map[i].key) {
2756 int old_searching = panel->searching;
2758 if (panel_map[i].command != CK_PanelStartSearch)
2759 panel->searching = 0;
2761 command = panel_map[i].command;
2762 res = panel_execute_cmd (panel, command);
2764 if (res == MSG_NOT_HANDLED)
2765 return res;
2767 if (panel->searching != old_searching)
2768 display_mini_info (panel);
2769 return MSG_HANDLED;
2773 if (torben_fj_mode && key == ALT ('h')) {
2774 goto_middle_file (panel);
2775 return MSG_HANDLED;
2778 if (is_abort_char (key)) {
2779 panel->searching = 0;
2780 display_mini_info (panel);
2781 return MSG_HANDLED;
2784 /* Do not eat characters not meant for the panel below ' ' (e.g. C-l). */
2785 if ((key >= ' ' && key <= 255) || key == KEY_BACKSPACE) {
2786 if (panel->searching) {
2787 do_search (panel, key);
2788 return MSG_HANDLED;
2791 if (!command_prompt) {
2792 start_search (panel);
2793 do_search (panel, key);
2794 return MSG_HANDLED;
2798 return MSG_NOT_HANDLED;
2801 static cb_ret_t
2802 panel_callback (Widget *w, widget_msg_t msg, int parm)
2804 WPanel *panel = (WPanel *) w;
2805 Dlg_head *h = panel->widget.parent;
2807 switch (msg) {
2808 case WIDGET_DRAW:
2809 paint_panel (panel);
2810 return MSG_HANDLED;
2812 case WIDGET_FOCUS:
2813 current_panel = panel;
2814 panel->active = 1;
2815 if (mc_chdir (panel->cwd) != 0) {
2816 char *cwd = strip_password (g_strdup (panel->cwd), 1);
2817 message (D_ERROR, MSG_ERROR, _(" Cannot chdir to \"%s\" \n %s "),
2818 cwd, unix_error_string (errno));
2819 g_free(cwd);
2820 } else
2821 subshell_chdir (panel->cwd);
2823 update_xterm_title_path ();
2824 select_item (panel);
2825 show_dir (panel);
2826 paint_dir (panel);
2827 panel->dirty = 0;
2829 buttonbar_set_label (h, 1, Q_("ButtonBar|Help"), help_cmd);
2830 buttonbar_set_label (h, 2, Q_("ButtonBar|Menu"), user_file_menu_cmd);
2831 buttonbar_set_label (h, 3, Q_("ButtonBar|View"), view_cmd);
2832 buttonbar_set_label (h, 4, Q_("ButtonBar|Edit"), edit_cmd);
2833 buttonbar_set_label (h, 5, Q_("ButtonBar|Copy"), copy_cmd);
2834 buttonbar_set_label (h, 6, Q_("ButtonBar|RenMov"), ren_cmd);
2835 buttonbar_set_label (h, 7, Q_("ButtonBar|Mkdir"), mkdir_cmd);
2836 buttonbar_set_label (h, 8, Q_("ButtonBar|Delete"), delete_cmd);
2837 buttonbar_redraw (h);
2838 return MSG_HANDLED;
2840 case WIDGET_UNFOCUS:
2841 /* Janne: look at this for the multiple panel options */
2842 if (panel->searching){
2843 panel->searching = 0;
2844 display_mini_info (panel);
2846 panel->active = 0;
2847 show_dir (panel);
2848 unselect_item (panel);
2849 return MSG_HANDLED;
2851 case WIDGET_KEY:
2852 return panel_key (panel, parm);
2854 case WIDGET_DESTROY:
2855 panel_destroy (panel);
2856 return MSG_HANDLED;
2858 default:
2859 return default_proc (msg, parm);
2863 void
2864 file_mark (WPanel *panel, int index, int val)
2866 if (panel->dir.list[index].f.marked != val) {
2867 panel->dir.list[index].f.marked = val;
2868 panel->dirty = 1;
2872 /* */
2873 /* Panel mouse events support routines */
2874 /* */
2875 static int mouse_marking = 0;
2877 static void
2878 mouse_toggle_mark (WPanel *panel)
2880 do_mark_file (panel, 0);
2881 mouse_marking = selection (panel)->f.marked;
2884 static void
2885 mouse_set_mark (WPanel *panel)
2887 if (mouse_marking && !(selection (panel)->f.marked))
2888 do_mark_file (panel, 0);
2889 else if (!mouse_marking && (selection (panel)->f.marked))
2890 do_mark_file (panel, 0);
2893 static int
2894 mark_if_marking (WPanel *panel, Gpm_Event *event)
2896 if (event->buttons & GPM_B_RIGHT){
2897 if (event->type & GPM_DOWN)
2898 mouse_toggle_mark (panel);
2899 else
2900 mouse_set_mark (panel);
2901 return 1;
2903 return 0;
2906 /* Determine which column was clicked, and sort the panel on
2907 * that column, or reverse sort on that column if already
2908 * sorted on that column.
2910 static void
2911 mouse_sort_col(Gpm_Event *event, WPanel *panel)
2913 int i;
2914 const char *sort_name = NULL;
2915 panel_field_t *col_sort_format = NULL;
2916 format_e *format;
2918 for (i = 0, format = panel->format; format != NULL; format = format->next) {
2919 i += format->field_len;
2920 if (event->x < i + 1) {
2921 /* found column */
2922 sort_name = format->title;
2923 break;
2927 if (sort_name == NULL)
2928 return;
2930 for(i = 0; panel_fields[i].id != NULL; i++) {
2931 if (!strcmp (sort_name, _(panel_fields[i].title)) && panel_fields[i].sort_routine) {
2932 col_sort_format = &panel_fields[i];
2933 break;
2937 if (!col_sort_format)
2938 return;
2940 if (panel->current_sort_field == col_sort_format) {
2941 /* reverse the sort if clicked column is already the sorted column */
2942 panel->reverse = !panel->reverse;
2943 } else {
2944 /* new sort is forced to be ascending */
2945 panel->reverse = 0;
2947 panel_set_sort_order (panel, col_sort_format);
2952 * Mouse callback of the panel minus repainting.
2953 * If the event is redirected to the menu, *redir is set to 1.
2955 static int
2956 do_panel_event (Gpm_Event *event, WPanel *panel, int *redir)
2958 const int lines = llines (panel);
2959 const gboolean is_active = dlg_widget_active (panel);
2960 const gboolean mouse_down = (event->type & GPM_DOWN) != 0;
2962 /* "." button show/hide hidden files */
2963 if (event->type & GPM_DOWN && event->x == panel->widget.cols - 5 && event->y == 1) {
2964 toggle_show_hidden();
2965 repaint_screen ();
2966 return MOU_NORMAL;
2969 /* "<" button */
2970 if (mouse_down && event->y == 1 && event->x == 2) {
2971 directory_history_prev (panel);
2972 return MOU_NORMAL;
2975 /* ">" button */
2976 if (mouse_down && event->y == 1 && event->x == panel->widget.cols - 1) {
2977 directory_history_next (panel);
2978 return MOU_NORMAL;
2981 /* "^" button */
2982 if (mouse_down && event->y == 1 && event->x >= panel->widget.cols - 4 && event->x <= panel->widget.cols - 2) {
2983 directory_history_list (panel);
2984 return MOU_NORMAL;
2987 /* sort on clicked column */
2988 if (event->type & GPM_DOWN && event->y == 2) {
2989 mouse_sort_col(event,panel);
2990 return MOU_NORMAL;
2993 /* rest of the upper frame, the menu is invisible - call menu */
2994 if (mouse_down && event->y == 1 && !menubar_visible) {
2995 *redir = 1;
2996 event->x += panel->widget.x;
2997 return the_menubar->widget.mouse (event, the_menubar);
3000 /* Mouse wheel events */
3001 if (mouse_down && (event->buttons & GPM_B_UP)) {
3002 if (is_active) {
3003 if (panel->top_file > 0)
3004 prev_page (panel);
3005 else /* We are in first page */
3006 move_up (panel);
3008 return MOU_NORMAL;
3011 if (mouse_down && (event->buttons & GPM_B_DOWN)) {
3012 if (is_active) {
3013 if (panel->top_file + ITEMS (panel) < panel->count)
3014 next_page (panel);
3015 else /* We are in last page */
3016 move_down (panel);
3018 return MOU_NORMAL;
3021 event->y -= 2;
3022 if ((event->type & (GPM_DOWN | GPM_DRAG))) {
3023 int my_index;
3025 if (!is_active)
3026 change_panel ();
3028 if (event->y <= 0) {
3029 mark_if_marking (panel, event);
3030 if (mouse_move_pages)
3031 prev_page (panel);
3032 else
3033 move_up (panel);
3034 return MOU_REPEAT;
3037 if (!((panel->top_file + event->y <= panel->count) && event->y <= lines)) {
3038 mark_if_marking (panel, event);
3039 if (mouse_move_pages)
3040 next_page (panel);
3041 else
3042 move_down (panel);
3043 return MOU_REPEAT;
3046 my_index = panel->top_file + event->y - 1;
3047 if (panel->split && (event->x > ((panel->widget.cols - 2) / 2)))
3048 my_index += llines (panel);
3050 if (my_index >= panel->count)
3051 my_index = panel->count - 1;
3053 if (my_index != panel->selected) {
3054 unselect_item (panel);
3055 panel->selected = my_index;
3056 select_item (panel);
3059 /* This one is new */
3060 mark_if_marking (panel, event);
3061 } else if ((event->type & (GPM_UP | GPM_DOUBLE)) == (GPM_UP | GPM_DOUBLE)) {
3062 if (event->y > 0 && event->y <= lines)
3063 do_enter (panel);
3065 return MOU_NORMAL;
3068 /* Mouse callback of the panel */
3069 static int
3070 panel_event (Gpm_Event *event, void *data)
3072 WPanel *panel = data;
3073 int ret;
3074 int redir = MOU_NORMAL;
3076 ret = do_panel_event (event, panel, &redir);
3077 if (!redir)
3078 paint_panel (panel);
3080 return ret;
3083 void
3084 panel_re_sort (WPanel *panel)
3086 char *filename;
3087 int i;
3089 if (panel == NULL)
3090 return;
3092 filename = g_strdup (selection (panel)->fname);
3093 unselect_item (panel);
3094 do_sort (&panel->dir, panel->current_sort_field->sort_routine, panel->count-1, panel->reverse,
3095 panel->case_sensitive, panel->exec_first);
3096 panel->selected = -1;
3097 for (i = panel->count; i; i--){
3098 if (!strcmp (panel->dir.list [i-1].fname, filename)){
3099 panel->selected = i-1;
3100 break;
3103 g_free (filename);
3104 panel->top_file = panel->selected - ITEMS (panel)/2;
3105 if (panel->top_file < 0)
3106 panel->top_file = 0;
3107 select_item (panel);
3108 panel->dirty = 1;
3111 void
3112 panel_set_sort_order (WPanel *panel, const panel_field_t *sort_order)
3114 if (sort_order == 0)
3115 return;
3117 panel->current_sort_field = sort_order;
3119 /* The directory is already sorted, we have to load the unsorted stuff */
3120 if (sort_order->sort_routine == (sortfn *) unsorted){
3121 char *current_file;
3123 current_file = g_strdup (panel->dir.list [panel->selected].fname);
3124 panel_reload (panel);
3125 try_to_select (panel, current_file);
3126 g_free (current_file);
3128 panel_re_sort (panel);
3131 void
3132 set_panel_encoding (WPanel *panel)
3134 const char *encoding = NULL;
3135 char *cd_path;
3136 #ifdef HAVE_CHARSET
3137 const char *errmsg;
3138 int r;
3140 r = select_charset (-1, -1, source_codepage, FALSE);
3142 if (r == SELECT_CHARSET_CANCEL)
3143 return; /* Cancel */
3145 if (r == SELECT_CHARSET_NO_TRANSLATE) {
3146 /* No translation */
3147 errmsg = init_translation_table (display_codepage, display_codepage);
3148 cd_path = remove_encoding_from_path (panel->cwd);
3149 do_panel_cd (panel, cd_path, 0);
3150 g_free (cd_path);
3151 return;
3154 source_codepage = r;
3156 errmsg = init_translation_table (source_codepage, display_codepage);
3157 if (errmsg) {
3158 message (D_ERROR, MSG_ERROR, "%s", errmsg);
3159 return;
3162 encoding = get_codepage_id (source_codepage);
3163 #endif
3164 if (encoding != NULL) {
3165 cd_path = add_encoding_to_path (panel->cwd, encoding);
3166 if (!do_panel_cd (panel, cd_path, 0))
3167 message (D_ERROR, MSG_ERROR, _(" Cannot chdir to %s "), cd_path);
3168 g_free (cd_path);
3172 static void
3173 reload_panelized (WPanel *panel)
3175 int i, j;
3176 dir_list *list = &panel->dir;
3178 if (panel != current_panel)
3179 mc_chdir (panel->cwd);
3181 for (i = 0, j = 0; i < panel->count; i++) {
3182 if (list->list[i].f.marked) {
3183 /* Unmark the file in advance. In case the following mc_lstat
3184 * fails we are done, else we have to mark the file again
3185 * (Note: do_file_mark depends on a valid "list->list [i].buf").
3186 * IMO that's the best way to update the panel's summary status
3187 * -- Norbert
3189 do_file_mark (panel, i, 0);
3191 if (mc_lstat (list->list[i].fname, &list->list[i].st)) {
3192 g_free (list->list[i].fname);
3193 continue;
3195 if (list->list[i].f.marked)
3196 do_file_mark (panel, i, 1);
3197 if (j != i)
3198 list->list[j] = list->list[i];
3199 j++;
3201 if (j == 0)
3202 panel->count = set_zero_dir (list);
3203 else
3204 panel->count = j;
3206 if (panel != current_panel)
3207 mc_chdir (current_panel->cwd);
3210 static void
3211 update_one_panel_widget (WPanel *panel, int force_update,
3212 const char *current_file)
3214 int free_pointer;
3215 char *my_current_file = NULL;
3217 if (force_update & UP_RELOAD) {
3218 panel->is_panelized = 0;
3219 mc_setctl (panel->cwd, VFS_SETCTL_FLUSH, 0);
3220 memset (&(panel->dir_stat), 0, sizeof (panel->dir_stat));
3223 /* If current_file == -1 (an invalid pointer) then preserve selection */
3224 if (current_file == UP_KEEPSEL) {
3225 free_pointer = 1;
3226 my_current_file = g_strdup (panel->dir.list[panel->selected].fname);
3227 current_file = my_current_file;
3228 } else
3229 free_pointer = 0;
3231 if (panel->is_panelized)
3232 reload_panelized (panel);
3233 else
3234 panel_reload (panel);
3236 try_to_select (panel, current_file);
3237 panel->dirty = 1;
3239 if (free_pointer)
3240 g_free (my_current_file);
3243 static void
3244 update_one_panel (int which, int force_update, const char *current_file)
3246 WPanel *panel;
3248 if (get_display_type (which) != view_listing)
3249 return;
3251 panel = (WPanel *) get_panel_widget (which);
3252 update_one_panel_widget (panel, force_update, current_file);
3255 /* This routine reloads the directory in both panels. It tries to
3256 * select current_file in current_panel and other_file in other_panel.
3257 * If current_file == -1 then it automatically sets current_file and
3258 * other_file to the currently selected files in the panels.
3260 * if force_update has the UP_ONLY_CURRENT bit toggled on, then it
3261 * will not reload the other panel.
3263 void
3264 update_panels (int force_update, const char *current_file)
3266 int reload_other = !(force_update & UP_ONLY_CURRENT);
3267 WPanel *panel;
3269 update_one_panel (get_current_index (), force_update, current_file);
3270 if (reload_other)
3271 update_one_panel (get_other_index (), force_update, UP_KEEPSEL);
3273 if (get_current_type () == view_listing)
3274 panel = (WPanel *) get_panel_widget (get_current_index ());
3275 else
3276 panel = (WPanel *) get_panel_widget (get_other_index ());
3278 mc_chdir (panel->cwd);
3281 gsize
3282 panel_get_num_of_sortable_fields(void)
3284 gsize ret = 0, index;
3286 for(index=0; panel_fields[index].id != NULL; index ++)
3287 if (panel_fields[index].title_hotkey != NULL)
3288 ret++;
3289 return ret;
3293 const char **
3294 panel_get_sortable_fields(gsize *array_size)
3296 char **ret;
3297 gsize index, i;
3299 index = panel_get_num_of_sortable_fields();
3301 ret = g_new0 (char *, index + 1);
3302 if (ret == NULL)
3303 return NULL;
3305 if (array_size != NULL)
3306 *array_size = index;
3308 index=0;
3310 for(i=0; panel_fields[i].id != NULL; i ++)
3311 if (panel_fields[i].title_hotkey != NULL)
3312 ret[index++] = g_strdup(_(panel_fields[i].title_hotkey));
3313 return (const char**) ret;
3316 const panel_field_t *
3317 panel_get_field_by_id(const char *name)
3319 gsize index;
3320 for(index=0; panel_fields[index].id != NULL; index ++)
3321 if (
3322 panel_fields[index].id != NULL &&
3323 strcmp(name, panel_fields[index].id) == 0
3325 return &panel_fields[index];
3326 return NULL;
3329 const panel_field_t *
3330 panel_get_field_by_title_hotkey(const char *name)
3332 gsize index;
3333 for(index=0; panel_fields[index].id != NULL; index ++)
3334 if (
3335 panel_fields[index].title_hotkey != NULL &&
3336 strcmp(name, _(panel_fields[index].title_hotkey)) == 0
3338 return &panel_fields[index];
3339 return NULL;
3342 const panel_field_t *
3343 panel_get_field_by_title(const char *name)
3345 gsize index;
3346 for(index=0; panel_fields[index].id != NULL; index ++)
3347 if (
3348 panel_fields[index].title_hotkey != NULL &&
3349 strcmp(name, _(panel_fields[index].title)) == 0
3351 return &panel_fields[index];
3352 return NULL;
3355 gsize
3356 panel_get_num_of_user_possible_fields(void)
3358 gsize ret = 0, index;
3360 for(index=0; panel_fields[index].id != NULL; index ++)
3361 if (panel_fields[index].use_in_user_format)
3362 ret++;
3363 return ret;
3366 const char **
3367 panel_get_user_possible_fields(gsize *array_size)
3369 char **ret;
3370 gsize index, i;
3372 index = panel_get_num_of_user_possible_fields();
3374 ret = g_new0 (char *, index + 1);
3375 if (ret == NULL)
3376 return NULL;
3378 if (array_size != NULL)
3379 *array_size = index;
3381 index=0;
3383 for(i=0; panel_fields[i].id != NULL; i ++)
3384 if (panel_fields[i].use_in_user_format)
3385 ret[index++] = g_strdup(_(panel_fields[i].title_hotkey));
3386 return (const char**) ret;
3389 void
3390 panel_init(void)
3392 panel_sort_up_sign = mc_skin_get("widget-common","sort-sign-up","'");
3393 panel_sort_down_sign = mc_skin_get("widget-common","sort-sign-down",",");
3395 panel_hiddenfiles_sign_show = mc_skin_get("widget-panel", "hiddenfiles-sign-show", ".");
3396 panel_hiddenfiles_sign_hide = mc_skin_get("widget-panel", "hiddenfiles-sign-hide", ".");
3397 panel_history_prev_item_sign = mc_skin_get("widget-panel", "history-prev-item-sign", "<");
3398 panel_history_next_item_sign = mc_skin_get("widget-panel", "history-next-item-sign", ">");
3399 panel_history_show_list_sign = mc_skin_get("widget-panel", "history-show-list-sign", "^");
3403 void
3404 panel_deinit(void)
3406 g_free(panel_sort_up_sign);
3407 g_free(panel_sort_down_sign);
3409 g_free(panel_hiddenfiles_sign_show);
3410 g_free(panel_hiddenfiles_sign_hide);
3411 g_free(panel_history_prev_item_sign);
3412 g_free(panel_history_next_item_sign);
3413 g_free(panel_history_show_list_sign);