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. */
23 * \brief Source: panel managin module
41 #include "ext.h" /* regexp_command */
42 #include "mouse.h" /* For Gpm_Event */
43 #include "layout.h" /* Most layout variables are here */
44 #include "wtools.h" /* for message (...) */
46 #include "key.h" /* XCTRL and ALT macros */
47 #include "setup.h" /* For loading/saving panel options */
49 #include "../src/mcconfig/mcconfig.h"
52 #include "menu.h" /* menubar_visible */
53 #include "main-widgets.h"
54 #include "main.h" /* the_menubar */
55 #include "unixcompat.h"
56 #include "mountlist.h" /* my_statfs */
57 #include "selcodepage.h" /* do_select_codepage () */
58 #include "charsets.h" /* get_codepage_id () */
61 #define ELEMENTS(arr) ( sizeof(arr) / sizeof((arr)[0]) )
66 #define MARKED_SELECTED 3
70 * This describes a format item. The parse_display_format routine parses
71 * the user specified format and creates a linked list of format_e structures.
73 typedef struct format_e
{
74 struct format_e
*next
;
75 int requested_field_len
;
77 align_crt_t just_mode
;
79 const char *(*string_fn
)(file_entry
*, int len
);
84 /* If true, show the mini-info on the panel */
85 int show_mini_info
= 1;
87 /* If true, then use stat() on the cwd to determine directory changes */
90 /* If true, use some usability hacks by Torben */
91 int torben_fj_mode
= 0;
93 /* If true, up/down keys scroll the pane listing by pages */
94 int panel_scroll_pages
= 1;
96 /* If 1, we use permission hilighting */
97 int permission_mode
= 0;
99 /* If 1 - then add per file type hilighting */
100 int filetype_mode
= 1;
102 /* The hook list for the select file function */
103 Hook
*select_file_hook
= 0;
105 static cb_ret_t
panel_callback (Widget
*, widget_msg_t msg
, int parm
);
106 static int panel_event (Gpm_Event
*event
, void *);
107 static void paint_frame (WPanel
*panel
);
108 static const char *panel_format (WPanel
*panel
);
109 static const char *mini_status_format (WPanel
*panel
);
111 /* This macro extracts the number of available lines in a panel */
112 #define llines(p) (p->widget.lines-3 - (show_mini_info ? 2 : 0))
115 set_colors (WPanel
*panel
)
119 attrset (NORMAL_COLOR
);
122 /* Delete format string, it is a linked list */
124 delete_format (format_e
*format
)
135 /* This code relies on the default justification!!! */
137 add_permission_string (char *dest
, int width
, file_entry
*fe
, int attr
, int color
, int is_octal
)
141 l
= get_user_permissions (&fe
->st
);
144 /* Place of the access bit in octal mode */
148 /* The same to the triplet in string mode */
153 for(i
= 0; i
< width
; i
++){
154 if (i
>= l
&& i
< r
){
155 if (attr
== SELECTED
|| attr
== MARKED_SELECTED
)
156 attrset (MARKED_SELECTED_COLOR
);
158 attrset (MARKED_COLOR
);
166 /* String representations of various file attributes */
169 string_file_name (file_entry
*fe
, int len
)
171 static char buffer
[MC_MAXPATHLEN
* MB_LEN_MAX
+ 1];
174 g_strlcpy (buffer
, fe
->fname
, sizeof(buffer
));
178 static inline unsigned int ilog10(dev_t n
)
180 unsigned int digits
= 0;
187 static void format_device_number (char *buf
, size_t bufsize
, dev_t dev
)
189 dev_t major_dev
= major(dev
);
190 dev_t minor_dev
= minor(dev
);
191 unsigned int major_digits
= ilog10(major_dev
);
192 unsigned int minor_digits
= ilog10(minor_dev
);
194 g_assert(bufsize
>= 1);
195 if (major_digits
+ 1 + minor_digits
+ 1 <= bufsize
) {
196 g_snprintf(buf
, bufsize
, "%lu,%lu", (unsigned long) major_dev
,
197 (unsigned long) minor_dev
);
199 g_strlcpy(buf
, _("[dev]"), bufsize
);
205 string_file_size (file_entry
*fe
, int len
)
207 static char buffer
[BUF_TINY
];
209 /* Don't ever show size of ".." since we don't calculate it */
210 if (!strcmp (fe
->fname
, "..")) {
214 #ifdef HAVE_STRUCT_STAT_ST_RDEV
215 if (S_ISBLK (fe
->st
.st_mode
) || S_ISCHR (fe
->st
.st_mode
))
216 format_device_number (buffer
, len
+ 1, fe
->st
.st_rdev
);
220 size_trunc_len (buffer
, len
, fe
->st
.st_size
, 0);
227 string_file_size_brief (file_entry
*fe
, int len
)
229 if (S_ISLNK (fe
->st
.st_mode
) && !fe
->f
.link_to_dir
) {
233 if ((S_ISDIR (fe
->st
.st_mode
) || fe
->f
.link_to_dir
) && strcmp (fe
->fname
, "..")) {
237 return string_file_size (fe
, len
);
240 /* This functions return a string representation of a file entry */
243 string_file_type (file_entry
*fe
, int len
)
245 static char buffer
[2];
248 if (S_ISDIR (fe
->st
.st_mode
))
249 buffer
[0] = PATH_SEP
;
250 else if (S_ISLNK (fe
->st
.st_mode
)) {
251 if (fe
->f
.link_to_dir
)
253 else if (fe
->f
.stale_link
)
257 } else if (S_ISCHR (fe
->st
.st_mode
))
259 else if (S_ISSOCK (fe
->st
.st_mode
))
261 else if (S_ISDOOR (fe
->st
.st_mode
))
263 else if (S_ISBLK (fe
->st
.st_mode
))
265 else if (S_ISFIFO (fe
->st
.st_mode
))
267 else if (S_ISNAM (fe
->st
.st_mode
))
269 else if (!S_ISREG (fe
->st
.st_mode
))
270 buffer
[0] = '?'; /* non-regular of unknown kind */
271 else if (is_exe (fe
->st
.st_mode
))
281 string_file_mtime (file_entry
*fe
, int len
)
284 if (!strcmp (fe
->fname
, "..")) {
287 return file_date (fe
->st
.st_mtime
);
292 string_file_atime (file_entry
*fe
, int len
)
295 if (!strcmp (fe
->fname
, "..")) {
298 return file_date (fe
->st
.st_atime
);
303 string_file_ctime (file_entry
*fe
, int len
)
306 if (!strcmp (fe
->fname
, "..")) {
309 return file_date (fe
->st
.st_ctime
);
314 string_file_permission (file_entry
*fe
, int len
)
317 return string_perm (fe
->st
.st_mode
);
322 string_file_perm_octal (file_entry
*fe
, int len
)
324 static char buffer
[10];
327 g_snprintf (buffer
, sizeof (buffer
), "0%06lo", (unsigned long) fe
->st
.st_mode
);
333 string_file_nlinks (file_entry
*fe
, int len
)
335 static char buffer
[BUF_TINY
];
338 g_snprintf (buffer
, sizeof (buffer
), "%16d", (int) fe
->st
.st_nlink
);
344 string_inode (file_entry
*fe
, int len
)
346 static char buffer
[10];
349 g_snprintf (buffer
, sizeof (buffer
), "%lu",
350 (unsigned long) fe
->st
.st_ino
);
356 string_file_nuid (file_entry
*fe
, int len
)
358 static char buffer
[10];
361 g_snprintf (buffer
, sizeof (buffer
), "%lu",
362 (unsigned long) fe
->st
.st_uid
);
368 string_file_ngid (file_entry
*fe
, int len
)
370 static char buffer
[10];
373 g_snprintf (buffer
, sizeof (buffer
), "%lu",
374 (unsigned long) fe
->st
.st_gid
);
380 string_file_owner (file_entry
*fe
, int len
)
383 return get_owner (fe
->st
.st_uid
);
388 string_file_group (file_entry
*fe
, int len
)
391 return get_group (fe
->st
.st_gid
);
396 string_marked (file_entry
*fe
, int len
)
399 return fe
->f
.marked
? "*" : " ";
404 string_space (file_entry
*fe
, int len
)
413 string_dot (file_entry
*fe
, int len
)
426 align_crt_t default_just
;
429 const char *(*string_fn
)(file_entry
*, int);
430 sortfn
*sort_routine
; /* This field is currently unused. */
432 { "name", 12, 1, J_LEFT_FIT
, N_("Name"), 1, string_file_name
, (sortfn
*) sort_name
},
433 { "size", 7, 0, J_RIGHT
, N_("Size"), 1, string_file_size
, (sortfn
*) sort_size
},
434 { "bsize", 7, 0, J_RIGHT
, N_("Size"), 1, string_file_size_brief
, (sortfn
*) sort_size
},
435 { "type", GT
, 0, J_LEFT
, "", 2, string_file_type
, NULL
},
436 { "mtime", 12, 0, J_RIGHT
, N_("MTime"), 1, string_file_mtime
, (sortfn
*) sort_time
},
437 { "atime", 12, 0, J_RIGHT
, N_("ATime"), 1, string_file_atime
, (sortfn
*) sort_atime
},
438 { "ctime", 12, 0, J_RIGHT
, N_("CTime"), 1, string_file_ctime
, (sortfn
*) sort_ctime
},
439 { "perm", 10, 0, J_LEFT
, N_("Permission"),1,string_file_permission
, NULL
},
440 { "mode", 6, 0, J_RIGHT
, N_("Perm"), 1, string_file_perm_octal
, NULL
},
441 { "nlink", 2, 0, J_RIGHT
, N_("Nl"), 1, string_file_nlinks
, NULL
},
442 { "inode", 5, 0, J_RIGHT
, N_("Inode"), 1, string_inode
, (sortfn
*) sort_inode
},
443 { "nuid", 5, 0, J_RIGHT
, N_("UID"), 1, string_file_nuid
, NULL
},
444 { "ngid", 5, 0, J_RIGHT
, N_("GID"), 1, string_file_ngid
, NULL
},
445 { "owner", 8, 0, J_LEFT_FIT
, N_("Owner"), 1, string_file_owner
, NULL
},
446 { "group", 8, 0, J_LEFT_FIT
, N_("Group"), 1, string_file_group
, NULL
},
447 { "mark", 1, 0, J_RIGHT
, " ", 1, string_marked
, NULL
},
448 { "|", 1, 0, J_RIGHT
, " ", 0, NULL
, NULL
},
449 { "space", 1, 0, J_RIGHT
, " ", 0, string_space
, NULL
},
450 { "dot", 1, 0, J_RIGHT
, " ", 0, string_dot
, NULL
},
454 file_compute_color (int attr
, file_entry
*fe
)
458 return (SELECTED_COLOR
);
460 return (MARKED_COLOR
);
461 case MARKED_SELECTED
:
462 return (MARKED_SELECTED_COLOR
);
464 return (NORMAL_COLOR
);
468 return (NORMAL_COLOR
);
471 /* if filetype_mode == true */
472 if (S_ISDIR (fe
->st
.st_mode
))
473 return (DIRECTORY_COLOR
);
474 else if (S_ISLNK (fe
->st
.st_mode
)) {
475 if (fe
->f
.link_to_dir
)
476 return (DIRECTORY_COLOR
);
477 else if (fe
->f
.stale_link
)
478 return (STALE_LINK_COLOR
);
481 } else if (S_ISSOCK (fe
->st
.st_mode
))
482 return (SPECIAL_COLOR
);
483 else if (S_ISCHR (fe
->st
.st_mode
))
484 return (DEVICE_COLOR
);
485 else if (S_ISBLK (fe
->st
.st_mode
))
486 return (DEVICE_COLOR
);
487 else if (S_ISNAM (fe
->st
.st_mode
))
488 return (DEVICE_COLOR
);
489 else if (S_ISFIFO (fe
->st
.st_mode
))
490 return (SPECIAL_COLOR
);
491 else if (S_ISDOOR (fe
->st
.st_mode
))
492 return (SPECIAL_COLOR
);
493 else if (!S_ISREG (fe
->st
.st_mode
))
494 return (STALE_LINK_COLOR
); /* non-regular file of unknown kind */
495 else if (is_exe (fe
->st
.st_mode
))
496 return (EXECUTABLE_COLOR
);
497 else if (fe
->fname
&& (!strcmp (fe
->fname
, "core")
498 || !strcmp (extension (fe
->fname
), "core")))
501 return (NORMAL_COLOR
);
504 /* Formats the file number file_index of panel in the buffer dest */
506 format_file (char *dest
, int limit
, WPanel
*panel
, int file_index
, int width
, int attr
, int isstatus
)
508 int color
, length
, empty_line
;
510 format_e
*format
, *home
;
516 empty_line
= (file_index
>= panel
->count
);
517 home
= (isstatus
) ? panel
->status_format
: panel
->format
;
518 fe
= &panel
->dir
.list
[file_index
];
521 color
= file_compute_color (attr
, fe
);
523 color
= NORMAL_COLOR
;
525 for (format
= home
; format
; format
= format
->next
){
530 if (format
->string_fn
){
537 txt
= (*format
->string_fn
)(fe
, format
->field_len
);
539 len
= format
->field_len
;
540 if (len
+ length
> width
)
541 len
= width
- length
;
546 if (permission_mode
) {
547 if (!strcmp(format
->id
, "perm"))
549 else if (!strcmp(format
->id
, "mode"))
555 preperad_text
= (char*) str_fit_to_term(txt
, len
, format
->just_mode
);
557 add_permission_string (preperad_text
, format
->field_len
, fe
,
558 attr
, color
, perm
- 1);
560 addstr (preperad_text
);
564 if (attr
== SELECTED
|| attr
== MARKED_SELECTED
)
565 attrset (SELECTED_COLOR
);
567 attrset (NORMAL_COLOR
);
574 int still
= width
- length
;
581 repaint_file (WPanel
*panel
, int file_index
, int mv
, int attr
, int isstatus
)
583 int second_column
= 0;
585 char buffer
[BUF_MEDIUM
];
588 if (!isstatus
&& panel
->split
){
590 second_column
= (file_index
- panel
->top_file
) / llines (panel
);
591 width
= (panel
->widget
.cols
- 2)/2 - 1;
595 width
= (panel
->widget
.cols
-2) - (panel
->widget
.cols
-2)/2 - 1;
598 width
= (panel
->widget
.cols
- 2);
600 /* Nothing to paint */
605 if (!isstatus
&& panel
->split
){
606 widget_move (&panel
->widget
,
607 (file_index
- panel
->top_file
) %
611 widget_move (&panel
->widget
, file_index
- panel
->top_file
+ 2, 1);
614 format_file (buffer
, sizeof(buffer
), panel
, file_index
, width
, attr
, isstatus
);
616 if (!isstatus
&& panel
->split
){
620 attrset (NORMAL_COLOR
);
627 display_mini_info (WPanel
*panel
)
629 widget_move (&panel
->widget
, llines (panel
)+3, 1);
631 if (panel
->searching
){
632 attrset (INPUT_COLOR
);
634 addstr (str_fit_to_term (panel
->search_buffer
,
635 panel
->widget
.cols
- 3, J_LEFT
));
636 attrset (NORMAL_COLOR
);
640 /* Status resolves links and show them */
643 if (S_ISLNK (panel
->dir
.list
[panel
->selected
].st
.st_mode
)){
644 char *link
, link_target
[MC_MAXPATHLEN
];
647 link
= concat_dir_and_file (panel
->cwd
, panel
->dir
.list
[panel
->selected
].fname
);
648 len
= mc_readlink (link
, link_target
, MC_MAXPATHLEN
- 1);
651 link_target
[len
] = 0;
653 addstr (str_fit_to_term (link_target
, panel
->widget
.cols
- 5,
656 addstr (str_fit_to_term (_("<readlink failed>"),
657 panel
->widget
.cols
- 2, J_LEFT
));
658 } else if (strcmp (panel
->dir
.list
[panel
->selected
].fname
, "..") == 0) {
660 * while loading directory (do_load_dir() and do_reload_dir(),
661 * the actual stat info about ".." directory isn't got;
662 * so just don't display incorrect info about ".." directory */
663 addstr (str_fit_to_term (_("UP--DIR"), panel
->widget
.cols
- 2, J_LEFT
));
665 /* Default behavior */
666 repaint_file (panel
, panel
->selected
, 0, STATUS
, 1);
670 paint_dir (WPanel
*panel
)
673 int color
; /* Color value of the line */
674 int items
; /* Number of items */
676 items
= llines (panel
) * (panel
->split
? 2 : 1);
678 for (i
= 0; i
< items
; i
++){
679 if (i
+panel
->top_file
>= panel
->count
)
682 color
= 2 * (panel
->dir
.list
[i
+panel
->top_file
].f
.marked
);
683 color
+= (panel
->selected
==i
+panel
->top_file
&& panel
->active
);
685 repaint_file (panel
, i
+panel
->top_file
, 1, color
, 0);
691 display_total_marked_size (WPanel
*panel
, int y
, int x
, gboolean size_only
)
693 char buffer
[BUF_SMALL
], b_bytes
[BUF_SMALL
], *buf
;
697 if (panel
->marked
<= 0)
700 buf
= size_only
? b_bytes
: buffer
;
701 cols
= panel
->widget
.cols
- 2;
704 * This is a trick to use two ngettext() calls in one sentence.
705 * First make "N bytes", then insert it into "X in M files".
707 g_snprintf (b_bytes
, sizeof (b_bytes
),
708 ngettext("%s byte", "%s bytes", (unsigned long) panel
->total
),
709 size_trunc_sep (panel
->total
));
711 g_snprintf (buffer
, sizeof (buffer
),
712 ngettext("%s in %d file", "%s in %d files", panel
->marked
),
713 b_bytes
, panel
->marked
);
717 /* don't forget spaces around buffer content */
718 if ((int) blen
> cols
- 2) {
719 buf
[cols
- 2] = '\0';
720 blen
= (size_t) (cols
- 2);
724 /* center in panel */
725 x
= (panel
->widget
.cols
- (int) blen
) / 2 - 1;
728 * y == llines (panel) + 2 for mini_info_separator
729 * y == panel->widget.lines - 1 for panel bottom frame
731 widget_move (&panel
->widget
, y
, x
);
732 attrset (MARKED_COLOR
);
733 tty_printf (" %s ", buf
);
737 mini_info_separator (WPanel
*panel
)
739 const int y
= llines (panel
) + 2;
742 widget_move (&panel
->widget
, y
, 1);
743 attrset (NORMAL_COLOR
);
745 hline (ACS_HLINE
, panel
->widget
.cols
- 2);
747 hline ((slow_terminal
? '-' : ACS_HLINE
) | NORMAL_COLOR
,
748 panel
->widget
.cols
- 2);
749 #endif /* !HAVE_SLANG */
751 /* Status displays total marked size.
752 * Centered in panel, full format. */
753 display_total_marked_size (panel
, y
, -1, FALSE
);
757 show_free_space (WPanel
*panel
)
759 /* Used to figure out how many free space we have */
760 static struct my_statfs myfs_stats
;
761 /* Old current working directory for displaying free space */
762 static char *old_cwd
= NULL
;
764 /* Don't try to stat non-local fs */
765 if (!vfs_file_is_local (panel
->cwd
) || !free_space
)
768 if (old_cwd
== NULL
|| strcmp (old_cwd
, panel
->cwd
) != 0) {
769 char rpath
[PATH_MAX
];
773 old_cwd
= g_strdup (panel
->cwd
);
775 if (mc_realpath (panel
->cwd
, rpath
) == NULL
)
778 my_statfs (&myfs_stats
, rpath
);
781 if (myfs_stats
.avail
> 0 || myfs_stats
.total
> 0) {
782 char buffer1
[6], buffer2
[6], tmp
[BUF_SMALL
];
783 size_trunc_len (buffer1
, sizeof(buffer1
) - 1, myfs_stats
.avail
, 1);
784 size_trunc_len (buffer2
, sizeof(buffer2
) - 1, myfs_stats
.total
, 1);
785 g_snprintf (tmp
, sizeof(tmp
), " %s/%s (%d%%) ", buffer1
, buffer2
,
786 myfs_stats
.total
> 0 ?
787 (int)(100 * (double)myfs_stats
.avail
/ myfs_stats
.total
) : 0);
788 widget_move (&panel
->widget
, panel
->widget
.lines
- 1,
789 panel
->widget
.cols
- 2 - (int) strlen (tmp
));
790 attrset (NORMAL_COLOR
);
796 show_dir (WPanel
*panel
)
801 draw_double_box (panel
->widget
.parent
,
802 panel
->widget
.y
, panel
->widget
.x
,
803 panel
->widget
.lines
, panel
->widget
.cols
);
805 if (show_mini_info
) {
806 widget_move (&panel
->widget
, llines (panel
) + 2, 0);
807 tty_print_alt_char (ACS_LTEE
);
808 widget_move (&panel
->widget
, llines (panel
) + 2,
809 panel
->widget
.cols
- 1);
810 tty_print_alt_char (ACS_RTEE
);
814 attrset (REVERSE_COLOR
);
816 widget_move (&panel
->widget
, 0, 3);
819 len
= min (max (panel
->widget
.cols
- 9, 0), panel
->widget
.cols
);
820 addstr (str_term_trim (strip_home_and_password (panel
->cwd
), len
));
823 widget_move (&panel
->widget
, 0, 1);
825 widget_move (&panel
->widget
, 0, panel
->widget
.cols
- 2);
827 widget_move (&panel
->widget
, 0, panel
->widget
.cols
- 3);
830 if (!show_mini_info
) {
831 if (panel
->marked
== 0) {
832 /* Show size of curret file in the bottom of panel */
833 if (S_ISREG (panel
->dir
.list
[panel
->selected
].st
.st_mode
)) {
834 char buffer
[BUF_SMALL
];
836 g_snprintf (buffer
, sizeof (buffer
), " %s ",
837 size_trunc_sep (panel
->dir
.list
[panel
->selected
].st
.st_size
));
838 attrset (NORMAL_COLOR
);
839 widget_move (&panel
->widget
, panel
->widget
.lines
- 1, 2);
843 /* Show total size of marked files
844 * In the bottom of panel, display size only. */
845 display_total_marked_size (panel
, panel
->widget
.lines
- 1, 2, TRUE
);
849 show_free_space (panel
);
855 /* To be used only by long_frame and full_frame to adjust top_file */
857 adjust_top_file (WPanel
*panel
)
859 int old_top
= panel
->top_file
;
861 if (panel
->selected
- old_top
> llines (panel
))
862 panel
->top_file
= panel
->selected
;
863 if (old_top
- panel
->count
> llines (panel
))
864 panel
->top_file
= panel
->count
- llines (panel
);
867 /* Repaint everything, including frame and separator */
869 paint_panel (WPanel
*panel
)
871 paint_frame (panel
); /* including show_dir */
874 if (show_mini_info
) {
875 mini_info_separator (panel
);
876 display_mini_info (panel
);
882 /* add "#enc:encodning" to end of path */
883 /* if path end width a previous #enc:, only encoding is changed no additional
888 *add_encoding_to_path (const char *path
, const char *encoding
)
894 semi
= g_strrstr (path
, "#enc:");
897 slash
= strchr (semi
, PATH_SEP
);
899 result
= g_strconcat (path
, "/#enc:", encoding
, NULL
);
902 result
= g_strconcat (path
, "/#enc:", encoding
, NULL
);
906 result
= g_strconcat (path
, "/#enc:", encoding
, NULL
);
913 remove_encoding_from_path (const char *path
)
916 GString
*tmp_path
, *tmp_conv
;
921 ret
= g_string_new("");
922 tmp_conv
= g_string_new("");
924 tmp_path
= g_string_new(path
);
926 while ((tmp
= g_strrstr (tmp_path
->str
, "/#enc:")) != NULL
){
927 enc
= vfs_get_encoding ((const char *) tmp
);
928 converter
= enc
? str_crt_conv_to (enc
): str_cnv_to_term
;
929 if (converter
== INVALID_CONV
) converter
= str_cnv_to_term
;
932 while (*tmp2
&& *tmp2
!= '/')
936 str_vfs_convert_from (converter
, tmp2
, tmp_conv
);
937 g_string_prepend(ret
, tmp_conv
->str
);
938 g_string_set_size(tmp_conv
,0);
940 g_string_set_size(tmp_path
,tmp
- tmp_path
->str
);
941 str_close_conv (converter
);
943 g_string_prepend(ret
, tmp_path
->str
);
944 g_string_free(tmp_path
,TRUE
);
945 g_string_free(tmp_conv
,TRUE
);
948 g_string_free(ret
, FALSE
);
953 * Repaint the contents of the panels without frames. To schedule panel
954 * for repainting, set panel->dirty to 1. There are many reasons why
955 * the panels need to be repainted, and this is a costly operation, so
956 * it's done once per event.
959 update_dirty_panels (void)
961 if (current_panel
->dirty
)
962 paint_panel (current_panel
);
964 if ((get_other_type () == view_listing
) && other_panel
->dirty
)
965 paint_panel (other_panel
);
969 do_select (WPanel
*panel
, int i
)
971 if (i
!= panel
->selected
) {
974 panel
->top_file
= panel
->selected
- (panel
->widget
.lines
- 2) / 2;
975 if (panel
->top_file
< 0)
981 do_try_to_select (WPanel
*panel
, const char *name
)
991 /* We only want the last component of the directory,
992 * and from this only the name without suffix. */
993 subdir
= vfs_strip_suffix_from_filename (x_basename(name
));
995 /* Search that subdirectory, if found select it */
996 for (i
= 0; i
< panel
->count
; i
++){
997 if (strcmp (subdir
, panel
->dir
.list
[i
].fname
) == 0) {
998 do_select (panel
, i
);
1004 /* Try to select a file near the file that is missing */
1005 if (panel
->selected
>= panel
->count
)
1006 do_select (panel
, panel
->count
-1);
1011 try_to_select (WPanel
*panel
, const char *name
)
1013 do_try_to_select (panel
, name
);
1014 select_item (panel
);
1018 panel_update_cols (Widget
*widget
, int frame_size
)
1022 if (horizontal_split
){
1023 widget
->cols
= COLS
;
1027 if (frame_size
== frame_full
){
1031 if (widget
== get_panel_widget (0)){
1032 cols
= first_panel_size
;
1035 cols
= COLS
-first_panel_size
;
1036 origin
= first_panel_size
;
1040 widget
->cols
= cols
;
1045 panel_save_name (WPanel
*panel
)
1047 extern int saving_setup
;
1049 /* If the program is shuting down */
1050 if ((midnight_shutdown
&& auto_save_setup
) || saving_setup
)
1051 return g_strdup (panel
->panel_name
);
1053 return g_strconcat ("Temporal:", panel
->panel_name
, (char *) NULL
);
1057 panel_destroy (WPanel
*p
)
1061 char *name
= panel_save_name (p
);
1063 panel_save_setup (p
, name
);
1064 panel_clean_dir (p
);
1066 /* save and clean history */
1067 if (p
->dir_history
) {
1068 history_put (p
->hist_name
, p
->dir_history
);
1070 p
->dir_history
= g_list_first (p
->dir_history
);
1071 g_list_foreach (p
->dir_history
, (GFunc
) g_free
, NULL
);
1072 g_list_free (p
->dir_history
);
1075 g_free (p
->hist_name
);
1077 delete_format (p
->format
);
1078 delete_format (p
->status_format
);
1080 g_free (p
->user_format
);
1081 for (i
= 0; i
< LIST_TYPES
; i
++)
1082 g_free (p
->user_status_format
[i
]);
1083 g_free (p
->dir
.list
);
1084 g_free (p
->panel_name
);
1089 panel_format_modified (WPanel
*panel
)
1091 panel
->format_modified
= 1;
1094 /* Panel creation */
1095 /* The parameter specifies the name of the panel for setup retieving */
1097 panel_new (const char *panel_name
)
1103 panel
= g_new0 (WPanel
, 1);
1105 /* No know sizes of the panel at startup */
1106 init_widget (&panel
->widget
, 0, 0, 0, 0, panel_callback
, panel_event
);
1108 /* We do not want the cursor */
1109 widget_want_cursor (panel
->widget
, 0);
1111 mc_get_current_wd (panel
->cwd
, sizeof (panel
->cwd
) - 2);
1112 strcpy (panel
->lwd
, ".");
1114 panel
->hist_name
= g_strconcat ("Dir Hist ", panel_name
, (char *) NULL
);
1115 panel
->dir_history
= history_get (panel
->hist_name
);
1116 directory_history_add (panel
, panel
->cwd
);
1118 panel
->dir
.list
= g_new (file_entry
, MIN_FILES
);
1119 panel
->dir
.size
= MIN_FILES
;
1123 panel
->top_file
= 0;
1124 panel
->selected
= 0;
1129 panel
->searching
= 0;
1130 panel
->dirs_marked
= 0;
1131 panel
->is_panelized
= 0;
1133 panel
->status_format
= 0;
1134 panel
->format_modified
= 1;
1136 panel
->panel_name
= g_strdup (panel_name
);
1137 panel
->user_format
= g_strdup (DEFAULT_USER_FORMAT
);
1139 for (i
= 0; i
< LIST_TYPES
; i
++)
1140 panel
->user_status_format
[i
] = g_strdup (DEFAULT_USER_FORMAT
);
1142 panel
->search_buffer
[0] = 0;
1143 panel
->frame_size
= frame_half
;
1144 section
= g_strconcat ("Temporal:", panel
->panel_name
, (char *) NULL
);
1145 if (!mc_config_has_group (mc_main_config
, section
)) {
1147 section
= g_strdup (panel
->panel_name
);
1149 panel_load_setup (panel
, section
);
1152 /* Load format strings */
1153 err
= set_panel_formats (panel
);
1155 set_panel_formats (panel
);
1158 /* Load the default format */
1160 do_load_dir (panel
->cwd
, &panel
->dir
, panel
->sort_type
,
1161 panel
->reverse
, panel
->case_sensitive
,
1162 panel
->exec_first
, panel
->filter
);
1167 panel_reload (WPanel
*panel
)
1169 struct stat current_stat
;
1171 if (fast_reload
&& !stat (panel
->cwd
, ¤t_stat
)
1172 && current_stat
.st_ctime
== panel
->dir_stat
.st_ctime
1173 && current_stat
.st_mtime
== panel
->dir_stat
.st_mtime
)
1176 while (mc_chdir (panel
->cwd
) == -1) {
1179 if (panel
->cwd
[0] == PATH_SEP
&& panel
->cwd
[1] == 0) {
1180 panel_clean_dir (panel
);
1181 panel
->count
= set_zero_dir (&panel
->dir
);
1184 last_slash
= strrchr (panel
->cwd
, PATH_SEP
);
1185 if (!last_slash
|| last_slash
== panel
->cwd
)
1186 strcpy (panel
->cwd
, PATH_SEP_STR
);
1189 memset (&(panel
->dir_stat
), 0, sizeof (panel
->dir_stat
));
1194 do_reload_dir (panel
->cwd
, &panel
->dir
, panel
->sort_type
,
1195 panel
->count
, panel
->reverse
, panel
->case_sensitive
,
1196 panel
->exec_first
, panel
->filter
);
1199 if (panel
->selected
>= panel
->count
)
1200 do_select (panel
, panel
->count
- 1);
1202 recalculate_panel_summary (panel
);
1206 paint_frame (WPanel
*panel
)
1213 adjust_top_file (panel
);
1215 widget_erase (&panel
->widget
);
1218 widget_move (&panel
->widget
, 1, 1);
1220 for (side
= 0; side
<= panel
->split
; side
++){
1224 attrset (NORMAL_COLOR
);
1226 width
= panel
->widget
.cols
- panel
->widget
.cols
/2 - 1;
1227 } else if (panel
->split
)
1228 width
= panel
->widget
.cols
/2 - 3;
1230 width
= panel
->widget
.cols
- 2;
1232 for (format
= panel
->format
; format
; format
= format
->next
){
1233 if (format
->string_fn
){
1234 txt
= format
->title
;
1236 header_len
= strlen (txt
);
1237 if (header_len
> format
->field_len
)
1238 header_len
= format
->field_len
;
1240 attrset (MARKED_COLOR
);
1241 addstr (str_fit_to_term (txt
, format
->field_len
, J_CENTER_LEFT
));
1242 width
-= format
->field_len
;
1244 attrset (NORMAL_COLOR
);
1252 tty_printf ("%*s", width
, "");
1257 parse_panel_size (WPanel
*panel
, const char *format
, int isstatus
)
1259 int frame
= frame_half
;
1260 format
= skip_separators (format
);
1262 if (!strncmp (format
, "full", 4)){
1265 } else if (!strncmp (format
, "half", 4)){
1271 panel
->frame_size
= frame
;
1275 /* Now, the optional column specifier */
1276 format
= skip_separators (format
);
1278 if (*format
== '1' || *format
== '2'){
1280 panel
->split
= *format
== '2';
1285 panel_update_cols (&(panel
->widget
), panel
->frame_size
);
1287 return skip_separators (format
);
1292 all := panel_format? format
1293 panel_format := [full|half] [1|2]
1294 format := one_format_e
1295 | format , one_format_e
1297 one_format_e := just format.id [opt_size]
1299 opt_size := : size [opt_expand]
1306 parse_display_format (WPanel
*panel
, const char *format
, char **error
, int isstatus
, int *res_total_cols
)
1308 format_e
*darr
, *old
= 0, *home
= 0; /* The formats we return */
1309 int total_cols
= 0; /* Used columns by the format */
1310 int set_justify
; /* flag: set justification mode? */
1311 align_crt_t justify
= J_LEFT
; /* Which mode. */
1312 int items
= 0; /* Number of items in the format */
1315 static size_t i18n_timelength
= 0; /* flag: check ?Time length at startup */
1319 if (i18n_timelength
== 0) {
1320 i18n_timelength
= i18n_checktimelength (); /* Musn't be 0 */
1322 for (i
= 0; i
< ELEMENTS(formats
); i
++)
1323 if (strcmp ("time", formats
[i
].id
+1) == 0)
1324 formats
[i
].min_size
= i18n_timelength
;
1328 * This makes sure that the panel and mini status full/half mode
1331 format
= parse_panel_size (panel
, format
, isstatus
);
1333 while (*format
){ /* format can be an empty string */
1336 darr
= g_new (format_e
, 1);
1338 /* I'm so ugly, don't look at me :-) */
1346 format
= skip_separators (format
);
1348 if (strchr ("<=>", *format
)){
1363 format
= skip_separators (format
+1);
1367 for (i
= 0; i
< ELEMENTS(formats
); i
++){
1368 int klen
= strlen (formats
[i
].id
);
1370 if (strncmp (format
, formats
[i
].id
, klen
) != 0)
1375 if (formats
[i
].use_in_gui
)
1378 darr
->requested_field_len
= formats
[i
].min_size
;
1379 darr
->string_fn
= formats
[i
].string_fn
;
1380 if (formats
[i
].title
[0])
1381 darr
->title
= _(formats
[i
].title
);
1384 darr
->id
= formats
[i
].id
;
1385 darr
->expand
= formats
[i
].expands
;
1386 darr
->just_mode
= formats
[i
].default_just
;
1389 if (IS_FIT(darr
->just_mode
))
1390 darr
->just_mode
= MAKE_FIT(justify
);
1392 darr
->just_mode
= justify
;
1396 format
= skip_separators (format
);
1398 /* If we have a size specifier */
1399 if (*format
== ':'){
1402 /* If the size was specified, we don't want
1403 * auto-expansion by default
1407 req_length
= atoi (format
);
1408 darr
->requested_field_len
= req_length
;
1410 format
= skip_numbers (format
);
1412 /* Now, if they insist on expansion */
1413 if (*format
== '+'){
1423 char *tmp_format
= g_strdup (format
);
1425 int pos
= min (8, strlen (format
));
1426 delete_format (home
);
1427 tmp_format
[pos
] = 0;
1428 *error
= g_strconcat (_("Unknown tag on display format: "), tmp_format
, (char *) NULL
);
1429 g_free (tmp_format
);
1432 total_cols
+= darr
->requested_field_len
;
1435 *res_total_cols
= total_cols
;
1440 use_display_format (WPanel
*panel
, const char *format
, char **error
, int isstatus
)
1442 #define MAX_EXPAND 4
1443 int expand_top
= 0; /* Max used element in expand */
1444 int usable_columns
; /* Usable columns in the panel */
1447 format_e
*darr
, *home
;
1450 format
= DEFAULT_USER_FORMAT
;
1452 home
= parse_display_format (panel
, format
, error
, isstatus
, &total_cols
);
1459 /* Status needn't to be split */
1460 usable_columns
= ((panel
->widget
.cols
-2)/((isstatus
)
1462 : (panel
->split
+1))) - (!isstatus
&& panel
->split
);
1464 /* Look for the expandable fields and set field_len based on the requested field len */
1465 for (darr
= home
; darr
&& expand_top
< MAX_EXPAND
; darr
= darr
->next
){
1466 darr
->field_len
= darr
->requested_field_len
;
1471 /* If we used more columns than the available columns, adjust that */
1472 if (total_cols
> usable_columns
){
1473 int pdif
, dif
= total_cols
- usable_columns
;
1477 for (darr
= home
; darr
; darr
= darr
->next
){
1478 if (dif
&& darr
->field_len
- 1){
1484 /* avoid endless loop if num fields > 40 */
1488 total_cols
= usable_columns
; /* give up, the rest should be truncated */
1491 /* Expand the available space */
1492 if ((usable_columns
> total_cols
) && expand_top
){
1493 int spaces
= (usable_columns
- total_cols
) / expand_top
;
1494 int extra
= (usable_columns
- total_cols
) % expand_top
;
1496 for (i
= 0, darr
= home
; darr
&& (i
< expand_top
); darr
= darr
->next
)
1498 darr
->field_len
+= (spaces
+ ((i
== 0) ? extra
: 0));
1505 /* Switches the panel to the mode specified in the format */
1506 /* Seting up both format and status string. Return: 0 - on success; */
1507 /* 1 - format error; 2 - status error; 3 - errors in both formats. */
1509 set_panel_formats (WPanel
*p
)
1515 form
= use_display_format (p
, panel_format (p
), &err
, 0);
1523 delete_format (p
->format
);
1528 if (show_mini_info
){
1530 form
= use_display_format (p
, mini_status_format (p
), &err
, 1);
1537 if (p
->status_format
)
1538 delete_format (p
->status_format
);
1540 p
->status_format
= form
;
1544 panel_format_modified (p
);
1545 panel_update_cols (&(p
->widget
), p
->frame_size
);
1548 message (D_ERROR
, _("Warning" ), _( "User supplied format looks invalid, reverting to default." ) );
1549 if (retcode
& 0x01){
1550 g_free (p
->user_format
);
1551 p
->user_format
= g_strdup (DEFAULT_USER_FORMAT
);
1553 if (retcode
& 0x02){
1554 g_free (p
->user_status_format
[p
->list_type
]);
1555 p
->user_status_format
[p
->list_type
] = g_strdup (DEFAULT_USER_FORMAT
);
1561 /* Given the panel->view_type returns the format string to be parsed */
1563 panel_format (WPanel
*panel
)
1565 switch (panel
->list_type
){
1568 return "full perm space nlink space owner space group space size space mtime space name";
1571 return "half 2 type name";
1574 return panel
->user_format
;
1578 return "half type name | size | mtime";
1583 mini_status_format (WPanel
*panel
)
1585 if (panel
->user_mini_status
)
1586 return panel
->user_status_format
[panel
->list_type
];
1588 switch (panel
->list_type
){
1591 return "full perm space nlink space owner space group space size space mtime space name";
1594 return "half type name space bsize space perm space";
1597 return "half type name";
1601 return panel
->user_format
;
1606 /* Panel operation commands */
1609 /* Returns the number of items in the given panel */
1614 return llines (p
) * 2;
1619 /* Select current item and readjust the panel */
1621 select_item (WPanel
*panel
)
1623 int items
= ITEMS (panel
);
1625 /* Although currently all over the code we set the selection and
1626 top file to decent values before calling select_item, I could
1627 forget it someday, so it's better to do the actual fitting here */
1629 if (panel
->top_file
< 0)
1630 panel
->top_file
= 0;
1632 if (panel
->selected
< 0)
1633 panel
->selected
= 0;
1635 if (panel
->selected
> panel
->count
- 1)
1636 panel
->selected
= panel
->count
- 1;
1638 if (panel
->top_file
> panel
->count
- 1)
1639 panel
->top_file
= panel
->count
- 1;
1641 if ((panel
->count
- panel
->top_file
) < items
) {
1642 panel
->top_file
= panel
->count
- items
;
1643 if (panel
->top_file
< 0)
1644 panel
->top_file
= 0;
1647 if (panel
->selected
< panel
->top_file
)
1648 panel
->top_file
= panel
->selected
;
1650 if ((panel
->selected
- panel
->top_file
) >= items
)
1651 panel
->top_file
= panel
->selected
- items
+ 1;
1655 execute_hooks (select_file_hook
);
1658 /* Clears all files in the panel, used only when one file was marked */
1660 unmark_files (WPanel
*panel
)
1666 for (i
= 0; i
< panel
->count
; i
++)
1667 file_mark (panel
, i
, 0);
1669 panel
->dirs_marked
= 0;
1675 unselect_item (WPanel
*panel
)
1677 repaint_file (panel
, panel
->selected
, 1, 2*selection (panel
)->f
.marked
, 0);
1681 move_down (WPanel
*panel
)
1683 if (panel
->selected
+1 == panel
->count
)
1686 unselect_item (panel
);
1689 if (panel
->selected
- panel
->top_file
== ITEMS (panel
) &&
1690 panel_scroll_pages
){
1691 /* Scroll window half screen */
1692 panel
->top_file
+= ITEMS (panel
)/2;
1693 if (panel
->top_file
> panel
->count
- ITEMS (panel
))
1694 panel
->top_file
= panel
->count
- ITEMS (panel
);
1696 select_item (panel
);
1698 select_item (panel
);
1702 move_up (WPanel
*panel
)
1704 if (panel
->selected
== 0)
1707 unselect_item (panel
);
1709 if (panel
->selected
< panel
->top_file
&& panel_scroll_pages
){
1710 /* Scroll window half screen */
1711 panel
->top_file
-= ITEMS (panel
)/2;
1712 if (panel
->top_file
< 0) panel
->top_file
= 0;
1715 select_item (panel
);
1718 /* Changes the selection by lines (may be negative) */
1720 move_selection (WPanel
*panel
, int lines
)
1725 new_pos
= panel
->selected
+ lines
;
1726 if (new_pos
>= panel
->count
)
1727 new_pos
= panel
->count
-1;
1732 unselect_item (panel
);
1733 panel
->selected
= new_pos
;
1735 if (panel
->selected
- panel
->top_file
>= ITEMS (panel
)){
1736 panel
->top_file
+= lines
;
1740 if (panel
->selected
- panel
->top_file
< 0){
1741 panel
->top_file
+= lines
;
1746 if (panel
->top_file
> panel
->selected
)
1747 panel
->top_file
= panel
->selected
;
1748 if (panel
->top_file
< 0)
1749 panel
->top_file
= 0;
1752 select_item (panel
);
1756 move_left (WPanel
*panel
, int c_code
)
1760 move_selection (panel
, -llines (panel
));
1763 return maybe_cd (1); /* cd .. */
1767 move_right (WPanel
*panel
, int c_code
)
1771 move_selection (panel
, llines (panel
));
1774 return maybe_cd (0); /* cd (selection) */
1778 prev_page (WPanel
*panel
)
1782 if (!panel
->selected
&& !panel
->top_file
)
1784 unselect_item (panel
);
1785 items
= ITEMS (panel
);
1786 if (panel
->top_file
< items
)
1787 items
= panel
->top_file
;
1789 panel
->selected
= 0;
1791 panel
->selected
-= items
;
1792 panel
->top_file
-= items
;
1794 /* This keeps the selection in a reasonable place */
1795 if (panel
->selected
< 0)
1796 panel
->selected
= 0;
1797 if (panel
->top_file
< 0)
1798 panel
->top_file
= 0;
1799 select_item (panel
);
1804 ctrl_prev_page (WPanel
*panel
)
1807 do_cd ("..", cd_exact
);
1811 next_page (WPanel
*panel
)
1815 if (panel
->selected
== panel
->count
- 1)
1817 unselect_item (panel
);
1818 items
= ITEMS (panel
);
1819 if (panel
->top_file
> panel
->count
- 2 * items
)
1820 items
= panel
->count
- items
- panel
->top_file
;
1821 if (panel
->top_file
+ items
< 0)
1822 items
= -panel
->top_file
;
1824 panel
->selected
= panel
->count
- 1;
1826 panel
->selected
+= items
;
1827 panel
->top_file
+= items
;
1829 /* This keeps the selection in it's relative position */
1830 if (panel
->selected
>= panel
->count
)
1831 panel
->selected
= panel
->count
- 1;
1832 if (panel
->top_file
>= panel
->count
)
1833 panel
->top_file
= panel
->count
- 1;
1834 select_item (panel
);
1839 ctrl_next_page (WPanel
*panel
)
1841 if ((S_ISDIR (selection (panel
)->st
.st_mode
)
1842 || link_isdir (selection (panel
)))) {
1843 do_cd (selection (panel
)->fname
, cd_exact
);
1848 goto_top_file (WPanel
*panel
)
1850 unselect_item (panel
);
1851 panel
->selected
= panel
->top_file
;
1852 select_item (panel
);
1856 goto_middle_file (WPanel
*panel
)
1858 unselect_item (panel
);
1859 panel
->selected
= panel
->top_file
+ (ITEMS (panel
)/2);
1860 if (panel
->selected
>= panel
->count
)
1861 panel
->selected
= panel
->count
- 1;
1862 select_item (panel
);
1866 goto_bottom_file (WPanel
*panel
)
1868 unselect_item (panel
);
1869 panel
->selected
= panel
->top_file
+ ITEMS (panel
)-1;
1870 if (panel
->selected
>= panel
->count
)
1871 panel
->selected
= panel
->count
- 1;
1872 select_item (panel
);
1876 move_home (WPanel
*panel
)
1878 if (panel
->selected
== 0)
1880 unselect_item (panel
);
1882 if (torben_fj_mode
){
1883 int middle_pos
= panel
->top_file
+ (ITEMS (panel
)/2);
1885 if (panel
->selected
> middle_pos
){
1886 goto_middle_file (panel
);
1889 if (panel
->selected
!= panel
->top_file
){
1890 goto_top_file (panel
);
1895 panel
->top_file
= 0;
1896 panel
->selected
= 0;
1899 select_item (panel
);
1903 move_end (WPanel
*panel
)
1905 if (panel
->selected
== panel
->count
-1)
1907 unselect_item (panel
);
1908 if (torben_fj_mode
){
1909 int middle_pos
= panel
->top_file
+ (ITEMS (panel
)/2);
1911 if (panel
->selected
< middle_pos
){
1912 goto_middle_file (panel
);
1915 if (panel
->selected
!= (panel
->top_file
+ ITEMS(panel
)-1)){
1916 goto_bottom_file (panel
);
1921 panel
->selected
= panel
->count
-1;
1923 select_item (panel
);
1926 /* Recalculate the panels summary information, used e.g. when marked
1927 files might have been removed by an external command */
1929 recalculate_panel_summary (WPanel
*panel
)
1934 panel
->dirs_marked
= 0;
1937 for (i
= 0; i
< panel
->count
; i
++)
1938 if (panel
->dir
.list
[i
].f
.marked
){
1939 /* do_file_mark will return immediately if newmark == oldmark.
1940 So we have to first unmark it to get panel's summary information
1941 updated. (Norbert) */
1942 panel
->dir
.list
[i
].f
.marked
= 0;
1943 do_file_mark (panel
, i
, 1);
1947 /* This routine marks a file or a directory */
1949 do_file_mark (WPanel
*panel
, int idx
, int mark
)
1951 if (panel
->dir
.list
[idx
].f
.marked
== mark
)
1954 /* Only '..' can't be marked, '.' isn't visible */
1955 if (!strcmp (panel
->dir
.list
[idx
].fname
, ".."))
1958 file_mark (panel
, idx
, mark
);
1959 if (panel
->dir
.list
[idx
].f
.marked
) {
1961 if (S_ISDIR (panel
->dir
.list
[idx
].st
.st_mode
)) {
1962 if (panel
->dir
.list
[idx
].f
.dir_size_computed
)
1963 panel
->total
+= panel
->dir
.list
[idx
].st
.st_size
;
1964 panel
->dirs_marked
++;
1966 panel
->total
+= panel
->dir
.list
[idx
].st
.st_size
;
1969 if (S_ISDIR (panel
->dir
.list
[idx
].st
.st_mode
)) {
1970 if (panel
->dir
.list
[idx
].f
.dir_size_computed
)
1971 panel
->total
-= panel
->dir
.list
[idx
].st
.st_size
;
1972 panel
->dirs_marked
--;
1974 panel
->total
-= panel
->dir
.list
[idx
].st
.st_size
;
1980 do_mark_file (WPanel
*panel
, int do_move
)
1982 do_file_mark (panel
, panel
->selected
,
1983 selection (panel
)->f
.marked
? 0 : 1);
1984 if (mark_moves_down
&& do_move
)
1989 mark_file (WPanel
*panel
)
1991 do_mark_file (panel
, 1);
1994 /* Incremental search of a file name in the panel */
1996 do_search (WPanel
*panel
, int c_code
)
1998 size_t l
, max
, buf_max
;
2003 l
= strlen (panel
->search_buffer
);
2004 if (c_code
== KEY_BACKSPACE
) {
2006 act
= panel
->search_buffer
+ l
;
2007 str_prev_noncomb_char (&act
, panel
->search_buffer
);
2010 panel
->search_chpoint
= 0;
2012 if (c_code
&& (gsize
) panel
->search_chpoint
< sizeof (panel
->search_char
)) {
2013 panel
->search_char
[panel
->search_chpoint
] = c_code
;
2014 panel
->search_chpoint
++;
2017 if (panel
->search_chpoint
> 0) {
2018 switch (str_is_valid_char (panel
->search_char
, panel
->search_chpoint
)) {
2022 panel
->search_chpoint
= 0;
2025 if (l
+ panel
->search_chpoint
< sizeof (panel
->search_buffer
)) {
2026 memcpy (panel
->search_buffer
+ l
, panel
->search_char
,
2027 panel
->search_chpoint
);
2028 l
+= panel
->search_chpoint
;
2029 (panel
->search_buffer
+ l
)[0] = '\0';
2030 panel
->search_chpoint
= 0;
2036 buf_max
= panel
->case_sensitive
?
2037 str_prefix (panel
->search_buffer
, panel
->search_buffer
) :
2038 str_caseprefix (panel
->search_buffer
, panel
->search_buffer
);
2040 sel
= panel
->selected
;
2041 for (i
= panel
->selected
; !wrapped
|| i
!= panel
->selected
; i
++) {
2042 if (i
>= panel
->count
) {
2048 l
= panel
->case_sensitive
?
2049 str_prefix (panel
->dir
.list
[i
].fname
, panel
->search_buffer
) :
2050 str_caseprefix (panel
->dir
.list
[i
].fname
, panel
->search_buffer
);
2054 if (max
== buf_max
) break;
2058 unselect_item (panel
);
2059 panel
->selected
= sel
;
2060 select_item (panel
);
2062 act
= panel
->search_buffer
+ strlen (panel
->search_buffer
);
2063 while (max
< buf_max
) {
2064 str_prev_char_safe (&act
);
2066 buf_max
= panel
->case_sensitive
?
2067 str_prefix (panel
->search_buffer
, panel
->search_buffer
) :
2068 str_caseprefix (panel
->search_buffer
, panel
->search_buffer
);
2071 paint_panel (panel
);
2075 start_search (WPanel
*panel
)
2077 if (panel
->searching
){
2078 if (panel
->selected
+1 == panel
->count
)
2079 panel
->selected
= 0;
2082 do_search (panel
, 0);
2084 panel
->searching
= 1;
2085 panel
->search_buffer
[0] = '\0';
2086 panel
->search_char
[0] = '\0';
2087 panel
->search_chpoint
= 0;
2088 display_mini_info (panel
);
2093 /* Return 1 if the Enter key has been processed, 0 otherwise */
2095 do_enter_on_file_entry (file_entry
*fe
)
2100 * Directory or link to directory - change directory.
2101 * Try the same for the entries on which mc_lstat() has failed.
2103 if (S_ISDIR (fe
->st
.st_mode
) || link_isdir (fe
)
2104 || (fe
->st
.st_mode
== 0)) {
2105 if (!do_cd (fe
->fname
, cd_exact
))
2106 message (D_ERROR
, MSG_ERROR
, _("Cannot change directory"));
2110 /* Try associated command */
2111 if (regex_command (fe
->fname
, "Open", 0) != 0)
2114 /* Check if the file is executable */
2115 full_name
= concat_dir_and_file (current_panel
->cwd
, fe
->fname
);
2116 if (!is_exe (fe
->st
.st_mode
) || !if_link_is_exe (full_name
, fe
)) {
2122 if (confirm_execute
) {
2124 (_(" The Midnight Commander "),
2125 _(" Do you really want to execute? "), D_NORMAL
, 2, _("&Yes"),
2130 if (!vfs_current_is_local ()) {
2134 tmp
= concat_dir_and_file (vfs_get_current_dir (), fe
->fname
);
2135 ret
= mc_setctl (tmp
, VFS_SETCTL_RUN
, NULL
);
2137 /* We took action only if the dialog was shown or the execution
2139 return confirm_execute
|| (ret
== 0);
2144 char *tmp
= name_quote (fe
->fname
, 0);
2145 char *cmd
= g_strconcat (".", PATH_SEP_STR
, tmp
, (char *) NULL
);
2147 shell_execute (cmd
, 0);
2155 do_enter (WPanel
*panel
)
2157 return do_enter_on_file_entry (selection (panel
));
2161 chdir_other_panel (WPanel
*panel
)
2164 char *sel_entry
= NULL
;
2166 if (get_other_type () != view_listing
) {
2167 set_display_type (get_other_index (), view_listing
);
2170 if (!S_ISDIR (panel
->dir
.list
[panel
->selected
].st
.st_mode
)) {
2171 new_dir
= concat_dir_and_file (panel
->cwd
, "..");
2172 sel_entry
= strrchr(panel
->cwd
, PATH_SEP
);
2174 new_dir
= concat_dir_and_file (panel
->cwd
, panel
->dir
.list
[panel
->selected
].fname
);
2177 do_cd (new_dir
, cd_exact
);
2179 try_to_select (current_panel
, sel_entry
);
2188 * Make the current directory of the current panel also the current
2189 * directory of the other panel. Put the other panel to the listing
2190 * mode if needed. If the current panel is panelized, the other panel
2191 * doesn't become panelized.
2194 sync_other_panel (WPanel
*panel
)
2196 if (get_other_type () != view_listing
) {
2197 set_display_type (get_other_index (), view_listing
);
2200 do_panel_cd (other_panel
, current_panel
->cwd
, cd_exact
);
2202 /* try to select current filename on the other panel */
2203 if (!panel
->is_panelized
) {
2204 try_to_select (other_panel
, selection (panel
)->fname
);
2209 chdir_to_readlink (WPanel
*panel
)
2213 if (get_other_type () != view_listing
)
2216 if (S_ISLNK (panel
->dir
.list
[panel
->selected
].st
.st_mode
)) {
2217 char buffer
[MC_MAXPATHLEN
], *p
;
2221 i
= readlink (selection (panel
)->fname
, buffer
, MC_MAXPATHLEN
- 1);
2224 if (mc_stat (selection (panel
)->fname
, &st
) < 0)
2227 if (!S_ISDIR (st
.st_mode
)) {
2228 p
= strrchr (buffer
, PATH_SEP
);
2231 p
= strrchr (buffer
, PATH_SEP
);
2237 if (*buffer
== PATH_SEP
)
2238 new_dir
= g_strdup (buffer
);
2240 new_dir
= concat_dir_and_file (panel
->cwd
, buffer
);
2243 do_cd (new_dir
, cd_exact
);
2252 typedef void (*panel_key_callback
) (WPanel
*);
2255 panel_key_callback fn
;
2258 static void cmd_do_enter(WPanel
*wp
) { (void) do_enter(wp
); }
2259 static void cmd_view_simple(WPanel
*wp
) { (void) wp
; view_simple_cmd(); }
2260 static void cmd_edit_new(WPanel
*wp
) { (void) wp
; edit_cmd_new(); }
2261 static void cmd_copy_local(WPanel
*wp
) { (void) wp
;copy_cmd_local(); }
2262 static void cmd_rename_local(WPanel
*wp
) { (void) wp
;ren_cmd_local(); }
2263 static void cmd_delete_local(WPanel
*wp
) { (void) wp
;delete_cmd_local(); }
2264 static void cmd_select(WPanel
*wp
) { (void) wp
;select_cmd(); }
2265 static void cmd_unselect(WPanel
*wp
) { (void) wp
;unselect_cmd(); }
2266 static void cmd_reverse_selection(WPanel
*wp
) { (void) wp
;reverse_selection_cmd(); }
2268 static const panel_key_map panel_keymap
[] = {
2269 { KEY_DOWN
, move_down
},
2270 { KEY_UP
, move_up
},
2272 /* The action button :-) */
2273 { '\n', cmd_do_enter
},
2274 { KEY_ENTER
, cmd_do_enter
},
2276 { KEY_IC
, mark_file
},
2277 { KEY_HOME
, move_home
},
2278 { KEY_A1
, move_home
},
2279 { ALT ('<'), move_home
},
2280 { KEY_C1
, move_end
},
2281 { KEY_END
, move_end
},
2282 { ALT ('>'), move_end
},
2283 { KEY_NPAGE
, next_page
},
2284 { KEY_PPAGE
, prev_page
},
2285 { KEY_NPAGE
| KEY_M_CTRL
, ctrl_next_page
},
2286 { KEY_PPAGE
| KEY_M_CTRL
, ctrl_prev_page
},
2288 /* To quickly move in the panel */
2289 { ALT('g'), goto_top_file
},
2290 { ALT('r'), goto_middle_file
}, /* M-r like emacs */
2291 { ALT('j'), goto_bottom_file
},
2293 /* Emacs-like bindings */
2294 { XCTRL('v'), next_page
}, /* C-v like emacs */
2295 { ALT('v'), prev_page
}, /* M-v like emacs */
2296 { XCTRL('p'), move_up
}, /* C-p like emacs */
2297 { XCTRL('n'), move_down
}, /* C-n like emacs */
2298 { XCTRL('s'), start_search
}, /* C-s like emacs */
2299 { ALT('s'), start_search
}, /* M-s not like emacs */
2300 /* { XCTRL('t'), mark_file },*/
2301 { XCTRL('t'), set_panel_encoding
},
2302 { ALT('o'), chdir_other_panel
},
2303 { ALT('i'), sync_other_panel
},
2304 { ALT('l'), chdir_to_readlink
},
2305 { ALT('H'), directory_history_list
},
2306 { KEY_F(13), cmd_view_simple
},
2307 { KEY_F(14), cmd_edit_new
},
2308 { KEY_F(15), cmd_copy_local
},
2309 { KEY_F(16), cmd_rename_local
},
2310 { KEY_F(18), cmd_delete_local
},
2311 { ALT('y'), directory_history_prev
},
2312 { ALT('u'), directory_history_next
},
2313 { ALT('+'), cmd_select
},
2314 { KEY_KP_ADD
, cmd_select
},
2315 { ALT('\\'), cmd_unselect
},
2316 { ALT('-'), cmd_unselect
},
2317 { KEY_KP_SUBTRACT
, cmd_unselect
},
2318 { ALT('*'), cmd_reverse_selection
},
2319 { KEY_KP_MULTIPLY
, cmd_reverse_selection
},
2323 static inline cb_ret_t
2324 panel_key (WPanel
*panel
, int key
)
2328 for (i
= 0; panel_keymap
[i
].key_code
; i
++) {
2329 if (key
== panel_keymap
[i
].key_code
) {
2330 int old_searching
= panel
->searching
;
2332 if (panel_keymap
[i
].fn
!= start_search
)
2333 panel
->searching
= 0;
2335 (*panel_keymap
[i
].fn
) (panel
);
2337 if (panel
->searching
!= old_searching
)
2338 display_mini_info (panel
);
2342 if (torben_fj_mode
&& key
== ALT ('h')) {
2343 goto_middle_file (panel
);
2347 /* We do not want to take a key press if nothing can be done with it */
2348 /* The command line widget may do something more useful */
2349 if (key
== KEY_LEFT
)
2350 return move_left (panel
, key
);
2352 if (key
== KEY_RIGHT
)
2353 return move_right (panel
, key
);
2355 if (is_abort_char (key
)) {
2356 panel
->searching
= 0;
2357 display_mini_info (panel
);
2361 /* Do not eat characters not meant for the panel below ' ' (e.g. C-l). */
2362 if ((key
>= ' ' && key
<= 255) || key
== KEY_BACKSPACE
) {
2363 if (panel
->searching
) {
2364 do_search (panel
, key
);
2368 if (!command_prompt
) {
2369 start_search (panel
);
2370 do_search (panel
, key
);
2375 return MSG_NOT_HANDLED
;
2379 panel_callback (Widget
*w
, widget_msg_t msg
, int parm
)
2381 WPanel
*panel
= (WPanel
*) w
;
2382 Dlg_head
*h
= panel
->widget
.parent
;
2386 paint_panel (panel
);
2390 current_panel
= panel
;
2392 if (mc_chdir (panel
->cwd
) != 0) {
2393 char *cwd
= strip_password (g_strdup (panel
->cwd
), 1);
2394 message (D_ERROR
, MSG_ERROR
, _(" Cannot chdir to \"%s\" \n %s "),
2395 cwd
, unix_error_string (errno
));
2398 subshell_chdir (panel
->cwd
);
2400 update_xterm_title_path ();
2401 select_item (panel
);
2406 buttonbar_set_label (h
, 1, _("Help"), help_cmd
);
2407 buttonbar_set_label (h
, 2, _("Menu"), user_file_menu_cmd
);
2408 buttonbar_set_label (h
, 3, _("View"), view_cmd
);
2409 buttonbar_set_label (h
, 4, _("Edit"), edit_cmd
);
2410 buttonbar_set_label (h
, 5, _("Copy"), copy_cmd
);
2411 buttonbar_set_label (h
, 6, _("RenMov"), ren_cmd
);
2412 buttonbar_set_label (h
, 7, _("Mkdir"), mkdir_cmd
);
2413 buttonbar_set_label (h
, 8, _("Delete"), delete_cmd
);
2414 buttonbar_redraw (h
);
2417 case WIDGET_UNFOCUS
:
2418 /* Janne: look at this for the multiple panel options */
2419 if (panel
->searching
){
2420 panel
->searching
= 0;
2421 display_mini_info (panel
);
2425 unselect_item (panel
);
2429 return panel_key (panel
, parm
);
2431 case WIDGET_DESTROY
:
2432 panel_destroy (panel
);
2436 return default_proc (msg
, parm
);
2441 file_mark (WPanel
*panel
, int index
, int val
)
2443 if (panel
->dir
.list
[index
].f
.marked
!= val
) {
2444 panel
->dir
.list
[index
].f
.marked
= val
;
2450 /* Panel mouse events support routines */
2452 static int mouse_marking
= 0;
2455 mouse_toggle_mark (WPanel
*panel
)
2457 do_mark_file (panel
, 0);
2458 mouse_marking
= selection (panel
)->f
.marked
;
2462 mouse_set_mark (WPanel
*panel
)
2464 if (mouse_marking
&& !(selection (panel
)->f
.marked
))
2465 do_mark_file (panel
, 0);
2466 else if (!mouse_marking
&& (selection (panel
)->f
.marked
))
2467 do_mark_file (panel
, 0);
2471 mark_if_marking (WPanel
*panel
, Gpm_Event
*event
)
2473 if (event
->buttons
& GPM_B_RIGHT
){
2474 if (event
->type
& GPM_DOWN
)
2475 mouse_toggle_mark (panel
);
2477 mouse_set_mark (panel
);
2484 * Mouse callback of the panel minus repainting.
2485 * If the event is redirected to the menu, *redir is set to 1.
2488 do_panel_event (Gpm_Event
*event
, WPanel
*panel
, int *redir
)
2490 const int lines
= llines (panel
);
2494 /* Mouse wheel events */
2495 if ((event
->buttons
& GPM_B_UP
) && (event
->type
& GPM_DOWN
)) {
2499 if ((event
->buttons
& GPM_B_DOWN
) && (event
->type
& GPM_DOWN
)) {
2505 if (event
->type
& GPM_DOWN
&& event
->x
== 2 && event
->y
== 1) {
2506 directory_history_prev (panel
);
2511 if (event
->type
& GPM_DOWN
&& event
->x
== panel
->widget
.cols
- 1
2513 directory_history_next (panel
);
2518 if (event
->type
& GPM_DOWN
&& event
->x
== panel
->widget
.cols
- 2
2520 directory_history_list (panel
);
2524 /* rest of the upper frame, the menu is invisible - call menu */
2525 if (event
->type
& GPM_DOWN
&& event
->y
== 1 && !menubar_visible
) {
2527 event
->x
+= panel
->widget
.x
;
2528 return (*(the_menubar
->widget
.mouse
)) (event
, the_menubar
);
2532 if ((event
->type
& (GPM_DOWN
| GPM_DRAG
))) {
2534 if (!dlg_widget_active (panel
))
2537 if (event
->y
<= 0) {
2538 mark_if_marking (panel
, event
);
2539 if (mouse_move_pages
)
2546 if (!((panel
->top_file
+ event
->y
<= panel
->count
)
2547 && event
->y
<= lines
)) {
2548 mark_if_marking (panel
, event
);
2549 if (mouse_move_pages
)
2555 my_index
= panel
->top_file
+ event
->y
- 1;
2557 if (event
->x
> ((panel
->widget
.cols
- 2) / 2))
2558 my_index
+= llines (panel
);
2561 if (my_index
>= panel
->count
)
2562 my_index
= panel
->count
- 1;
2564 if (my_index
!= panel
->selected
) {
2565 unselect_item (panel
);
2566 panel
->selected
= my_index
;
2567 select_item (panel
);
2570 /* This one is new */
2571 mark_if_marking (panel
, event
);
2573 } else if ((event
->type
& (GPM_UP
| GPM_DOUBLE
)) ==
2574 (GPM_UP
| GPM_DOUBLE
)) {
2575 if (event
->y
> 0 && event
->y
<= lines
)
2581 /* Mouse callback of the panel */
2583 panel_event (Gpm_Event
*event
, void *data
)
2585 WPanel
*panel
= data
;
2589 ret
= do_panel_event (event
, panel
, &redir
);
2591 paint_panel (panel
);
2597 panel_re_sort (WPanel
*panel
)
2605 filename
= g_strdup (selection (panel
)->fname
);
2606 unselect_item (panel
);
2607 do_sort (&panel
->dir
, panel
->sort_type
, panel
->count
-1, panel
->reverse
,
2608 panel
->case_sensitive
, panel
->exec_first
);
2609 panel
->selected
= -1;
2610 for (i
= panel
->count
; i
; i
--){
2611 if (!strcmp (panel
->dir
.list
[i
-1].fname
, filename
)){
2612 panel
->selected
= i
-1;
2617 panel
->top_file
= panel
->selected
- ITEMS (panel
)/2;
2618 if (panel
->top_file
< 0)
2619 panel
->top_file
= 0;
2620 select_item (panel
);
2625 panel_set_sort_order (WPanel
*panel
, sortfn
*sort_order
)
2627 if (sort_order
== 0)
2630 panel
->sort_type
= sort_order
;
2632 /* The directory is already sorted, we have to load the unsorted stuff */
2633 if (sort_order
== (sortfn
*) unsorted
){
2636 current_file
= g_strdup (panel
->dir
.list
[panel
->selected
].fname
);
2637 panel_reload (panel
);
2638 try_to_select (panel
, current_file
);
2639 g_free (current_file
);
2641 panel_re_sort (panel
);
2645 set_panel_encoding (WPanel
*panel
)
2647 char *encoding
= NULL
;
2652 int width
= (panel
->widget
.x
)? panel
->widget
.cols
: panel
->widget
.cols
* (-1);
2654 r
= select_charset (width
, 0, source_codepage
, 0);
2661 errmsg
= init_translation_table (display_codepage
, display_codepage
);
2662 cd_path
= remove_encoding_from_path (panel
->cwd
);
2663 do_panel_cd (panel
, cd_path
, 0);
2668 source_codepage
= r
;
2670 errmsg
= init_translation_table (source_codepage
, display_codepage
);
2672 message (D_ERROR
, MSG_ERROR
, "%s", errmsg
);
2675 encoding
= g_strdup( get_codepage_id ( source_codepage
) );
2678 cd_path
= add_encoding_to_path (panel
->cwd
, encoding
);
2679 if (!do_panel_cd (panel
, cd_path
, 0))
2680 message (1, MSG_ERROR
, _(" Cannot chdir to %s "), cd_path
);