Removed type SHELL_ESCAPED_STR in favour of plain char*
[midnight-commander.git] / src / screen.c
blob981028724420e17434cf387212a595db47240cae
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 <mhl/memory.h>
31 #include <mhl/string.h>
33 #include "global.h"
34 #include "tty.h"
35 #include "dir.h"
36 #include "panel.h"
37 #include "color.h"
38 #include "tree.h"
39 #include "win.h"
40 #include "ext.h" /* regexp_command */
41 #include "mouse.h" /* For Gpm_Event */
42 #include "layout.h" /* Most layout variables are here */
43 #include "wtools.h" /* for message (...) */
44 #include "cmd.h"
45 #include "key.h" /* XCTRL and ALT macros */
46 #include "setup.h" /* For loading/saving panel options */
47 #include "user.h"
48 #include "profile.h"
49 #include "execute.h"
50 #include "widget.h"
51 #include "menu.h" /* menubar_visible */
52 #include "main-widgets.h"
53 #include "main.h" /* the_menubar */
54 #include "unixcompat.h"
56 #define ELEMENTS(arr) ( sizeof(arr) / sizeof((arr)[0]) )
58 #define J_LEFT 1
59 #define J_RIGHT 2
60 #define J_CENTER 3
62 #define IS_FIT(x) ((x) & 0x0004)
63 #define MAKE_FIT(x) ((x) | 0x0004)
64 #define HIDE_FIT(x) ((x) & 0x0003)
66 #define J_LEFT_FIT 5
67 #define J_RIGHT_FIT 6
68 #define J_CENTER_FIT 7
70 #define NORMAL 0
71 #define SELECTED 1
72 #define MARKED 2
73 #define MARKED_SELECTED 3
74 #define STATUS 5
77 * This describes a format item. The parse_display_format routine parses
78 * the user specified format and creates a linked list of format_e structures.
80 typedef struct format_e {
81 struct format_e *next;
82 int requested_field_len;
83 int field_len;
84 int just_mode;
85 int expand;
86 const char *(*string_fn)(file_entry *, int len);
87 const char *title;
88 const char *id;
89 } format_e;
91 /* If true, show the mini-info on the panel */
92 int show_mini_info = 1;
94 /* If true, then use stat() on the cwd to determine directory changes */
95 int fast_reload = 0;
97 /* If true, use some usability hacks by Torben */
98 int torben_fj_mode = 0;
100 /* If true, up/down keys scroll the pane listing by pages */
101 int panel_scroll_pages = 1;
103 /* If 1, we use permission hilighting */
104 int permission_mode = 0;
106 /* If 1 - then add per file type hilighting */
107 int filetype_mode = 1;
109 /* The hook list for the select file function */
110 Hook *select_file_hook = 0;
112 static cb_ret_t panel_callback (Widget *, widget_msg_t msg, int parm);
113 static int panel_event (Gpm_Event *event, void *);
114 static void paint_frame (WPanel *panel);
115 static const char *panel_format (WPanel *panel);
116 static const char *mini_status_format (WPanel *panel);
118 /* This macro extracts the number of available lines in a panel */
119 #define llines(p) (p->widget.lines-3 - (show_mini_info ? 2 : 0))
121 static void
122 set_colors (WPanel *panel)
124 standend ();
125 attrset (NORMAL_COLOR);
128 /* Delete format string, it is a linked list */
129 static void
130 delete_format (format_e *format)
132 format_e *next;
134 while (format){
135 next = format->next;
136 g_free (format);
137 format = next;
141 /* This code relies on the default justification!!! */
142 static void
143 add_permission_string (char *dest, int width, file_entry *fe, int attr, int color, int is_octal)
145 int i, r, l;
147 l = get_user_permissions (&fe->st);
149 if (is_octal){
150 /* Place of the access bit in octal mode */
151 l = width + l - 3;
152 r = l + 1;
153 } else {
154 /* The same to the triplet in string mode */
155 l = l * 3 + 1;
156 r = l + 3;
159 for(i = 0; i < width; i++){
160 if (i >= l && i < r){
161 if (attr == SELECTED || attr == MARKED_SELECTED)
162 attrset (MARKED_SELECTED_COLOR);
163 else
164 attrset (MARKED_COLOR);
165 } else
166 attrset (color);
168 addch (dest[i]);
172 /* String representations of various file attributes */
173 /* name */
174 static const char *
175 string_file_name (file_entry *fe, int len)
177 static char buffer [MC_MAXPATHLEN + 1];
178 size_t i;
180 for (i = 0; i < sizeof(buffer) - 1; i++) {
181 char c;
183 c = fe->fname[i];
185 if (!c)
186 break;
188 if (!is_printable(c))
189 c = '?';
191 buffer[i] = c;
194 buffer[i] = 0;
195 return buffer;
198 static inline unsigned int ilog10(dev_t n)
200 unsigned int digits = 0;
201 do {
202 digits++, n /= 10;
203 } while (n != 0);
204 return digits;
207 static void format_device_number (char *buf, size_t bufsize, dev_t dev)
209 dev_t major_dev = major(dev);
210 dev_t minor_dev = minor(dev);
211 unsigned int major_digits = ilog10(major_dev);
212 unsigned int minor_digits = ilog10(minor_dev);
214 g_assert(bufsize >= 1);
215 if (major_digits + 1 + minor_digits + 1 <= bufsize) {
216 g_snprintf(buf, bufsize, "%lu,%lu", (unsigned long) major_dev,
217 (unsigned long) minor_dev);
218 } else {
219 g_strlcpy(buf, _("[dev]"), bufsize);
223 /* size */
224 static const char *
225 string_file_size (file_entry *fe, int len)
227 static char buffer [BUF_TINY];
229 /* Don't ever show size of ".." since we don't calculate it */
230 if (!strcmp (fe->fname, "..")) {
231 return _("UP--DIR");
234 #ifdef HAVE_STRUCT_STAT_ST_RDEV
235 if (S_ISBLK (fe->st.st_mode) || S_ISCHR (fe->st.st_mode))
236 format_device_number (buffer, len + 1, fe->st.st_rdev);
237 else
238 #endif
240 size_trunc_len (buffer, len, fe->st.st_size, 0);
242 return buffer;
245 /* bsize */
246 static const char *
247 string_file_size_brief (file_entry *fe, int len)
249 if (S_ISLNK (fe->st.st_mode) && !fe->f.link_to_dir) {
250 return _("SYMLINK");
253 if ((S_ISDIR (fe->st.st_mode) || fe->f.link_to_dir) && strcmp (fe->fname, "..")) {
254 return _("SUB-DIR");
257 return string_file_size (fe, len);
260 /* This functions return a string representation of a file entry */
261 /* type */
262 static const char *
263 string_file_type (file_entry *fe, int len)
265 static char buffer[2];
267 if (S_ISDIR (fe->st.st_mode))
268 buffer[0] = PATH_SEP;
269 else if (S_ISLNK (fe->st.st_mode)) {
270 if (fe->f.link_to_dir)
271 buffer[0] = '~';
272 else if (fe->f.stale_link)
273 buffer[0] = '!';
274 else
275 buffer[0] = '@';
276 } else if (S_ISCHR (fe->st.st_mode))
277 buffer[0] = '-';
278 else if (S_ISSOCK (fe->st.st_mode))
279 buffer[0] = '=';
280 else if (S_ISDOOR (fe->st.st_mode))
281 buffer[0] = '>';
282 else if (S_ISBLK (fe->st.st_mode))
283 buffer[0] = '+';
284 else if (S_ISFIFO (fe->st.st_mode))
285 buffer[0] = '|';
286 else if (S_ISNAM (fe->st.st_mode))
287 buffer[0] = '#';
288 else if (!S_ISREG (fe->st.st_mode))
289 buffer[0] = '?'; /* non-regular of unknown kind */
290 else if (is_exe (fe->st.st_mode))
291 buffer[0] = '*';
292 else
293 buffer[0] = ' ';
294 buffer[1] = '\0';
295 return buffer;
298 /* mtime */
299 static const char *
300 string_file_mtime (file_entry *fe, int len)
302 if (!strcmp (fe->fname, "..")) {
303 return "";
305 return file_date (fe->st.st_mtime);
308 /* atime */
309 static const char *
310 string_file_atime (file_entry *fe, int len)
312 if (!strcmp (fe->fname, "..")) {
313 return "";
315 return file_date (fe->st.st_atime);
318 /* ctime */
319 static const char *
320 string_file_ctime (file_entry *fe, int len)
322 if (!strcmp (fe->fname, "..")) {
323 return "";
325 return file_date (fe->st.st_ctime);
328 /* perm */
329 static const char *
330 string_file_permission (file_entry *fe, int len)
332 return string_perm (fe->st.st_mode);
335 /* mode */
336 static const char *
337 string_file_perm_octal (file_entry *fe, int len)
339 static char buffer [10];
341 g_snprintf (buffer, sizeof (buffer), "0%06lo", (unsigned long) fe->st.st_mode);
342 return buffer;
345 /* nlink */
346 static const char *
347 string_file_nlinks (file_entry *fe, int len)
349 static char buffer[BUF_TINY];
351 g_snprintf (buffer, sizeof (buffer), "%16d", (int) fe->st.st_nlink);
352 return buffer;
355 /* inode */
356 static const char *
357 string_inode (file_entry *fe, int len)
359 static char buffer [10];
361 g_snprintf (buffer, sizeof (buffer), "%lu",
362 (unsigned long) fe->st.st_ino);
363 return buffer;
366 /* nuid */
367 static const char *
368 string_file_nuid (file_entry *fe, int len)
370 static char buffer [10];
372 g_snprintf (buffer, sizeof (buffer), "%lu",
373 (unsigned long) fe->st.st_uid);
374 return buffer;
377 /* ngid */
378 static const char *
379 string_file_ngid (file_entry *fe, int len)
381 static char buffer [10];
383 g_snprintf (buffer, sizeof (buffer), "%lu",
384 (unsigned long) fe->st.st_gid);
385 return buffer;
388 /* owner */
389 static const char *
390 string_file_owner (file_entry *fe, int len)
392 return get_owner (fe->st.st_uid);
395 /* group */
396 static const char *
397 string_file_group (file_entry *fe, int len)
399 return get_group (fe->st.st_gid);
402 /* mark */
403 static const char *
404 string_marked (file_entry *fe, int len)
406 return fe->f.marked ? "*" : " ";
409 /* space */
410 static const char *
411 string_space (file_entry *fe, int len)
413 return " ";
416 /* dot */
417 static const char *
418 string_dot (file_entry *fe, int len)
420 return ".";
423 #define GT 1
425 static struct {
426 const char *id;
427 int min_size;
428 int expands;
429 int default_just;
430 const char *title;
431 int use_in_gui;
432 const char *(*string_fn)(file_entry *, int);
433 sortfn *sort_routine; /* This field is currently unused. */
434 } formats [] = {
435 { "name", 12, 1, J_LEFT_FIT, N_("Name"), 1, string_file_name, (sortfn *) sort_name },
436 { "size", 7, 0, J_RIGHT, N_("Size"), 1, string_file_size, (sortfn *) sort_size },
437 { "bsize", 7, 0, J_RIGHT, N_("Size"), 1, string_file_size_brief, (sortfn *) sort_size },
438 { "type", GT, 0, J_LEFT, "", 2, string_file_type, NULL },
439 { "mtime", 12, 0, J_RIGHT, N_("MTime"), 1, string_file_mtime, (sortfn *) sort_time },
440 { "atime", 12, 0, J_RIGHT, N_("ATime"), 1, string_file_atime, (sortfn *) sort_atime },
441 { "ctime", 12, 0, J_RIGHT, N_("CTime"), 1, string_file_ctime, (sortfn *) sort_ctime },
442 { "perm", 10, 0, J_LEFT, N_("Permission"),1,string_file_permission, NULL },
443 { "mode", 6, 0, J_RIGHT, N_("Perm"), 1, string_file_perm_octal, NULL },
444 { "nlink", 2, 0, J_RIGHT, N_("Nl"), 1, string_file_nlinks, NULL },
445 { "inode", 5, 0, J_RIGHT, N_("Inode"), 1, string_inode, (sortfn *) sort_inode },
446 { "nuid", 5, 0, J_RIGHT, N_("UID"), 1, string_file_nuid, NULL },
447 { "ngid", 5, 0, J_RIGHT, N_("GID"), 1, string_file_ngid, NULL },
448 { "owner", 8, 0, J_LEFT_FIT, N_("Owner"), 1, string_file_owner, NULL },
449 { "group", 8, 0, J_LEFT_FIT, N_("Group"), 1, string_file_group, NULL },
450 { "mark", 1, 0, J_RIGHT, " ", 1, string_marked, NULL },
451 { "|", 1, 0, J_RIGHT, " ", 0, NULL, NULL },
452 { "space", 1, 0, J_RIGHT, " ", 0, string_space, NULL },
453 { "dot", 1, 0, J_RIGHT, " ", 0, string_dot, NULL },
456 static char *
457 to_buffer (char *dest, int just_mode, int len, const char *txt)
459 int txtlen = strlen (txt);
460 int still, over;
462 /* Fill buffer with spaces */
463 memset (dest, ' ', len);
465 still = (over=(txtlen > len)) ? (txtlen - len) : (len - txtlen);
467 switch (HIDE_FIT(just_mode)){
468 case J_LEFT:
469 still = 0;
470 break;
471 case J_CENTER:
472 still /= 2;
473 break;
474 case J_RIGHT:
475 default:
476 break;
479 if (over){
480 if (IS_FIT(just_mode))
481 strcpy (dest, name_trunc(txt, len));
482 else
483 strncpy (dest, txt+still, len);
484 } else
485 strncpy (dest+still, txt, txtlen);
487 dest[len] = '\0';
489 return (dest + len);
492 static int
493 file_compute_color (int attr, file_entry *fe)
495 switch (attr) {
496 case SELECTED:
497 return (SELECTED_COLOR);
498 case MARKED:
499 return (MARKED_COLOR);
500 case MARKED_SELECTED:
501 return (MARKED_SELECTED_COLOR);
502 case STATUS:
503 return (NORMAL_COLOR);
504 case NORMAL:
505 default:
506 if (!filetype_mode)
507 return (NORMAL_COLOR);
510 /* if filetype_mode == true */
511 if (S_ISDIR (fe->st.st_mode))
512 return (DIRECTORY_COLOR);
513 else if (S_ISLNK (fe->st.st_mode)) {
514 if (fe->f.link_to_dir)
515 return (DIRECTORY_COLOR);
516 else if (fe->f.stale_link)
517 return (STALE_LINK_COLOR);
518 else
519 return (LINK_COLOR);
520 } else if (S_ISSOCK (fe->st.st_mode))
521 return (SPECIAL_COLOR);
522 else if (S_ISCHR (fe->st.st_mode))
523 return (DEVICE_COLOR);
524 else if (S_ISBLK (fe->st.st_mode))
525 return (DEVICE_COLOR);
526 else if (S_ISNAM (fe->st.st_mode))
527 return (DEVICE_COLOR);
528 else if (S_ISFIFO (fe->st.st_mode))
529 return (SPECIAL_COLOR);
530 else if (S_ISDOOR (fe->st.st_mode))
531 return (SPECIAL_COLOR);
532 else if (!S_ISREG (fe->st.st_mode))
533 return (STALE_LINK_COLOR); /* non-regular file of unknown kind */
534 else if (is_exe (fe->st.st_mode))
535 return (EXECUTABLE_COLOR);
536 else if (fe->fname && (!strcmp (fe->fname, "core")
537 || !strcmp (extension (fe->fname), "core")))
538 return (CORE_COLOR);
540 return (NORMAL_COLOR);
543 /* Formats the file number file_index of panel in the buffer dest */
544 static void
545 format_file (char *dest, int limit, WPanel *panel, int file_index, int width, int attr, int isstatus)
547 int color, length, empty_line;
548 const char *txt;
549 char *old_pos;
550 char *cdest = dest;
551 format_e *format, *home;
552 file_entry *fe;
554 length = 0;
555 empty_line = (file_index >= panel->count);
556 home = (isstatus) ? panel->status_format : panel->format;
557 fe = &panel->dir.list [file_index];
559 if (!empty_line)
560 color = file_compute_color (attr, fe);
561 else
562 color = NORMAL_COLOR;
564 for (format = home; format; format = format->next){
566 if (length == width)
567 break;
569 if (format->string_fn){
570 int len;
572 if (empty_line)
573 txt = " ";
574 else
575 txt = (*format->string_fn)(fe, format->field_len);
577 old_pos = cdest;
579 len = format->field_len;
580 if (len + length > width)
581 len = width - length;
582 if (len + (cdest - dest) > limit)
583 len = limit - (cdest - dest);
584 if (len <= 0)
585 break;
586 cdest = to_buffer (cdest, format->just_mode, len, txt);
587 length += len;
589 attrset (color);
591 if (permission_mode && !strcmp(format->id, "perm"))
592 add_permission_string (old_pos, format->field_len, fe, attr, color, 0);
593 else if (permission_mode && !strcmp(format->id, "mode"))
594 add_permission_string (old_pos, format->field_len, fe, attr, color, 1);
595 else
596 addstr (old_pos);
598 } else {
599 if (attr == SELECTED || attr == MARKED_SELECTED)
600 attrset (SELECTED_COLOR);
601 else
602 attrset (NORMAL_COLOR);
603 one_vline ();
604 length++;
608 if (length < width){
609 int still = width - length;
610 while (still--)
611 addch (' ');
615 static void
616 repaint_file (WPanel *panel, int file_index, int mv, int attr, int isstatus)
618 int second_column = 0;
619 int width, offset;
620 char buffer [BUF_MEDIUM];
622 offset = 0;
623 if (!isstatus && panel->split){
625 second_column = (file_index - panel->top_file) / llines (panel);
626 width = (panel->widget.cols - 2)/2 - 1;
628 if (second_column){
629 offset = 1 + width;
630 width = (panel->widget.cols-2) - (panel->widget.cols-2)/2 - 1;
632 } else
633 width = (panel->widget.cols - 2);
635 /* Nothing to paint */
636 if (width <= 0)
637 return;
639 if (mv){
640 if (!isstatus && panel->split){
641 widget_move (&panel->widget,
642 (file_index - panel->top_file) %
643 llines (panel) + 2,
644 (offset + 1));
645 } else
646 widget_move (&panel->widget, file_index - panel->top_file + 2, 1);
649 format_file (buffer, sizeof(buffer), panel, file_index, width, attr, isstatus);
651 if (!isstatus && panel->split){
652 if (second_column)
653 addch (' ');
654 else {
655 attrset (NORMAL_COLOR);
656 one_vline ();
661 static void
662 display_mini_info (WPanel *panel)
664 if (!show_mini_info)
665 return;
667 widget_move (&panel->widget, llines (panel)+3, 1);
669 if (panel->searching){
670 attrset (INPUT_COLOR);
671 tty_printf ("/%-*s", panel->widget.cols-3, panel->search_buffer);
672 attrset (NORMAL_COLOR);
673 return;
676 /* Status displays total marked size */
677 if (panel->marked){
678 char buffer[BUF_SMALL], b_bytes[BUF_SMALL];
679 const char *p = " %-*s";
680 int cols = panel->widget.cols-2;
682 attrset (MARKED_COLOR);
683 tty_printf ("%*s", cols, " ");
684 widget_move (&panel->widget, llines (panel)+3, 1);
687 * This is a trick to use two ngettext() calls in one sentence.
688 * First make "N bytes", then insert it into "X in M files".
690 g_snprintf(b_bytes, sizeof (b_bytes),
691 ngettext("%s byte", "%s bytes",
692 (unsigned long)panel->total),
693 size_trunc_sep(panel->total));
694 g_snprintf(buffer, sizeof (buffer),
695 ngettext("%s in %d file", "%s in %d files", panel->marked),
696 b_bytes, panel->marked);
698 if ((int) strlen (buffer) > cols-2){
699 buffer [cols] = 0;
700 p += 2;
701 } else
702 cols -= 2;
703 tty_printf (p, cols, buffer);
704 return;
707 /* Status resolves links and show them */
708 set_colors (panel);
710 if (S_ISLNK (panel->dir.list [panel->selected].st.st_mode)){
711 char *link, link_target [MC_MAXPATHLEN];
712 int len;
714 link = mhl_str_dir_plus_file (panel->cwd, panel->dir.list [panel->selected].fname);
715 len = mc_readlink (link, link_target, MC_MAXPATHLEN - 1);
716 g_free (link);
717 if (len > 0){
718 link_target[len] = 0;
719 tty_printf ("-> %-*s", panel->widget.cols - 5,
720 name_trunc (link_target, panel->widget.cols - 5));
721 } else
722 tty_printf ("%-*s", panel->widget.cols - 2, _("<readlink failed>"));
723 return;
726 /* Default behavior */
727 repaint_file (panel, panel->selected, 0, STATUS, 1);
728 return;
731 static void
732 paint_dir (WPanel *panel)
734 int i;
735 int color; /* Color value of the line */
736 int items; /* Number of items */
738 items = llines (panel) * (panel->split ? 2 : 1);
740 for (i = 0; i < items; i++){
741 if (i+panel->top_file >= panel->count)
742 color = 0;
743 else {
744 color = 2 * (panel->dir.list [i+panel->top_file].f.marked);
745 color += (panel->selected==i+panel->top_file && panel->active);
747 repaint_file (panel, i+panel->top_file, 1, color, 0);
749 standend ();
752 static void
753 mini_info_separator (WPanel *panel)
755 if (!show_mini_info)
756 return;
758 standend ();
759 widget_move (&panel->widget, llines (panel) + 2, 1);
760 #ifdef HAVE_SLANG
761 attrset (NORMAL_COLOR);
762 hline (ACS_HLINE, panel->widget.cols - 2);
763 #else
764 hline ((slow_terminal ? '-' : ACS_HLINE) | NORMAL_COLOR,
765 panel->widget.cols - 2);
766 #endif /* !HAVE_SLANG */
769 static void
770 show_dir (WPanel *panel)
772 char *tmp;
774 set_colors (panel);
775 draw_double_box (panel->widget.parent,
776 panel->widget.y, panel->widget.x,
777 panel->widget.lines, panel->widget.cols);
779 #ifdef HAVE_SLANG
780 if (show_mini_info) {
781 SLsmg_draw_object (panel->widget.y + llines (panel) + 2,
782 panel->widget.x, SLSMG_LTEE_CHAR);
783 SLsmg_draw_object (panel->widget.y + llines (panel) + 2,
784 panel->widget.x + panel->widget.cols - 1,
785 SLSMG_RTEE_CHAR);
787 #endif /* HAVE_SLANG */
789 if (panel->active)
790 attrset (REVERSE_COLOR);
792 widget_move (&panel->widget, 0, 3);
793 addch (' ');
795 tmp = g_malloc (panel->widget.cols + 1);
796 tmp[panel->widget.cols] = '\0';
798 trim (strip_home_and_password (panel->cwd), tmp,
799 max (panel->widget.cols - 9, 0));
800 addstr (tmp);
801 g_free (tmp);
803 addch (' ');
804 widget_move (&panel->widget, 0, 1);
805 addstr ("<");
806 widget_move (&panel->widget, 0, panel->widget.cols - 2);
807 addstr (">");
808 widget_move (&panel->widget, 0, panel->widget.cols - 3);
809 addstr ("v");
811 if (panel->active)
812 standend ();
815 /* To be used only by long_frame and full_frame to adjust top_file */
816 static void
817 adjust_top_file (WPanel *panel)
819 int old_top = panel->top_file;
821 if (panel->selected - old_top > llines (panel))
822 panel->top_file = panel->selected;
823 if (old_top - panel->count > llines (panel))
824 panel->top_file = panel->count - llines (panel);
828 * Repaint everything that can change on the panel - title, entries and
829 * mini status. The rest of the frame and the mini status separator are
830 * not repainted.
832 static void
833 panel_update_contents (WPanel *panel)
835 show_dir (panel);
836 paint_dir (panel);
837 display_mini_info (panel);
838 panel->dirty = 0;
841 /* Repaint everything, including frame and separator */
842 static void
843 paint_panel (WPanel *panel)
845 paint_frame (panel);
846 panel_update_contents (panel);
847 mini_info_separator (panel);
851 * Repaint the contents of the panels without frames. To schedule panel
852 * for repainting, set panel->dirty to 1. There are many reasons why
853 * the panels need to be repainted, and this is a costly operation, so
854 * it's done once per event.
856 void
857 update_dirty_panels (void)
859 if (current_panel->dirty)
860 panel_update_contents (current_panel);
862 if ((get_other_type () == view_listing) && other_panel->dirty)
863 panel_update_contents (other_panel);
866 static void
867 do_select (WPanel *panel, int i)
869 if (i != panel->selected) {
870 panel->dirty = 1;
871 panel->selected = i;
872 panel->top_file = panel->selected - (panel->widget.lines - 2) / 2;
873 if (panel->top_file < 0)
874 panel->top_file = 0;
878 static inline void
879 do_try_to_select (WPanel *panel, const char *name)
881 int i;
882 char *subdir;
884 if (!name) {
885 do_select(panel, 0);
886 return;
889 /* We only want the last component of the directory,
890 * and from this only the name without suffix. */
891 subdir = vfs_strip_suffix_from_filename (x_basename(name));
893 /* Search that subdirectory, if found select it */
894 for (i = 0; i < panel->count; i++){
895 if (strcmp (subdir, panel->dir.list [i].fname) == 0) {
896 do_select (panel, i);
897 g_free (subdir);
898 return;
902 /* Try to select a file near the file that is missing */
903 if (panel->selected >= panel->count)
904 do_select (panel, panel->count-1);
905 g_free (subdir);
908 void
909 try_to_select (WPanel *panel, const char *name)
911 do_try_to_select (panel, name);
912 select_item (panel);
915 void
916 panel_update_cols (Widget *widget, int frame_size)
918 int cols, origin;
920 if (horizontal_split){
921 widget->cols = COLS;
922 return;
925 if (frame_size == frame_full){
926 cols = COLS;
927 origin = 0;
928 } else {
929 if (widget == get_panel_widget (0)){
930 cols = first_panel_size;
931 origin = 0;
932 } else {
933 cols = COLS-first_panel_size;
934 origin = first_panel_size;
938 widget->cols = cols;
939 widget->x = origin;
942 static char *
943 panel_save_name (WPanel *panel)
945 extern int saving_setup;
947 /* If the program is shuting down */
948 if ((midnight_shutdown && auto_save_setup) || saving_setup)
949 return g_strdup (panel->panel_name);
950 else
951 return g_strconcat ("Temporal:", panel->panel_name, (char *) NULL);
954 static void
955 panel_destroy (WPanel *p)
957 int i;
959 char *name = panel_save_name (p);
961 panel_save_setup (p, name);
962 panel_clean_dir (p);
964 /* save and clean history */
965 if (p->dir_history) {
966 history_put (p->hist_name, p->dir_history);
968 p->dir_history = g_list_first (p->dir_history);
969 g_list_foreach (p->dir_history, (GFunc) g_free, NULL);
970 g_list_free (p->dir_history);
973 g_free (p->hist_name);
975 delete_format (p->format);
976 delete_format (p->status_format);
978 g_free (p->user_format);
979 for (i = 0; i < LIST_TYPES; i++)
980 g_free (p->user_status_format[i]);
981 g_free (p->dir.list);
982 g_free (p->panel_name);
983 g_free (name);
986 static void
987 panel_format_modified (WPanel *panel)
989 panel->format_modified = 1;
992 /* Panel creation */
993 /* The parameter specifies the name of the panel for setup retieving */
994 WPanel *
995 panel_new (const char *panel_name)
997 WPanel *panel;
998 char *section;
999 int i, err;
1001 panel = g_new0 (WPanel, 1);
1003 /* No know sizes of the panel at startup */
1004 init_widget (&panel->widget, 0, 0, 0, 0, panel_callback, panel_event);
1006 /* We do not want the cursor */
1007 widget_want_cursor (panel->widget, 0);
1009 mc_get_current_wd (panel->cwd, sizeof (panel->cwd) - 2);
1010 strcpy (panel->lwd, ".");
1012 panel->hist_name = g_strconcat ("Dir Hist ", panel_name, (char *) NULL);
1013 panel->dir_history = history_get (panel->hist_name);
1014 directory_history_add (panel, panel->cwd);
1016 panel->dir.list = g_new (file_entry, MIN_FILES);
1017 panel->dir.size = MIN_FILES;
1018 panel->active = 0;
1019 panel->filter = 0;
1020 panel->split = 0;
1021 panel->top_file = 0;
1022 panel->selected = 0;
1023 panel->marked = 0;
1024 panel->total = 0;
1025 panel->reverse = 0;
1026 panel->dirty = 1;
1027 panel->searching = 0;
1028 panel->dirs_marked = 0;
1029 panel->is_panelized = 0;
1030 panel->format = 0;
1031 panel->status_format = 0;
1032 panel->format_modified = 1;
1034 panel->panel_name = g_strdup (panel_name);
1035 panel->user_format = g_strdup (DEFAULT_USER_FORMAT);
1037 for (i = 0; i < LIST_TYPES; i++)
1038 panel->user_status_format[i] = g_strdup (DEFAULT_USER_FORMAT);
1040 panel->search_buffer[0] = 0;
1041 panel->frame_size = frame_half;
1042 section = g_strconcat ("Temporal:", panel->panel_name, (char *) NULL);
1043 if (!profile_has_section (section, profile_name)) {
1044 g_free (section);
1045 section = g_strdup (panel->panel_name);
1047 panel_load_setup (panel, section);
1048 g_free (section);
1050 /* Load format strings */
1051 err = set_panel_formats (panel);
1052 if (err) {
1053 set_panel_formats (panel);
1056 /* Load the default format */
1057 panel->count =
1058 do_load_dir (panel->cwd, &panel->dir, panel->sort_type,
1059 panel->reverse, panel->case_sensitive,
1060 panel->exec_first, panel->filter);
1061 return panel;
1064 void
1065 panel_reload (WPanel *panel)
1067 struct stat current_stat;
1069 if (fast_reload && !stat (panel->cwd, &current_stat)
1070 && current_stat.st_ctime == panel->dir_stat.st_ctime
1071 && current_stat.st_mtime == panel->dir_stat.st_mtime)
1072 return;
1074 while (mc_chdir (panel->cwd) == -1) {
1075 char *last_slash;
1077 if (panel->cwd[0] == PATH_SEP && panel->cwd[1] == 0) {
1078 panel_clean_dir (panel);
1079 panel->count = set_zero_dir (&panel->dir);
1080 return;
1082 last_slash = strrchr (panel->cwd, PATH_SEP);
1083 if (!last_slash || last_slash == panel->cwd)
1084 strcpy (panel->cwd, PATH_SEP_STR);
1085 else
1086 *last_slash = 0;
1087 memset (&(panel->dir_stat), 0, sizeof (panel->dir_stat));
1088 show_dir (panel);
1091 panel->count =
1092 do_reload_dir (panel->cwd, &panel->dir, panel->sort_type,
1093 panel->count, panel->reverse, panel->case_sensitive,
1094 panel->exec_first, panel->filter);
1096 panel->dirty = 1;
1097 if (panel->selected >= panel->count)
1098 do_select (panel, panel->count - 1);
1100 recalculate_panel_summary (panel);
1103 static void
1104 paint_frame (WPanel *panel)
1106 int header_len;
1107 int spaces, extra;
1108 int side, width;
1110 const char *txt;
1111 if (!panel->split)
1112 adjust_top_file (panel);
1114 widget_erase (&panel->widget);
1115 show_dir (panel);
1117 widget_move (&panel->widget, 1, 1);
1119 for (side = 0; side <= panel->split; side++){
1120 format_e *format;
1122 if (side){
1123 attrset (NORMAL_COLOR);
1124 one_vline ();
1125 width = panel->widget.cols - panel->widget.cols/2 - 1;
1126 } else if (panel->split)
1127 width = panel->widget.cols/2 - 3;
1128 else
1129 width = panel->widget.cols - 2;
1131 for (format = panel->format; format; format = format->next){
1132 if (format->string_fn){
1133 txt = format->title;
1135 header_len = strlen (txt);
1136 if (header_len > format->field_len)
1137 header_len = format->field_len;
1139 attrset (MARKED_COLOR);
1140 spaces = (format->field_len - header_len) / 2;
1141 extra = (format->field_len - header_len) % 2;
1142 tty_printf ("%*s%.*s%*s", spaces, "",
1143 header_len, txt, spaces+extra, "");
1144 width -= 2 * spaces + extra + header_len;
1145 } else {
1146 attrset (NORMAL_COLOR);
1147 one_vline ();
1148 width --;
1149 continue;
1153 if (width > 0)
1154 tty_printf ("%*s", width, "");
1158 static const char *
1159 parse_panel_size (WPanel *panel, const char *format, int isstatus)
1161 int frame = frame_half;
1162 format = skip_separators (format);
1164 if (!strncmp (format, "full", 4)){
1165 frame = frame_full;
1166 format += 4;
1167 } else if (!strncmp (format, "half", 4)){
1168 frame = frame_half;
1169 format += 4;
1172 if (!isstatus){
1173 panel->frame_size = frame;
1174 panel->split = 0;
1177 /* Now, the optional column specifier */
1178 format = skip_separators (format);
1180 if (*format == '1' || *format == '2'){
1181 if (!isstatus)
1182 panel->split = *format == '2';
1183 format++;
1186 if (!isstatus)
1187 panel_update_cols (&(panel->widget), panel->frame_size);
1189 return skip_separators (format);
1192 /* Format is:
1194 all := panel_format? format
1195 panel_format := [full|half] [1|2]
1196 format := one_format_e
1197 | format , one_format_e
1199 one_format_e := just format.id [opt_size]
1200 just := [<=>]
1201 opt_size := : size [opt_expand]
1202 size := [0-9]+
1203 opt_expand := +
1207 static format_e *
1208 parse_display_format (WPanel *panel, const char *format, char **error, int isstatus, int *res_total_cols)
1210 format_e *darr, *old = 0, *home = 0; /* The formats we return */
1211 int total_cols = 0; /* Used columns by the format */
1212 int set_justify; /* flag: set justification mode? */
1213 int justify = 0; /* Which mode. */
1214 int items = 0; /* Number of items in the format */
1215 size_t i;
1217 static size_t i18n_timelength = 0; /* flag: check ?Time length at startup */
1219 *error = 0;
1221 if (i18n_timelength == 0) {
1222 i18n_timelength = i18n_checktimelength (); /* Musn't be 0 */
1224 for (i = 0; i < ELEMENTS(formats); i++)
1225 if (strcmp ("time", formats [i].id+1) == 0)
1226 formats [i].min_size = i18n_timelength;
1230 * This makes sure that the panel and mini status full/half mode
1231 * setting is equal
1233 format = parse_panel_size (panel, format, isstatus);
1235 while (*format){ /* format can be an empty string */
1236 int found = 0;
1238 darr = g_new (format_e, 1);
1240 /* I'm so ugly, don't look at me :-) */
1241 if (!home)
1242 home = old = darr;
1244 old->next = darr;
1245 darr->next = 0;
1246 old = darr;
1248 format = skip_separators (format);
1250 if (strchr ("<=>", *format)){
1251 set_justify = 1;
1252 switch (*format)
1254 case '<':
1255 justify = J_LEFT;
1256 break;
1257 case '=':
1258 justify = J_CENTER;
1259 break;
1260 case '>':
1261 default:
1262 justify = J_RIGHT;
1263 break;
1265 format = skip_separators (format+1);
1266 } else
1267 set_justify = 0;
1269 for (i = 0; i < ELEMENTS(formats); i++){
1270 int klen = strlen (formats [i].id);
1272 if (strncmp (format, formats [i].id, klen) != 0)
1273 continue;
1275 format += klen;
1277 if (formats [i].use_in_gui)
1278 items++;
1280 darr->requested_field_len = formats [i].min_size;
1281 darr->string_fn = formats [i].string_fn;
1282 if (formats [i].title [0])
1283 darr->title = _(formats [i].title);
1284 else
1285 darr->title = "";
1286 darr->id = formats [i].id;
1287 darr->expand = formats [i].expands;
1288 darr->just_mode = formats [i].default_just;
1290 if (set_justify) {
1291 if (IS_FIT(darr->just_mode))
1292 darr->just_mode = MAKE_FIT(justify);
1293 else
1294 darr->just_mode = justify;
1296 found = 1;
1298 format = skip_separators (format);
1300 /* If we have a size specifier */
1301 if (*format == ':'){
1302 int req_length;
1304 /* If the size was specified, we don't want
1305 * auto-expansion by default
1307 darr->expand = 0;
1308 format++;
1309 req_length = atoi (format);
1310 darr->requested_field_len = req_length;
1312 format = skip_numbers (format);
1314 /* Now, if they insist on expansion */
1315 if (*format == '+'){
1316 darr->expand = 1;
1317 format++;
1322 break;
1324 if (!found){
1325 char *tmp_format = g_strdup (format);
1327 int pos = min (8, strlen (format));
1328 delete_format (home);
1329 tmp_format [pos] = 0;
1330 *error = g_strconcat (_("Unknown tag on display format: "), tmp_format, (char *) NULL);
1331 g_free (tmp_format);
1332 return 0;
1334 total_cols += darr->requested_field_len;
1337 *res_total_cols = total_cols;
1338 return home;
1341 static format_e *
1342 use_display_format (WPanel *panel, const char *format, char **error, int isstatus)
1344 #define MAX_EXPAND 4
1345 int expand_top = 0; /* Max used element in expand */
1346 int usable_columns; /* Usable columns in the panel */
1347 int total_cols = 0;
1348 int i;
1349 format_e *darr, *home;
1351 if (!format)
1352 format = DEFAULT_USER_FORMAT;
1354 home = parse_display_format (panel, format, error, isstatus, &total_cols);
1356 if (*error)
1357 return 0;
1359 panel->dirty = 1;
1361 /* Status needn't to be split */
1362 usable_columns = ((panel->widget.cols-2)/((isstatus)
1364 : (panel->split+1))) - (!isstatus && panel->split);
1366 /* Look for the expandable fields and set field_len based on the requested field len */
1367 for (darr = home; darr && expand_top < MAX_EXPAND; darr = darr->next){
1368 darr->field_len = darr->requested_field_len;
1369 if (darr->expand)
1370 expand_top++;
1373 /* If we used more columns than the available columns, adjust that */
1374 if (total_cols > usable_columns){
1375 int pdif, dif = total_cols - usable_columns;
1377 while (dif){
1378 pdif = dif;
1379 for (darr = home; darr; darr = darr->next){
1380 if (dif && darr->field_len - 1){
1381 darr->field_len--;
1382 dif--;
1386 /* avoid endless loop if num fields > 40 */
1387 if (pdif == dif)
1388 break;
1390 total_cols = usable_columns; /* give up, the rest should be truncated */
1393 /* Expand the available space */
1394 if ((usable_columns > total_cols) && expand_top){
1395 int spaces = (usable_columns - total_cols) / expand_top;
1396 int extra = (usable_columns - total_cols) % expand_top;
1398 for (i = 0, darr = home; darr && (i < expand_top); darr = darr->next)
1399 if (darr->expand){
1400 darr->field_len += (spaces + ((i == 0) ? extra : 0));
1401 i++;
1404 return home;
1407 /* Switches the panel to the mode specified in the format */
1408 /* Seting up both format and status string. Return: 0 - on success; */
1409 /* 1 - format error; 2 - status error; 3 - errors in both formats. */
1411 set_panel_formats (WPanel *p)
1413 format_e *form;
1414 char *err;
1415 int retcode = 0;
1417 form = use_display_format (p, panel_format (p), &err, 0);
1419 if (err){
1420 g_free (err);
1421 retcode = 1;
1423 else {
1424 if (p->format)
1425 delete_format (p->format);
1427 p->format = form;
1430 if (show_mini_info){
1432 form = use_display_format (p, mini_status_format (p), &err, 1);
1434 if (err){
1435 g_free (err);
1436 retcode += 2;
1438 else {
1439 if (p->status_format)
1440 delete_format (p->status_format);
1442 p->status_format = form;
1446 panel_format_modified (p);
1447 panel_update_cols (&(p->widget), p->frame_size);
1449 if (retcode)
1450 message( 1, _("Warning" ), _( "User supplied format looks invalid, reverting to default." ) );
1451 if (retcode & 0x01){
1452 g_free (p->user_format);
1453 p->user_format = g_strdup (DEFAULT_USER_FORMAT);
1455 if (retcode & 0x02){
1456 g_free (p->user_status_format [p->list_type]);
1457 p->user_status_format [p->list_type] = g_strdup (DEFAULT_USER_FORMAT);
1460 return retcode;
1463 /* Given the panel->view_type returns the format string to be parsed */
1464 static const char *
1465 panel_format (WPanel *panel)
1467 switch (panel->list_type){
1469 case list_long:
1470 return "full perm space nlink space owner space group space size space mtime space name";
1472 case list_brief:
1473 return "half 2 type name";
1475 case list_user:
1476 return panel->user_format;
1478 default:
1479 case list_full:
1480 return "half type name | size | mtime";
1484 static const char *
1485 mini_status_format (WPanel *panel)
1487 if (panel->user_mini_status)
1488 return panel->user_status_format [panel->list_type];
1490 switch (panel->list_type){
1492 case list_long:
1493 return "full perm space nlink space owner space group space size space mtime space name";
1495 case list_brief:
1496 return "half type name space bsize space perm space";
1498 case list_full:
1499 return "half type name";
1501 default:
1502 case list_user:
1503 return panel->user_format;
1507 /* */
1508 /* Panel operation commands */
1509 /* */
1511 /* Returns the number of items in the given panel */
1512 static int
1513 ITEMS (WPanel *p)
1515 if (p->split)
1516 return llines (p) * 2;
1517 else
1518 return llines (p);
1521 /* Select current item and readjust the panel */
1522 void
1523 select_item (WPanel *panel)
1525 int items = ITEMS (panel);
1527 /* Although currently all over the code we set the selection and
1528 top file to decent values before calling select_item, I could
1529 forget it someday, so it's better to do the actual fitting here */
1531 if (panel->top_file < 0)
1532 panel->top_file = 0;
1534 if (panel->selected < 0)
1535 panel->selected = 0;
1537 if (panel->selected > panel->count - 1)
1538 panel->selected = panel->count - 1;
1540 if (panel->top_file > panel->count - 1)
1541 panel->top_file = panel->count - 1;
1543 if ((panel->count - panel->top_file) < items) {
1544 panel->top_file = panel->count - items;
1545 if (panel->top_file < 0)
1546 panel->top_file = 0;
1549 if (panel->selected < panel->top_file)
1550 panel->top_file = panel->selected;
1552 if ((panel->selected - panel->top_file) >= items)
1553 panel->top_file = panel->selected - items + 1;
1555 panel->dirty = 1;
1557 execute_hooks (select_file_hook);
1560 /* Clears all files in the panel, used only when one file was marked */
1561 void
1562 unmark_files (WPanel *panel)
1564 int i;
1566 if (!panel->marked)
1567 return;
1568 for (i = 0; i < panel->count; i++)
1569 file_mark (panel, i, 0);
1571 panel->dirs_marked = 0;
1572 panel->marked = 0;
1573 panel->total = 0;
1576 static void
1577 unselect_item (WPanel *panel)
1579 repaint_file (panel, panel->selected, 1, 2*selection (panel)->f.marked, 0);
1582 static void
1583 move_down (WPanel *panel)
1585 if (panel->selected+1 == panel->count)
1586 return;
1588 unselect_item (panel);
1589 panel->selected++;
1591 if (panel->selected - panel->top_file == ITEMS (panel) &&
1592 panel_scroll_pages){
1593 /* Scroll window half screen */
1594 panel->top_file += ITEMS (panel)/2;
1595 if (panel->top_file > panel->count - ITEMS (panel))
1596 panel->top_file = panel->count - ITEMS (panel);
1597 paint_dir (panel);
1598 select_item (panel);
1600 select_item (panel);
1603 static void
1604 move_up (WPanel *panel)
1606 if (panel->selected == 0)
1607 return;
1609 unselect_item (panel);
1610 panel->selected--;
1611 if (panel->selected < panel->top_file && panel_scroll_pages){
1612 /* Scroll window half screen */
1613 panel->top_file -= ITEMS (panel)/2;
1614 if (panel->top_file < 0) panel->top_file = 0;
1615 paint_dir (panel);
1617 select_item (panel);
1620 /* Changes the selection by lines (may be negative) */
1621 static void
1622 move_selection (WPanel *panel, int lines)
1624 int new_pos;
1625 int adjust = 0;
1627 new_pos = panel->selected + lines;
1628 if (new_pos >= panel->count)
1629 new_pos = panel->count-1;
1631 if (new_pos < 0)
1632 new_pos = 0;
1634 unselect_item (panel);
1635 panel->selected = new_pos;
1637 if (panel->selected - panel->top_file >= ITEMS (panel)){
1638 panel->top_file += lines;
1639 adjust = 1;
1642 if (panel->selected - panel->top_file < 0){
1643 panel->top_file += lines;
1644 adjust = 1;
1647 if (adjust){
1648 if (panel->top_file > panel->selected)
1649 panel->top_file = panel->selected;
1650 if (panel->top_file < 0)
1651 panel->top_file = 0;
1652 paint_dir (panel);
1654 select_item (panel);
1657 static cb_ret_t
1658 move_left (WPanel *panel, int c_code)
1660 if (panel->split) {
1661 move_selection (panel, -llines (panel));
1662 return MSG_HANDLED;
1663 } else
1664 return maybe_cd (1); /* cd .. */
1667 static int
1668 move_right (WPanel *panel, int c_code)
1670 if (panel->split) {
1671 move_selection (panel, llines (panel));
1672 return MSG_HANDLED;
1673 } else
1674 return maybe_cd (0); /* cd (selection) */
1677 static void
1678 prev_page (WPanel *panel)
1680 int items;
1682 if (!panel->selected && !panel->top_file)
1683 return;
1684 unselect_item (panel);
1685 items = ITEMS (panel);
1686 if (panel->top_file < items)
1687 items = panel->top_file;
1688 if (!items)
1689 panel->selected = 0;
1690 else
1691 panel->selected -= items;
1692 panel->top_file -= items;
1694 /* This keeps the selection in a reasonable place */
1695 if (panel->selected < 0)
1696 panel->selected = 0;
1697 if (panel->top_file < 0)
1698 panel->top_file = 0;
1699 select_item (panel);
1700 paint_dir (panel);
1703 static void
1704 ctrl_prev_page (WPanel *panel)
1706 do_cd ("..", cd_exact);
1709 static void
1710 next_page (WPanel *panel)
1712 int items;
1714 if (panel->selected == panel->count - 1)
1715 return;
1716 unselect_item (panel);
1717 items = ITEMS (panel);
1718 if (panel->top_file > panel->count - 2 * items)
1719 items = panel->count - items - panel->top_file;
1720 if (panel->top_file + items < 0)
1721 items = -panel->top_file;
1722 if (!items)
1723 panel->selected = panel->count - 1;
1724 else
1725 panel->selected += items;
1726 panel->top_file += items;
1728 /* This keeps the selection in it's relative position */
1729 if (panel->selected >= panel->count)
1730 panel->selected = panel->count - 1;
1731 if (panel->top_file >= panel->count)
1732 panel->top_file = panel->count - 1;
1733 select_item (panel);
1734 paint_dir (panel);
1737 static void
1738 ctrl_next_page (WPanel *panel)
1740 if ((S_ISDIR (selection (panel)->st.st_mode)
1741 || link_isdir (selection (panel)))) {
1742 do_cd (selection (panel)->fname, cd_exact);
1746 static void
1747 goto_top_file (WPanel *panel)
1749 unselect_item (panel);
1750 panel->selected = panel->top_file;
1751 select_item (panel);
1754 static void
1755 goto_middle_file (WPanel *panel)
1757 unselect_item (panel);
1758 panel->selected = panel->top_file + (ITEMS (panel)/2);
1759 if (panel->selected >= panel->count)
1760 panel->selected = panel->count - 1;
1761 select_item (panel);
1764 static void
1765 goto_bottom_file (WPanel *panel)
1767 unselect_item (panel);
1768 panel->selected = panel->top_file + ITEMS (panel)-1;
1769 if (panel->selected >= panel->count)
1770 panel->selected = panel->count - 1;
1771 select_item (panel);
1774 static void
1775 move_home (WPanel *panel)
1777 if (panel->selected == 0)
1778 return;
1779 unselect_item (panel);
1781 if (torben_fj_mode){
1782 int middle_pos = panel->top_file + (ITEMS (panel)/2);
1784 if (panel->selected > middle_pos){
1785 goto_middle_file (panel);
1786 return;
1788 if (panel->selected != panel->top_file){
1789 goto_top_file (panel);
1790 return;
1794 panel->top_file = 0;
1795 panel->selected = 0;
1797 paint_dir (panel);
1798 select_item (panel);
1801 static void
1802 move_end (WPanel *panel)
1804 if (panel->selected == panel->count-1)
1805 return;
1806 unselect_item (panel);
1807 if (torben_fj_mode){
1808 int middle_pos = panel->top_file + (ITEMS (panel)/2);
1810 if (panel->selected < middle_pos){
1811 goto_middle_file (panel);
1812 return;
1814 if (panel->selected != (panel->top_file + ITEMS(panel)-1)){
1815 goto_bottom_file (panel);
1816 return;
1820 panel->selected = panel->count-1;
1821 paint_dir (panel);
1822 select_item (panel);
1825 /* Recalculate the panels summary information, used e.g. when marked
1826 files might have been removed by an external command */
1827 void
1828 recalculate_panel_summary (WPanel *panel)
1830 int i;
1832 panel->marked = 0;
1833 panel->dirs_marked = 0;
1834 panel->total = 0;
1836 for (i = 0; i < panel->count; i++)
1837 if (panel->dir.list [i].f.marked){
1838 /* do_file_mark will return immediately if newmark == oldmark.
1839 So we have to first unmark it to get panel's summary information
1840 updated. (Norbert) */
1841 panel->dir.list [i].f.marked = 0;
1842 do_file_mark (panel, i, 1);
1846 /* This routine marks a file or a directory */
1847 void
1848 do_file_mark (WPanel *panel, int idx, int mark)
1850 if (panel->dir.list[idx].f.marked == mark)
1851 return;
1853 /* Only '..' can't be marked, '.' isn't visible */
1854 if (!strcmp (panel->dir.list[idx].fname, ".."))
1855 return;
1857 file_mark (panel, idx, mark);
1858 if (panel->dir.list[idx].f.marked) {
1859 panel->marked++;
1860 if (S_ISDIR (panel->dir.list[idx].st.st_mode)) {
1861 if (panel->dir.list[idx].f.dir_size_computed)
1862 panel->total += panel->dir.list[idx].st.st_size;
1863 panel->dirs_marked++;
1864 } else
1865 panel->total += panel->dir.list[idx].st.st_size;
1866 set_colors (panel);
1867 } else {
1868 if (S_ISDIR (panel->dir.list[idx].st.st_mode)) {
1869 if (panel->dir.list[idx].f.dir_size_computed)
1870 panel->total -= panel->dir.list[idx].st.st_size;
1871 panel->dirs_marked--;
1872 } else
1873 panel->total -= panel->dir.list[idx].st.st_size;
1874 panel->marked--;
1878 static void
1879 do_mark_file (WPanel *panel, int do_move)
1881 do_file_mark (panel, panel->selected,
1882 selection (panel)->f.marked ? 0 : 1);
1883 if (mark_moves_down && do_move)
1884 move_down (panel);
1887 static void
1888 mark_file (WPanel *panel)
1890 do_mark_file (panel, 1);
1893 /* Incremental search of a file name in the panel */
1894 static void
1895 do_search (WPanel *panel, int c_code)
1897 size_t l;
1898 int i;
1899 int wrapped = 0;
1900 int found;
1902 l = strlen (panel->search_buffer);
1903 if (c_code == KEY_BACKSPACE) {
1904 if (l)
1905 panel->search_buffer[--l] = '\0';
1906 } else {
1907 if (c_code && l < sizeof (panel->search_buffer)) {
1908 panel->search_buffer[l] = c_code;
1909 panel->search_buffer[l + 1] = 0;
1910 l++;
1914 found = 0;
1915 for (i = panel->selected; !wrapped || i != panel->selected; i++) {
1916 if (i >= panel->count) {
1917 i = 0;
1918 if (wrapped)
1919 break;
1920 wrapped = 1;
1922 if (panel->
1923 case_sensitive
1924 ? (strncmp (panel->dir.list[i].fname, panel->search_buffer, l)
1925 == 0) : (g_strncasecmp (panel->dir.list[i].fname,
1926 panel->search_buffer, l) == 0)) {
1927 unselect_item (panel);
1928 panel->selected = i;
1929 select_item (panel);
1930 found = 1;
1931 break;
1934 if (!found)
1935 panel->search_buffer[--l] = 0;
1937 paint_panel (panel);
1940 static void
1941 start_search (WPanel *panel)
1943 if (panel->searching){
1944 if (panel->selected+1 == panel->count)
1945 panel->selected = 0;
1946 else
1947 move_down (panel);
1948 do_search (panel, 0);
1949 } else {
1950 panel->searching = 1;
1951 panel->search_buffer [0] = 0;
1952 display_mini_info (panel);
1953 mc_refresh ();
1957 /* Return 1 if the Enter key has been processed, 0 otherwise */
1958 static int
1959 do_enter_on_file_entry (file_entry *fe)
1961 char *full_name;
1964 * Directory or link to directory - change directory.
1965 * Try the same for the entries on which mc_lstat() has failed.
1967 if (S_ISDIR (fe->st.st_mode) || link_isdir (fe)
1968 || (fe->st.st_mode == 0)) {
1969 if (!do_cd (fe->fname, cd_exact))
1970 message (D_ERROR, MSG_ERROR, _("Cannot change directory"));
1971 return 1;
1974 /* Try associated command */
1975 if (regex_command (fe->fname, "Open", 0) != 0)
1976 return 1;
1978 /* Check if the file is executable */
1979 full_name = mhl_str_dir_plus_file (current_panel->cwd, fe->fname);
1980 if (!is_exe (fe->st.st_mode) || !if_link_is_exe (full_name, fe)) {
1981 g_free (full_name);
1982 return 0;
1984 g_free (full_name);
1986 if (confirm_execute) {
1987 if (query_dialog
1988 (_(" The Midnight Commander "),
1989 _(" Do you really want to execute? "), D_NORMAL, 2, _("&Yes"),
1990 _("&No")) != 0)
1991 return 1;
1993 #ifdef USE_VFS
1994 if (!vfs_current_is_local ()) {
1995 char *tmp;
1996 int ret;
1998 tmp = mhl_str_dir_plus_file (vfs_get_current_dir (), fe->fname);
1999 ret = mc_setctl (tmp, VFS_SETCTL_RUN, NULL);
2000 g_free (tmp);
2001 /* We took action only if the dialog was shown or the execution
2002 * was successful */
2003 return confirm_execute || (ret == 0);
2005 #endif
2008 char *tmp = name_quote (fe->fname, 0);
2009 char *cmd = g_strconcat (".", PATH_SEP_STR, tmp, (char *) NULL);
2010 g_free (tmp);
2011 shell_execute (cmd, 0);
2012 g_free (cmd);
2015 return 1;
2018 static int
2019 do_enter (WPanel *panel)
2021 return do_enter_on_file_entry (selection (panel));
2024 static void
2025 chdir_other_panel (WPanel *panel)
2027 char *new_dir;
2028 char *sel_entry = NULL;
2030 if (get_other_type () != view_listing) {
2031 set_display_type (get_other_index (), view_listing);
2034 if (!S_ISDIR (panel->dir.list [panel->selected].st.st_mode)) {
2035 new_dir = mhl_str_dir_plus_file (panel->cwd, "..");
2036 sel_entry = strrchr(panel->cwd, PATH_SEP);
2037 } else
2038 new_dir = mhl_str_dir_plus_file (panel->cwd, panel->dir.list [panel->selected].fname);
2040 change_panel ();
2041 do_cd (new_dir, cd_exact);
2042 if (sel_entry)
2043 try_to_select (current_panel, sel_entry);
2044 change_panel ();
2046 move_down (panel);
2048 g_free (new_dir);
2052 * Make the current directory of the current panel also the current
2053 * directory of the other panel. Put the other panel to the listing
2054 * mode if needed. If the current panel is panelized, the other panel
2055 * doesn't become panelized.
2057 static void
2058 sync_other_panel (WPanel *panel)
2060 if (get_other_type () != view_listing) {
2061 set_display_type (get_other_index (), view_listing);
2064 do_panel_cd (other_panel, current_panel->cwd, cd_exact);
2066 /* try to select current filename on the other panel */
2067 if (!panel->is_panelized) {
2068 try_to_select (other_panel, selection (panel)->fname);
2072 static void
2073 chdir_to_readlink (WPanel *panel)
2075 char *new_dir;
2077 if (get_other_type () != view_listing)
2078 return;
2080 if (S_ISLNK (panel->dir.list [panel->selected].st.st_mode)) {
2081 char buffer [MC_MAXPATHLEN], *p;
2082 int i;
2083 struct stat st;
2085 i = readlink (selection (panel)->fname, buffer, MC_MAXPATHLEN - 1);
2086 if (i < 0)
2087 return;
2088 if (mc_stat (selection (panel)->fname, &st) < 0)
2089 return;
2090 buffer [i] = 0;
2091 if (!S_ISDIR (st.st_mode)) {
2092 p = strrchr (buffer, PATH_SEP);
2093 if (p && !p[1]) {
2094 *p = 0;
2095 p = strrchr (buffer, PATH_SEP);
2097 if (!p)
2098 return;
2099 p[1] = 0;
2101 if (*buffer == PATH_SEP)
2102 new_dir = g_strdup (buffer);
2103 else
2104 new_dir = mhl_str_dir_plus_file (panel->cwd, buffer);
2106 change_panel ();
2107 do_cd (new_dir, cd_exact);
2108 change_panel ();
2110 move_down (panel);
2112 g_free (new_dir);
2116 typedef void (*panel_key_callback) (WPanel *);
2117 typedef struct {
2118 int key_code;
2119 panel_key_callback fn;
2120 } panel_key_map;
2122 static void cmd_do_enter(WPanel *wp) { (void) do_enter(wp); }
2123 static void cmd_view_simple(WPanel *wp) { view_simple_cmd(); }
2124 static void cmd_edit_new(WPanel *wp) { edit_cmd_new(); }
2125 static void cmd_copy_local(WPanel *wp) { copy_cmd_local(); }
2126 static void cmd_rename_local(WPanel *wp) { ren_cmd_local(); }
2127 static void cmd_delete_local(WPanel *wp) { delete_cmd_local(); }
2128 static void cmd_select(WPanel *wp) { select_cmd(); }
2129 static void cmd_unselect(WPanel *wp) { unselect_cmd(); }
2130 static void cmd_reverse_selection(WPanel *wp) { reverse_selection_cmd(); }
2132 static const panel_key_map panel_keymap [] = {
2133 { KEY_DOWN, move_down },
2134 { KEY_UP, move_up },
2136 /* The action button :-) */
2137 { '\n', cmd_do_enter },
2138 { KEY_ENTER, cmd_do_enter },
2140 { KEY_IC, mark_file },
2141 { KEY_HOME, move_home },
2142 { KEY_A1, move_home },
2143 { ALT ('<'), move_home },
2144 { KEY_C1, move_end },
2145 { KEY_END, move_end },
2146 { ALT ('>'), move_end },
2147 { KEY_NPAGE, next_page },
2148 { KEY_PPAGE, prev_page },
2149 { KEY_NPAGE | KEY_M_CTRL, ctrl_next_page },
2150 { KEY_PPAGE | KEY_M_CTRL, ctrl_prev_page },
2152 /* To quickly move in the panel */
2153 { ALT('g'), goto_top_file },
2154 { ALT('r'), goto_middle_file }, /* M-r like emacs */
2155 { ALT('j'), goto_bottom_file },
2157 /* Emacs-like bindings */
2158 { XCTRL('v'), next_page }, /* C-v like emacs */
2159 { ALT('v'), prev_page }, /* M-v like emacs */
2160 { XCTRL('p'), move_up }, /* C-p like emacs */
2161 { XCTRL('n'), move_down }, /* C-n like emacs */
2162 { XCTRL('s'), start_search }, /* C-s like emacs */
2163 { ALT('s'), start_search }, /* M-s not like emacs */
2164 { XCTRL('t'), mark_file },
2165 { ALT('o'), chdir_other_panel },
2166 { ALT('i'), sync_other_panel },
2167 { ALT('l'), chdir_to_readlink },
2168 { ALT('H'), directory_history_list },
2169 { KEY_F(13), cmd_view_simple },
2170 { KEY_F(14), cmd_edit_new },
2171 { KEY_F(15), cmd_copy_local },
2172 { KEY_F(16), cmd_rename_local },
2173 { KEY_F(18), cmd_delete_local },
2174 { ALT('y'), directory_history_prev },
2175 { ALT('u'), directory_history_next },
2176 { ALT('+'), cmd_select },
2177 { KEY_KP_ADD, cmd_select },
2178 { ALT('\\'), cmd_unselect },
2179 { ALT('-'), cmd_unselect },
2180 { KEY_KP_SUBTRACT, cmd_unselect },
2181 { ALT('*'), cmd_reverse_selection },
2182 { KEY_KP_MULTIPLY, cmd_reverse_selection },
2183 { 0, 0 }
2186 static inline cb_ret_t
2187 panel_key (WPanel *panel, int key)
2189 int i;
2191 for (i = 0; panel_keymap[i].key_code; i++) {
2192 if (key == panel_keymap[i].key_code) {
2193 int old_searching = panel->searching;
2195 if (panel_keymap[i].fn != start_search)
2196 panel->searching = 0;
2198 (*panel_keymap[i].fn) (panel);
2200 if (panel->searching != old_searching)
2201 display_mini_info (panel);
2202 return MSG_HANDLED;
2205 if (torben_fj_mode && key == ALT ('h')) {
2206 goto_middle_file (panel);
2207 return MSG_HANDLED;
2210 /* We do not want to take a key press if nothing can be done with it */
2211 /* The command line widget may do something more useful */
2212 if (key == KEY_LEFT)
2213 return move_left (panel, key);
2215 if (key == KEY_RIGHT)
2216 return move_right (panel, key);
2218 if (is_abort_char (key)) {
2219 panel->searching = 0;
2220 display_mini_info (panel);
2221 return MSG_HANDLED;
2224 /* Do not eat characters not meant for the panel below ' ' (e.g. C-l). */
2225 if ((key >= ' ' && key <= 255) || key == KEY_BACKSPACE) {
2226 if (panel->searching) {
2227 do_search (panel, key);
2228 return MSG_HANDLED;
2231 if (!command_prompt) {
2232 start_search (panel);
2233 do_search (panel, key);
2234 return MSG_HANDLED;
2238 return MSG_NOT_HANDLED;
2241 static cb_ret_t
2242 panel_callback (Widget *w, widget_msg_t msg, int parm)
2244 WPanel *panel = (WPanel *) w;
2245 Dlg_head *h = panel->widget.parent;
2247 switch (msg) {
2248 case WIDGET_DRAW:
2249 paint_panel (panel);
2250 return MSG_HANDLED;
2252 case WIDGET_FOCUS:
2253 current_panel = panel;
2254 panel->active = 1;
2255 if (mc_chdir (panel->cwd) != 0) {
2256 char *cwd = strip_password (g_strdup (panel->cwd), 1);
2257 message (D_ERROR, MSG_ERROR, _(" Cannot chdir to \"%s\" \n %s "),
2258 cwd, unix_error_string (errno));
2259 g_free(cwd);
2260 } else
2261 subshell_chdir (panel->cwd);
2263 update_xterm_title_path ();
2264 select_item (panel);
2265 show_dir (panel);
2266 paint_dir (panel);
2267 panel->dirty = 0;
2269 buttonbar_set_label (h, 1, _("Help"), help_cmd);
2270 buttonbar_set_label (h, 2, _("Menu"), user_file_menu_cmd);
2271 buttonbar_set_label (h, 3, _("View"), view_cmd);
2272 buttonbar_set_label (h, 4, _("Edit"), edit_cmd);
2273 buttonbar_set_label (h, 5, _("Copy"), copy_cmd);
2274 buttonbar_set_label (h, 6, _("RenMov"), ren_cmd);
2275 buttonbar_set_label (h, 7, _("Mkdir"), mkdir_cmd);
2276 buttonbar_set_label (h, 8, _("Delete"), delete_cmd);
2277 buttonbar_redraw (h);
2278 return MSG_HANDLED;
2280 case WIDGET_UNFOCUS:
2281 /* Janne: look at this for the multiple panel options */
2282 if (panel->searching){
2283 panel->searching = 0;
2284 display_mini_info (panel);
2286 panel->active = 0;
2287 show_dir (panel);
2288 unselect_item (panel);
2289 return MSG_HANDLED;
2291 case WIDGET_KEY:
2292 return panel_key (panel, parm);
2294 case WIDGET_DESTROY:
2295 panel_destroy (panel);
2296 return MSG_HANDLED;
2298 default:
2299 return default_proc (msg, parm);
2303 void
2304 file_mark (WPanel *panel, int index, int val)
2306 if (panel->dir.list[index].f.marked != val) {
2307 panel->dir.list[index].f.marked = val;
2308 panel->dirty = 1;
2312 /* */
2313 /* Panel mouse events support routines */
2314 /* */
2315 static int mouse_marking = 0;
2317 static void
2318 mouse_toggle_mark (WPanel *panel)
2320 do_mark_file (panel, 0);
2321 mouse_marking = selection (panel)->f.marked;
2324 static void
2325 mouse_set_mark (WPanel *panel)
2327 if (mouse_marking && !(selection (panel)->f.marked))
2328 do_mark_file (panel, 0);
2329 else if (!mouse_marking && (selection (panel)->f.marked))
2330 do_mark_file (panel, 0);
2333 static inline int
2334 mark_if_marking (WPanel *panel, Gpm_Event *event)
2336 if (event->buttons & GPM_B_RIGHT){
2337 if (event->type & GPM_DOWN)
2338 mouse_toggle_mark (panel);
2339 else
2340 mouse_set_mark (panel);
2341 return 1;
2343 return 0;
2347 * Mouse callback of the panel minus repainting.
2348 * If the event is redirected to the menu, *redir is set to 1.
2350 static int
2351 do_panel_event (Gpm_Event *event, WPanel *panel, int *redir)
2353 const int lines = llines (panel);
2355 int my_index;
2357 /* Mouse wheel events */
2358 if ((event->buttons & GPM_B_UP) && (event->type & GPM_DOWN)) {
2359 prev_page (panel);
2360 return MOU_NORMAL;
2362 if ((event->buttons & GPM_B_DOWN) && (event->type & GPM_DOWN)) {
2363 next_page (panel);
2364 return MOU_NORMAL;
2367 /* "<" button */
2368 if (event->type & GPM_DOWN && event->x == 2 && event->y == 1) {
2369 directory_history_prev (panel);
2370 return MOU_NORMAL;
2373 /* ">" button */
2374 if (event->type & GPM_DOWN && event->x == panel->widget.cols - 1
2375 && event->y == 1) {
2376 directory_history_next (panel);
2377 return MOU_NORMAL;
2380 /* "v" button */
2381 if (event->type & GPM_DOWN && event->x == panel->widget.cols - 2
2382 && event->y == 1) {
2383 directory_history_list (panel);
2384 return MOU_NORMAL;
2387 /* rest of the upper frame, the menu is invisible - call menu */
2388 if (event->type & GPM_DOWN && event->y == 1 && !menubar_visible) {
2389 *redir = 1;
2390 event->x += panel->widget.x;
2391 return (*(the_menubar->widget.mouse)) (event, the_menubar);
2394 event->y -= 2;
2395 if ((event->type & (GPM_DOWN | GPM_DRAG))) {
2397 if (!dlg_widget_active (panel))
2398 change_panel ();
2400 if (event->y <= 0) {
2401 mark_if_marking (panel, event);
2402 if (mouse_move_pages)
2403 prev_page (panel);
2404 else
2405 move_up (panel);
2406 return MOU_REPEAT;
2409 if (!((panel->top_file + event->y <= panel->count)
2410 && event->y <= lines)) {
2411 mark_if_marking (panel, event);
2412 if (mouse_move_pages)
2413 next_page (panel);
2414 else
2415 move_down (panel);
2416 return MOU_REPEAT;
2418 my_index = panel->top_file + event->y - 1;
2419 if (panel->split) {
2420 if (event->x > ((panel->widget.cols - 2) / 2))
2421 my_index += llines (panel);
2424 if (my_index >= panel->count)
2425 my_index = panel->count - 1;
2427 if (my_index != panel->selected) {
2428 unselect_item (panel);
2429 panel->selected = my_index;
2430 select_item (panel);
2433 /* This one is new */
2434 mark_if_marking (panel, event);
2436 } else if ((event->type & (GPM_UP | GPM_DOUBLE)) ==
2437 (GPM_UP | GPM_DOUBLE)) {
2438 if (event->y > 0 && event->y <= lines)
2439 do_enter (panel);
2441 return MOU_NORMAL;
2444 /* Mouse callback of the panel */
2445 static int
2446 panel_event (Gpm_Event *event, void *data)
2448 WPanel *panel = data;
2449 int ret;
2450 int redir = 0;
2452 ret = do_panel_event (event, panel, &redir);
2453 if (!redir)
2454 panel_update_contents (panel);
2456 return ret;
2459 void
2460 panel_re_sort (WPanel *panel)
2462 char *filename;
2463 int i;
2465 if (panel == NULL)
2466 return;
2468 filename = g_strdup (selection (panel)->fname);
2469 unselect_item (panel);
2470 do_sort (&panel->dir, panel->sort_type, panel->count-1, panel->reverse,
2471 panel->case_sensitive, panel->exec_first);
2472 panel->selected = -1;
2473 for (i = panel->count; i; i--){
2474 if (!strcmp (panel->dir.list [i-1].fname, filename)){
2475 panel->selected = i-1;
2476 break;
2479 g_free (filename);
2480 panel->top_file = panel->selected - ITEMS (panel)/2;
2481 if (panel->top_file < 0)
2482 panel->top_file = 0;
2483 select_item (panel);
2484 panel->dirty = 1;
2487 void
2488 panel_set_sort_order (WPanel *panel, sortfn *sort_order)
2490 if (sort_order == 0)
2491 return;
2493 panel->sort_type = sort_order;
2495 /* The directory is already sorted, we have to load the unsorted stuff */
2496 if (sort_order == (sortfn *) unsorted){
2497 char *current_file;
2499 current_file = g_strdup (panel->dir.list [panel->selected].fname);
2500 panel_reload (panel);
2501 try_to_select (panel, current_file);
2502 g_free (current_file);
2504 panel_re_sort (panel);