fix for #188
[midnight-commander.git] / src / screen.c
blobb069b17818daf9b0639b63048ea9c57bce01c38b
1 /* Panel managing.
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. */
22 #include <config.h>
24 #include <errno.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <unistd.h>
30 #include "global.h"
31 #include "tty.h"
32 #include "dir.h"
33 #include "panel.h"
34 #include "color.h"
35 #include "tree.h"
36 #include "win.h"
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 (...) */
41 #include "cmd.h"
42 #include "key.h" /* XCTRL and ALT macros */
43 #include "setup.h" /* For loading/saving panel options */
44 #include "user.h"
45 #include "profile.h"
46 #include "execute.h"
47 #include "widget.h"
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]) )
56 #define J_LEFT 1
57 #define J_RIGHT 2
58 #define J_CENTER 3
60 #define IS_FIT(x) ((x) & 0x0004)
61 #define MAKE_FIT(x) ((x) | 0x0004)
62 #define HIDE_FIT(x) ((x) & 0x0003)
64 #define J_LEFT_FIT 5
65 #define J_RIGHT_FIT 6
66 #define J_CENTER_FIT 7
68 #define NORMAL 0
69 #define SELECTED 1
70 #define MARKED 2
71 #define MARKED_SELECTED 3
72 #define STATUS 5
75 * This describes a format item. The parse_display_format routine parses
76 * the user specified format and creates a linked list of format_e structures.
78 typedef struct format_e {
79 struct format_e *next;
80 int requested_field_len;
81 int field_len;
82 int just_mode;
83 int expand;
84 const char *(*string_fn)(file_entry *, int len);
85 const char *title;
86 const char *id;
87 } format_e;
89 /* If true, show the mini-info on the panel */
90 int show_mini_info = 1;
92 /* If true, then use stat() on the cwd to determine directory changes */
93 int fast_reload = 0;
95 /* If true, use some usability hacks by Torben */
96 int torben_fj_mode = 0;
98 /* If true, up/down keys scroll the pane listing by pages */
99 int panel_scroll_pages = 1;
101 /* If 1, we use permission hilighting */
102 int permission_mode = 0;
104 /* If 1 - then add per file type hilighting */
105 int filetype_mode = 1;
107 /* The hook list for the select file function */
108 Hook *select_file_hook = 0;
110 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))
119 static void
120 set_colors (WPanel *panel)
122 standend ();
123 attrset (NORMAL_COLOR);
126 /* Delete format string, it is a linked list */
127 static void
128 delete_format (format_e *format)
130 format_e *next;
132 while (format){
133 next = format->next;
134 g_free (format);
135 format = next;
139 /* This code relies on the default justification!!! */
140 static void
141 add_permission_string (char *dest, int width, file_entry *fe, int attr, int color, int is_octal)
143 int i, r, l;
145 l = get_user_permissions (&fe->st);
147 if (is_octal){
148 /* Place of the access bit in octal mode */
149 l = width + l - 3;
150 r = l + 1;
151 } else {
152 /* The same to the triplet in string mode */
153 l = l * 3 + 1;
154 r = l + 3;
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);
161 else
162 attrset (MARKED_COLOR);
163 } else
164 attrset (color);
166 addch (dest[i]);
170 /* String representations of various file attributes */
171 /* name */
172 static const char *
173 string_file_name (file_entry *fe, int len)
175 static char buffer [MC_MAXPATHLEN + 1];
176 size_t i;
178 for (i = 0; i < sizeof(buffer) - 1; i++) {
179 char c;
181 c = fe->fname[i];
183 if (!c)
184 break;
186 if (!is_printable(c))
187 c = '?';
189 buffer[i] = c;
192 buffer[i] = 0;
193 return buffer;
196 static inline unsigned int ilog10(dev_t n)
198 unsigned int digits = 0;
199 do {
200 digits++, n /= 10;
201 } while (n != 0);
202 return digits;
205 static void format_device_number (char *buf, size_t bufsize, dev_t dev)
207 dev_t major_dev = major(dev);
208 dev_t minor_dev = minor(dev);
209 unsigned int major_digits = ilog10(major_dev);
210 unsigned int minor_digits = ilog10(minor_dev);
212 g_assert(bufsize >= 1);
213 if (major_digits + 1 + minor_digits + 1 <= bufsize) {
214 g_snprintf(buf, bufsize, "%lu,%lu", (unsigned long) major_dev,
215 (unsigned long) minor_dev);
216 } else {
217 g_strlcpy(buf, _("[dev]"), bufsize);
221 /* size */
222 static const char *
223 string_file_size (file_entry *fe, int len)
225 static char buffer [BUF_TINY];
227 /* Don't ever show size of ".." since we don't calculate it */
228 if (!strcmp (fe->fname, "..")) {
229 return _("UP--DIR");
232 #ifdef HAVE_STRUCT_STAT_ST_RDEV
233 if (S_ISBLK (fe->st.st_mode) || S_ISCHR (fe->st.st_mode))
234 format_device_number (buffer, len + 1, fe->st.st_rdev);
235 else
236 #endif
238 size_trunc_len (buffer, len, fe->st.st_size, 0);
240 return buffer;
243 /* bsize */
244 static const char *
245 string_file_size_brief (file_entry *fe, int len)
247 if (S_ISLNK (fe->st.st_mode) && !fe->f.link_to_dir) {
248 return _("SYMLINK");
251 if ((S_ISDIR (fe->st.st_mode) || fe->f.link_to_dir) && strcmp (fe->fname, "..")) {
252 return _("SUB-DIR");
255 return string_file_size (fe, len);
258 /* This functions return a string representation of a file entry */
259 /* type */
260 static const char *
261 string_file_type (file_entry *fe, int len)
263 static char buffer[2];
265 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)
269 buffer[0] = '~';
270 else if (fe->f.stale_link)
271 buffer[0] = '!';
272 else
273 buffer[0] = '@';
274 } else if (S_ISCHR (fe->st.st_mode))
275 buffer[0] = '-';
276 else if (S_ISSOCK (fe->st.st_mode))
277 buffer[0] = '=';
278 else if (S_ISDOOR (fe->st.st_mode))
279 buffer[0] = '>';
280 else if (S_ISBLK (fe->st.st_mode))
281 buffer[0] = '+';
282 else if (S_ISFIFO (fe->st.st_mode))
283 buffer[0] = '|';
284 else if (S_ISNAM (fe->st.st_mode))
285 buffer[0] = '#';
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))
289 buffer[0] = '*';
290 else
291 buffer[0] = ' ';
292 buffer[1] = '\0';
293 return buffer;
296 /* mtime */
297 static const char *
298 string_file_mtime (file_entry *fe, int len)
300 if (!strcmp (fe->fname, "..")) {
301 return "";
303 return file_date (fe->st.st_mtime);
306 /* atime */
307 static const char *
308 string_file_atime (file_entry *fe, int len)
310 if (!strcmp (fe->fname, "..")) {
311 return "";
313 return file_date (fe->st.st_atime);
316 /* ctime */
317 static const char *
318 string_file_ctime (file_entry *fe, int len)
320 if (!strcmp (fe->fname, "..")) {
321 return "";
323 return file_date (fe->st.st_ctime);
326 /* perm */
327 static const char *
328 string_file_permission (file_entry *fe, int len)
330 return string_perm (fe->st.st_mode);
333 /* mode */
334 static const char *
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);
340 return buffer;
343 /* nlink */
344 static const char *
345 string_file_nlinks (file_entry *fe, int len)
347 static char buffer[BUF_TINY];
349 g_snprintf (buffer, sizeof (buffer), "%16d", (int) fe->st.st_nlink);
350 return buffer;
353 /* inode */
354 static const char *
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);
361 return buffer;
364 /* nuid */
365 static const char *
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);
372 return buffer;
375 /* ngid */
376 static const char *
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);
383 return buffer;
386 /* owner */
387 static const char *
388 string_file_owner (file_entry *fe, int len)
390 return get_owner (fe->st.st_uid);
393 /* group */
394 static const char *
395 string_file_group (file_entry *fe, int len)
397 return get_group (fe->st.st_gid);
400 /* mark */
401 static const char *
402 string_marked (file_entry *fe, int len)
404 return fe->f.marked ? "*" : " ";
407 /* space */
408 static const char *
409 string_space (file_entry *fe, int len)
411 return " ";
414 /* dot */
415 static const char *
416 string_dot (file_entry *fe, int len)
418 return ".";
421 #define GT 1
423 static struct {
424 const char *id;
425 int min_size;
426 int expands;
427 int default_just;
428 const char *title;
429 int use_in_gui;
430 const char *(*string_fn)(file_entry *, int);
431 sortfn *sort_routine; /* This field is currently unused. */
432 } formats [] = {
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 },
454 static char *
455 to_buffer (char *dest, int just_mode, int len, const char *txt)
457 int txtlen = strlen (txt);
458 int still, over;
460 /* Fill buffer with spaces */
461 memset (dest, ' ', len);
463 still = (over=(txtlen > len)) ? (txtlen - len) : (len - txtlen);
465 switch (HIDE_FIT(just_mode)){
466 case J_LEFT:
467 still = 0;
468 break;
469 case J_CENTER:
470 still /= 2;
471 break;
472 case J_RIGHT:
473 default:
474 break;
477 if (over){
478 if (IS_FIT(just_mode))
479 strcpy (dest, name_trunc(txt, len));
480 else
481 strncpy (dest, txt+still, len);
482 } else
483 strncpy (dest+still, txt, txtlen);
485 dest[len] = '\0';
487 return (dest + len);
490 static int
491 file_compute_color (int attr, file_entry *fe)
493 switch (attr) {
494 case SELECTED:
495 return (SELECTED_COLOR);
496 case MARKED:
497 return (MARKED_COLOR);
498 case MARKED_SELECTED:
499 return (MARKED_SELECTED_COLOR);
500 case STATUS:
501 return (NORMAL_COLOR);
502 case NORMAL:
503 default:
504 if (!filetype_mode)
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);
516 else
517 return (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")))
536 return (CORE_COLOR);
538 return (NORMAL_COLOR);
541 /* Formats the file number file_index of panel in the buffer dest */
542 static void
543 format_file (char *dest, int limit, WPanel *panel, int file_index, int width, int attr, int isstatus)
545 int color, length, empty_line;
546 const char *txt;
547 char *old_pos;
548 char *cdest = dest;
549 format_e *format, *home;
550 file_entry *fe;
552 length = 0;
553 empty_line = (file_index >= panel->count);
554 home = (isstatus) ? panel->status_format : panel->format;
555 fe = &panel->dir.list [file_index];
557 if (!empty_line)
558 color = file_compute_color (attr, fe);
559 else
560 color = NORMAL_COLOR;
562 for (format = home; format; format = format->next){
564 if (length == width)
565 break;
567 if (format->string_fn){
568 int len;
570 if (empty_line)
571 txt = " ";
572 else
573 txt = (*format->string_fn)(fe, format->field_len);
575 old_pos = cdest;
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);
582 if (len <= 0)
583 break;
584 cdest = to_buffer (cdest, format->just_mode, len, txt);
585 length += len;
587 attrset (color);
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);
593 else
594 addstr (old_pos);
596 } else {
597 if (attr == SELECTED || attr == MARKED_SELECTED)
598 attrset (SELECTED_COLOR);
599 else
600 attrset (NORMAL_COLOR);
601 one_vline ();
602 length++;
606 if (length < width){
607 int still = width - length;
608 while (still--)
609 addch (' ');
613 static void
614 repaint_file (WPanel *panel, int file_index, int mv, int attr, int isstatus)
616 int second_column = 0;
617 int width, offset;
618 char buffer [BUF_MEDIUM];
620 offset = 0;
621 if (!isstatus && panel->split){
623 second_column = (file_index - panel->top_file) / llines (panel);
624 width = (panel->widget.cols - 2)/2 - 1;
626 if (second_column){
627 offset = 1 + width;
628 width = (panel->widget.cols-2) - (panel->widget.cols-2)/2 - 1;
630 } else
631 width = (panel->widget.cols - 2);
633 /* Nothing to paint */
634 if (width <= 0)
635 return;
637 if (mv){
638 if (!isstatus && panel->split){
639 widget_move (&panel->widget,
640 (file_index - panel->top_file) %
641 llines (panel) + 2,
642 (offset + 1));
643 } else
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){
650 if (second_column)
651 addch (' ');
652 else {
653 attrset (NORMAL_COLOR);
654 one_vline ();
659 static void
660 display_mini_info (WPanel *panel)
662 if (!show_mini_info)
663 return;
665 widget_move (&panel->widget, llines (panel)+3, 1);
667 if (panel->searching){
668 attrset (INPUT_COLOR);
669 tty_printf ("/%-*s", panel->widget.cols-3, panel->search_buffer);
670 attrset (NORMAL_COLOR);
671 return;
674 /* Status displays total marked size */
675 if (panel->marked){
676 char buffer[BUF_SMALL], b_bytes[BUF_SMALL];
677 const char *p = " %-*s";
678 int cols = panel->widget.cols-2;
680 attrset (MARKED_COLOR);
681 tty_printf ("%*s", cols, " ");
682 widget_move (&panel->widget, llines (panel)+3, 1);
685 * This is a trick to use two ngettext() calls in one sentence.
686 * First make "N bytes", then insert it into "X in M files".
688 g_snprintf(b_bytes, sizeof (b_bytes),
689 ngettext("%s byte", "%s bytes",
690 (unsigned long)panel->total),
691 size_trunc_sep(panel->total));
692 g_snprintf(buffer, sizeof (buffer),
693 ngettext("%s in %d file", "%s in %d files", panel->marked),
694 b_bytes, panel->marked);
696 if ((int) strlen (buffer) > cols-2){
697 buffer [cols] = 0;
698 p += 2;
699 } else
700 cols -= 2;
701 tty_printf (p, cols, buffer);
702 return;
705 /* Status resolves links and show them */
706 set_colors (panel);
708 if (S_ISLNK (panel->dir.list [panel->selected].st.st_mode)){
709 char *link, link_target [MC_MAXPATHLEN];
710 int len;
712 link = concat_dir_and_file (panel->cwd, panel->dir.list [panel->selected].fname);
713 len = mc_readlink (link, link_target, MC_MAXPATHLEN - 1);
714 g_free (link);
715 if (len > 0){
716 link_target[len] = 0;
717 tty_printf ("-> %-*s", panel->widget.cols - 5,
718 name_trunc (link_target, panel->widget.cols - 5));
719 } else
720 tty_printf ("%-*s", panel->widget.cols - 2, _("<readlink failed>"));
721 return;
724 /* Default behavior */
725 repaint_file (panel, panel->selected, 0, STATUS, 1);
726 return;
729 static void
730 paint_dir (WPanel *panel)
732 int i;
733 int color; /* Color value of the line */
734 int items; /* Number of items */
736 items = llines (panel) * (panel->split ? 2 : 1);
738 for (i = 0; i < items; i++){
739 if (i+panel->top_file >= panel->count)
740 color = 0;
741 else {
742 color = 2 * (panel->dir.list [i+panel->top_file].f.marked);
743 color += (panel->selected==i+panel->top_file && panel->active);
745 repaint_file (panel, i+panel->top_file, 1, color, 0);
747 standend ();
750 static void
751 mini_info_separator (WPanel *panel)
753 if (!show_mini_info)
754 return;
756 standend ();
757 widget_move (&panel->widget, llines (panel) + 2, 1);
758 #ifdef HAVE_SLANG
759 attrset (NORMAL_COLOR);
760 hline (ACS_HLINE, panel->widget.cols - 2);
761 #else
762 hline ((slow_terminal ? '-' : ACS_HLINE) | NORMAL_COLOR,
763 panel->widget.cols - 2);
764 #endif /* !HAVE_SLANG */
767 static void
768 show_free_space (WPanel *panel)
770 /* Used to figure out how many free space we have */
771 static struct my_statfs myfs_stats;
772 /* Old current working directory for displaying free space */
773 static char *old_cwd = NULL;
775 /* Don't try to stat non-local fs */
776 if (!vfs_file_is_local (panel->cwd) || !free_space)
777 return;
779 if (old_cwd == NULL || strcmp (old_cwd, panel->cwd) != 0) {
780 char rpath[PATH_MAX];
782 init_my_statfs ();
783 mhl_mem_free (old_cwd);
784 old_cwd = mhl_str_dup (panel->cwd);
786 if (mc_realpath (panel->cwd, rpath) == NULL)
787 return;
789 my_statfs (&myfs_stats, rpath);
792 if (myfs_stats.avail > 0 || myfs_stats.total > 0) {
793 char buffer1 [6], buffer2[6], tmp[256];
794 size_trunc_len (buffer1, 5, myfs_stats.avail, 1);
795 size_trunc_len (buffer2, 5, myfs_stats.total, 1);
796 snprintf (tmp, sizeof(tmp), " %s/%s (%d%%) ", buffer1, buffer2,
797 myfs_stats.total > 0 ?
798 (int)(100 * (double)myfs_stats.avail / myfs_stats.total) : 0);
799 widget_move (&panel->widget, panel->widget.lines - 1,
800 panel->widget.cols - 2 - strlen(tmp));
801 addstr (tmp);
805 static void
806 show_dir (WPanel *panel)
808 char *tmp;
810 set_colors (panel);
811 draw_double_box (panel->widget.parent,
812 panel->widget.y, panel->widget.x,
813 panel->widget.lines, panel->widget.cols);
815 #ifdef HAVE_SLANG
816 if (show_mini_info) {
817 SLsmg_draw_object (panel->widget.y + llines (panel) + 2,
818 panel->widget.x, SLSMG_LTEE_CHAR);
819 SLsmg_draw_object (panel->widget.y + llines (panel) + 2,
820 panel->widget.x + panel->widget.cols - 1,
821 SLSMG_RTEE_CHAR);
823 #endif /* HAVE_SLANG */
825 if (panel->active)
826 attrset (REVERSE_COLOR);
828 widget_move (&panel->widget, 0, 3);
829 addch (' ');
831 tmp = g_malloc (panel->widget.cols + 1);
832 tmp[panel->widget.cols] = '\0';
834 trim (strip_home_and_password (panel->cwd), tmp,
835 max (panel->widget.cols - 9, 0));
836 addstr (tmp);
837 g_free (tmp);
839 addch (' ');
840 widget_move (&panel->widget, 0, 1);
841 addstr ("<");
842 widget_move (&panel->widget, 0, panel->widget.cols - 2);
843 addstr (">");
844 widget_move (&panel->widget, 0, panel->widget.cols - 3);
845 addstr ("v");
847 attrset (NORMAL_COLOR);
849 show_free_space (panel);
851 if (panel->active)
852 standend ();
855 /* To be used only by long_frame and full_frame to adjust top_file */
856 static void
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);
868 * Repaint everything that can change on the panel - title, entries and
869 * mini status. The rest of the frame and the mini status separator are
870 * not repainted.
872 static void
873 panel_update_contents (WPanel *panel)
875 show_dir (panel);
876 paint_dir (panel);
877 display_mini_info (panel);
878 panel->dirty = 0;
881 /* Repaint everything, including frame and separator */
882 static void
883 paint_panel (WPanel *panel)
885 paint_frame (panel);
886 panel_update_contents (panel);
887 mini_info_separator (panel);
891 * Repaint the contents of the panels without frames. To schedule panel
892 * for repainting, set panel->dirty to 1. There are many reasons why
893 * the panels need to be repainted, and this is a costly operation, so
894 * it's done once per event.
896 void
897 update_dirty_panels (void)
899 if (current_panel->dirty)
900 panel_update_contents (current_panel);
902 if ((get_other_type () == view_listing) && other_panel->dirty)
903 panel_update_contents (other_panel);
906 static void
907 do_select (WPanel *panel, int i)
909 if (i != panel->selected) {
910 panel->dirty = 1;
911 panel->selected = i;
912 panel->top_file = panel->selected - (panel->widget.lines - 2) / 2;
913 if (panel->top_file < 0)
914 panel->top_file = 0;
918 static inline void
919 do_try_to_select (WPanel *panel, const char *name)
921 int i;
922 char *subdir;
924 if (!name) {
925 do_select(panel, 0);
926 return;
929 /* We only want the last component of the directory,
930 * and from this only the name without suffix. */
931 subdir = vfs_strip_suffix_from_filename (x_basename(name));
933 /* Search that subdirectory, if found select it */
934 for (i = 0; i < panel->count; i++){
935 if (strcmp (subdir, panel->dir.list [i].fname) == 0) {
936 do_select (panel, i);
937 g_free (subdir);
938 return;
942 /* Try to select a file near the file that is missing */
943 if (panel->selected >= panel->count)
944 do_select (panel, panel->count-1);
945 g_free (subdir);
948 void
949 try_to_select (WPanel *panel, const char *name)
951 do_try_to_select (panel, name);
952 select_item (panel);
955 void
956 panel_update_cols (Widget *widget, int frame_size)
958 int cols, origin;
960 if (horizontal_split){
961 widget->cols = COLS;
962 return;
965 if (frame_size == frame_full){
966 cols = COLS;
967 origin = 0;
968 } else {
969 if (widget == get_panel_widget (0)){
970 cols = first_panel_size;
971 origin = 0;
972 } else {
973 cols = COLS-first_panel_size;
974 origin = first_panel_size;
978 widget->cols = cols;
979 widget->x = origin;
982 static char *
983 panel_save_name (WPanel *panel)
985 extern int saving_setup;
987 /* If the program is shuting down */
988 if ((midnight_shutdown && auto_save_setup) || saving_setup)
989 return g_strdup (panel->panel_name);
990 else
991 return g_strconcat ("Temporal:", panel->panel_name, (char *) NULL);
994 static void
995 panel_destroy (WPanel *p)
997 int i;
999 char *name = panel_save_name (p);
1001 panel_save_setup (p, name);
1002 panel_clean_dir (p);
1004 /* save and clean history */
1005 if (p->dir_history) {
1006 history_put (p->hist_name, p->dir_history);
1008 p->dir_history = g_list_first (p->dir_history);
1009 g_list_foreach (p->dir_history, (GFunc) g_free, NULL);
1010 g_list_free (p->dir_history);
1013 g_free (p->hist_name);
1015 delete_format (p->format);
1016 delete_format (p->status_format);
1018 g_free (p->user_format);
1019 for (i = 0; i < LIST_TYPES; i++)
1020 g_free (p->user_status_format[i]);
1021 g_free (p->dir.list);
1022 g_free (p->panel_name);
1023 g_free (name);
1026 static void
1027 panel_format_modified (WPanel *panel)
1029 panel->format_modified = 1;
1032 /* Panel creation */
1033 /* The parameter specifies the name of the panel for setup retieving */
1034 WPanel *
1035 panel_new (const char *panel_name)
1037 WPanel *panel;
1038 char *section;
1039 int i, err;
1041 panel = g_new0 (WPanel, 1);
1043 /* No know sizes of the panel at startup */
1044 init_widget (&panel->widget, 0, 0, 0, 0, panel_callback, panel_event);
1046 /* We do not want the cursor */
1047 widget_want_cursor (panel->widget, 0);
1049 mc_get_current_wd (panel->cwd, sizeof (panel->cwd) - 2);
1050 strcpy (panel->lwd, ".");
1052 panel->hist_name = g_strconcat ("Dir Hist ", panel_name, (char *) NULL);
1053 panel->dir_history = history_get (panel->hist_name);
1054 directory_history_add (panel, panel->cwd);
1056 panel->dir.list = g_new (file_entry, MIN_FILES);
1057 panel->dir.size = MIN_FILES;
1058 panel->active = 0;
1059 panel->filter = 0;
1060 panel->split = 0;
1061 panel->top_file = 0;
1062 panel->selected = 0;
1063 panel->marked = 0;
1064 panel->total = 0;
1065 panel->reverse = 0;
1066 panel->dirty = 1;
1067 panel->searching = 0;
1068 panel->dirs_marked = 0;
1069 panel->is_panelized = 0;
1070 panel->format = 0;
1071 panel->status_format = 0;
1072 panel->format_modified = 1;
1074 panel->panel_name = g_strdup (panel_name);
1075 panel->user_format = g_strdup (DEFAULT_USER_FORMAT);
1077 for (i = 0; i < LIST_TYPES; i++)
1078 panel->user_status_format[i] = g_strdup (DEFAULT_USER_FORMAT);
1080 panel->search_buffer[0] = 0;
1081 panel->frame_size = frame_half;
1082 section = g_strconcat ("Temporal:", panel->panel_name, (char *) NULL);
1083 if (!profile_has_section (section, profile_name)) {
1084 g_free (section);
1085 section = g_strdup (panel->panel_name);
1087 panel_load_setup (panel, section);
1088 g_free (section);
1090 /* Load format strings */
1091 err = set_panel_formats (panel);
1092 if (err) {
1093 set_panel_formats (panel);
1096 /* Load the default format */
1097 panel->count =
1098 do_load_dir (panel->cwd, &panel->dir, panel->sort_type,
1099 panel->reverse, panel->case_sensitive,
1100 panel->exec_first, panel->filter);
1101 return panel;
1104 void
1105 panel_reload (WPanel *panel)
1107 struct stat current_stat;
1109 if (fast_reload && !stat (panel->cwd, &current_stat)
1110 && current_stat.st_ctime == panel->dir_stat.st_ctime
1111 && current_stat.st_mtime == panel->dir_stat.st_mtime)
1112 return;
1114 while (mc_chdir (panel->cwd) == -1) {
1115 char *last_slash;
1117 if (panel->cwd[0] == PATH_SEP && panel->cwd[1] == 0) {
1118 panel_clean_dir (panel);
1119 panel->count = set_zero_dir (&panel->dir);
1120 return;
1122 last_slash = strrchr (panel->cwd, PATH_SEP);
1123 if (!last_slash || last_slash == panel->cwd)
1124 strcpy (panel->cwd, PATH_SEP_STR);
1125 else
1126 *last_slash = 0;
1127 memset (&(panel->dir_stat), 0, sizeof (panel->dir_stat));
1128 show_dir (panel);
1131 panel->count =
1132 do_reload_dir (panel->cwd, &panel->dir, panel->sort_type,
1133 panel->count, panel->reverse, panel->case_sensitive,
1134 panel->exec_first, panel->filter);
1136 panel->dirty = 1;
1137 if (panel->selected >= panel->count)
1138 do_select (panel, panel->count - 1);
1140 recalculate_panel_summary (panel);
1143 static void
1144 paint_frame (WPanel *panel)
1146 int header_len;
1147 int spaces, extra;
1148 int side, width;
1150 const char *txt;
1151 if (!panel->split)
1152 adjust_top_file (panel);
1154 widget_erase (&panel->widget);
1155 show_dir (panel);
1157 widget_move (&panel->widget, 1, 1);
1159 for (side = 0; side <= panel->split; side++){
1160 format_e *format;
1162 if (side){
1163 attrset (NORMAL_COLOR);
1164 one_vline ();
1165 width = panel->widget.cols - panel->widget.cols/2 - 1;
1166 } else if (panel->split)
1167 width = panel->widget.cols/2 - 3;
1168 else
1169 width = panel->widget.cols - 2;
1171 for (format = panel->format; format; format = format->next){
1172 if (format->string_fn){
1173 txt = format->title;
1175 header_len = strlen (txt);
1176 if (header_len > format->field_len)
1177 header_len = format->field_len;
1179 attrset (MARKED_COLOR);
1180 spaces = (format->field_len - header_len) / 2;
1181 extra = (format->field_len - header_len) % 2;
1182 tty_printf ("%*s%.*s%*s", spaces, "",
1183 header_len, txt, spaces+extra, "");
1184 width -= 2 * spaces + extra + header_len;
1185 } else {
1186 attrset (NORMAL_COLOR);
1187 one_vline ();
1188 width --;
1189 continue;
1193 if (width > 0)
1194 tty_printf ("%*s", width, "");
1198 static const char *
1199 parse_panel_size (WPanel *panel, const char *format, int isstatus)
1201 int frame = frame_half;
1202 format = skip_separators (format);
1204 if (!strncmp (format, "full", 4)){
1205 frame = frame_full;
1206 format += 4;
1207 } else if (!strncmp (format, "half", 4)){
1208 frame = frame_half;
1209 format += 4;
1212 if (!isstatus){
1213 panel->frame_size = frame;
1214 panel->split = 0;
1217 /* Now, the optional column specifier */
1218 format = skip_separators (format);
1220 if (*format == '1' || *format == '2'){
1221 if (!isstatus)
1222 panel->split = *format == '2';
1223 format++;
1226 if (!isstatus)
1227 panel_update_cols (&(panel->widget), panel->frame_size);
1229 return skip_separators (format);
1232 /* Format is:
1234 all := panel_format? format
1235 panel_format := [full|half] [1|2]
1236 format := one_format_e
1237 | format , one_format_e
1239 one_format_e := just format.id [opt_size]
1240 just := [<=>]
1241 opt_size := : size [opt_expand]
1242 size := [0-9]+
1243 opt_expand := +
1247 static format_e *
1248 parse_display_format (WPanel *panel, const char *format, char **error, int isstatus, int *res_total_cols)
1250 format_e *darr, *old = 0, *home = 0; /* The formats we return */
1251 int total_cols = 0; /* Used columns by the format */
1252 int set_justify; /* flag: set justification mode? */
1253 int justify = 0; /* Which mode. */
1254 int items = 0; /* Number of items in the format */
1255 size_t i;
1257 static size_t i18n_timelength = 0; /* flag: check ?Time length at startup */
1259 *error = 0;
1261 if (i18n_timelength == 0) {
1262 i18n_timelength = i18n_checktimelength (); /* Musn't be 0 */
1264 for (i = 0; i < ELEMENTS(formats); i++)
1265 if (strcmp ("time", formats [i].id+1) == 0)
1266 formats [i].min_size = i18n_timelength;
1270 * This makes sure that the panel and mini status full/half mode
1271 * setting is equal
1273 format = parse_panel_size (panel, format, isstatus);
1275 while (*format){ /* format can be an empty string */
1276 int found = 0;
1278 darr = g_new (format_e, 1);
1280 /* I'm so ugly, don't look at me :-) */
1281 if (!home)
1282 home = old = darr;
1284 old->next = darr;
1285 darr->next = 0;
1286 old = darr;
1288 format = skip_separators (format);
1290 if (strchr ("<=>", *format)){
1291 set_justify = 1;
1292 switch (*format)
1294 case '<':
1295 justify = J_LEFT;
1296 break;
1297 case '=':
1298 justify = J_CENTER;
1299 break;
1300 case '>':
1301 default:
1302 justify = J_RIGHT;
1303 break;
1305 format = skip_separators (format+1);
1306 } else
1307 set_justify = 0;
1309 for (i = 0; i < ELEMENTS(formats); i++){
1310 int klen = strlen (formats [i].id);
1312 if (strncmp (format, formats [i].id, klen) != 0)
1313 continue;
1315 format += klen;
1317 if (formats [i].use_in_gui)
1318 items++;
1320 darr->requested_field_len = formats [i].min_size;
1321 darr->string_fn = formats [i].string_fn;
1322 if (formats [i].title [0])
1323 darr->title = _(formats [i].title);
1324 else
1325 darr->title = "";
1326 darr->id = formats [i].id;
1327 darr->expand = formats [i].expands;
1328 darr->just_mode = formats [i].default_just;
1330 if (set_justify) {
1331 if (IS_FIT(darr->just_mode))
1332 darr->just_mode = MAKE_FIT(justify);
1333 else
1334 darr->just_mode = justify;
1336 found = 1;
1338 format = skip_separators (format);
1340 /* If we have a size specifier */
1341 if (*format == ':'){
1342 int req_length;
1344 /* If the size was specified, we don't want
1345 * auto-expansion by default
1347 darr->expand = 0;
1348 format++;
1349 req_length = atoi (format);
1350 darr->requested_field_len = req_length;
1352 format = skip_numbers (format);
1354 /* Now, if they insist on expansion */
1355 if (*format == '+'){
1356 darr->expand = 1;
1357 format++;
1362 break;
1364 if (!found){
1365 char *tmp_format = g_strdup (format);
1367 int pos = min (8, strlen (format));
1368 delete_format (home);
1369 tmp_format [pos] = 0;
1370 *error = g_strconcat (_("Unknown tag on display format: "), tmp_format, (char *) NULL);
1371 g_free (tmp_format);
1372 return 0;
1374 total_cols += darr->requested_field_len;
1377 *res_total_cols = total_cols;
1378 return home;
1381 static format_e *
1382 use_display_format (WPanel *panel, const char *format, char **error, int isstatus)
1384 #define MAX_EXPAND 4
1385 int expand_top = 0; /* Max used element in expand */
1386 int usable_columns; /* Usable columns in the panel */
1387 int total_cols = 0;
1388 int i;
1389 format_e *darr, *home;
1391 if (!format)
1392 format = DEFAULT_USER_FORMAT;
1394 home = parse_display_format (panel, format, error, isstatus, &total_cols);
1396 if (*error)
1397 return 0;
1399 panel->dirty = 1;
1401 /* Status needn't to be split */
1402 usable_columns = ((panel->widget.cols-2)/((isstatus)
1404 : (panel->split+1))) - (!isstatus && panel->split);
1406 /* Look for the expandable fields and set field_len based on the requested field len */
1407 for (darr = home; darr && expand_top < MAX_EXPAND; darr = darr->next){
1408 darr->field_len = darr->requested_field_len;
1409 if (darr->expand)
1410 expand_top++;
1413 /* If we used more columns than the available columns, adjust that */
1414 if (total_cols > usable_columns){
1415 int pdif, dif = total_cols - usable_columns;
1417 while (dif){
1418 pdif = dif;
1419 for (darr = home; darr; darr = darr->next){
1420 if (dif && darr->field_len - 1){
1421 darr->field_len--;
1422 dif--;
1426 /* avoid endless loop if num fields > 40 */
1427 if (pdif == dif)
1428 break;
1430 total_cols = usable_columns; /* give up, the rest should be truncated */
1433 /* Expand the available space */
1434 if ((usable_columns > total_cols) && expand_top){
1435 int spaces = (usable_columns - total_cols) / expand_top;
1436 int extra = (usable_columns - total_cols) % expand_top;
1438 for (i = 0, darr = home; darr && (i < expand_top); darr = darr->next)
1439 if (darr->expand){
1440 darr->field_len += (spaces + ((i == 0) ? extra : 0));
1441 i++;
1444 return home;
1447 /* Switches the panel to the mode specified in the format */
1448 /* Seting up both format and status string. Return: 0 - on success; */
1449 /* 1 - format error; 2 - status error; 3 - errors in both formats. */
1451 set_panel_formats (WPanel *p)
1453 format_e *form;
1454 char *err;
1455 int retcode = 0;
1457 form = use_display_format (p, panel_format (p), &err, 0);
1459 if (err){
1460 g_free (err);
1461 retcode = 1;
1463 else {
1464 if (p->format)
1465 delete_format (p->format);
1467 p->format = form;
1470 if (show_mini_info){
1472 form = use_display_format (p, mini_status_format (p), &err, 1);
1474 if (err){
1475 g_free (err);
1476 retcode += 2;
1478 else {
1479 if (p->status_format)
1480 delete_format (p->status_format);
1482 p->status_format = form;
1486 panel_format_modified (p);
1487 panel_update_cols (&(p->widget), p->frame_size);
1489 if (retcode)
1490 message( 1, _("Warning" ), _( "User supplied format looks invalid, reverting to default." ) );
1491 if (retcode & 0x01){
1492 g_free (p->user_format);
1493 p->user_format = g_strdup (DEFAULT_USER_FORMAT);
1495 if (retcode & 0x02){
1496 g_free (p->user_status_format [p->list_type]);
1497 p->user_status_format [p->list_type] = g_strdup (DEFAULT_USER_FORMAT);
1500 return retcode;
1503 /* Given the panel->view_type returns the format string to be parsed */
1504 static const char *
1505 panel_format (WPanel *panel)
1507 switch (panel->list_type){
1509 case list_long:
1510 return "full perm space nlink space owner space group space size space mtime space name";
1512 case list_brief:
1513 return "half 2 type name";
1515 case list_user:
1516 return panel->user_format;
1518 default:
1519 case list_full:
1520 return "half type name | size | mtime";
1524 static const char *
1525 mini_status_format (WPanel *panel)
1527 if (panel->user_mini_status)
1528 return panel->user_status_format [panel->list_type];
1530 switch (panel->list_type){
1532 case list_long:
1533 return "full perm space nlink space owner space group space size space mtime space name";
1535 case list_brief:
1536 return "half type name space bsize space perm space";
1538 case list_full:
1539 return "half type name";
1541 default:
1542 case list_user:
1543 return panel->user_format;
1547 /* */
1548 /* Panel operation commands */
1549 /* */
1551 /* Returns the number of items in the given panel */
1552 static int
1553 ITEMS (WPanel *p)
1555 if (p->split)
1556 return llines (p) * 2;
1557 else
1558 return llines (p);
1561 /* Select current item and readjust the panel */
1562 void
1563 select_item (WPanel *panel)
1565 int items = ITEMS (panel);
1567 /* Although currently all over the code we set the selection and
1568 top file to decent values before calling select_item, I could
1569 forget it someday, so it's better to do the actual fitting here */
1571 if (panel->top_file < 0)
1572 panel->top_file = 0;
1574 if (panel->selected < 0)
1575 panel->selected = 0;
1577 if (panel->selected > panel->count - 1)
1578 panel->selected = panel->count - 1;
1580 if (panel->top_file > panel->count - 1)
1581 panel->top_file = panel->count - 1;
1583 if ((panel->count - panel->top_file) < items) {
1584 panel->top_file = panel->count - items;
1585 if (panel->top_file < 0)
1586 panel->top_file = 0;
1589 if (panel->selected < panel->top_file)
1590 panel->top_file = panel->selected;
1592 if ((panel->selected - panel->top_file) >= items)
1593 panel->top_file = panel->selected - items + 1;
1595 panel->dirty = 1;
1597 execute_hooks (select_file_hook);
1600 /* Clears all files in the panel, used only when one file was marked */
1601 void
1602 unmark_files (WPanel *panel)
1604 int i;
1606 if (!panel->marked)
1607 return;
1608 for (i = 0; i < panel->count; i++)
1609 file_mark (panel, i, 0);
1611 panel->dirs_marked = 0;
1612 panel->marked = 0;
1613 panel->total = 0;
1616 static void
1617 unselect_item (WPanel *panel)
1619 repaint_file (panel, panel->selected, 1, 2*selection (panel)->f.marked, 0);
1622 static void
1623 move_down (WPanel *panel)
1625 if (panel->selected+1 == panel->count)
1626 return;
1628 unselect_item (panel);
1629 panel->selected++;
1631 if (panel->selected - panel->top_file == ITEMS (panel) &&
1632 panel_scroll_pages){
1633 /* Scroll window half screen */
1634 panel->top_file += ITEMS (panel)/2;
1635 if (panel->top_file > panel->count - ITEMS (panel))
1636 panel->top_file = panel->count - ITEMS (panel);
1637 paint_dir (panel);
1638 select_item (panel);
1640 select_item (panel);
1643 static void
1644 move_up (WPanel *panel)
1646 if (panel->selected == 0)
1647 return;
1649 unselect_item (panel);
1650 panel->selected--;
1651 if (panel->selected < panel->top_file && panel_scroll_pages){
1652 /* Scroll window half screen */
1653 panel->top_file -= ITEMS (panel)/2;
1654 if (panel->top_file < 0) panel->top_file = 0;
1655 paint_dir (panel);
1657 select_item (panel);
1660 /* Changes the selection by lines (may be negative) */
1661 static void
1662 move_selection (WPanel *panel, int lines)
1664 int new_pos;
1665 int adjust = 0;
1667 new_pos = panel->selected + lines;
1668 if (new_pos >= panel->count)
1669 new_pos = panel->count-1;
1671 if (new_pos < 0)
1672 new_pos = 0;
1674 unselect_item (panel);
1675 panel->selected = new_pos;
1677 if (panel->selected - panel->top_file >= ITEMS (panel)){
1678 panel->top_file += lines;
1679 adjust = 1;
1682 if (panel->selected - panel->top_file < 0){
1683 panel->top_file += lines;
1684 adjust = 1;
1687 if (adjust){
1688 if (panel->top_file > panel->selected)
1689 panel->top_file = panel->selected;
1690 if (panel->top_file < 0)
1691 panel->top_file = 0;
1692 paint_dir (panel);
1694 select_item (panel);
1697 static cb_ret_t
1698 move_left (WPanel *panel, int c_code)
1700 if (panel->split) {
1701 move_selection (panel, -llines (panel));
1702 return MSG_HANDLED;
1703 } else
1704 return maybe_cd (1); /* cd .. */
1707 static int
1708 move_right (WPanel *panel, int c_code)
1710 if (panel->split) {
1711 move_selection (panel, llines (panel));
1712 return MSG_HANDLED;
1713 } else
1714 return maybe_cd (0); /* cd (selection) */
1717 static void
1718 prev_page (WPanel *panel)
1720 int items;
1722 if (!panel->selected && !panel->top_file)
1723 return;
1724 unselect_item (panel);
1725 items = ITEMS (panel);
1726 if (panel->top_file < items)
1727 items = panel->top_file;
1728 if (!items)
1729 panel->selected = 0;
1730 else
1731 panel->selected -= items;
1732 panel->top_file -= items;
1734 /* This keeps the selection in a reasonable place */
1735 if (panel->selected < 0)
1736 panel->selected = 0;
1737 if (panel->top_file < 0)
1738 panel->top_file = 0;
1739 select_item (panel);
1740 paint_dir (panel);
1743 static void
1744 ctrl_prev_page (WPanel *panel)
1746 do_cd ("..", cd_exact);
1749 static void
1750 next_page (WPanel *panel)
1752 int items;
1754 if (panel->selected == panel->count - 1)
1755 return;
1756 unselect_item (panel);
1757 items = ITEMS (panel);
1758 if (panel->top_file > panel->count - 2 * items)
1759 items = panel->count - items - panel->top_file;
1760 if (panel->top_file + items < 0)
1761 items = -panel->top_file;
1762 if (!items)
1763 panel->selected = panel->count - 1;
1764 else
1765 panel->selected += items;
1766 panel->top_file += items;
1768 /* This keeps the selection in it's relative position */
1769 if (panel->selected >= panel->count)
1770 panel->selected = panel->count - 1;
1771 if (panel->top_file >= panel->count)
1772 panel->top_file = panel->count - 1;
1773 select_item (panel);
1774 paint_dir (panel);
1777 static void
1778 ctrl_next_page (WPanel *panel)
1780 if ((S_ISDIR (selection (panel)->st.st_mode)
1781 || link_isdir (selection (panel)))) {
1782 do_cd (selection (panel)->fname, cd_exact);
1786 static void
1787 goto_top_file (WPanel *panel)
1789 unselect_item (panel);
1790 panel->selected = panel->top_file;
1791 select_item (panel);
1794 static void
1795 goto_middle_file (WPanel *panel)
1797 unselect_item (panel);
1798 panel->selected = panel->top_file + (ITEMS (panel)/2);
1799 if (panel->selected >= panel->count)
1800 panel->selected = panel->count - 1;
1801 select_item (panel);
1804 static void
1805 goto_bottom_file (WPanel *panel)
1807 unselect_item (panel);
1808 panel->selected = panel->top_file + ITEMS (panel)-1;
1809 if (panel->selected >= panel->count)
1810 panel->selected = panel->count - 1;
1811 select_item (panel);
1814 static void
1815 move_home (WPanel *panel)
1817 if (panel->selected == 0)
1818 return;
1819 unselect_item (panel);
1821 if (torben_fj_mode){
1822 int middle_pos = panel->top_file + (ITEMS (panel)/2);
1824 if (panel->selected > middle_pos){
1825 goto_middle_file (panel);
1826 return;
1828 if (panel->selected != panel->top_file){
1829 goto_top_file (panel);
1830 return;
1834 panel->top_file = 0;
1835 panel->selected = 0;
1837 paint_dir (panel);
1838 select_item (panel);
1841 static void
1842 move_end (WPanel *panel)
1844 if (panel->selected == panel->count-1)
1845 return;
1846 unselect_item (panel);
1847 if (torben_fj_mode){
1848 int middle_pos = panel->top_file + (ITEMS (panel)/2);
1850 if (panel->selected < middle_pos){
1851 goto_middle_file (panel);
1852 return;
1854 if (panel->selected != (panel->top_file + ITEMS(panel)-1)){
1855 goto_bottom_file (panel);
1856 return;
1860 panel->selected = panel->count-1;
1861 paint_dir (panel);
1862 select_item (panel);
1865 /* Recalculate the panels summary information, used e.g. when marked
1866 files might have been removed by an external command */
1867 void
1868 recalculate_panel_summary (WPanel *panel)
1870 int i;
1872 panel->marked = 0;
1873 panel->dirs_marked = 0;
1874 panel->total = 0;
1876 for (i = 0; i < panel->count; i++)
1877 if (panel->dir.list [i].f.marked){
1878 /* do_file_mark will return immediately if newmark == oldmark.
1879 So we have to first unmark it to get panel's summary information
1880 updated. (Norbert) */
1881 panel->dir.list [i].f.marked = 0;
1882 do_file_mark (panel, i, 1);
1886 /* This routine marks a file or a directory */
1887 void
1888 do_file_mark (WPanel *panel, int idx, int mark)
1890 if (panel->dir.list[idx].f.marked == mark)
1891 return;
1893 /* Only '..' can't be marked, '.' isn't visible */
1894 if (!strcmp (panel->dir.list[idx].fname, ".."))
1895 return;
1897 file_mark (panel, idx, mark);
1898 if (panel->dir.list[idx].f.marked) {
1899 panel->marked++;
1900 if (S_ISDIR (panel->dir.list[idx].st.st_mode)) {
1901 if (panel->dir.list[idx].f.dir_size_computed)
1902 panel->total += panel->dir.list[idx].st.st_size;
1903 panel->dirs_marked++;
1904 } else
1905 panel->total += panel->dir.list[idx].st.st_size;
1906 set_colors (panel);
1907 } else {
1908 if (S_ISDIR (panel->dir.list[idx].st.st_mode)) {
1909 if (panel->dir.list[idx].f.dir_size_computed)
1910 panel->total -= panel->dir.list[idx].st.st_size;
1911 panel->dirs_marked--;
1912 } else
1913 panel->total -= panel->dir.list[idx].st.st_size;
1914 panel->marked--;
1918 static void
1919 do_mark_file (WPanel *panel, int do_move)
1921 do_file_mark (panel, panel->selected,
1922 selection (panel)->f.marked ? 0 : 1);
1923 if (mark_moves_down && do_move)
1924 move_down (panel);
1927 static void
1928 mark_file (WPanel *panel)
1930 do_mark_file (panel, 1);
1933 /* Incremental search of a file name in the panel */
1934 static void
1935 do_search (WPanel *panel, int c_code)
1937 size_t l;
1938 int i;
1939 int wrapped = 0;
1940 int found;
1942 l = strlen (panel->search_buffer);
1943 if (c_code == KEY_BACKSPACE) {
1944 if (l)
1945 panel->search_buffer[--l] = '\0';
1946 } else {
1947 if (c_code && l < sizeof (panel->search_buffer)) {
1948 panel->search_buffer[l] = c_code;
1949 panel->search_buffer[l + 1] = 0;
1950 l++;
1954 found = 0;
1955 for (i = panel->selected; !wrapped || i != panel->selected; i++) {
1956 if (i >= panel->count) {
1957 i = 0;
1958 if (wrapped)
1959 break;
1960 wrapped = 1;
1962 if (panel->
1963 case_sensitive
1964 ? (strncmp (panel->dir.list[i].fname, panel->search_buffer, l)
1965 == 0) : (g_strncasecmp (panel->dir.list[i].fname,
1966 panel->search_buffer, l) == 0)) {
1967 unselect_item (panel);
1968 panel->selected = i;
1969 select_item (panel);
1970 found = 1;
1971 break;
1974 if (!found)
1975 panel->search_buffer[--l] = 0;
1977 paint_panel (panel);
1980 static void
1981 start_search (WPanel *panel)
1983 if (panel->searching){
1984 if (panel->selected+1 == panel->count)
1985 panel->selected = 0;
1986 else
1987 move_down (panel);
1988 do_search (panel, 0);
1989 } else {
1990 panel->searching = 1;
1991 panel->search_buffer [0] = 0;
1992 display_mini_info (panel);
1993 mc_refresh ();
1997 /* Return 1 if the Enter key has been processed, 0 otherwise */
1998 static int
1999 do_enter_on_file_entry (file_entry *fe)
2001 char *full_name;
2004 * Directory or link to directory - change directory.
2005 * Try the same for the entries on which mc_lstat() has failed.
2007 if (S_ISDIR (fe->st.st_mode) || link_isdir (fe)
2008 || (fe->st.st_mode == 0)) {
2009 if (!do_cd (fe->fname, cd_exact))
2010 message (D_ERROR, MSG_ERROR, _("Cannot change directory"));
2011 return 1;
2014 /* Try associated command */
2015 if (regex_command (fe->fname, "Open", 0) != 0)
2016 return 1;
2018 /* Check if the file is executable */
2019 full_name = concat_dir_and_file (current_panel->cwd, fe->fname);
2020 if (!is_exe (fe->st.st_mode) || !if_link_is_exe (full_name, fe)) {
2021 g_free (full_name);
2022 return 0;
2024 g_free (full_name);
2026 if (confirm_execute) {
2027 if (query_dialog
2028 (_(" The Midnight Commander "),
2029 _(" Do you really want to execute? "), D_NORMAL, 2, _("&Yes"),
2030 _("&No")) != 0)
2031 return 1;
2033 #ifdef USE_VFS
2034 if (!vfs_current_is_local ()) {
2035 char *tmp;
2036 int ret;
2038 tmp = concat_dir_and_file (vfs_get_current_dir (), fe->fname);
2039 ret = mc_setctl (tmp, VFS_SETCTL_RUN, NULL);
2040 g_free (tmp);
2041 /* We took action only if the dialog was shown or the execution
2042 * was successful */
2043 return confirm_execute || (ret == 0);
2045 #endif
2048 char *tmp = name_quote (fe->fname, 0);
2049 char *cmd = g_strconcat (".", PATH_SEP_STR, tmp, (char *) NULL);
2050 g_free (tmp);
2051 shell_execute (cmd, 0);
2052 g_free (cmd);
2055 return 1;
2058 static int
2059 do_enter (WPanel *panel)
2061 return do_enter_on_file_entry (selection (panel));
2064 static void
2065 chdir_other_panel (WPanel *panel)
2067 char *new_dir;
2068 char *sel_entry = NULL;
2070 if (get_other_type () != view_listing) {
2071 set_display_type (get_other_index (), view_listing);
2074 if (!S_ISDIR (panel->dir.list [panel->selected].st.st_mode)) {
2075 new_dir = concat_dir_and_file (panel->cwd, "..");
2076 sel_entry = strrchr(panel->cwd, PATH_SEP);
2077 } else
2078 new_dir = concat_dir_and_file (panel->cwd, panel->dir.list [panel->selected].fname);
2080 change_panel ();
2081 do_cd (new_dir, cd_exact);
2082 if (sel_entry)
2083 try_to_select (current_panel, sel_entry);
2084 change_panel ();
2086 move_down (panel);
2088 g_free (new_dir);
2092 * Make the current directory of the current panel also the current
2093 * directory of the other panel. Put the other panel to the listing
2094 * mode if needed. If the current panel is panelized, the other panel
2095 * doesn't become panelized.
2097 static void
2098 sync_other_panel (WPanel *panel)
2100 if (get_other_type () != view_listing) {
2101 set_display_type (get_other_index (), view_listing);
2104 do_panel_cd (other_panel, current_panel->cwd, cd_exact);
2106 /* try to select current filename on the other panel */
2107 if (!panel->is_panelized) {
2108 try_to_select (other_panel, selection (panel)->fname);
2112 static void
2113 chdir_to_readlink (WPanel *panel)
2115 char *new_dir;
2117 if (get_other_type () != view_listing)
2118 return;
2120 if (S_ISLNK (panel->dir.list [panel->selected].st.st_mode)) {
2121 char buffer [MC_MAXPATHLEN], *p;
2122 int i;
2123 struct stat st;
2125 i = readlink (selection (panel)->fname, buffer, MC_MAXPATHLEN - 1);
2126 if (i < 0)
2127 return;
2128 if (mc_stat (selection (panel)->fname, &st) < 0)
2129 return;
2130 buffer [i] = 0;
2131 if (!S_ISDIR (st.st_mode)) {
2132 p = strrchr (buffer, PATH_SEP);
2133 if (p && !p[1]) {
2134 *p = 0;
2135 p = strrchr (buffer, PATH_SEP);
2137 if (!p)
2138 return;
2139 p[1] = 0;
2141 if (*buffer == PATH_SEP)
2142 new_dir = g_strdup (buffer);
2143 else
2144 new_dir = concat_dir_and_file (panel->cwd, buffer);
2146 change_panel ();
2147 do_cd (new_dir, cd_exact);
2148 change_panel ();
2150 move_down (panel);
2152 g_free (new_dir);
2156 typedef void (*panel_key_callback) (WPanel *);
2157 typedef struct {
2158 int key_code;
2159 panel_key_callback fn;
2160 } panel_key_map;
2162 static void cmd_do_enter(WPanel *wp) { (void) do_enter(wp); }
2163 static void cmd_view_simple(WPanel *wp) { view_simple_cmd(); }
2164 static void cmd_edit_new(WPanel *wp) { edit_cmd_new(); }
2165 static void cmd_copy_local(WPanel *wp) { copy_cmd_local(); }
2166 static void cmd_rename_local(WPanel *wp) { ren_cmd_local(); }
2167 static void cmd_delete_local(WPanel *wp) { delete_cmd_local(); }
2168 static void cmd_select(WPanel *wp) { select_cmd(); }
2169 static void cmd_unselect(WPanel *wp) { unselect_cmd(); }
2170 static void cmd_reverse_selection(WPanel *wp) { reverse_selection_cmd(); }
2172 static const panel_key_map panel_keymap [] = {
2173 { KEY_DOWN, move_down },
2174 { KEY_UP, move_up },
2176 /* The action button :-) */
2177 { '\n', cmd_do_enter },
2178 { KEY_ENTER, cmd_do_enter },
2180 { KEY_IC, mark_file },
2181 { KEY_HOME, move_home },
2182 { KEY_A1, move_home },
2183 { ALT ('<'), move_home },
2184 { KEY_C1, move_end },
2185 { KEY_END, move_end },
2186 { ALT ('>'), move_end },
2187 { KEY_NPAGE, next_page },
2188 { KEY_PPAGE, prev_page },
2189 { KEY_NPAGE | KEY_M_CTRL, ctrl_next_page },
2190 { KEY_PPAGE | KEY_M_CTRL, ctrl_prev_page },
2192 /* To quickly move in the panel */
2193 { ALT('g'), goto_top_file },
2194 { ALT('r'), goto_middle_file }, /* M-r like emacs */
2195 { ALT('j'), goto_bottom_file },
2197 /* Emacs-like bindings */
2198 { XCTRL('v'), next_page }, /* C-v like emacs */
2199 { ALT('v'), prev_page }, /* M-v like emacs */
2200 { XCTRL('p'), move_up }, /* C-p like emacs */
2201 { XCTRL('n'), move_down }, /* C-n like emacs */
2202 { XCTRL('s'), start_search }, /* C-s like emacs */
2203 { ALT('s'), start_search }, /* M-s not like emacs */
2204 { XCTRL('t'), mark_file },
2205 { ALT('o'), chdir_other_panel },
2206 { ALT('i'), sync_other_panel },
2207 { ALT('l'), chdir_to_readlink },
2208 { ALT('H'), directory_history_list },
2209 { KEY_F(13), cmd_view_simple },
2210 { KEY_F(14), cmd_edit_new },
2211 { KEY_F(15), cmd_copy_local },
2212 { KEY_F(16), cmd_rename_local },
2213 { KEY_F(18), cmd_delete_local },
2214 { ALT('y'), directory_history_prev },
2215 { ALT('u'), directory_history_next },
2216 { ALT('+'), cmd_select },
2217 { KEY_KP_ADD, cmd_select },
2218 { ALT('\\'), cmd_unselect },
2219 { ALT('-'), cmd_unselect },
2220 { KEY_KP_SUBTRACT, cmd_unselect },
2221 { ALT('*'), cmd_reverse_selection },
2222 { KEY_KP_MULTIPLY, cmd_reverse_selection },
2223 { 0, 0 }
2226 static inline cb_ret_t
2227 panel_key (WPanel *panel, int key)
2229 int i;
2231 for (i = 0; panel_keymap[i].key_code; i++) {
2232 if (key == panel_keymap[i].key_code) {
2233 int old_searching = panel->searching;
2235 if (panel_keymap[i].fn != start_search)
2236 panel->searching = 0;
2238 (*panel_keymap[i].fn) (panel);
2240 if (panel->searching != old_searching)
2241 display_mini_info (panel);
2242 return MSG_HANDLED;
2245 if (torben_fj_mode && key == ALT ('h')) {
2246 goto_middle_file (panel);
2247 return MSG_HANDLED;
2250 /* We do not want to take a key press if nothing can be done with it */
2251 /* The command line widget may do something more useful */
2252 if (key == KEY_LEFT)
2253 return move_left (panel, key);
2255 if (key == KEY_RIGHT)
2256 return move_right (panel, key);
2258 if (is_abort_char (key)) {
2259 panel->searching = 0;
2260 display_mini_info (panel);
2261 return MSG_HANDLED;
2264 /* Do not eat characters not meant for the panel below ' ' (e.g. C-l). */
2265 if ((key >= ' ' && key <= 255) || key == KEY_BACKSPACE) {
2266 if (panel->searching) {
2267 do_search (panel, key);
2268 return MSG_HANDLED;
2271 if (!command_prompt) {
2272 start_search (panel);
2273 do_search (panel, key);
2274 return MSG_HANDLED;
2278 return MSG_NOT_HANDLED;
2281 static cb_ret_t
2282 panel_callback (Widget *w, widget_msg_t msg, int parm)
2284 WPanel *panel = (WPanel *) w;
2285 Dlg_head *h = panel->widget.parent;
2287 switch (msg) {
2288 case WIDGET_DRAW:
2289 paint_panel (panel);
2290 return MSG_HANDLED;
2292 case WIDGET_FOCUS:
2293 current_panel = panel;
2294 panel->active = 1;
2295 if (mc_chdir (panel->cwd) != 0) {
2296 char *cwd = strip_password (g_strdup (panel->cwd), 1);
2297 message (D_ERROR, MSG_ERROR, _(" Cannot chdir to \"%s\" \n %s "),
2298 cwd, unix_error_string (errno));
2299 g_free(cwd);
2300 } else
2301 subshell_chdir (panel->cwd);
2303 update_xterm_title_path ();
2304 select_item (panel);
2305 show_dir (panel);
2306 paint_dir (panel);
2307 panel->dirty = 0;
2309 buttonbar_set_label (h, 1, _("Help"), help_cmd);
2310 buttonbar_set_label (h, 2, _("Menu"), user_file_menu_cmd);
2311 buttonbar_set_label (h, 3, _("View"), view_cmd);
2312 buttonbar_set_label (h, 4, _("Edit"), edit_cmd);
2313 buttonbar_set_label (h, 5, _("Copy"), copy_cmd);
2314 buttonbar_set_label (h, 6, _("RenMov"), ren_cmd);
2315 buttonbar_set_label (h, 7, _("Mkdir"), mkdir_cmd);
2316 buttonbar_set_label (h, 8, _("Delete"), delete_cmd);
2317 buttonbar_redraw (h);
2318 return MSG_HANDLED;
2320 case WIDGET_UNFOCUS:
2321 /* Janne: look at this for the multiple panel options */
2322 if (panel->searching){
2323 panel->searching = 0;
2324 display_mini_info (panel);
2326 panel->active = 0;
2327 show_dir (panel);
2328 unselect_item (panel);
2329 return MSG_HANDLED;
2331 case WIDGET_KEY:
2332 return panel_key (panel, parm);
2334 case WIDGET_DESTROY:
2335 panel_destroy (panel);
2336 return MSG_HANDLED;
2338 default:
2339 return default_proc (msg, parm);
2343 void
2344 file_mark (WPanel *panel, int index, int val)
2346 if (panel->dir.list[index].f.marked != val) {
2347 panel->dir.list[index].f.marked = val;
2348 panel->dirty = 1;
2352 /* */
2353 /* Panel mouse events support routines */
2354 /* */
2355 static int mouse_marking = 0;
2357 static void
2358 mouse_toggle_mark (WPanel *panel)
2360 do_mark_file (panel, 0);
2361 mouse_marking = selection (panel)->f.marked;
2364 static void
2365 mouse_set_mark (WPanel *panel)
2367 if (mouse_marking && !(selection (panel)->f.marked))
2368 do_mark_file (panel, 0);
2369 else if (!mouse_marking && (selection (panel)->f.marked))
2370 do_mark_file (panel, 0);
2373 static inline int
2374 mark_if_marking (WPanel *panel, Gpm_Event *event)
2376 if (event->buttons & GPM_B_RIGHT){
2377 if (event->type & GPM_DOWN)
2378 mouse_toggle_mark (panel);
2379 else
2380 mouse_set_mark (panel);
2381 return 1;
2383 return 0;
2387 * Mouse callback of the panel minus repainting.
2388 * If the event is redirected to the menu, *redir is set to 1.
2390 static int
2391 do_panel_event (Gpm_Event *event, WPanel *panel, int *redir)
2393 const int lines = llines (panel);
2395 int my_index;
2397 /* Mouse wheel events */
2398 if ((event->buttons & GPM_B_UP) && (event->type & GPM_DOWN)) {
2399 prev_page (panel);
2400 return MOU_NORMAL;
2402 if ((event->buttons & GPM_B_DOWN) && (event->type & GPM_DOWN)) {
2403 next_page (panel);
2404 return MOU_NORMAL;
2407 /* "<" button */
2408 if (event->type & GPM_DOWN && event->x == 2 && event->y == 1) {
2409 directory_history_prev (panel);
2410 return MOU_NORMAL;
2413 /* ">" button */
2414 if (event->type & GPM_DOWN && event->x == panel->widget.cols - 1
2415 && event->y == 1) {
2416 directory_history_next (panel);
2417 return MOU_NORMAL;
2420 /* "v" button */
2421 if (event->type & GPM_DOWN && event->x == panel->widget.cols - 2
2422 && event->y == 1) {
2423 directory_history_list (panel);
2424 return MOU_NORMAL;
2427 /* rest of the upper frame, the menu is invisible - call menu */
2428 if (event->type & GPM_DOWN && event->y == 1 && !menubar_visible) {
2429 *redir = 1;
2430 event->x += panel->widget.x;
2431 return (*(the_menubar->widget.mouse)) (event, the_menubar);
2434 event->y -= 2;
2435 if ((event->type & (GPM_DOWN | GPM_DRAG))) {
2437 if (!dlg_widget_active (panel))
2438 change_panel ();
2440 if (event->y <= 0) {
2441 mark_if_marking (panel, event);
2442 if (mouse_move_pages)
2443 prev_page (panel);
2444 else
2445 move_up (panel);
2446 return MOU_REPEAT;
2449 if (!((panel->top_file + event->y <= panel->count)
2450 && event->y <= lines)) {
2451 mark_if_marking (panel, event);
2452 if (mouse_move_pages)
2453 next_page (panel);
2454 else
2455 move_down (panel);
2456 return MOU_REPEAT;
2458 my_index = panel->top_file + event->y - 1;
2459 if (panel->split) {
2460 if (event->x > ((panel->widget.cols - 2) / 2))
2461 my_index += llines (panel);
2464 if (my_index >= panel->count)
2465 my_index = panel->count - 1;
2467 if (my_index != panel->selected) {
2468 unselect_item (panel);
2469 panel->selected = my_index;
2470 select_item (panel);
2473 /* This one is new */
2474 mark_if_marking (panel, event);
2476 } else if ((event->type & (GPM_UP | GPM_DOUBLE)) ==
2477 (GPM_UP | GPM_DOUBLE)) {
2478 if (event->y > 0 && event->y <= lines)
2479 do_enter (panel);
2481 return MOU_NORMAL;
2484 /* Mouse callback of the panel */
2485 static int
2486 panel_event (Gpm_Event *event, void *data)
2488 WPanel *panel = data;
2489 int ret;
2490 int redir = 0;
2492 ret = do_panel_event (event, panel, &redir);
2493 if (!redir)
2494 panel_update_contents (panel);
2496 return ret;
2499 void
2500 panel_re_sort (WPanel *panel)
2502 char *filename;
2503 int i;
2505 if (panel == NULL)
2506 return;
2508 filename = g_strdup (selection (panel)->fname);
2509 unselect_item (panel);
2510 do_sort (&panel->dir, panel->sort_type, panel->count-1, panel->reverse,
2511 panel->case_sensitive, panel->exec_first);
2512 panel->selected = -1;
2513 for (i = panel->count; i; i--){
2514 if (!strcmp (panel->dir.list [i-1].fname, filename)){
2515 panel->selected = i-1;
2516 break;
2519 g_free (filename);
2520 panel->top_file = panel->selected - ITEMS (panel)/2;
2521 if (panel->top_file < 0)
2522 panel->top_file = 0;
2523 select_item (panel);
2524 panel->dirty = 1;
2527 void
2528 panel_set_sort_order (WPanel *panel, sortfn *sort_order)
2530 if (sort_order == 0)
2531 return;
2533 panel->sort_type = sort_order;
2535 /* The directory is already sorted, we have to load the unsorted stuff */
2536 if (sort_order == (sortfn *) unsorted){
2537 char *current_file;
2539 current_file = g_strdup (panel->dir.list [panel->selected].fname);
2540 panel_reload (panel);
2541 try_to_select (panel, current_file);
2542 g_free (current_file);
2544 panel_re_sort (panel);