Original patch as attached on the bugreport
[midnight-commander.git] / src / screen.c
blobd1121d8d1b1375403d94cce371cf9ee817ee6301
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>
29 #include <unistd.h>
31 #include "global.h"
32 #include "tty.h"
33 #include "dir.h"
34 #include "panel.h"
35 #include "color.h"
36 #include "tree.h"
37 #include "win.h"
38 #include "ext.h" /* regexp_command */
39 #include "mouse.h" /* For Gpm_Event */
40 #include "layout.h" /* Most layout variables are here */
41 #include "wtools.h" /* for message (...) */
42 #include "cmd.h"
43 #include "key.h" /* XCTRL and ALT macros */
44 #include "setup.h" /* For loading/saving panel options */
45 #include "user.h"
46 #include "profile.h"
47 #include "execute.h"
48 #include "widget.h"
49 #include "menu.h" /* menubar_visible */
50 #define WANT_WIDGETS
51 #include "main.h" /* the_menubar */
52 #include "unixcompat.h"
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_dir (WPanel *panel)
770 char *tmp;
772 set_colors (panel);
773 draw_double_box (panel->widget.parent,
774 panel->widget.y, panel->widget.x,
775 panel->widget.lines, panel->widget.cols);
777 #ifdef HAVE_SLANG
778 if (show_mini_info) {
779 SLsmg_draw_object (panel->widget.y + llines (panel) + 2,
780 panel->widget.x, SLSMG_LTEE_CHAR);
781 SLsmg_draw_object (panel->widget.y + llines (panel) + 2,
782 panel->widget.x + panel->widget.cols - 1,
783 SLSMG_RTEE_CHAR);
785 #endif /* HAVE_SLANG */
787 if (panel->active)
788 attrset (REVERSE_COLOR);
790 widget_move (&panel->widget, 0, 3);
791 addch (' ');
793 tmp = g_malloc (panel->widget.cols + 1);
794 tmp[panel->widget.cols] = '\0';
796 trim (strip_home_and_password (panel->cwd), tmp,
797 max (panel->widget.cols - 9, 0));
798 addstr (tmp);
799 g_free (tmp);
801 addch (' ');
802 widget_move (&panel->widget, 0, 1);
803 addstr ("<");
804 widget_move (&panel->widget, 0, panel->widget.cols - 2);
805 addstr (">");
806 widget_move (&panel->widget, 0, panel->widget.cols - 3);
807 addstr ("v");
809 if (panel->active)
810 standend ();
813 /* To be used only by long_frame and full_frame to adjust top_file */
814 static void
815 adjust_top_file (WPanel *panel)
817 int old_top = panel->top_file;
819 if (panel->selected - old_top > llines (panel))
820 panel->top_file = panel->selected;
821 if (old_top - panel->count > llines (panel))
822 panel->top_file = panel->count - llines (panel);
826 * Repaint everything that can change on the panel - title, entries and
827 * mini status. The rest of the frame and the mini status separator are
828 * not repainted.
830 static void
831 panel_update_contents (WPanel *panel)
833 show_dir (panel);
834 paint_dir (panel);
835 display_mini_info (panel);
836 panel->dirty = 0;
839 /* Repaint everything, including frame and separator */
840 static void
841 paint_panel (WPanel *panel)
843 paint_frame (panel);
844 panel_update_contents (panel);
845 mini_info_separator (panel);
849 * Repaint the contents of the panels without frames. To schedule panel
850 * for repainting, set panel->dirty to 1. There are many reasons why
851 * the panels need to be repainted, and this is a costly operation, so
852 * it's done once per event.
854 void
855 update_dirty_panels (void)
857 if (current_panel->dirty)
858 panel_update_contents (current_panel);
860 if ((get_other_type () == view_listing) && other_panel->dirty)
861 panel_update_contents (other_panel);
864 static void
865 do_select (WPanel *panel, int i)
867 if (i != panel->selected) {
868 panel->dirty = 1;
869 panel->selected = i;
870 panel->top_file = panel->selected - (panel->widget.lines - 2) / 2;
871 if (panel->top_file < 0)
872 panel->top_file = 0;
876 static inline void
877 do_try_to_select (WPanel *panel, const char *name)
879 int i;
880 char *subdir;
882 if (!name) {
883 do_select(panel, 0);
884 return;
887 /* We only want the last component of the directory,
888 * and from this only the name without suffix. */
889 subdir = vfs_strip_suffix_from_filename (x_basename(name));
891 /* Search that subdirectory, if found select it */
892 for (i = 0; i < panel->count; i++){
893 if (strcmp (subdir, panel->dir.list [i].fname) == 0) {
894 do_select (panel, i);
895 g_free (subdir);
896 return;
900 /* Try to select a file near the file that is missing */
901 if (panel->selected >= panel->count)
902 do_select (panel, panel->count-1);
903 g_free (subdir);
906 void
907 try_to_select (WPanel *panel, const char *name)
909 do_try_to_select (panel, name);
910 select_item (panel);
913 void
914 panel_update_cols (Widget *widget, int frame_size)
916 int cols, origin;
918 if (horizontal_split){
919 widget->cols = COLS;
920 return;
923 if (frame_size == frame_full){
924 cols = COLS;
925 origin = 0;
926 } else {
927 if (widget == get_panel_widget (0)){
928 cols = first_panel_size;
929 origin = 0;
930 } else {
931 cols = COLS-first_panel_size;
932 origin = first_panel_size;
936 widget->cols = cols;
937 widget->x = origin;
940 static char *
941 panel_save_name (WPanel *panel)
943 extern int saving_setup;
945 /* If the program is shuting down */
946 if ((midnight_shutdown && auto_save_setup) || saving_setup)
947 return g_strdup (panel->panel_name);
948 else
949 return g_strconcat ("Temporal:", panel->panel_name, (char *) NULL);
952 static void
953 panel_destroy (WPanel *p)
955 int i;
957 char *name = panel_save_name (p);
959 panel_save_setup (p, name);
960 panel_clean_dir (p);
962 /* save and clean history */
963 if (p->dir_history) {
964 history_put (p->hist_name, p->dir_history);
966 p->dir_history = g_list_first (p->dir_history);
967 g_list_foreach (p->dir_history, (GFunc) g_free, NULL);
968 g_list_free (p->dir_history);
971 g_free (p->hist_name);
973 delete_format (p->format);
974 delete_format (p->status_format);
976 g_free (p->user_format);
977 for (i = 0; i < LIST_TYPES; i++)
978 g_free (p->user_status_format[i]);
979 g_free (p->dir.list);
980 g_free (p->panel_name);
981 g_free (name);
984 static void
985 panel_format_modified (WPanel *panel)
987 panel->format_modified = 1;
990 /* Panel creation */
991 /* The parameter specifies the name of the panel for setup retieving */
992 WPanel *
993 panel_new (const char *panel_name)
995 WPanel *panel;
996 char *section;
997 int i, err;
999 panel = g_new0 (WPanel, 1);
1001 /* No know sizes of the panel at startup */
1002 init_widget (&panel->widget, 0, 0, 0, 0, panel_callback, panel_event);
1004 /* We do not want the cursor */
1005 widget_want_cursor (panel->widget, 0);
1007 mc_get_current_wd (panel->cwd, sizeof (panel->cwd) - 2);
1008 strcpy (panel->lwd, ".");
1010 panel->hist_name = g_strconcat ("Dir Hist ", panel_name, (char *) NULL);
1011 panel->dir_history = history_get (panel->hist_name);
1012 directory_history_add (panel, panel->cwd);
1014 panel->dir.list = g_new (file_entry, MIN_FILES);
1015 panel->dir.size = MIN_FILES;
1016 panel->active = 0;
1017 panel->filter = 0;
1018 panel->split = 0;
1019 panel->top_file = 0;
1020 panel->selected = 0;
1021 panel->marked = 0;
1022 panel->total = 0;
1023 panel->reverse = 0;
1024 panel->dirty = 1;
1025 panel->searching = 0;
1026 panel->dirs_marked = 0;
1027 panel->is_panelized = 0;
1028 panel->format = 0;
1029 panel->status_format = 0;
1030 panel->format_modified = 1;
1032 panel->panel_name = g_strdup (panel_name);
1033 panel->user_format = g_strdup (DEFAULT_USER_FORMAT);
1035 for (i = 0; i < LIST_TYPES; i++)
1036 panel->user_status_format[i] = g_strdup (DEFAULT_USER_FORMAT);
1038 panel->search_buffer[0] = 0;
1039 panel->frame_size = frame_half;
1040 section = g_strconcat ("Temporal:", panel->panel_name, (char *) NULL);
1041 if (!profile_has_section (section, profile_name)) {
1042 g_free (section);
1043 section = g_strdup (panel->panel_name);
1045 panel_load_setup (panel, section);
1046 g_free (section);
1048 /* Load format strings */
1049 err = set_panel_formats (panel);
1050 if (err) {
1051 set_panel_formats (panel);
1054 /* Load the default format */
1055 panel->count =
1056 do_load_dir (panel->cwd, &panel->dir, panel->sort_type,
1057 panel->reverse, panel->case_sensitive,
1058 panel->exec_first, panel->filter);
1059 return panel;
1062 void
1063 panel_reload (WPanel *panel)
1065 struct stat current_stat;
1067 if (fast_reload && !stat (panel->cwd, &current_stat)
1068 && current_stat.st_ctime == panel->dir_stat.st_ctime
1069 && current_stat.st_mtime == panel->dir_stat.st_mtime)
1070 return;
1072 while (mc_chdir (panel->cwd) == -1) {
1073 char *last_slash;
1075 if (panel->cwd[0] == PATH_SEP && panel->cwd[1] == 0) {
1076 panel_clean_dir (panel);
1077 panel->count = set_zero_dir (&panel->dir);
1078 return;
1080 last_slash = strrchr (panel->cwd, PATH_SEP);
1081 if (!last_slash || last_slash == panel->cwd)
1082 strcpy (panel->cwd, PATH_SEP_STR);
1083 else
1084 *last_slash = 0;
1085 memset (&(panel->dir_stat), 0, sizeof (panel->dir_stat));
1086 show_dir (panel);
1089 panel->count =
1090 do_reload_dir (panel->cwd, &panel->dir, panel->sort_type,
1091 panel->count, panel->reverse, panel->case_sensitive,
1092 panel->exec_first, panel->filter);
1094 panel->dirty = 1;
1095 if (panel->selected >= panel->count)
1096 do_select (panel, panel->count - 1);
1098 recalculate_panel_summary (panel);
1101 static void
1102 paint_frame (WPanel *panel)
1104 int header_len;
1105 int spaces, extra;
1106 int side, width;
1108 const char *txt;
1109 if (!panel->split)
1110 adjust_top_file (panel);
1112 widget_erase (&panel->widget);
1113 show_dir (panel);
1115 widget_move (&panel->widget, 1, 1);
1117 for (side = 0; side <= panel->split; side++){
1118 format_e *format;
1120 if (side){
1121 attrset (NORMAL_COLOR);
1122 one_vline ();
1123 width = panel->widget.cols - panel->widget.cols/2 - 1;
1124 } else if (panel->split)
1125 width = panel->widget.cols/2 - 3;
1126 else
1127 width = panel->widget.cols - 2;
1129 for (format = panel->format; format; format = format->next){
1130 if (format->string_fn){
1131 txt = format->title;
1133 header_len = strlen (txt);
1134 if (header_len > format->field_len)
1135 header_len = format->field_len;
1137 attrset (MARKED_COLOR);
1138 spaces = (format->field_len - header_len) / 2;
1139 extra = (format->field_len - header_len) % 2;
1140 tty_printf ("%*s%.*s%*s", spaces, "",
1141 header_len, txt, spaces+extra, "");
1142 width -= 2 * spaces + extra + header_len;
1143 } else {
1144 attrset (NORMAL_COLOR);
1145 one_vline ();
1146 width --;
1147 continue;
1151 if (width > 0)
1152 tty_printf ("%*s", width, "");
1156 static const char *
1157 parse_panel_size (WPanel *panel, const char *format, int isstatus)
1159 int frame = frame_half;
1160 format = skip_separators (format);
1162 if (!strncmp (format, "full", 4)){
1163 frame = frame_full;
1164 format += 4;
1165 } else if (!strncmp (format, "half", 4)){
1166 frame = frame_half;
1167 format += 4;
1170 if (!isstatus){
1171 panel->frame_size = frame;
1172 panel->split = 0;
1175 /* Now, the optional column specifier */
1176 format = skip_separators (format);
1178 if (*format == '1' || *format == '2'){
1179 if (!isstatus)
1180 panel->split = *format == '2';
1181 format++;
1184 if (!isstatus)
1185 panel_update_cols (&(panel->widget), panel->frame_size);
1187 return skip_separators (format);
1190 /* Format is:
1192 all := panel_format? format
1193 panel_format := [full|half] [1|2]
1194 format := one_format_e
1195 | format , one_format_e
1197 one_format_e := just format.id [opt_size]
1198 just := [<=>]
1199 opt_size := : size [opt_expand]
1200 size := [0-9]+
1201 opt_expand := +
1205 static format_e *
1206 parse_display_format (WPanel *panel, const char *format, char **error, int isstatus, int *res_total_cols)
1208 format_e *darr, *old = 0, *home = 0; /* The formats we return */
1209 int total_cols = 0; /* Used columns by the format */
1210 int set_justify; /* flag: set justification mode? */
1211 int justify = 0; /* Which mode. */
1212 int items = 0; /* Number of items in the format */
1213 size_t i;
1215 static size_t i18n_timelength = 0; /* flag: check ?Time length at startup */
1217 *error = 0;
1219 if (i18n_timelength == 0) {
1220 i18n_timelength = i18n_checktimelength (); /* Musn't be 0 */
1222 for (i = 0; i < ELEMENTS(formats); i++)
1223 if (strcmp ("time", formats [i].id+1) == 0)
1224 formats [i].min_size = i18n_timelength;
1228 * This makes sure that the panel and mini status full/half mode
1229 * setting is equal
1231 format = parse_panel_size (panel, format, isstatus);
1233 while (*format){ /* format can be an empty string */
1234 int found = 0;
1236 darr = g_new (format_e, 1);
1238 /* I'm so ugly, don't look at me :-) */
1239 if (!home)
1240 home = old = darr;
1242 old->next = darr;
1243 darr->next = 0;
1244 old = darr;
1246 format = skip_separators (format);
1248 if (strchr ("<=>", *format)){
1249 set_justify = 1;
1250 switch (*format)
1252 case '<':
1253 justify = J_LEFT;
1254 break;
1255 case '=':
1256 justify = J_CENTER;
1257 break;
1258 case '>':
1259 default:
1260 justify = J_RIGHT;
1261 break;
1263 format = skip_separators (format+1);
1264 } else
1265 set_justify = 0;
1267 for (i = 0; i < ELEMENTS(formats); i++){
1268 int klen = strlen (formats [i].id);
1270 if (strncmp (format, formats [i].id, klen) != 0)
1271 continue;
1273 format += klen;
1275 if (formats [i].use_in_gui)
1276 items++;
1278 darr->requested_field_len = formats [i].min_size;
1279 darr->string_fn = formats [i].string_fn;
1280 if (formats [i].title [0])
1281 darr->title = _(formats [i].title);
1282 else
1283 darr->title = "";
1284 darr->id = formats [i].id;
1285 darr->expand = formats [i].expands;
1286 darr->just_mode = formats [i].default_just;
1288 if (set_justify) {
1289 if (IS_FIT(darr->just_mode))
1290 darr->just_mode = MAKE_FIT(justify);
1291 else
1292 darr->just_mode = justify;
1294 found = 1;
1296 format = skip_separators (format);
1298 /* If we have a size specifier */
1299 if (*format == ':'){
1300 int req_length;
1302 /* If the size was specified, we don't want
1303 * auto-expansion by default
1305 darr->expand = 0;
1306 format++;
1307 req_length = atoi (format);
1308 darr->requested_field_len = req_length;
1310 format = skip_numbers (format);
1312 /* Now, if they insist on expansion */
1313 if (*format == '+'){
1314 darr->expand = 1;
1315 format++;
1320 break;
1322 if (!found){
1323 char *tmp_format = g_strdup (format);
1325 int pos = min (8, strlen (format));
1326 delete_format (home);
1327 tmp_format [pos] = 0;
1328 *error = g_strconcat (_("Unknown tag on display format: "), tmp_format, (char *) NULL);
1329 g_free (tmp_format);
1330 return 0;
1332 total_cols += darr->requested_field_len;
1335 *res_total_cols = total_cols;
1336 return home;
1339 static format_e *
1340 use_display_format (WPanel *panel, const char *format, char **error, int isstatus)
1342 #define MAX_EXPAND 4
1343 int expand_top = 0; /* Max used element in expand */
1344 int usable_columns; /* Usable columns in the panel */
1345 int total_cols = 0;
1346 int i;
1347 format_e *darr, *home;
1349 if (!format)
1350 format = DEFAULT_USER_FORMAT;
1352 home = parse_display_format (panel, format, error, isstatus, &total_cols);
1354 if (*error)
1355 return 0;
1357 panel->dirty = 1;
1359 /* Status needn't to be split */
1360 usable_columns = ((panel->widget.cols-2)/((isstatus)
1362 : (panel->split+1))) - (!isstatus && panel->split);
1364 /* Look for the expandable fields and set field_len based on the requested field len */
1365 for (darr = home; darr && expand_top < MAX_EXPAND; darr = darr->next){
1366 darr->field_len = darr->requested_field_len;
1367 if (darr->expand)
1368 expand_top++;
1371 /* If we used more columns than the available columns, adjust that */
1372 if (total_cols > usable_columns){
1373 int pdif, dif = total_cols - usable_columns;
1375 while (dif){
1376 pdif = dif;
1377 for (darr = home; darr; darr = darr->next){
1378 if (dif && darr->field_len - 1){
1379 darr->field_len--;
1380 dif--;
1384 /* avoid endless loop if num fields > 40 */
1385 if (pdif == dif)
1386 break;
1388 total_cols = usable_columns; /* give up, the rest should be truncated */
1391 /* Expand the available space */
1392 if ((usable_columns > total_cols) && expand_top){
1393 int spaces = (usable_columns - total_cols) / expand_top;
1394 int extra = (usable_columns - total_cols) % expand_top;
1396 for (i = 0, darr = home; darr && (i < expand_top); darr = darr->next)
1397 if (darr->expand){
1398 darr->field_len += (spaces + ((i == 0) ? extra : 0));
1399 i++;
1402 return home;
1405 /* Switches the panel to the mode specified in the format */
1406 /* Seting up both format and status string. Return: 0 - on success; */
1407 /* 1 - format error; 2 - status error; 3 - errors in both formats. */
1409 set_panel_formats (WPanel *p)
1411 format_e *form;
1412 char *err;
1413 int retcode = 0;
1415 form = use_display_format (p, panel_format (p), &err, 0);
1417 if (err){
1418 g_free (err);
1419 retcode = 1;
1421 else {
1422 if (p->format)
1423 delete_format (p->format);
1425 p->format = form;
1428 if (show_mini_info){
1430 form = use_display_format (p, mini_status_format (p), &err, 1);
1432 if (err){
1433 g_free (err);
1434 retcode += 2;
1436 else {
1437 if (p->status_format)
1438 delete_format (p->status_format);
1440 p->status_format = form;
1444 panel_format_modified (p);
1445 panel_update_cols (&(p->widget), p->frame_size);
1447 if (retcode)
1448 message( 1, _("Warning" ), _( "User supplied format looks invalid, reverting to default." ) );
1449 if (retcode & 0x01){
1450 g_free (p->user_format);
1451 p->user_format = g_strdup (DEFAULT_USER_FORMAT);
1453 if (retcode & 0x02){
1454 g_free (p->user_status_format [p->list_type]);
1455 p->user_status_format [p->list_type] = g_strdup (DEFAULT_USER_FORMAT);
1458 return retcode;
1461 /* Given the panel->view_type returns the format string to be parsed */
1462 static const char *
1463 panel_format (WPanel *panel)
1465 switch (panel->list_type){
1467 case list_long:
1468 return "full perm space nlink space owner space group space size space mtime space name";
1470 case list_brief:
1471 return "half 2 type name";
1473 case list_user:
1474 return panel->user_format;
1476 default:
1477 case list_full:
1478 return "half type name | size | mtime";
1482 static const char *
1483 mini_status_format (WPanel *panel)
1485 if (panel->user_mini_status)
1486 return panel->user_status_format [panel->list_type];
1488 switch (panel->list_type){
1490 case list_long:
1491 return "full perm space nlink space owner space group space size space mtime space name";
1493 case list_brief:
1494 return "half type name space bsize space perm space";
1496 case list_full:
1497 return "half type name";
1499 default:
1500 case list_user:
1501 return panel->user_format;
1505 /* */
1506 /* Panel operation commands */
1507 /* */
1509 /* Returns the number of items in the given panel */
1510 static int
1511 ITEMS (WPanel *p)
1513 if (p->split)
1514 return llines (p) * 2;
1515 else
1516 return llines (p);
1519 /* Select current item and readjust the panel */
1520 void
1521 select_item (WPanel *panel)
1523 int items = ITEMS (panel);
1525 /* Although currently all over the code we set the selection and
1526 top file to decent values before calling select_item, I could
1527 forget it someday, so it's better to do the actual fitting here */
1529 if (panel->top_file < 0)
1530 panel->top_file = 0;
1532 if (panel->selected < 0)
1533 panel->selected = 0;
1535 if (panel->selected > panel->count - 1)
1536 panel->selected = panel->count - 1;
1538 if (panel->top_file > panel->count - 1)
1539 panel->top_file = panel->count - 1;
1541 if ((panel->count - panel->top_file) < items) {
1542 panel->top_file = panel->count - items;
1543 if (panel->top_file < 0)
1544 panel->top_file = 0;
1547 if (panel->selected < panel->top_file)
1548 panel->top_file = panel->selected;
1550 if ((panel->selected - panel->top_file) >= items)
1551 panel->top_file = panel->selected - items + 1;
1553 panel->dirty = 1;
1555 execute_hooks (select_file_hook);
1558 /* Clears all files in the panel, used only when one file was marked */
1559 void
1560 unmark_files (WPanel *panel)
1562 int i;
1564 if (!panel->marked)
1565 return;
1566 for (i = 0; i < panel->count; i++)
1567 file_mark (panel, i, 0);
1569 panel->dirs_marked = 0;
1570 panel->marked = 0;
1571 panel->total = 0;
1574 static void
1575 unselect_item (WPanel *panel)
1577 repaint_file (panel, panel->selected, 1, 2*selection (panel)->f.marked, 0);
1580 static void
1581 move_down (WPanel *panel)
1583 if (panel->selected+1 == panel->count)
1584 return;
1586 unselect_item (panel);
1587 panel->selected++;
1589 if (panel->selected - panel->top_file == ITEMS (panel) &&
1590 panel_scroll_pages){
1591 /* Scroll window half screen */
1592 panel->top_file += ITEMS (panel)/2;
1593 if (panel->top_file > panel->count - ITEMS (panel))
1594 panel->top_file = panel->count - ITEMS (panel);
1595 paint_dir (panel);
1596 select_item (panel);
1598 select_item (panel);
1601 static void
1602 move_up (WPanel *panel)
1604 if (panel->selected == 0)
1605 return;
1607 unselect_item (panel);
1608 panel->selected--;
1609 if (panel->selected < panel->top_file && panel_scroll_pages){
1610 /* Scroll window half screen */
1611 panel->top_file -= ITEMS (panel)/2;
1612 if (panel->top_file < 0) panel->top_file = 0;
1613 paint_dir (panel);
1615 select_item (panel);
1618 /* Changes the selection by lines (may be negative) */
1619 static void
1620 move_selection (WPanel *panel, int lines)
1622 int new_pos;
1623 int adjust = 0;
1625 new_pos = panel->selected + lines;
1626 if (new_pos >= panel->count)
1627 new_pos = panel->count-1;
1629 if (new_pos < 0)
1630 new_pos = 0;
1632 unselect_item (panel);
1633 panel->selected = new_pos;
1635 if (panel->selected - panel->top_file >= ITEMS (panel)){
1636 panel->top_file += lines;
1637 adjust = 1;
1640 if (panel->selected - panel->top_file < 0){
1641 panel->top_file += lines;
1642 adjust = 1;
1645 if (adjust){
1646 if (panel->top_file > panel->selected)
1647 panel->top_file = panel->selected;
1648 if (panel->top_file < 0)
1649 panel->top_file = 0;
1650 paint_dir (panel);
1652 select_item (panel);
1655 static cb_ret_t
1656 move_left (WPanel *panel, int c_code)
1658 if (panel->split) {
1659 move_selection (panel, -llines (panel));
1660 return MSG_HANDLED;
1661 } else
1662 return maybe_cd (1); /* cd .. */
1665 static int
1666 move_right (WPanel *panel, int c_code)
1668 if (panel->split) {
1669 move_selection (panel, llines (panel));
1670 return MSG_HANDLED;
1671 } else
1672 return maybe_cd (0); /* cd (selection) */
1675 static void
1676 prev_page (WPanel *panel)
1678 int items;
1680 if (!panel->selected && !panel->top_file)
1681 return;
1682 unselect_item (panel);
1683 items = ITEMS (panel);
1684 if (panel->top_file < items)
1685 items = panel->top_file;
1686 if (!items)
1687 panel->selected = 0;
1688 else
1689 panel->selected -= items;
1690 panel->top_file -= items;
1692 /* This keeps the selection in a reasonable place */
1693 if (panel->selected < 0)
1694 panel->selected = 0;
1695 if (panel->top_file < 0)
1696 panel->top_file = 0;
1697 select_item (panel);
1698 paint_dir (panel);
1701 static void
1702 ctrl_prev_page (WPanel *panel)
1704 do_cd ("..", cd_exact);
1707 static void
1708 next_page (WPanel *panel)
1710 int items;
1712 if (panel->selected == panel->count - 1)
1713 return;
1714 unselect_item (panel);
1715 items = ITEMS (panel);
1716 if (panel->top_file > panel->count - 2 * items)
1717 items = panel->count - items - panel->top_file;
1718 if (panel->top_file + items < 0)
1719 items = -panel->top_file;
1720 if (!items)
1721 panel->selected = panel->count - 1;
1722 else
1723 panel->selected += items;
1724 panel->top_file += items;
1726 /* This keeps the selection in it's relative position */
1727 if (panel->selected >= panel->count)
1728 panel->selected = panel->count - 1;
1729 if (panel->top_file >= panel->count)
1730 panel->top_file = panel->count - 1;
1731 select_item (panel);
1732 paint_dir (panel);
1735 static void
1736 ctrl_next_page (WPanel *panel)
1738 if ((S_ISDIR (selection (panel)->st.st_mode)
1739 || link_isdir (selection (panel)))) {
1740 do_cd (selection (panel)->fname, cd_exact);
1744 static void
1745 goto_top_file (WPanel *panel)
1747 unselect_item (panel);
1748 panel->selected = panel->top_file;
1749 select_item (panel);
1752 static void
1753 goto_middle_file (WPanel *panel)
1755 unselect_item (panel);
1756 panel->selected = panel->top_file + (ITEMS (panel)/2);
1757 if (panel->selected >= panel->count)
1758 panel->selected = panel->count - 1;
1759 select_item (panel);
1762 static void
1763 goto_bottom_file (WPanel *panel)
1765 unselect_item (panel);
1766 panel->selected = panel->top_file + ITEMS (panel)-1;
1767 if (panel->selected >= panel->count)
1768 panel->selected = panel->count - 1;
1769 select_item (panel);
1772 static void
1773 move_home (WPanel *panel)
1775 if (panel->selected == 0)
1776 return;
1777 unselect_item (panel);
1779 if (torben_fj_mode){
1780 int middle_pos = panel->top_file + (ITEMS (panel)/2);
1782 if (panel->selected > middle_pos){
1783 goto_middle_file (panel);
1784 return;
1786 if (panel->selected != panel->top_file){
1787 goto_top_file (panel);
1788 return;
1792 panel->top_file = 0;
1793 panel->selected = 0;
1795 paint_dir (panel);
1796 select_item (panel);
1799 static void
1800 move_end (WPanel *panel)
1802 if (panel->selected == panel->count-1)
1803 return;
1804 unselect_item (panel);
1805 if (torben_fj_mode){
1806 int middle_pos = panel->top_file + (ITEMS (panel)/2);
1808 if (panel->selected < middle_pos){
1809 goto_middle_file (panel);
1810 return;
1812 if (panel->selected != (panel->top_file + ITEMS(panel)-1)){
1813 goto_bottom_file (panel);
1814 return;
1818 panel->selected = panel->count-1;
1819 paint_dir (panel);
1820 select_item (panel);
1823 /* Recalculate the panels summary information, used e.g. when marked
1824 files might have been removed by an external command */
1825 void
1826 recalculate_panel_summary (WPanel *panel)
1828 int i;
1830 panel->marked = 0;
1831 panel->dirs_marked = 0;
1832 panel->total = 0;
1834 for (i = 0; i < panel->count; i++)
1835 if (panel->dir.list [i].f.marked){
1836 /* do_file_mark will return immediately if newmark == oldmark.
1837 So we have to first unmark it to get panel's summary information
1838 updated. (Norbert) */
1839 panel->dir.list [i].f.marked = 0;
1840 do_file_mark (panel, i, 1);
1844 /* This routine marks a file or a directory */
1845 void
1846 do_file_mark (WPanel *panel, int idx, int mark)
1848 if (panel->dir.list[idx].f.marked == mark)
1849 return;
1851 /* Only '..' can't be marked, '.' isn't visible */
1852 if (!strcmp (panel->dir.list[idx].fname, ".."))
1853 return;
1855 file_mark (panel, idx, mark);
1856 if (panel->dir.list[idx].f.marked) {
1857 panel->marked++;
1858 if (S_ISDIR (panel->dir.list[idx].st.st_mode)) {
1859 if (panel->dir.list[idx].f.dir_size_computed)
1860 panel->total += panel->dir.list[idx].st.st_size;
1861 panel->dirs_marked++;
1862 } else
1863 panel->total += panel->dir.list[idx].st.st_size;
1864 set_colors (panel);
1865 } else {
1866 if (S_ISDIR (panel->dir.list[idx].st.st_mode)) {
1867 if (panel->dir.list[idx].f.dir_size_computed)
1868 panel->total -= panel->dir.list[idx].st.st_size;
1869 panel->dirs_marked--;
1870 } else
1871 panel->total -= panel->dir.list[idx].st.st_size;
1872 panel->marked--;
1876 static void
1877 do_mark_file (WPanel *panel, int do_move)
1879 do_file_mark (panel, panel->selected,
1880 selection (panel)->f.marked ? 0 : 1);
1881 if (mark_moves_down && do_move)
1882 move_down (panel);
1885 static void
1886 mark_file (WPanel *panel)
1888 do_mark_file (panel, 1);
1891 /* Incremental search of a file name in the panel */
1892 static void
1893 do_search (WPanel *panel, int c_code)
1895 size_t l;
1896 int i;
1897 int wrapped = 0;
1898 int found;
1900 l = strlen (panel->search_buffer);
1901 if (c_code == KEY_BACKSPACE) {
1902 if (l)
1903 panel->search_buffer[--l] = '\0';
1904 } else {
1905 if (c_code && l < sizeof (panel->search_buffer)) {
1906 panel->search_buffer[l] = c_code;
1907 panel->search_buffer[l + 1] = 0;
1908 l++;
1912 found = 0;
1913 for (i = panel->selected; !wrapped || i != panel->selected; i++) {
1914 if (i >= panel->count) {
1915 i = 0;
1916 if (wrapped)
1917 break;
1918 wrapped = 1;
1920 if (panel->
1921 case_sensitive
1922 ? (strncmp (panel->dir.list[i].fname, panel->search_buffer, l)
1923 == 0) : (g_strncasecmp (panel->dir.list[i].fname,
1924 panel->search_buffer, l) == 0)) {
1925 unselect_item (panel);
1926 panel->selected = i;
1927 select_item (panel);
1928 found = 1;
1929 break;
1932 if (!found)
1933 panel->search_buffer[--l] = 0;
1935 paint_panel (panel);
1938 static void
1939 start_search (WPanel *panel)
1941 if (panel->searching){
1942 if (panel->selected+1 == panel->count)
1943 panel->selected = 0;
1944 else
1945 move_down (panel);
1946 do_search (panel, 0);
1947 } else {
1948 panel->searching = 1;
1949 panel->search_buffer [0] = 0;
1950 display_mini_info (panel);
1951 mc_refresh ();
1955 /* Return 1 if the Enter key has been processed, 0 otherwise */
1956 static int
1957 do_enter_on_file_entry (file_entry *fe)
1959 char *full_name;
1962 * Directory or link to directory - change directory.
1963 * Try the same for the entries on which mc_lstat() has failed.
1965 if (S_ISDIR (fe->st.st_mode) || link_isdir (fe)
1966 || (fe->st.st_mode == 0)) {
1967 if (!do_cd (fe->fname, cd_exact))
1968 message (1, MSG_ERROR, _("Cannot change directory"));
1969 return 1;
1972 /* Try associated command */
1973 if (regex_command (fe->fname, "Open", 0) != 0)
1974 return 1;
1976 /* Check if the file is executable */
1977 full_name = concat_dir_and_file (current_panel->cwd, fe->fname);
1978 if (!is_exe (fe->st.st_mode) || !if_link_is_exe (full_name, fe)) {
1979 g_free (full_name);
1980 return 0;
1982 g_free (full_name);
1984 if (confirm_execute) {
1985 if (query_dialog
1986 (_(" The Midnight Commander "),
1987 _(" Do you really want to execute? "), 0, 2, _("&Yes"),
1988 _("&No")) != 0)
1989 return 1;
1991 #ifdef USE_VFS
1992 if (!vfs_current_is_local ()) {
1993 char *tmp;
1994 int ret;
1996 tmp = concat_dir_and_file (vfs_get_current_dir (), fe->fname);
1997 ret = mc_setctl (tmp, VFS_SETCTL_RUN, NULL);
1998 g_free (tmp);
1999 /* We took action only if the dialog was shown or the execution
2000 * was successful */
2001 return confirm_execute || (ret == 0);
2003 #endif
2006 char *tmp = name_quote (fe->fname, 0);
2007 char *cmd = g_strconcat (".", PATH_SEP_STR, tmp, (char *) NULL);
2008 g_free (tmp);
2009 shell_execute (cmd, 0);
2010 g_free (cmd);
2013 return 1;
2016 static int
2017 do_enter (WPanel *panel)
2019 return do_enter_on_file_entry (selection (panel));
2022 static void
2023 chdir_other_panel (WPanel *panel)
2025 char *new_dir;
2026 char *sel_entry = NULL;
2028 if (get_other_type () != view_listing) {
2029 set_display_type (get_other_index (), view_listing);
2032 if (!S_ISDIR (panel->dir.list [panel->selected].st.st_mode)) {
2033 new_dir = concat_dir_and_file (panel->cwd, "..");
2034 sel_entry = strrchr(panel->cwd, PATH_SEP);
2035 } else
2036 new_dir = concat_dir_and_file (panel->cwd, panel->dir.list [panel->selected].fname);
2038 change_panel ();
2039 do_cd (new_dir, cd_exact);
2040 if (sel_entry)
2041 try_to_select (current_panel, sel_entry);
2042 change_panel ();
2044 move_down (panel);
2046 g_free (new_dir);
2050 * Make the current directory of the current panel also the current
2051 * directory of the other panel. Put the other panel to the listing
2052 * mode if needed. If the current panel is panelized, the other panel
2053 * doesn't become panelized.
2055 static void
2056 sync_other_panel (WPanel *panel)
2058 if (get_other_type () != view_listing) {
2059 set_display_type (get_other_index (), view_listing);
2062 do_panel_cd (other_panel, current_panel->cwd, cd_exact);
2064 /* try to select current filename on the other panel */
2065 if (!panel->is_panelized) {
2066 try_to_select (other_panel, selection (panel)->fname);
2070 static void
2071 chdir_to_readlink (WPanel *panel)
2073 char *new_dir;
2075 if (get_other_type () != view_listing)
2076 return;
2078 if (S_ISLNK (panel->dir.list [panel->selected].st.st_mode)) {
2079 char buffer [MC_MAXPATHLEN], *p;
2080 int i;
2081 struct stat st;
2083 i = readlink (selection (panel)->fname, buffer, MC_MAXPATHLEN - 1);
2084 if (i < 0)
2085 return;
2086 if (mc_stat (selection (panel)->fname, &st) < 0)
2087 return;
2088 buffer [i] = 0;
2089 if (!S_ISDIR (st.st_mode)) {
2090 p = strrchr (buffer, PATH_SEP);
2091 if (p && !p[1]) {
2092 *p = 0;
2093 p = strrchr (buffer, PATH_SEP);
2095 if (!p)
2096 return;
2097 p[1] = 0;
2099 if (*buffer == PATH_SEP)
2100 new_dir = g_strdup (buffer);
2101 else
2102 new_dir = concat_dir_and_file (panel->cwd, buffer);
2104 change_panel ();
2105 do_cd (new_dir, cd_exact);
2106 change_panel ();
2108 move_down (panel);
2110 g_free (new_dir);
2114 typedef void (*panel_key_callback) (WPanel *);
2115 typedef struct {
2116 int key_code;
2117 panel_key_callback fn;
2118 } panel_key_map;
2120 static void cmd_do_enter(WPanel *wp) { (void) do_enter(wp); }
2121 static void cmd_view_simple(WPanel *wp) { view_simple_cmd(); }
2122 static void cmd_edit_new(WPanel *wp) { edit_cmd_new(); }
2123 static void cmd_copy_local(WPanel *wp) { copy_cmd_local(); }
2124 static void cmd_rename_local(WPanel *wp) { ren_cmd_local(); }
2125 static void cmd_delete_local(WPanel *wp) { delete_cmd_local(); }
2126 static void cmd_select(WPanel *wp) { select_cmd(); }
2127 static void cmd_unselect(WPanel *wp) { unselect_cmd(); }
2128 static void cmd_reverse_selection(WPanel *wp) { reverse_selection_cmd(); }
2130 static const panel_key_map panel_keymap [] = {
2131 { KEY_DOWN, move_down },
2132 { KEY_UP, move_up },
2134 /* The action button :-) */
2135 { '\n', cmd_do_enter },
2136 { KEY_ENTER, cmd_do_enter },
2138 { KEY_IC, mark_file },
2139 { KEY_HOME, move_home },
2140 { KEY_A1, move_home },
2141 { ALT ('<'), move_home },
2142 { KEY_C1, move_end },
2143 { KEY_END, move_end },
2144 { ALT ('>'), move_end },
2145 { KEY_NPAGE, next_page },
2146 { KEY_PPAGE, prev_page },
2147 { KEY_NPAGE | KEY_M_CTRL, ctrl_next_page },
2148 { KEY_PPAGE | KEY_M_CTRL, ctrl_prev_page },
2150 /* To quickly move in the panel */
2151 { ALT('g'), goto_top_file },
2152 { ALT('r'), goto_middle_file }, /* M-r like emacs */
2153 { ALT('j'), goto_bottom_file },
2155 /* Emacs-like bindings */
2156 { XCTRL('v'), next_page }, /* C-v like emacs */
2157 { ALT('v'), prev_page }, /* M-v like emacs */
2158 { XCTRL('p'), move_up }, /* C-p like emacs */
2159 { XCTRL('n'), move_down }, /* C-n like emacs */
2160 { XCTRL('s'), start_search }, /* C-s like emacs */
2161 { ALT('s'), start_search }, /* M-s not like emacs */
2162 { XCTRL('t'), mark_file },
2163 { ALT('o'), chdir_other_panel },
2164 { ALT('i'), sync_other_panel },
2165 { ALT('l'), chdir_to_readlink },
2166 { ALT('H'), directory_history_list },
2167 { KEY_F(13), cmd_view_simple },
2168 { KEY_F(14), cmd_edit_new },
2169 { KEY_F(15), cmd_copy_local },
2170 { KEY_F(16), cmd_rename_local },
2171 { KEY_F(18), cmd_delete_local },
2172 { ALT('y'), directory_history_prev },
2173 { ALT('u'), directory_history_next },
2174 { ALT('+'), cmd_select },
2175 { KEY_KP_ADD, cmd_select },
2176 { ALT('\\'), cmd_unselect },
2177 { ALT('-'), cmd_unselect },
2178 { KEY_KP_SUBTRACT, cmd_unselect },
2179 { ALT('*'), cmd_reverse_selection },
2180 { KEY_KP_MULTIPLY, cmd_reverse_selection },
2181 { 0, 0 }
2184 static inline cb_ret_t
2185 panel_key (WPanel *panel, int key)
2187 int i;
2189 for (i = 0; panel_keymap[i].key_code; i++) {
2190 if (key == panel_keymap[i].key_code) {
2191 int old_searching = panel->searching;
2193 if (panel_keymap[i].fn != start_search)
2194 panel->searching = 0;
2196 (*panel_keymap[i].fn) (panel);
2198 if (panel->searching != old_searching)
2199 display_mini_info (panel);
2200 return MSG_HANDLED;
2203 if (torben_fj_mode && key == ALT ('h')) {
2204 goto_middle_file (panel);
2205 return MSG_HANDLED;
2208 /* We do not want to take a key press if nothing can be done with it */
2209 /* The command line widget may do something more useful */
2210 if (key == KEY_LEFT)
2211 return move_left (panel, key);
2213 if (key == KEY_RIGHT)
2214 return move_right (panel, key);
2216 if (is_abort_char (key)) {
2217 panel->searching = 0;
2218 display_mini_info (panel);
2219 return MSG_HANDLED;
2222 /* Do not eat characters not meant for the panel below ' ' (e.g. C-l). */
2223 if ((key >= ' ' && key <= 255) || key == KEY_BACKSPACE) {
2224 if (panel->searching) {
2225 do_search (panel, key);
2226 return MSG_HANDLED;
2229 if (!command_prompt) {
2230 start_search (panel);
2231 do_search (panel, key);
2232 return MSG_HANDLED;
2236 return MSG_NOT_HANDLED;
2239 static cb_ret_t
2240 panel_callback (Widget *w, widget_msg_t msg, int parm)
2242 WPanel *panel = (WPanel *) w;
2243 Dlg_head *h = panel->widget.parent;
2245 switch (msg) {
2246 case WIDGET_DRAW:
2247 paint_panel (panel);
2248 return MSG_HANDLED;
2250 case WIDGET_FOCUS:
2251 current_panel = panel;
2252 panel->active = 1;
2253 if (mc_chdir (panel->cwd) != 0) {
2254 char *cwd = strip_password (g_strdup (panel->cwd), 1);
2255 message (1, MSG_ERROR, _(" Cannot chdir to \"%s\" \n %s "),
2256 cwd, unix_error_string (errno));
2257 g_free(cwd);
2258 } else
2259 subshell_chdir (panel->cwd);
2261 update_xterm_title_path ();
2262 select_item (panel);
2263 show_dir (panel);
2264 paint_dir (panel);
2265 panel->dirty = 0;
2267 buttonbar_set_label (h, 1, _("Help"), help_cmd);
2268 buttonbar_set_label (h, 2, _("Menu"), user_file_menu_cmd);
2269 buttonbar_set_label (h, 3, _("View"), view_cmd);
2270 buttonbar_set_label (h, 4, _("Edit"), edit_cmd);
2271 buttonbar_set_label (h, 5, _("Copy"), copy_cmd);
2272 buttonbar_set_label (h, 6, _("RenMov"), ren_cmd);
2273 buttonbar_set_label (h, 7, _("Mkdir"), mkdir_cmd);
2274 buttonbar_set_label (h, 8, _("Delete"), delete_cmd);
2275 buttonbar_redraw (h);
2276 return MSG_HANDLED;
2278 case WIDGET_UNFOCUS:
2279 /* Janne: look at this for the multiple panel options */
2280 if (panel->searching){
2281 panel->searching = 0;
2282 display_mini_info (panel);
2284 panel->active = 0;
2285 show_dir (panel);
2286 unselect_item (panel);
2287 return MSG_HANDLED;
2289 case WIDGET_KEY:
2290 return panel_key (panel, parm);
2292 case WIDGET_DESTROY:
2293 panel_destroy (panel);
2294 return MSG_HANDLED;
2296 default:
2297 return default_proc (msg, parm);
2301 void
2302 file_mark (WPanel *panel, int index, int val)
2304 if (panel->dir.list[index].f.marked != val) {
2305 panel->dir.list[index].f.marked = val;
2306 panel->dirty = 1;
2310 /* */
2311 /* Panel mouse events support routines */
2312 /* */
2313 static int mouse_marking = 0;
2315 static void
2316 mouse_toggle_mark (WPanel *panel)
2318 do_mark_file (panel, 0);
2319 mouse_marking = selection (panel)->f.marked;
2322 static void
2323 mouse_set_mark (WPanel *panel)
2325 if (mouse_marking && !(selection (panel)->f.marked))
2326 do_mark_file (panel, 0);
2327 else if (!mouse_marking && (selection (panel)->f.marked))
2328 do_mark_file (panel, 0);
2331 static inline int
2332 mark_if_marking (WPanel *panel, Gpm_Event *event)
2334 if (event->buttons & GPM_B_RIGHT){
2335 if (event->type & GPM_DOWN)
2336 mouse_toggle_mark (panel);
2337 else
2338 mouse_set_mark (panel);
2339 return 1;
2341 return 0;
2345 * Mouse callback of the panel minus repainting.
2346 * If the event is redirected to the menu, *redir is set to 1.
2348 static int
2349 do_panel_event (Gpm_Event *event, WPanel *panel, int *redir)
2351 const int lines = llines (panel);
2353 int my_index;
2355 /* Mouse wheel events */
2356 if ((event->buttons & GPM_B_UP) && (event->type & GPM_DOWN)) {
2357 prev_page (panel);
2358 return MOU_NORMAL;
2360 if ((event->buttons & GPM_B_DOWN) && (event->type & GPM_DOWN)) {
2361 next_page (panel);
2362 return MOU_NORMAL;
2365 /* "<" button */
2366 if (event->type & GPM_DOWN && event->x == 2 && event->y == 1) {
2367 directory_history_prev (panel);
2368 return MOU_NORMAL;
2371 /* ">" button */
2372 if (event->type & GPM_DOWN && event->x == panel->widget.cols - 1
2373 && event->y == 1) {
2374 directory_history_next (panel);
2375 return MOU_NORMAL;
2378 /* "v" button */
2379 if (event->type & GPM_DOWN && event->x == panel->widget.cols - 2
2380 && event->y == 1) {
2381 directory_history_list (panel);
2382 return MOU_NORMAL;
2385 /* rest of the upper frame, the menu is invisible - call menu */
2386 if (event->type & GPM_DOWN && event->y == 1 && !menubar_visible) {
2387 *redir = 1;
2388 event->x += panel->widget.x;
2389 return (*(the_menubar->widget.mouse)) (event, the_menubar);
2392 event->y -= 2;
2393 if ((event->type & (GPM_DOWN | GPM_DRAG))) {
2395 if (!dlg_widget_active (panel))
2396 change_panel ();
2398 if (event->y <= 0) {
2399 mark_if_marking (panel, event);
2400 if (mouse_move_pages)
2401 prev_page (panel);
2402 else
2403 move_up (panel);
2404 return MOU_REPEAT;
2407 if (!((panel->top_file + event->y <= panel->count)
2408 && event->y <= lines)) {
2409 mark_if_marking (panel, event);
2410 if (mouse_move_pages)
2411 next_page (panel);
2412 else
2413 move_down (panel);
2414 return MOU_REPEAT;
2416 my_index = panel->top_file + event->y - 1;
2417 if (panel->split) {
2418 if (event->x > ((panel->widget.cols - 2) / 2))
2419 my_index += llines (panel);
2422 if (my_index >= panel->count)
2423 my_index = panel->count - 1;
2425 if (my_index != panel->selected) {
2426 unselect_item (panel);
2427 panel->selected = my_index;
2428 select_item (panel);
2431 /* This one is new */
2432 mark_if_marking (panel, event);
2434 } else if ((event->type & (GPM_UP | GPM_DOUBLE)) ==
2435 (GPM_UP | GPM_DOUBLE)) {
2436 if (event->y > 0 && event->y <= lines)
2437 do_enter (panel);
2439 return MOU_NORMAL;
2442 /* Mouse callback of the panel */
2443 static int
2444 panel_event (Gpm_Event *event, void *data)
2446 WPanel *panel = data;
2447 int ret;
2448 int redir = 0;
2450 ret = do_panel_event (event, panel, &redir);
2451 if (!redir)
2452 panel_update_contents (panel);
2454 return ret;
2457 void
2458 panel_re_sort (WPanel *panel)
2460 char *filename;
2461 int i;
2463 if (panel == NULL)
2464 return;
2466 filename = g_strdup (selection (panel)->fname);
2467 unselect_item (panel);
2468 do_sort (&panel->dir, panel->sort_type, panel->count-1, panel->reverse,
2469 panel->case_sensitive, panel->exec_first);
2470 panel->selected = -1;
2471 for (i = panel->count; i; i--){
2472 if (!strcmp (panel->dir.list [i-1].fname, filename)){
2473 panel->selected = i-1;
2474 break;
2477 g_free (filename);
2478 panel->top_file = panel->selected - ITEMS (panel)/2;
2479 if (panel->top_file < 0)
2480 panel->top_file = 0;
2481 select_item (panel);
2482 panel->dirty = 1;
2485 void
2486 panel_set_sort_order (WPanel *panel, sortfn *sort_order)
2488 if (sort_order == 0)
2489 return;
2491 panel->sort_type = sort_order;
2493 /* The directory is already sorted, we have to load the unsorted stuff */
2494 if (sort_order == (sortfn *) unsorted){
2495 char *current_file;
2497 current_file = g_strdup (panel->dir.list [panel->selected].fname);
2498 panel_reload (panel);
2499 try_to_select (panel, current_file);
2500 g_free (current_file);
2502 panel_re_sort (panel);