2 Copyright (C) 1994, 1995, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
3 2005, 2006, 2007 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. */
37 #include "ext.h" /* regexp_command */
38 #include "mouse.h" /* For Gpm_Event */
39 #include "layout.h" /* Most layout variables are here */
40 #include "wtools.h" /* for message (...) */
42 #include "key.h" /* XCTRL and ALT macros */
43 #include "setup.h" /* For loading/saving panel options */
48 #include "menu.h" /* menubar_visible */
49 #include "main-widgets.h"
50 #include "main.h" /* the_menubar */
51 #include "unixcompat.h"
52 #include "mountlist.h" /* my_statfs */
54 #define ELEMENTS(arr) ( sizeof(arr) / sizeof((arr)[0]) )
60 #define IS_FIT(x) ((x) & 0x0004)
61 #define MAKE_FIT(x) ((x) | 0x0004)
62 #define HIDE_FIT(x) ((x) & 0x0003)
66 #define J_CENTER_FIT 7
71 #define MARKED_SELECTED 3
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
;
84 const char *(*string_fn
)(file_entry
*, int len
);
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 */
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 static cb_ret_t
panel_callback (Widget
*, widget_msg_t msg
, int parm
);
111 static int panel_event (Gpm_Event
*event
, void *);
112 static void paint_frame (WPanel
*panel
);
113 static const char *panel_format (WPanel
*panel
);
114 static const char *mini_status_format (WPanel
*panel
);
116 /* This macro extracts the number of available lines in a panel */
117 #define llines(p) (p->widget.lines-3 - (show_mini_info ? 2 : 0))
120 set_colors (WPanel
*panel
)
123 attrset (NORMAL_COLOR
);
126 /* Delete format string, it is a linked list */
128 delete_format (format_e
*format
)
139 /* This code relies on the default justification!!! */
141 add_permission_string (char *dest
, int width
, file_entry
*fe
, int attr
, int color
, int is_octal
)
145 l
= get_user_permissions (&fe
->st
);
148 /* Place of the access bit in octal mode */
152 /* The same to the triplet in string mode */
157 for(i
= 0; i
< width
; i
++){
158 if (i
>= l
&& i
< r
){
159 if (attr
== SELECTED
|| attr
== MARKED_SELECTED
)
160 attrset (MARKED_SELECTED_COLOR
);
162 attrset (MARKED_COLOR
);
170 /* String representations of various file attributes */
173 string_file_name (file_entry
*fe
, int len
)
175 static char buffer
[MC_MAXPATHLEN
+ 1];
178 for (i
= 0; i
< sizeof(buffer
) - 1; i
++) {
186 if (!is_printable(c
))
196 static inline unsigned int ilog10(dev_t n
)
198 unsigned int digits
= 0;
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
);
217 g_strlcpy(buf
, _("[dev]"), bufsize
);
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
, "..")) {
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
);
238 size_trunc_len (buffer
, len
, fe
->st
.st_size
, 0);
245 string_file_size_brief (file_entry
*fe
, int len
)
247 if (S_ISLNK (fe
->st
.st_mode
) && !fe
->f
.link_to_dir
) {
251 if ((S_ISDIR (fe
->st
.st_mode
) || fe
->f
.link_to_dir
) && strcmp (fe
->fname
, "..")) {
255 return string_file_size (fe
, len
);
258 /* This functions return a string representation of a file entry */
261 string_file_type (file_entry
*fe
, int len
)
263 static char buffer
[2];
265 if (S_ISDIR (fe
->st
.st_mode
))
266 buffer
[0] = PATH_SEP
;
267 else if (S_ISLNK (fe
->st
.st_mode
)) {
268 if (fe
->f
.link_to_dir
)
270 else if (fe
->f
.stale_link
)
274 } else if (S_ISCHR (fe
->st
.st_mode
))
276 else if (S_ISSOCK (fe
->st
.st_mode
))
278 else if (S_ISDOOR (fe
->st
.st_mode
))
280 else if (S_ISBLK (fe
->st
.st_mode
))
282 else if (S_ISFIFO (fe
->st
.st_mode
))
284 else if (S_ISNAM (fe
->st
.st_mode
))
286 else if (!S_ISREG (fe
->st
.st_mode
))
287 buffer
[0] = '?'; /* non-regular of unknown kind */
288 else if (is_exe (fe
->st
.st_mode
))
298 string_file_mtime (file_entry
*fe
, int len
)
300 if (!strcmp (fe
->fname
, "..")) {
303 return file_date (fe
->st
.st_mtime
);
308 string_file_atime (file_entry
*fe
, int len
)
310 if (!strcmp (fe
->fname
, "..")) {
313 return file_date (fe
->st
.st_atime
);
318 string_file_ctime (file_entry
*fe
, int len
)
320 if (!strcmp (fe
->fname
, "..")) {
323 return file_date (fe
->st
.st_ctime
);
328 string_file_permission (file_entry
*fe
, int len
)
330 return string_perm (fe
->st
.st_mode
);
335 string_file_perm_octal (file_entry
*fe
, int len
)
337 static char buffer
[10];
339 g_snprintf (buffer
, sizeof (buffer
), "0%06lo", (unsigned long) fe
->st
.st_mode
);
345 string_file_nlinks (file_entry
*fe
, int len
)
347 static char buffer
[BUF_TINY
];
349 g_snprintf (buffer
, sizeof (buffer
), "%16d", (int) fe
->st
.st_nlink
);
355 string_inode (file_entry
*fe
, int len
)
357 static char buffer
[10];
359 g_snprintf (buffer
, sizeof (buffer
), "%lu",
360 (unsigned long) fe
->st
.st_ino
);
366 string_file_nuid (file_entry
*fe
, int len
)
368 static char buffer
[10];
370 g_snprintf (buffer
, sizeof (buffer
), "%lu",
371 (unsigned long) fe
->st
.st_uid
);
377 string_file_ngid (file_entry
*fe
, int len
)
379 static char buffer
[10];
381 g_snprintf (buffer
, sizeof (buffer
), "%lu",
382 (unsigned long) fe
->st
.st_gid
);
388 string_file_owner (file_entry
*fe
, int len
)
390 return get_owner (fe
->st
.st_uid
);
395 string_file_group (file_entry
*fe
, int len
)
397 return get_group (fe
->st
.st_gid
);
402 string_marked (file_entry
*fe
, int len
)
404 return fe
->f
.marked
? "*" : " ";
409 string_space (file_entry
*fe
, int len
)
416 string_dot (file_entry
*fe
, int len
)
430 const char *(*string_fn
)(file_entry
*, int);
431 sortfn
*sort_routine
; /* This field is currently unused. */
433 { "name", 12, 1, J_LEFT_FIT
, N_("Name"), 1, string_file_name
, (sortfn
*) sort_name
},
434 { "size", 7, 0, J_RIGHT
, N_("Size"), 1, string_file_size
, (sortfn
*) sort_size
},
435 { "bsize", 7, 0, J_RIGHT
, N_("Size"), 1, string_file_size_brief
, (sortfn
*) sort_size
},
436 { "type", GT
, 0, J_LEFT
, "", 2, string_file_type
, NULL
},
437 { "mtime", 12, 0, J_RIGHT
, N_("MTime"), 1, string_file_mtime
, (sortfn
*) sort_time
},
438 { "atime", 12, 0, J_RIGHT
, N_("ATime"), 1, string_file_atime
, (sortfn
*) sort_atime
},
439 { "ctime", 12, 0, J_RIGHT
, N_("CTime"), 1, string_file_ctime
, (sortfn
*) sort_ctime
},
440 { "perm", 10, 0, J_LEFT
, N_("Permission"),1,string_file_permission
, NULL
},
441 { "mode", 6, 0, J_RIGHT
, N_("Perm"), 1, string_file_perm_octal
, NULL
},
442 { "nlink", 2, 0, J_RIGHT
, N_("Nl"), 1, string_file_nlinks
, NULL
},
443 { "inode", 5, 0, J_RIGHT
, N_("Inode"), 1, string_inode
, (sortfn
*) sort_inode
},
444 { "nuid", 5, 0, J_RIGHT
, N_("UID"), 1, string_file_nuid
, NULL
},
445 { "ngid", 5, 0, J_RIGHT
, N_("GID"), 1, string_file_ngid
, NULL
},
446 { "owner", 8, 0, J_LEFT_FIT
, N_("Owner"), 1, string_file_owner
, NULL
},
447 { "group", 8, 0, J_LEFT_FIT
, N_("Group"), 1, string_file_group
, NULL
},
448 { "mark", 1, 0, J_RIGHT
, " ", 1, string_marked
, NULL
},
449 { "|", 1, 0, J_RIGHT
, " ", 0, NULL
, NULL
},
450 { "space", 1, 0, J_RIGHT
, " ", 0, string_space
, NULL
},
451 { "dot", 1, 0, J_RIGHT
, " ", 0, string_dot
, NULL
},
455 to_buffer (char *dest
, int just_mode
, int len
, const char *txt
)
457 int txtlen
= strlen (txt
);
460 /* Fill buffer with spaces */
461 memset (dest
, ' ', len
);
463 still
= (over
=(txtlen
> len
)) ? (txtlen
- len
) : (len
- txtlen
);
465 switch (HIDE_FIT(just_mode
)){
478 if (IS_FIT(just_mode
))
479 strcpy (dest
, name_trunc(txt
, len
));
481 strncpy (dest
, txt
+still
, len
);
483 strncpy (dest
+still
, txt
, txtlen
);
491 file_compute_color (int attr
, file_entry
*fe
)
495 return (SELECTED_COLOR
);
497 return (MARKED_COLOR
);
498 case MARKED_SELECTED
:
499 return (MARKED_SELECTED_COLOR
);
501 return (NORMAL_COLOR
);
505 return (NORMAL_COLOR
);
508 /* if filetype_mode == true */
509 if (S_ISDIR (fe
->st
.st_mode
))
510 return (DIRECTORY_COLOR
);
511 else if (S_ISLNK (fe
->st
.st_mode
)) {
512 if (fe
->f
.link_to_dir
)
513 return (DIRECTORY_COLOR
);
514 else if (fe
->f
.stale_link
)
515 return (STALE_LINK_COLOR
);
518 } else if (S_ISSOCK (fe
->st
.st_mode
))
519 return (SPECIAL_COLOR
);
520 else if (S_ISCHR (fe
->st
.st_mode
))
521 return (DEVICE_COLOR
);
522 else if (S_ISBLK (fe
->st
.st_mode
))
523 return (DEVICE_COLOR
);
524 else if (S_ISNAM (fe
->st
.st_mode
))
525 return (DEVICE_COLOR
);
526 else if (S_ISFIFO (fe
->st
.st_mode
))
527 return (SPECIAL_COLOR
);
528 else if (S_ISDOOR (fe
->st
.st_mode
))
529 return (SPECIAL_COLOR
);
530 else if (!S_ISREG (fe
->st
.st_mode
))
531 return (STALE_LINK_COLOR
); /* non-regular file of unknown kind */
532 else if (is_exe (fe
->st
.st_mode
))
533 return (EXECUTABLE_COLOR
);
534 else if (fe
->fname
&& (!strcmp (fe
->fname
, "core")
535 || !strcmp (extension (fe
->fname
), "core")))
538 return (NORMAL_COLOR
);
541 /* Formats the file number file_index of panel in the buffer dest */
543 format_file (char *dest
, int limit
, WPanel
*panel
, int file_index
, int width
, int attr
, int isstatus
)
545 int color
, length
, empty_line
;
549 format_e
*format
, *home
;
553 empty_line
= (file_index
>= panel
->count
);
554 home
= (isstatus
) ? panel
->status_format
: panel
->format
;
555 fe
= &panel
->dir
.list
[file_index
];
558 color
= file_compute_color (attr
, fe
);
560 color
= NORMAL_COLOR
;
562 for (format
= home
; format
; format
= format
->next
){
567 if (format
->string_fn
){
573 txt
= (*format
->string_fn
)(fe
, format
->field_len
);
577 len
= format
->field_len
;
578 if (len
+ length
> width
)
579 len
= width
- length
;
580 if (len
+ (cdest
- dest
) > limit
)
581 len
= limit
- (cdest
- dest
);
584 cdest
= to_buffer (cdest
, format
->just_mode
, len
, txt
);
589 if (permission_mode
&& !strcmp(format
->id
, "perm"))
590 add_permission_string (old_pos
, format
->field_len
, fe
, attr
, color
, 0);
591 else if (permission_mode
&& !strcmp(format
->id
, "mode"))
592 add_permission_string (old_pos
, format
->field_len
, fe
, attr
, color
, 1);
597 if (attr
== SELECTED
|| attr
== MARKED_SELECTED
)
598 attrset (SELECTED_COLOR
);
600 attrset (NORMAL_COLOR
);
607 int still
= width
- length
;
614 repaint_file (WPanel
*panel
, int file_index
, int mv
, int attr
, int isstatus
)
616 int second_column
= 0;
618 char buffer
[BUF_MEDIUM
];
621 if (!isstatus
&& panel
->split
){
623 second_column
= (file_index
- panel
->top_file
) / llines (panel
);
624 width
= (panel
->widget
.cols
- 2)/2 - 1;
628 width
= (panel
->widget
.cols
-2) - (panel
->widget
.cols
-2)/2 - 1;
631 width
= (panel
->widget
.cols
- 2);
633 /* Nothing to paint */
638 if (!isstatus
&& panel
->split
){
639 widget_move (&panel
->widget
,
640 (file_index
- panel
->top_file
) %
644 widget_move (&panel
->widget
, file_index
- panel
->top_file
+ 2, 1);
647 format_file (buffer
, sizeof(buffer
), panel
, file_index
, width
, attr
, isstatus
);
649 if (!isstatus
&& panel
->split
){
653 attrset (NORMAL_COLOR
);
660 display_mini_info (WPanel
*panel
)
662 widget_move (&panel
->widget
, llines (panel
)+3, 1);
664 if (panel
->searching
){
665 attrset (INPUT_COLOR
);
666 tty_printf ("/%-*s", panel
->widget
.cols
-3, panel
->search_buffer
);
667 attrset (NORMAL_COLOR
);
671 /* Status resolves links and show them */
674 if (S_ISLNK (panel
->dir
.list
[panel
->selected
].st
.st_mode
)){
675 char *link
, link_target
[MC_MAXPATHLEN
];
678 link
= concat_dir_and_file (panel
->cwd
, panel
->dir
.list
[panel
->selected
].fname
);
679 len
= mc_readlink (link
, link_target
, MC_MAXPATHLEN
- 1);
682 link_target
[len
] = 0;
683 tty_printf ("-> %-*s", panel
->widget
.cols
- 5,
684 name_trunc (link_target
, panel
->widget
.cols
- 5));
686 tty_printf ("%-*s", panel
->widget
.cols
- 2, _("<readlink failed>"));
688 /* Default behavior */
689 repaint_file (panel
, panel
->selected
, 0, STATUS
, 1);
693 paint_dir (WPanel
*panel
)
696 int color
; /* Color value of the line */
697 int items
; /* Number of items */
699 items
= llines (panel
) * (panel
->split
? 2 : 1);
701 for (i
= 0; i
< items
; i
++){
702 if (i
+panel
->top_file
>= panel
->count
)
705 color
= 2 * (panel
->dir
.list
[i
+panel
->top_file
].f
.marked
);
706 color
+= (panel
->selected
==i
+panel
->top_file
&& panel
->active
);
708 repaint_file (panel
, i
+panel
->top_file
, 1, color
, 0);
714 dispaly_total_marked_size (WPanel
*panel
, int y
, int x
, gboolean size_only
)
716 char buffer
[BUF_SMALL
], b_bytes
[BUF_SMALL
], *buf
;
720 if (panel
->marked
<= 0)
723 buf
= size_only
? b_bytes
: buffer
;
724 cols
= panel
->widget
.cols
- 2;
727 * This is a trick to use two ngettext() calls in one sentence.
728 * First make "N bytes", then insert it into "X in M files".
730 g_snprintf (b_bytes
, sizeof (b_bytes
),
731 ngettext("%s byte", "%s bytes", (unsigned long) panel
->total
),
732 size_trunc_sep (panel
->total
));
734 g_snprintf (buffer
, sizeof (buffer
),
735 ngettext("%s in %d file", "%s in %d files", panel
->marked
),
736 b_bytes
, panel
->marked
);
740 /* don't forget spaces around buffer content */
741 if ((int) blen
> cols
- 2) {
742 buf
[cols
- 2] = '\0';
743 blen
= (size_t) (cols
- 2);
747 /* center in panel */
748 x
= (panel
->widget
.cols
- (int) blen
) / 2 - 1;
751 * y == llines (panel) + 2 for mini_info_separator
752 * y == panel->widget.lines - 1 for panel bottom frame
754 widget_move (&panel
->widget
, y
, x
);
755 attrset (MARKED_COLOR
);
756 tty_printf (" %s ", buf
);
760 mini_info_separator (WPanel
*panel
)
762 const int y
= llines (panel
) + 2;
765 widget_move (&panel
->widget
, y
, 1);
766 attrset (NORMAL_COLOR
);
768 hline (ACS_HLINE
, panel
->widget
.cols
- 2);
770 hline ((slow_terminal
? '-' : ACS_HLINE
) | NORMAL_COLOR
,
771 panel
->widget
.cols
- 2);
772 #endif /* !HAVE_SLANG */
774 /* Status displays total marked size.
775 * Centered in panel, full format. */
776 dispaly_total_marked_size (panel
, y
, -1, FALSE
);
780 show_free_space (WPanel
*panel
)
782 /* Used to figure out how many free space we have */
783 static struct my_statfs myfs_stats
;
784 /* Old current working directory for displaying free space */
785 static char *old_cwd
= NULL
;
787 /* Don't try to stat non-local fs */
788 if (!vfs_file_is_local (panel
->cwd
) || !free_space
)
791 if (old_cwd
== NULL
|| strcmp (old_cwd
, panel
->cwd
) != 0) {
792 char rpath
[PATH_MAX
];
796 old_cwd
= g_strdup (panel
->cwd
);
798 if (mc_realpath (panel
->cwd
, rpath
) == NULL
)
801 my_statfs (&myfs_stats
, rpath
);
804 if (myfs_stats
.avail
> 0 || myfs_stats
.total
> 0) {
805 char buffer1
[6], buffer2
[6], tmp
[BUF_SMALL
];
806 size_trunc_len (buffer1
, sizeof(buffer1
) - 1, myfs_stats
.avail
, 1);
807 size_trunc_len (buffer2
, sizeof(buffer2
) - 1, myfs_stats
.total
, 1);
808 g_snprintf (tmp
, sizeof(tmp
), " %s/%s (%d%%) ", buffer1
, buffer2
,
809 myfs_stats
.total
> 0 ?
810 (int)(100 * (double)myfs_stats
.avail
/ myfs_stats
.total
) : 0);
811 widget_move (&panel
->widget
, panel
->widget
.lines
- 1,
812 panel
->widget
.cols
- 2 - (int) strlen (tmp
));
813 attrset (NORMAL_COLOR
);
819 show_dir (WPanel
*panel
)
824 draw_double_box (panel
->widget
.parent
,
825 panel
->widget
.y
, panel
->widget
.x
,
826 panel
->widget
.lines
, panel
->widget
.cols
);
829 if (show_mini_info
) {
830 SLsmg_draw_object (panel
->widget
.y
+ llines (panel
) + 2,
831 panel
->widget
.x
, SLSMG_LTEE_CHAR
);
832 SLsmg_draw_object (panel
->widget
.y
+ llines (panel
) + 2,
833 panel
->widget
.x
+ panel
->widget
.cols
- 1,
836 #endif /* HAVE_SLANG */
839 attrset (REVERSE_COLOR
);
841 widget_move (&panel
->widget
, 0, 3);
844 tmp
= g_malloc (panel
->widget
.cols
+ 1);
845 tmp
[panel
->widget
.cols
] = '\0';
847 trim (strip_home_and_password (panel
->cwd
), tmp
,
848 max (panel
->widget
.cols
- 9, 0));
853 widget_move (&panel
->widget
, 0, 1);
855 widget_move (&panel
->widget
, 0, panel
->widget
.cols
- 2);
857 widget_move (&panel
->widget
, 0, panel
->widget
.cols
- 3);
860 if (!show_mini_info
) {
861 if (panel
->marked
== 0) {
862 /* Show size of curret file in the bottom of panel */
863 if (S_ISREG (panel
->dir
.list
[panel
->selected
].st
.st_mode
)) {
864 char buffer
[BUF_SMALL
];
866 g_snprintf (buffer
, sizeof (buffer
), " %s ",
867 size_trunc_sep (panel
->dir
.list
[panel
->selected
].st
.st_size
));
868 attrset (NORMAL_COLOR
);
869 widget_move (&panel
->widget
, panel
->widget
.lines
- 1, 2);
873 /* Show total size of marked files
874 * In the bottom of panel, display size only. */
875 dispaly_total_marked_size (panel
, panel
->widget
.lines
- 1, 2, TRUE
);
879 show_free_space (panel
);
885 /* To be used only by long_frame and full_frame to adjust top_file */
887 adjust_top_file (WPanel
*panel
)
889 int old_top
= panel
->top_file
;
891 if (panel
->selected
- old_top
> llines (panel
))
892 panel
->top_file
= panel
->selected
;
893 if (old_top
- panel
->count
> llines (panel
))
894 panel
->top_file
= panel
->count
- llines (panel
);
897 /* Repaint everything, including frame and separator */
899 paint_panel (WPanel
*panel
)
901 paint_frame (panel
); /* including show_dir */
904 if (show_mini_info
) {
905 mini_info_separator (panel
);
906 display_mini_info (panel
);
913 * Repaint the contents of the panels without frames. To schedule panel
914 * for repainting, set panel->dirty to 1. There are many reasons why
915 * the panels need to be repainted, and this is a costly operation, so
916 * it's done once per event.
919 update_dirty_panels (void)
921 if (current_panel
->dirty
)
922 paint_panel (current_panel
);
924 if ((get_other_type () == view_listing
) && other_panel
->dirty
)
925 paint_panel (other_panel
);
929 do_select (WPanel
*panel
, int i
)
931 if (i
!= panel
->selected
) {
934 panel
->top_file
= panel
->selected
- (panel
->widget
.lines
- 2) / 2;
935 if (panel
->top_file
< 0)
941 do_try_to_select (WPanel
*panel
, const char *name
)
951 /* We only want the last component of the directory,
952 * and from this only the name without suffix. */
953 subdir
= vfs_strip_suffix_from_filename (x_basename(name
));
955 /* Search that subdirectory, if found select it */
956 for (i
= 0; i
< panel
->count
; i
++){
957 if (strcmp (subdir
, panel
->dir
.list
[i
].fname
) == 0) {
958 do_select (panel
, i
);
964 /* Try to select a file near the file that is missing */
965 if (panel
->selected
>= panel
->count
)
966 do_select (panel
, panel
->count
-1);
971 try_to_select (WPanel
*panel
, const char *name
)
973 do_try_to_select (panel
, name
);
978 panel_update_cols (Widget
*widget
, int frame_size
)
982 if (horizontal_split
){
987 if (frame_size
== frame_full
){
991 if (widget
== get_panel_widget (0)){
992 cols
= first_panel_size
;
995 cols
= COLS
-first_panel_size
;
996 origin
= first_panel_size
;
1000 widget
->cols
= cols
;
1005 panel_save_name (WPanel
*panel
)
1007 extern int saving_setup
;
1009 /* If the program is shuting down */
1010 if ((midnight_shutdown
&& auto_save_setup
) || saving_setup
)
1011 return g_strdup (panel
->panel_name
);
1013 return g_strconcat ("Temporal:", panel
->panel_name
, (char *) NULL
);
1017 panel_destroy (WPanel
*p
)
1021 char *name
= panel_save_name (p
);
1023 panel_save_setup (p
, name
);
1024 panel_clean_dir (p
);
1026 /* save and clean history */
1027 if (p
->dir_history
) {
1028 history_put (p
->hist_name
, p
->dir_history
);
1030 p
->dir_history
= g_list_first (p
->dir_history
);
1031 g_list_foreach (p
->dir_history
, (GFunc
) g_free
, NULL
);
1032 g_list_free (p
->dir_history
);
1035 g_free (p
->hist_name
);
1037 delete_format (p
->format
);
1038 delete_format (p
->status_format
);
1040 g_free (p
->user_format
);
1041 for (i
= 0; i
< LIST_TYPES
; i
++)
1042 g_free (p
->user_status_format
[i
]);
1043 g_free (p
->dir
.list
);
1044 g_free (p
->panel_name
);
1049 panel_format_modified (WPanel
*panel
)
1051 panel
->format_modified
= 1;
1054 /* Panel creation */
1055 /* The parameter specifies the name of the panel for setup retieving */
1057 panel_new (const char *panel_name
)
1063 panel
= g_new0 (WPanel
, 1);
1065 /* No know sizes of the panel at startup */
1066 init_widget (&panel
->widget
, 0, 0, 0, 0, panel_callback
, panel_event
);
1068 /* We do not want the cursor */
1069 widget_want_cursor (panel
->widget
, 0);
1071 mc_get_current_wd (panel
->cwd
, sizeof (panel
->cwd
) - 2);
1072 strcpy (panel
->lwd
, ".");
1074 panel
->hist_name
= g_strconcat ("Dir Hist ", panel_name
, (char *) NULL
);
1075 panel
->dir_history
= history_get (panel
->hist_name
);
1076 directory_history_add (panel
, panel
->cwd
);
1078 panel
->dir
.list
= g_new (file_entry
, MIN_FILES
);
1079 panel
->dir
.size
= MIN_FILES
;
1083 panel
->top_file
= 0;
1084 panel
->selected
= 0;
1089 panel
->searching
= 0;
1090 panel
->dirs_marked
= 0;
1091 panel
->is_panelized
= 0;
1093 panel
->status_format
= 0;
1094 panel
->format_modified
= 1;
1096 panel
->panel_name
= g_strdup (panel_name
);
1097 panel
->user_format
= g_strdup (DEFAULT_USER_FORMAT
);
1099 for (i
= 0; i
< LIST_TYPES
; i
++)
1100 panel
->user_status_format
[i
] = g_strdup (DEFAULT_USER_FORMAT
);
1102 panel
->search_buffer
[0] = 0;
1103 panel
->frame_size
= frame_half
;
1104 section
= g_strconcat ("Temporal:", panel
->panel_name
, (char *) NULL
);
1105 if (!profile_has_section (section
, profile_name
)) {
1107 section
= g_strdup (panel
->panel_name
);
1109 panel_load_setup (panel
, section
);
1112 /* Load format strings */
1113 err
= set_panel_formats (panel
);
1115 set_panel_formats (panel
);
1118 /* Load the default format */
1120 do_load_dir (panel
->cwd
, &panel
->dir
, panel
->sort_type
,
1121 panel
->reverse
, panel
->case_sensitive
,
1122 panel
->exec_first
, panel
->filter
);
1127 panel_reload (WPanel
*panel
)
1129 struct stat current_stat
;
1131 if (fast_reload
&& !stat (panel
->cwd
, ¤t_stat
)
1132 && current_stat
.st_ctime
== panel
->dir_stat
.st_ctime
1133 && current_stat
.st_mtime
== panel
->dir_stat
.st_mtime
)
1136 while (mc_chdir (panel
->cwd
) == -1) {
1139 if (panel
->cwd
[0] == PATH_SEP
&& panel
->cwd
[1] == 0) {
1140 panel_clean_dir (panel
);
1141 panel
->count
= set_zero_dir (&panel
->dir
);
1144 last_slash
= strrchr (panel
->cwd
, PATH_SEP
);
1145 if (!last_slash
|| last_slash
== panel
->cwd
)
1146 strcpy (panel
->cwd
, PATH_SEP_STR
);
1149 memset (&(panel
->dir_stat
), 0, sizeof (panel
->dir_stat
));
1154 do_reload_dir (panel
->cwd
, &panel
->dir
, panel
->sort_type
,
1155 panel
->count
, panel
->reverse
, panel
->case_sensitive
,
1156 panel
->exec_first
, panel
->filter
);
1159 if (panel
->selected
>= panel
->count
)
1160 do_select (panel
, panel
->count
- 1);
1162 recalculate_panel_summary (panel
);
1166 paint_frame (WPanel
*panel
)
1174 adjust_top_file (panel
);
1176 widget_erase (&panel
->widget
);
1179 widget_move (&panel
->widget
, 1, 1);
1181 for (side
= 0; side
<= panel
->split
; side
++){
1185 attrset (NORMAL_COLOR
);
1187 width
= panel
->widget
.cols
- panel
->widget
.cols
/2 - 1;
1188 } else if (panel
->split
)
1189 width
= panel
->widget
.cols
/2 - 3;
1191 width
= panel
->widget
.cols
- 2;
1193 for (format
= panel
->format
; format
; format
= format
->next
){
1194 if (format
->string_fn
){
1195 txt
= format
->title
;
1197 header_len
= strlen (txt
);
1198 if (header_len
> format
->field_len
)
1199 header_len
= format
->field_len
;
1201 attrset (MARKED_COLOR
);
1202 spaces
= (format
->field_len
- header_len
) / 2;
1203 extra
= (format
->field_len
- header_len
) % 2;
1204 tty_printf ("%*s%.*s%*s", spaces
, "",
1205 header_len
, txt
, spaces
+extra
, "");
1206 width
-= 2 * spaces
+ extra
+ header_len
;
1208 attrset (NORMAL_COLOR
);
1216 tty_printf ("%*s", width
, "");
1221 parse_panel_size (WPanel
*panel
, const char *format
, int isstatus
)
1223 int frame
= frame_half
;
1224 format
= skip_separators (format
);
1226 if (!strncmp (format
, "full", 4)){
1229 } else if (!strncmp (format
, "half", 4)){
1235 panel
->frame_size
= frame
;
1239 /* Now, the optional column specifier */
1240 format
= skip_separators (format
);
1242 if (*format
== '1' || *format
== '2'){
1244 panel
->split
= *format
== '2';
1249 panel_update_cols (&(panel
->widget
), panel
->frame_size
);
1251 return skip_separators (format
);
1256 all := panel_format? format
1257 panel_format := [full|half] [1|2]
1258 format := one_format_e
1259 | format , one_format_e
1261 one_format_e := just format.id [opt_size]
1263 opt_size := : size [opt_expand]
1270 parse_display_format (WPanel
*panel
, const char *format
, char **error
, int isstatus
, int *res_total_cols
)
1272 format_e
*darr
, *old
= 0, *home
= 0; /* The formats we return */
1273 int total_cols
= 0; /* Used columns by the format */
1274 int set_justify
; /* flag: set justification mode? */
1275 int justify
= 0; /* Which mode. */
1276 int items
= 0; /* Number of items in the format */
1279 static size_t i18n_timelength
= 0; /* flag: check ?Time length at startup */
1283 if (i18n_timelength
== 0) {
1284 i18n_timelength
= i18n_checktimelength (); /* Musn't be 0 */
1286 for (i
= 0; i
< ELEMENTS(formats
); i
++)
1287 if (strcmp ("time", formats
[i
].id
+1) == 0)
1288 formats
[i
].min_size
= i18n_timelength
;
1292 * This makes sure that the panel and mini status full/half mode
1295 format
= parse_panel_size (panel
, format
, isstatus
);
1297 while (*format
){ /* format can be an empty string */
1300 darr
= g_new (format_e
, 1);
1302 /* I'm so ugly, don't look at me :-) */
1310 format
= skip_separators (format
);
1312 if (strchr ("<=>", *format
)){
1327 format
= skip_separators (format
+1);
1331 for (i
= 0; i
< ELEMENTS(formats
); i
++){
1332 int klen
= strlen (formats
[i
].id
);
1334 if (strncmp (format
, formats
[i
].id
, klen
) != 0)
1339 if (formats
[i
].use_in_gui
)
1342 darr
->requested_field_len
= formats
[i
].min_size
;
1343 darr
->string_fn
= formats
[i
].string_fn
;
1344 if (formats
[i
].title
[0])
1345 darr
->title
= _(formats
[i
].title
);
1348 darr
->id
= formats
[i
].id
;
1349 darr
->expand
= formats
[i
].expands
;
1350 darr
->just_mode
= formats
[i
].default_just
;
1353 if (IS_FIT(darr
->just_mode
))
1354 darr
->just_mode
= MAKE_FIT(justify
);
1356 darr
->just_mode
= justify
;
1360 format
= skip_separators (format
);
1362 /* If we have a size specifier */
1363 if (*format
== ':'){
1366 /* If the size was specified, we don't want
1367 * auto-expansion by default
1371 req_length
= atoi (format
);
1372 darr
->requested_field_len
= req_length
;
1374 format
= skip_numbers (format
);
1376 /* Now, if they insist on expansion */
1377 if (*format
== '+'){
1387 char *tmp_format
= g_strdup (format
);
1389 int pos
= min (8, strlen (format
));
1390 delete_format (home
);
1391 tmp_format
[pos
] = 0;
1392 *error
= g_strconcat (_("Unknown tag on display format: "), tmp_format
, (char *) NULL
);
1393 g_free (tmp_format
);
1396 total_cols
+= darr
->requested_field_len
;
1399 *res_total_cols
= total_cols
;
1404 use_display_format (WPanel
*panel
, const char *format
, char **error
, int isstatus
)
1406 #define MAX_EXPAND 4
1407 int expand_top
= 0; /* Max used element in expand */
1408 int usable_columns
; /* Usable columns in the panel */
1411 format_e
*darr
, *home
;
1414 format
= DEFAULT_USER_FORMAT
;
1416 home
= parse_display_format (panel
, format
, error
, isstatus
, &total_cols
);
1423 /* Status needn't to be split */
1424 usable_columns
= ((panel
->widget
.cols
-2)/((isstatus
)
1426 : (panel
->split
+1))) - (!isstatus
&& panel
->split
);
1428 /* Look for the expandable fields and set field_len based on the requested field len */
1429 for (darr
= home
; darr
&& expand_top
< MAX_EXPAND
; darr
= darr
->next
){
1430 darr
->field_len
= darr
->requested_field_len
;
1435 /* If we used more columns than the available columns, adjust that */
1436 if (total_cols
> usable_columns
){
1437 int pdif
, dif
= total_cols
- usable_columns
;
1441 for (darr
= home
; darr
; darr
= darr
->next
){
1442 if (dif
&& darr
->field_len
- 1){
1448 /* avoid endless loop if num fields > 40 */
1452 total_cols
= usable_columns
; /* give up, the rest should be truncated */
1455 /* Expand the available space */
1456 if ((usable_columns
> total_cols
) && expand_top
){
1457 int spaces
= (usable_columns
- total_cols
) / expand_top
;
1458 int extra
= (usable_columns
- total_cols
) % expand_top
;
1460 for (i
= 0, darr
= home
; darr
&& (i
< expand_top
); darr
= darr
->next
)
1462 darr
->field_len
+= (spaces
+ ((i
== 0) ? extra
: 0));
1469 /* Switches the panel to the mode specified in the format */
1470 /* Seting up both format and status string. Return: 0 - on success; */
1471 /* 1 - format error; 2 - status error; 3 - errors in both formats. */
1473 set_panel_formats (WPanel
*p
)
1479 form
= use_display_format (p
, panel_format (p
), &err
, 0);
1487 delete_format (p
->format
);
1492 if (show_mini_info
){
1494 form
= use_display_format (p
, mini_status_format (p
), &err
, 1);
1501 if (p
->status_format
)
1502 delete_format (p
->status_format
);
1504 p
->status_format
= form
;
1508 panel_format_modified (p
);
1509 panel_update_cols (&(p
->widget
), p
->frame_size
);
1512 message( 1, _("Warning" ), _( "User supplied format looks invalid, reverting to default." ) );
1513 if (retcode
& 0x01){
1514 g_free (p
->user_format
);
1515 p
->user_format
= g_strdup (DEFAULT_USER_FORMAT
);
1517 if (retcode
& 0x02){
1518 g_free (p
->user_status_format
[p
->list_type
]);
1519 p
->user_status_format
[p
->list_type
] = g_strdup (DEFAULT_USER_FORMAT
);
1525 /* Given the panel->view_type returns the format string to be parsed */
1527 panel_format (WPanel
*panel
)
1529 switch (panel
->list_type
){
1532 return "full perm space nlink space owner space group space size space mtime space name";
1535 return "half 2 type name";
1538 return panel
->user_format
;
1542 return "half type name | size | mtime";
1547 mini_status_format (WPanel
*panel
)
1549 if (panel
->user_mini_status
)
1550 return panel
->user_status_format
[panel
->list_type
];
1552 switch (panel
->list_type
){
1555 return "full perm space nlink space owner space group space size space mtime space name";
1558 return "half type name space bsize space perm space";
1561 return "half type name";
1565 return panel
->user_format
;
1570 /* Panel operation commands */
1573 /* Returns the number of items in the given panel */
1578 return llines (p
) * 2;
1583 /* Select current item and readjust the panel */
1585 select_item (WPanel
*panel
)
1587 int items
= ITEMS (panel
);
1589 /* Although currently all over the code we set the selection and
1590 top file to decent values before calling select_item, I could
1591 forget it someday, so it's better to do the actual fitting here */
1593 if (panel
->top_file
< 0)
1594 panel
->top_file
= 0;
1596 if (panel
->selected
< 0)
1597 panel
->selected
= 0;
1599 if (panel
->selected
> panel
->count
- 1)
1600 panel
->selected
= panel
->count
- 1;
1602 if (panel
->top_file
> panel
->count
- 1)
1603 panel
->top_file
= panel
->count
- 1;
1605 if ((panel
->count
- panel
->top_file
) < items
) {
1606 panel
->top_file
= panel
->count
- items
;
1607 if (panel
->top_file
< 0)
1608 panel
->top_file
= 0;
1611 if (panel
->selected
< panel
->top_file
)
1612 panel
->top_file
= panel
->selected
;
1614 if ((panel
->selected
- panel
->top_file
) >= items
)
1615 panel
->top_file
= panel
->selected
- items
+ 1;
1619 execute_hooks (select_file_hook
);
1622 /* Clears all files in the panel, used only when one file was marked */
1624 unmark_files (WPanel
*panel
)
1630 for (i
= 0; i
< panel
->count
; i
++)
1631 file_mark (panel
, i
, 0);
1633 panel
->dirs_marked
= 0;
1639 unselect_item (WPanel
*panel
)
1641 repaint_file (panel
, panel
->selected
, 1, 2*selection (panel
)->f
.marked
, 0);
1645 move_down (WPanel
*panel
)
1647 if (panel
->selected
+1 == panel
->count
)
1650 unselect_item (panel
);
1653 if (panel
->selected
- panel
->top_file
== ITEMS (panel
) &&
1654 panel_scroll_pages
){
1655 /* Scroll window half screen */
1656 panel
->top_file
+= ITEMS (panel
)/2;
1657 if (panel
->top_file
> panel
->count
- ITEMS (panel
))
1658 panel
->top_file
= panel
->count
- ITEMS (panel
);
1660 select_item (panel
);
1662 select_item (panel
);
1666 move_up (WPanel
*panel
)
1668 if (panel
->selected
== 0)
1671 unselect_item (panel
);
1673 if (panel
->selected
< panel
->top_file
&& panel_scroll_pages
){
1674 /* Scroll window half screen */
1675 panel
->top_file
-= ITEMS (panel
)/2;
1676 if (panel
->top_file
< 0) panel
->top_file
= 0;
1679 select_item (panel
);
1682 /* Changes the selection by lines (may be negative) */
1684 move_selection (WPanel
*panel
, int lines
)
1689 new_pos
= panel
->selected
+ lines
;
1690 if (new_pos
>= panel
->count
)
1691 new_pos
= panel
->count
-1;
1696 unselect_item (panel
);
1697 panel
->selected
= new_pos
;
1699 if (panel
->selected
- panel
->top_file
>= ITEMS (panel
)){
1700 panel
->top_file
+= lines
;
1704 if (panel
->selected
- panel
->top_file
< 0){
1705 panel
->top_file
+= lines
;
1710 if (panel
->top_file
> panel
->selected
)
1711 panel
->top_file
= panel
->selected
;
1712 if (panel
->top_file
< 0)
1713 panel
->top_file
= 0;
1716 select_item (panel
);
1720 move_left (WPanel
*panel
, int c_code
)
1723 move_selection (panel
, -llines (panel
));
1726 return maybe_cd (1); /* cd .. */
1730 move_right (WPanel
*panel
, int c_code
)
1733 move_selection (panel
, llines (panel
));
1736 return maybe_cd (0); /* cd (selection) */
1740 prev_page (WPanel
*panel
)
1744 if (!panel
->selected
&& !panel
->top_file
)
1746 unselect_item (panel
);
1747 items
= ITEMS (panel
);
1748 if (panel
->top_file
< items
)
1749 items
= panel
->top_file
;
1751 panel
->selected
= 0;
1753 panel
->selected
-= items
;
1754 panel
->top_file
-= items
;
1756 /* This keeps the selection in a reasonable place */
1757 if (panel
->selected
< 0)
1758 panel
->selected
= 0;
1759 if (panel
->top_file
< 0)
1760 panel
->top_file
= 0;
1761 select_item (panel
);
1766 ctrl_prev_page (WPanel
*panel
)
1768 do_cd ("..", cd_exact
);
1772 next_page (WPanel
*panel
)
1776 if (panel
->selected
== panel
->count
- 1)
1778 unselect_item (panel
);
1779 items
= ITEMS (panel
);
1780 if (panel
->top_file
> panel
->count
- 2 * items
)
1781 items
= panel
->count
- items
- panel
->top_file
;
1782 if (panel
->top_file
+ items
< 0)
1783 items
= -panel
->top_file
;
1785 panel
->selected
= panel
->count
- 1;
1787 panel
->selected
+= items
;
1788 panel
->top_file
+= items
;
1790 /* This keeps the selection in it's relative position */
1791 if (panel
->selected
>= panel
->count
)
1792 panel
->selected
= panel
->count
- 1;
1793 if (panel
->top_file
>= panel
->count
)
1794 panel
->top_file
= panel
->count
- 1;
1795 select_item (panel
);
1800 ctrl_next_page (WPanel
*panel
)
1802 if ((S_ISDIR (selection (panel
)->st
.st_mode
)
1803 || link_isdir (selection (panel
)))) {
1804 do_cd (selection (panel
)->fname
, cd_exact
);
1809 goto_top_file (WPanel
*panel
)
1811 unselect_item (panel
);
1812 panel
->selected
= panel
->top_file
;
1813 select_item (panel
);
1817 goto_middle_file (WPanel
*panel
)
1819 unselect_item (panel
);
1820 panel
->selected
= panel
->top_file
+ (ITEMS (panel
)/2);
1821 if (panel
->selected
>= panel
->count
)
1822 panel
->selected
= panel
->count
- 1;
1823 select_item (panel
);
1827 goto_bottom_file (WPanel
*panel
)
1829 unselect_item (panel
);
1830 panel
->selected
= panel
->top_file
+ ITEMS (panel
)-1;
1831 if (panel
->selected
>= panel
->count
)
1832 panel
->selected
= panel
->count
- 1;
1833 select_item (panel
);
1837 move_home (WPanel
*panel
)
1839 if (panel
->selected
== 0)
1841 unselect_item (panel
);
1843 if (torben_fj_mode
){
1844 int middle_pos
= panel
->top_file
+ (ITEMS (panel
)/2);
1846 if (panel
->selected
> middle_pos
){
1847 goto_middle_file (panel
);
1850 if (panel
->selected
!= panel
->top_file
){
1851 goto_top_file (panel
);
1856 panel
->top_file
= 0;
1857 panel
->selected
= 0;
1860 select_item (panel
);
1864 move_end (WPanel
*panel
)
1866 if (panel
->selected
== panel
->count
-1)
1868 unselect_item (panel
);
1869 if (torben_fj_mode
){
1870 int middle_pos
= panel
->top_file
+ (ITEMS (panel
)/2);
1872 if (panel
->selected
< middle_pos
){
1873 goto_middle_file (panel
);
1876 if (panel
->selected
!= (panel
->top_file
+ ITEMS(panel
)-1)){
1877 goto_bottom_file (panel
);
1882 panel
->selected
= panel
->count
-1;
1884 select_item (panel
);
1887 /* Recalculate the panels summary information, used e.g. when marked
1888 files might have been removed by an external command */
1890 recalculate_panel_summary (WPanel
*panel
)
1895 panel
->dirs_marked
= 0;
1898 for (i
= 0; i
< panel
->count
; i
++)
1899 if (panel
->dir
.list
[i
].f
.marked
){
1900 /* do_file_mark will return immediately if newmark == oldmark.
1901 So we have to first unmark it to get panel's summary information
1902 updated. (Norbert) */
1903 panel
->dir
.list
[i
].f
.marked
= 0;
1904 do_file_mark (panel
, i
, 1);
1908 /* This routine marks a file or a directory */
1910 do_file_mark (WPanel
*panel
, int idx
, int mark
)
1912 if (panel
->dir
.list
[idx
].f
.marked
== mark
)
1915 /* Only '..' can't be marked, '.' isn't visible */
1916 if (!strcmp (panel
->dir
.list
[idx
].fname
, ".."))
1919 file_mark (panel
, idx
, mark
);
1920 if (panel
->dir
.list
[idx
].f
.marked
) {
1922 if (S_ISDIR (panel
->dir
.list
[idx
].st
.st_mode
)) {
1923 if (panel
->dir
.list
[idx
].f
.dir_size_computed
)
1924 panel
->total
+= panel
->dir
.list
[idx
].st
.st_size
;
1925 panel
->dirs_marked
++;
1927 panel
->total
+= panel
->dir
.list
[idx
].st
.st_size
;
1930 if (S_ISDIR (panel
->dir
.list
[idx
].st
.st_mode
)) {
1931 if (panel
->dir
.list
[idx
].f
.dir_size_computed
)
1932 panel
->total
-= panel
->dir
.list
[idx
].st
.st_size
;
1933 panel
->dirs_marked
--;
1935 panel
->total
-= panel
->dir
.list
[idx
].st
.st_size
;
1941 do_mark_file (WPanel
*panel
, int do_move
)
1943 do_file_mark (panel
, panel
->selected
,
1944 selection (panel
)->f
.marked
? 0 : 1);
1945 if (mark_moves_down
&& do_move
)
1950 mark_file (WPanel
*panel
)
1952 do_mark_file (panel
, 1);
1955 /* Incremental search of a file name in the panel */
1957 do_search (WPanel
*panel
, int c_code
)
1964 l
= strlen (panel
->search_buffer
);
1965 if (c_code
== KEY_BACKSPACE
) {
1967 panel
->search_buffer
[--l
] = '\0';
1969 if (c_code
&& l
< sizeof (panel
->search_buffer
)) {
1970 panel
->search_buffer
[l
] = c_code
;
1971 panel
->search_buffer
[l
+ 1] = 0;
1977 for (i
= panel
->selected
; !wrapped
|| i
!= panel
->selected
; i
++) {
1978 if (i
>= panel
->count
) {
1986 ? (strncmp (panel
->dir
.list
[i
].fname
, panel
->search_buffer
, l
)
1987 == 0) : (g_strncasecmp (panel
->dir
.list
[i
].fname
,
1988 panel
->search_buffer
, l
) == 0)) {
1989 unselect_item (panel
);
1990 panel
->selected
= i
;
1991 select_item (panel
);
1997 panel
->search_buffer
[--l
] = 0;
1999 paint_panel (panel
);
2003 start_search (WPanel
*panel
)
2005 if (panel
->searching
){
2006 if (panel
->selected
+1 == panel
->count
)
2007 panel
->selected
= 0;
2010 do_search (panel
, 0);
2012 panel
->searching
= 1;
2013 panel
->search_buffer
[0] = 0;
2014 display_mini_info (panel
);
2019 /* Return 1 if the Enter key has been processed, 0 otherwise */
2021 do_enter_on_file_entry (file_entry
*fe
)
2026 * Directory or link to directory - change directory.
2027 * Try the same for the entries on which mc_lstat() has failed.
2029 if (S_ISDIR (fe
->st
.st_mode
) || link_isdir (fe
)
2030 || (fe
->st
.st_mode
== 0)) {
2031 if (!do_cd (fe
->fname
, cd_exact
))
2032 message (D_ERROR
, MSG_ERROR
, _("Cannot change directory"));
2036 /* Try associated command */
2037 if (regex_command (fe
->fname
, "Open", 0) != 0)
2040 /* Check if the file is executable */
2041 full_name
= concat_dir_and_file (current_panel
->cwd
, fe
->fname
);
2042 if (!is_exe (fe
->st
.st_mode
) || !if_link_is_exe (full_name
, fe
)) {
2048 if (confirm_execute
) {
2050 (_(" The Midnight Commander "),
2051 _(" Do you really want to execute? "), D_NORMAL
, 2, _("&Yes"),
2056 if (!vfs_current_is_local ()) {
2060 tmp
= concat_dir_and_file (vfs_get_current_dir (), fe
->fname
);
2061 ret
= mc_setctl (tmp
, VFS_SETCTL_RUN
, NULL
);
2063 /* We took action only if the dialog was shown or the execution
2065 return confirm_execute
|| (ret
== 0);
2070 char *tmp
= name_quote (fe
->fname
, 0);
2071 char *cmd
= g_strconcat (".", PATH_SEP_STR
, tmp
, (char *) NULL
);
2073 shell_execute (cmd
, 0);
2081 do_enter (WPanel
*panel
)
2083 return do_enter_on_file_entry (selection (panel
));
2087 chdir_other_panel (WPanel
*panel
)
2090 char *sel_entry
= NULL
;
2092 if (get_other_type () != view_listing
) {
2093 set_display_type (get_other_index (), view_listing
);
2096 if (!S_ISDIR (panel
->dir
.list
[panel
->selected
].st
.st_mode
)) {
2097 new_dir
= concat_dir_and_file (panel
->cwd
, "..");
2098 sel_entry
= strrchr(panel
->cwd
, PATH_SEP
);
2100 new_dir
= concat_dir_and_file (panel
->cwd
, panel
->dir
.list
[panel
->selected
].fname
);
2103 do_cd (new_dir
, cd_exact
);
2105 try_to_select (current_panel
, sel_entry
);
2114 * Make the current directory of the current panel also the current
2115 * directory of the other panel. Put the other panel to the listing
2116 * mode if needed. If the current panel is panelized, the other panel
2117 * doesn't become panelized.
2120 sync_other_panel (WPanel
*panel
)
2122 if (get_other_type () != view_listing
) {
2123 set_display_type (get_other_index (), view_listing
);
2126 do_panel_cd (other_panel
, current_panel
->cwd
, cd_exact
);
2128 /* try to select current filename on the other panel */
2129 if (!panel
->is_panelized
) {
2130 try_to_select (other_panel
, selection (panel
)->fname
);
2135 chdir_to_readlink (WPanel
*panel
)
2139 if (get_other_type () != view_listing
)
2142 if (S_ISLNK (panel
->dir
.list
[panel
->selected
].st
.st_mode
)) {
2143 char buffer
[MC_MAXPATHLEN
], *p
;
2147 i
= readlink (selection (panel
)->fname
, buffer
, MC_MAXPATHLEN
- 1);
2150 if (mc_stat (selection (panel
)->fname
, &st
) < 0)
2153 if (!S_ISDIR (st
.st_mode
)) {
2154 p
= strrchr (buffer
, PATH_SEP
);
2157 p
= strrchr (buffer
, PATH_SEP
);
2163 if (*buffer
== PATH_SEP
)
2164 new_dir
= g_strdup (buffer
);
2166 new_dir
= concat_dir_and_file (panel
->cwd
, buffer
);
2169 do_cd (new_dir
, cd_exact
);
2178 typedef void (*panel_key_callback
) (WPanel
*);
2181 panel_key_callback fn
;
2184 static void cmd_do_enter(WPanel
*wp
) { (void) do_enter(wp
); }
2185 static void cmd_view_simple(WPanel
*wp
) { view_simple_cmd(); }
2186 static void cmd_edit_new(WPanel
*wp
) { edit_cmd_new(); }
2187 static void cmd_copy_local(WPanel
*wp
) { copy_cmd_local(); }
2188 static void cmd_rename_local(WPanel
*wp
) { ren_cmd_local(); }
2189 static void cmd_delete_local(WPanel
*wp
) { delete_cmd_local(); }
2190 static void cmd_select(WPanel
*wp
) { select_cmd(); }
2191 static void cmd_unselect(WPanel
*wp
) { unselect_cmd(); }
2192 static void cmd_reverse_selection(WPanel
*wp
) { reverse_selection_cmd(); }
2194 static const panel_key_map panel_keymap
[] = {
2195 { KEY_DOWN
, move_down
},
2196 { KEY_UP
, move_up
},
2198 /* The action button :-) */
2199 { '\n', cmd_do_enter
},
2200 { KEY_ENTER
, cmd_do_enter
},
2202 { KEY_IC
, mark_file
},
2203 { KEY_HOME
, move_home
},
2204 { KEY_A1
, move_home
},
2205 { ALT ('<'), move_home
},
2206 { KEY_C1
, move_end
},
2207 { KEY_END
, move_end
},
2208 { ALT ('>'), move_end
},
2209 { KEY_NPAGE
, next_page
},
2210 { KEY_PPAGE
, prev_page
},
2211 { KEY_NPAGE
| KEY_M_CTRL
, ctrl_next_page
},
2212 { KEY_PPAGE
| KEY_M_CTRL
, ctrl_prev_page
},
2214 /* To quickly move in the panel */
2215 { ALT('g'), goto_top_file
},
2216 { ALT('r'), goto_middle_file
}, /* M-r like emacs */
2217 { ALT('j'), goto_bottom_file
},
2219 /* Emacs-like bindings */
2220 { XCTRL('v'), next_page
}, /* C-v like emacs */
2221 { ALT('v'), prev_page
}, /* M-v like emacs */
2222 { XCTRL('p'), move_up
}, /* C-p like emacs */
2223 { XCTRL('n'), move_down
}, /* C-n like emacs */
2224 { XCTRL('s'), start_search
}, /* C-s like emacs */
2225 { ALT('s'), start_search
}, /* M-s not like emacs */
2226 { XCTRL('t'), mark_file
},
2227 { ALT('o'), chdir_other_panel
},
2228 { ALT('i'), sync_other_panel
},
2229 { ALT('l'), chdir_to_readlink
},
2230 { ALT('H'), directory_history_list
},
2231 { KEY_F(13), cmd_view_simple
},
2232 { KEY_F(14), cmd_edit_new
},
2233 { KEY_F(15), cmd_copy_local
},
2234 { KEY_F(16), cmd_rename_local
},
2235 { KEY_F(18), cmd_delete_local
},
2236 { ALT('y'), directory_history_prev
},
2237 { ALT('u'), directory_history_next
},
2238 { ALT('+'), cmd_select
},
2239 { KEY_KP_ADD
, cmd_select
},
2240 { ALT('\\'), cmd_unselect
},
2241 { ALT('-'), cmd_unselect
},
2242 { KEY_KP_SUBTRACT
, cmd_unselect
},
2243 { ALT('*'), cmd_reverse_selection
},
2244 { KEY_KP_MULTIPLY
, cmd_reverse_selection
},
2248 static inline cb_ret_t
2249 panel_key (WPanel
*panel
, int key
)
2253 for (i
= 0; panel_keymap
[i
].key_code
; i
++) {
2254 if (key
== panel_keymap
[i
].key_code
) {
2255 int old_searching
= panel
->searching
;
2257 if (panel_keymap
[i
].fn
!= start_search
)
2258 panel
->searching
= 0;
2260 (*panel_keymap
[i
].fn
) (panel
);
2262 if (panel
->searching
!= old_searching
)
2263 display_mini_info (panel
);
2267 if (torben_fj_mode
&& key
== ALT ('h')) {
2268 goto_middle_file (panel
);
2272 /* We do not want to take a key press if nothing can be done with it */
2273 /* The command line widget may do something more useful */
2274 if (key
== KEY_LEFT
)
2275 return move_left (panel
, key
);
2277 if (key
== KEY_RIGHT
)
2278 return move_right (panel
, key
);
2280 if (is_abort_char (key
)) {
2281 panel
->searching
= 0;
2282 display_mini_info (panel
);
2286 /* Do not eat characters not meant for the panel below ' ' (e.g. C-l). */
2287 if ((key
>= ' ' && key
<= 255) || key
== KEY_BACKSPACE
) {
2288 if (panel
->searching
) {
2289 do_search (panel
, key
);
2293 if (!command_prompt
) {
2294 start_search (panel
);
2295 do_search (panel
, key
);
2300 return MSG_NOT_HANDLED
;
2304 panel_callback (Widget
*w
, widget_msg_t msg
, int parm
)
2306 WPanel
*panel
= (WPanel
*) w
;
2307 Dlg_head
*h
= panel
->widget
.parent
;
2311 paint_panel (panel
);
2315 current_panel
= panel
;
2317 if (mc_chdir (panel
->cwd
) != 0) {
2318 char *cwd
= strip_password (g_strdup (panel
->cwd
), 1);
2319 message (D_ERROR
, MSG_ERROR
, _(" Cannot chdir to \"%s\" \n %s "),
2320 cwd
, unix_error_string (errno
));
2323 subshell_chdir (panel
->cwd
);
2325 update_xterm_title_path ();
2326 select_item (panel
);
2331 buttonbar_set_label (h
, 1, _("Help"), help_cmd
);
2332 buttonbar_set_label (h
, 2, _("Menu"), user_file_menu_cmd
);
2333 buttonbar_set_label (h
, 3, _("View"), view_cmd
);
2334 buttonbar_set_label (h
, 4, _("Edit"), edit_cmd
);
2335 buttonbar_set_label (h
, 5, _("Copy"), copy_cmd
);
2336 buttonbar_set_label (h
, 6, _("RenMov"), ren_cmd
);
2337 buttonbar_set_label (h
, 7, _("Mkdir"), mkdir_cmd
);
2338 buttonbar_set_label (h
, 8, _("Delete"), delete_cmd
);
2339 buttonbar_redraw (h
);
2342 case WIDGET_UNFOCUS
:
2343 /* Janne: look at this for the multiple panel options */
2344 if (panel
->searching
){
2345 panel
->searching
= 0;
2346 display_mini_info (panel
);
2350 unselect_item (panel
);
2354 return panel_key (panel
, parm
);
2356 case WIDGET_DESTROY
:
2357 panel_destroy (panel
);
2361 return default_proc (msg
, parm
);
2366 file_mark (WPanel
*panel
, int index
, int val
)
2368 if (panel
->dir
.list
[index
].f
.marked
!= val
) {
2369 panel
->dir
.list
[index
].f
.marked
= val
;
2375 /* Panel mouse events support routines */
2377 static int mouse_marking
= 0;
2380 mouse_toggle_mark (WPanel
*panel
)
2382 do_mark_file (panel
, 0);
2383 mouse_marking
= selection (panel
)->f
.marked
;
2387 mouse_set_mark (WPanel
*panel
)
2389 if (mouse_marking
&& !(selection (panel
)->f
.marked
))
2390 do_mark_file (panel
, 0);
2391 else if (!mouse_marking
&& (selection (panel
)->f
.marked
))
2392 do_mark_file (panel
, 0);
2396 mark_if_marking (WPanel
*panel
, Gpm_Event
*event
)
2398 if (event
->buttons
& GPM_B_RIGHT
){
2399 if (event
->type
& GPM_DOWN
)
2400 mouse_toggle_mark (panel
);
2402 mouse_set_mark (panel
);
2409 * Mouse callback of the panel minus repainting.
2410 * If the event is redirected to the menu, *redir is set to 1.
2413 do_panel_event (Gpm_Event
*event
, WPanel
*panel
, int *redir
)
2415 const int lines
= llines (panel
);
2419 /* Mouse wheel events */
2420 if ((event
->buttons
& GPM_B_UP
) && (event
->type
& GPM_DOWN
)) {
2424 if ((event
->buttons
& GPM_B_DOWN
) && (event
->type
& GPM_DOWN
)) {
2430 if (event
->type
& GPM_DOWN
&& event
->x
== 2 && event
->y
== 1) {
2431 directory_history_prev (panel
);
2436 if (event
->type
& GPM_DOWN
&& event
->x
== panel
->widget
.cols
- 1
2438 directory_history_next (panel
);
2443 if (event
->type
& GPM_DOWN
&& event
->x
== panel
->widget
.cols
- 2
2445 directory_history_list (panel
);
2449 /* rest of the upper frame, the menu is invisible - call menu */
2450 if (event
->type
& GPM_DOWN
&& event
->y
== 1 && !menubar_visible
) {
2452 event
->x
+= panel
->widget
.x
;
2453 return (*(the_menubar
->widget
.mouse
)) (event
, the_menubar
);
2457 if ((event
->type
& (GPM_DOWN
| GPM_DRAG
))) {
2459 if (!dlg_widget_active (panel
))
2462 if (event
->y
<= 0) {
2463 mark_if_marking (panel
, event
);
2464 if (mouse_move_pages
)
2471 if (!((panel
->top_file
+ event
->y
<= panel
->count
)
2472 && event
->y
<= lines
)) {
2473 mark_if_marking (panel
, event
);
2474 if (mouse_move_pages
)
2480 my_index
= panel
->top_file
+ event
->y
- 1;
2482 if (event
->x
> ((panel
->widget
.cols
- 2) / 2))
2483 my_index
+= llines (panel
);
2486 if (my_index
>= panel
->count
)
2487 my_index
= panel
->count
- 1;
2489 if (my_index
!= panel
->selected
) {
2490 unselect_item (panel
);
2491 panel
->selected
= my_index
;
2492 select_item (panel
);
2495 /* This one is new */
2496 mark_if_marking (panel
, event
);
2498 } else if ((event
->type
& (GPM_UP
| GPM_DOUBLE
)) ==
2499 (GPM_UP
| GPM_DOUBLE
)) {
2500 if (event
->y
> 0 && event
->y
<= lines
)
2506 /* Mouse callback of the panel */
2508 panel_event (Gpm_Event
*event
, void *data
)
2510 WPanel
*panel
= data
;
2514 ret
= do_panel_event (event
, panel
, &redir
);
2516 paint_panel (panel
);
2522 panel_re_sort (WPanel
*panel
)
2530 filename
= g_strdup (selection (panel
)->fname
);
2531 unselect_item (panel
);
2532 do_sort (&panel
->dir
, panel
->sort_type
, panel
->count
-1, panel
->reverse
,
2533 panel
->case_sensitive
, panel
->exec_first
);
2534 panel
->selected
= -1;
2535 for (i
= panel
->count
; i
; i
--){
2536 if (!strcmp (panel
->dir
.list
[i
-1].fname
, filename
)){
2537 panel
->selected
= i
-1;
2542 panel
->top_file
= panel
->selected
- ITEMS (panel
)/2;
2543 if (panel
->top_file
< 0)
2544 panel
->top_file
= 0;
2545 select_item (panel
);
2550 panel_set_sort_order (WPanel
*panel
, sortfn
*sort_order
)
2552 if (sort_order
== 0)
2555 panel
->sort_type
= sort_order
;
2557 /* The directory is already sorted, we have to load the unsorted stuff */
2558 if (sort_order
== (sortfn
*) unsorted
){
2561 current_file
= g_strdup (panel
->dir
.list
[panel
->selected
].fname
);
2562 panel_reload (panel
);
2563 try_to_select (panel
, current_file
);
2564 g_free (current_file
);
2566 panel_re_sort (panel
);