replaced buggy concat_dir_and_file() by mhl_str_dir_plus_file()
[free-mc.git] / src / screen.c
blob6c3821b74bf5ae7b0706b24abd99c2ab05697549
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/string.h>
32 #include "global.h"
33 #include "tty.h"
34 #include "dir.h"
35 #include "panel.h"
36 #include "color.h"
37 #include "tree.h"
38 #include "win.h"
39 #include "ext.h" /* regexp_command */
40 #include "mouse.h" /* For Gpm_Event */
41 #include "layout.h" /* Most layout variables are here */
42 #include "wtools.h" /* for message (...) */
43 #include "cmd.h"
44 #include "key.h" /* XCTRL and ALT macros */
45 #include "setup.h" /* For loading/saving panel options */
46 #include "user.h"
47 #include "profile.h"
48 #include "execute.h"
49 #include "widget.h"
50 #include "menu.h" /* menubar_visible */
51 #define WANT_WIDGETS
52 #include "main.h" /* the_menubar */
53 #include "unixcompat.h"
55 #define ELEMENTS(arr) ( sizeof(arr) / sizeof((arr)[0]) )
57 #define J_LEFT 1
58 #define J_RIGHT 2
59 #define J_CENTER 3
61 #define IS_FIT(x) ((x) & 0x0004)
62 #define MAKE_FIT(x) ((x) | 0x0004)
63 #define HIDE_FIT(x) ((x) & 0x0003)
65 #define J_LEFT_FIT 5
66 #define J_RIGHT_FIT 6
67 #define J_CENTER_FIT 7
69 #define NORMAL 0
70 #define SELECTED 1
71 #define MARKED 2
72 #define MARKED_SELECTED 3
73 #define STATUS 5
76 * This describes a format item. The parse_display_format routine parses
77 * the user specified format and creates a linked list of format_e structures.
79 typedef struct format_e {
80 struct format_e *next;
81 int requested_field_len;
82 int field_len;
83 int just_mode;
84 int expand;
85 const char *(*string_fn)(file_entry *, int len);
86 const char *title;
87 const char *id;
88 } format_e;
90 /* If true, show the mini-info on the panel */
91 int show_mini_info = 1;
93 /* If true, then use stat() on the cwd to determine directory changes */
94 int fast_reload = 0;
96 /* If true, use some usability hacks by Torben */
97 int torben_fj_mode = 0;
99 /* If true, up/down keys scroll the pane listing by pages */
100 int panel_scroll_pages = 1;
102 /* If 1, we use permission hilighting */
103 int permission_mode = 0;
105 /* If 1 - then add per file type hilighting */
106 int filetype_mode = 1;
108 /* The hook list for the select file function */
109 Hook *select_file_hook = 0;
111 static cb_ret_t panel_callback (Widget *, widget_msg_t msg, int parm);
112 static int panel_event (Gpm_Event *event, void *);
113 static void paint_frame (WPanel *panel);
114 static const char *panel_format (WPanel *panel);
115 static const char *mini_status_format (WPanel *panel);
117 /* This macro extracts the number of available lines in a panel */
118 #define llines(p) (p->widget.lines-3 - (show_mini_info ? 2 : 0))
120 static void
121 set_colors (WPanel *panel)
123 standend ();
124 attrset (NORMAL_COLOR);
127 /* Delete format string, it is a linked list */
128 static void
129 delete_format (format_e *format)
131 format_e *next;
133 while (format){
134 next = format->next;
135 g_free (format);
136 format = next;
140 /* This code relies on the default justification!!! */
141 static void
142 add_permission_string (char *dest, int width, file_entry *fe, int attr, int color, int is_octal)
144 int i, r, l;
146 l = get_user_permissions (&fe->st);
148 if (is_octal){
149 /* Place of the access bit in octal mode */
150 l = width + l - 3;
151 r = l + 1;
152 } else {
153 /* The same to the triplet in string mode */
154 l = l * 3 + 1;
155 r = l + 3;
158 for(i = 0; i < width; i++){
159 if (i >= l && i < r){
160 if (attr == SELECTED || attr == MARKED_SELECTED)
161 attrset (MARKED_SELECTED_COLOR);
162 else
163 attrset (MARKED_COLOR);
164 } else
165 attrset (color);
167 addch (dest[i]);
171 /* String representations of various file attributes */
172 /* name */
173 static const char *
174 string_file_name (file_entry *fe, int len)
176 static char buffer [MC_MAXPATHLEN + 1];
177 size_t i;
179 for (i = 0; i < sizeof(buffer) - 1; i++) {
180 char c;
182 c = fe->fname[i];
184 if (!c)
185 break;
187 if (!is_printable(c))
188 c = '?';
190 buffer[i] = c;
193 buffer[i] = 0;
194 return buffer;
197 static inline unsigned int ilog10(dev_t n)
199 unsigned int digits = 0;
200 do {
201 digits++, n /= 10;
202 } while (n != 0);
203 return digits;
206 static void format_device_number (char *buf, size_t bufsize, dev_t dev)
208 dev_t major_dev = major(dev);
209 dev_t minor_dev = minor(dev);
210 unsigned int major_digits = ilog10(major_dev);
211 unsigned int minor_digits = ilog10(minor_dev);
213 g_assert(bufsize >= 1);
214 if (major_digits + 1 + minor_digits + 1 <= bufsize) {
215 g_snprintf(buf, bufsize, "%lu,%lu", (unsigned long) major_dev,
216 (unsigned long) minor_dev);
217 } else {
218 g_strlcpy(buf, _("[dev]"), bufsize);
222 /* size */
223 static const char *
224 string_file_size (file_entry *fe, int len)
226 static char buffer [BUF_TINY];
228 /* Don't ever show size of ".." since we don't calculate it */
229 if (!strcmp (fe->fname, "..")) {
230 return _("UP--DIR");
233 #ifdef HAVE_STRUCT_STAT_ST_RDEV
234 if (S_ISBLK (fe->st.st_mode) || S_ISCHR (fe->st.st_mode))
235 format_device_number (buffer, len + 1, fe->st.st_rdev);
236 else
237 #endif
239 size_trunc_len (buffer, len, fe->st.st_size, 0);
241 return buffer;
244 /* bsize */
245 static const char *
246 string_file_size_brief (file_entry *fe, int len)
248 if (S_ISLNK (fe->st.st_mode) && !fe->f.link_to_dir) {
249 return _("SYMLINK");
252 if ((S_ISDIR (fe->st.st_mode) || fe->f.link_to_dir) && strcmp (fe->fname, "..")) {
253 return _("SUB-DIR");
256 return string_file_size (fe, len);
259 /* This functions return a string representation of a file entry */
260 /* type */
261 static const char *
262 string_file_type (file_entry *fe, int len)
264 static char buffer[2];
266 if (S_ISDIR (fe->st.st_mode))
267 buffer[0] = PATH_SEP;
268 else if (S_ISLNK (fe->st.st_mode)) {
269 if (fe->f.link_to_dir)
270 buffer[0] = '~';
271 else if (fe->f.stale_link)
272 buffer[0] = '!';
273 else
274 buffer[0] = '@';
275 } else if (S_ISCHR (fe->st.st_mode))
276 buffer[0] = '-';
277 else if (S_ISSOCK (fe->st.st_mode))
278 buffer[0] = '=';
279 else if (S_ISDOOR (fe->st.st_mode))
280 buffer[0] = '>';
281 else if (S_ISBLK (fe->st.st_mode))
282 buffer[0] = '+';
283 else if (S_ISFIFO (fe->st.st_mode))
284 buffer[0] = '|';
285 else if (S_ISNAM (fe->st.st_mode))
286 buffer[0] = '#';
287 else if (!S_ISREG (fe->st.st_mode))
288 buffer[0] = '?'; /* non-regular of unknown kind */
289 else if (is_exe (fe->st.st_mode))
290 buffer[0] = '*';
291 else
292 buffer[0] = ' ';
293 buffer[1] = '\0';
294 return buffer;
297 /* mtime */
298 static const char *
299 string_file_mtime (file_entry *fe, int len)
301 if (!strcmp (fe->fname, "..")) {
302 return "";
304 return file_date (fe->st.st_mtime);
307 /* atime */
308 static const char *
309 string_file_atime (file_entry *fe, int len)
311 if (!strcmp (fe->fname, "..")) {
312 return "";
314 return file_date (fe->st.st_atime);
317 /* ctime */
318 static const char *
319 string_file_ctime (file_entry *fe, int len)
321 if (!strcmp (fe->fname, "..")) {
322 return "";
324 return file_date (fe->st.st_ctime);
327 /* perm */
328 static const char *
329 string_file_permission (file_entry *fe, int len)
331 return string_perm (fe->st.st_mode);
334 /* mode */
335 static const char *
336 string_file_perm_octal (file_entry *fe, int len)
338 static char buffer [10];
340 g_snprintf (buffer, sizeof (buffer), "0%06lo", (unsigned long) fe->st.st_mode);
341 return buffer;
344 /* nlink */
345 static const char *
346 string_file_nlinks (file_entry *fe, int len)
348 static char buffer[BUF_TINY];
350 g_snprintf (buffer, sizeof (buffer), "%16d", (int) fe->st.st_nlink);
351 return buffer;
354 /* inode */
355 static const char *
356 string_inode (file_entry *fe, int len)
358 static char buffer [10];
360 g_snprintf (buffer, sizeof (buffer), "%lu",
361 (unsigned long) fe->st.st_ino);
362 return buffer;
365 /* nuid */
366 static const char *
367 string_file_nuid (file_entry *fe, int len)
369 static char buffer [10];
371 g_snprintf (buffer, sizeof (buffer), "%lu",
372 (unsigned long) fe->st.st_uid);
373 return buffer;
376 /* ngid */
377 static const char *
378 string_file_ngid (file_entry *fe, int len)
380 static char buffer [10];
382 g_snprintf (buffer, sizeof (buffer), "%lu",
383 (unsigned long) fe->st.st_gid);
384 return buffer;
387 /* owner */
388 static const char *
389 string_file_owner (file_entry *fe, int len)
391 return get_owner (fe->st.st_uid);
394 /* group */
395 static const char *
396 string_file_group (file_entry *fe, int len)
398 return get_group (fe->st.st_gid);
401 /* mark */
402 static const char *
403 string_marked (file_entry *fe, int len)
405 return fe->f.marked ? "*" : " ";
408 /* space */
409 static const char *
410 string_space (file_entry *fe, int len)
412 return " ";
415 /* dot */
416 static const char *
417 string_dot (file_entry *fe, int len)
419 return ".";
422 #define GT 1
424 static struct {
425 const char *id;
426 int min_size;
427 int expands;
428 int default_just;
429 const char *title;
430 int use_in_gui;
431 const char *(*string_fn)(file_entry *, int);
432 sortfn *sort_routine; /* This field is currently unused. */
433 } formats [] = {
434 { "name", 12, 1, J_LEFT_FIT, N_("Name"), 1, string_file_name, (sortfn *) sort_name },
435 { "size", 7, 0, J_RIGHT, N_("Size"), 1, string_file_size, (sortfn *) sort_size },
436 { "bsize", 7, 0, J_RIGHT, N_("Size"), 1, string_file_size_brief, (sortfn *) sort_size },
437 { "type", GT, 0, J_LEFT, "", 2, string_file_type, NULL },
438 { "mtime", 12, 0, J_RIGHT, N_("MTime"), 1, string_file_mtime, (sortfn *) sort_time },
439 { "atime", 12, 0, J_RIGHT, N_("ATime"), 1, string_file_atime, (sortfn *) sort_atime },
440 { "ctime", 12, 0, J_RIGHT, N_("CTime"), 1, string_file_ctime, (sortfn *) sort_ctime },
441 { "perm", 10, 0, J_LEFT, N_("Permission"),1,string_file_permission, NULL },
442 { "mode", 6, 0, J_RIGHT, N_("Perm"), 1, string_file_perm_octal, NULL },
443 { "nlink", 2, 0, J_RIGHT, N_("Nl"), 1, string_file_nlinks, NULL },
444 { "inode", 5, 0, J_RIGHT, N_("Inode"), 1, string_inode, (sortfn *) sort_inode },
445 { "nuid", 5, 0, J_RIGHT, N_("UID"), 1, string_file_nuid, NULL },
446 { "ngid", 5, 0, J_RIGHT, N_("GID"), 1, string_file_ngid, NULL },
447 { "owner", 8, 0, J_LEFT_FIT, N_("Owner"), 1, string_file_owner, NULL },
448 { "group", 8, 0, J_LEFT_FIT, N_("Group"), 1, string_file_group, NULL },
449 { "mark", 1, 0, J_RIGHT, " ", 1, string_marked, NULL },
450 { "|", 1, 0, J_RIGHT, " ", 0, NULL, NULL },
451 { "space", 1, 0, J_RIGHT, " ", 0, string_space, NULL },
452 { "dot", 1, 0, J_RIGHT, " ", 0, string_dot, NULL },
455 static char *
456 to_buffer (char *dest, int just_mode, int len, const char *txt)
458 int txtlen = strlen (txt);
459 int still, over;
461 /* Fill buffer with spaces */
462 memset (dest, ' ', len);
464 still = (over=(txtlen > len)) ? (txtlen - len) : (len - txtlen);
466 switch (HIDE_FIT(just_mode)){
467 case J_LEFT:
468 still = 0;
469 break;
470 case J_CENTER:
471 still /= 2;
472 break;
473 case J_RIGHT:
474 default:
475 break;
478 if (over){
479 if (IS_FIT(just_mode))
480 strcpy (dest, name_trunc(txt, len));
481 else
482 strncpy (dest, txt+still, len);
483 } else
484 strncpy (dest+still, txt, txtlen);
486 dest[len] = '\0';
488 return (dest + len);
491 static int
492 file_compute_color (int attr, file_entry *fe)
494 switch (attr) {
495 case SELECTED:
496 return (SELECTED_COLOR);
497 case MARKED:
498 return (MARKED_COLOR);
499 case MARKED_SELECTED:
500 return (MARKED_SELECTED_COLOR);
501 case STATUS:
502 return (NORMAL_COLOR);
503 case NORMAL:
504 default:
505 if (!filetype_mode)
506 return (NORMAL_COLOR);
509 /* if filetype_mode == true */
510 if (S_ISDIR (fe->st.st_mode))
511 return (DIRECTORY_COLOR);
512 else if (S_ISLNK (fe->st.st_mode)) {
513 if (fe->f.link_to_dir)
514 return (DIRECTORY_COLOR);
515 else if (fe->f.stale_link)
516 return (STALE_LINK_COLOR);
517 else
518 return (LINK_COLOR);
519 } else if (S_ISSOCK (fe->st.st_mode))
520 return (SPECIAL_COLOR);
521 else if (S_ISCHR (fe->st.st_mode))
522 return (DEVICE_COLOR);
523 else if (S_ISBLK (fe->st.st_mode))
524 return (DEVICE_COLOR);
525 else if (S_ISNAM (fe->st.st_mode))
526 return (DEVICE_COLOR);
527 else if (S_ISFIFO (fe->st.st_mode))
528 return (SPECIAL_COLOR);
529 else if (S_ISDOOR (fe->st.st_mode))
530 return (SPECIAL_COLOR);
531 else if (!S_ISREG (fe->st.st_mode))
532 return (STALE_LINK_COLOR); /* non-regular file of unknown kind */
533 else if (is_exe (fe->st.st_mode))
534 return (EXECUTABLE_COLOR);
535 else if (fe->fname && (!strcmp (fe->fname, "core")
536 || !strcmp (extension (fe->fname), "core")))
537 return (CORE_COLOR);
539 return (NORMAL_COLOR);
542 /* Formats the file number file_index of panel in the buffer dest */
543 static void
544 format_file (char *dest, int limit, WPanel *panel, int file_index, int width, int attr, int isstatus)
546 int color, length, empty_line;
547 const char *txt;
548 char *old_pos;
549 char *cdest = dest;
550 format_e *format, *home;
551 file_entry *fe;
553 length = 0;
554 empty_line = (file_index >= panel->count);
555 home = (isstatus) ? panel->status_format : panel->format;
556 fe = &panel->dir.list [file_index];
558 if (!empty_line)
559 color = file_compute_color (attr, fe);
560 else
561 color = NORMAL_COLOR;
563 for (format = home; format; format = format->next){
565 if (length == width)
566 break;
568 if (format->string_fn){
569 int len;
571 if (empty_line)
572 txt = " ";
573 else
574 txt = (*format->string_fn)(fe, format->field_len);
576 old_pos = cdest;
578 len = format->field_len;
579 if (len + length > width)
580 len = width - length;
581 if (len + (cdest - dest) > limit)
582 len = limit - (cdest - dest);
583 if (len <= 0)
584 break;
585 cdest = to_buffer (cdest, format->just_mode, len, txt);
586 length += len;
588 attrset (color);
590 if (permission_mode && !strcmp(format->id, "perm"))
591 add_permission_string (old_pos, format->field_len, fe, attr, color, 0);
592 else if (permission_mode && !strcmp(format->id, "mode"))
593 add_permission_string (old_pos, format->field_len, fe, attr, color, 1);
594 else
595 addstr (old_pos);
597 } else {
598 if (attr == SELECTED || attr == MARKED_SELECTED)
599 attrset (SELECTED_COLOR);
600 else
601 attrset (NORMAL_COLOR);
602 one_vline ();
603 length++;
607 if (length < width){
608 int still = width - length;
609 while (still--)
610 addch (' ');
614 static void
615 repaint_file (WPanel *panel, int file_index, int mv, int attr, int isstatus)
617 int second_column = 0;
618 int width, offset;
619 char buffer [BUF_MEDIUM];
621 offset = 0;
622 if (!isstatus && panel->split){
624 second_column = (file_index - panel->top_file) / llines (panel);
625 width = (panel->widget.cols - 2)/2 - 1;
627 if (second_column){
628 offset = 1 + width;
629 width = (panel->widget.cols-2) - (panel->widget.cols-2)/2 - 1;
631 } else
632 width = (panel->widget.cols - 2);
634 /* Nothing to paint */
635 if (width <= 0)
636 return;
638 if (mv){
639 if (!isstatus && panel->split){
640 widget_move (&panel->widget,
641 (file_index - panel->top_file) %
642 llines (panel) + 2,
643 (offset + 1));
644 } else
645 widget_move (&panel->widget, file_index - panel->top_file + 2, 1);
648 format_file (buffer, sizeof(buffer), panel, file_index, width, attr, isstatus);
650 if (!isstatus && panel->split){
651 if (second_column)
652 addch (' ');
653 else {
654 attrset (NORMAL_COLOR);
655 one_vline ();
660 static void
661 display_mini_info (WPanel *panel)
663 if (!show_mini_info)
664 return;
666 widget_move (&panel->widget, llines (panel)+3, 1);
668 if (panel->searching){
669 attrset (INPUT_COLOR);
670 tty_printf ("/%-*s", panel->widget.cols-3, panel->search_buffer);
671 attrset (NORMAL_COLOR);
672 return;
675 /* Status displays total marked size */
676 if (panel->marked){
677 char buffer[BUF_SMALL], b_bytes[BUF_SMALL];
678 const char *p = " %-*s";
679 int cols = panel->widget.cols-2;
681 attrset (MARKED_COLOR);
682 tty_printf ("%*s", cols, " ");
683 widget_move (&panel->widget, llines (panel)+3, 1);
686 * This is a trick to use two ngettext() calls in one sentence.
687 * First make "N bytes", then insert it into "X in M files".
689 g_snprintf(b_bytes, sizeof (b_bytes),
690 ngettext("%s byte", "%s bytes",
691 (unsigned long)panel->total),
692 size_trunc_sep(panel->total));
693 g_snprintf(buffer, sizeof (buffer),
694 ngettext("%s in %d file", "%s in %d files", panel->marked),
695 b_bytes, panel->marked);
697 if ((int) strlen (buffer) > cols-2){
698 buffer [cols] = 0;
699 p += 2;
700 } else
701 cols -= 2;
702 tty_printf (p, cols, buffer);
703 return;
706 /* Status resolves links and show them */
707 set_colors (panel);
709 if (S_ISLNK (panel->dir.list [panel->selected].st.st_mode)){
710 char *link, link_target [MC_MAXPATHLEN];
711 int len;
713 link = mhl_str_dir_plus_file (panel->cwd, panel->dir.list [panel->selected].fname);
714 len = mc_readlink (link, link_target, MC_MAXPATHLEN - 1);
715 g_free (link);
716 if (len > 0){
717 link_target[len] = 0;
718 tty_printf ("-> %-*s", panel->widget.cols - 5,
719 name_trunc (link_target, panel->widget.cols - 5));
720 } else
721 tty_printf ("%-*s", panel->widget.cols - 2, _("<readlink failed>"));
722 return;
725 /* Default behavior */
726 repaint_file (panel, panel->selected, 0, STATUS, 1);
727 return;
730 static void
731 paint_dir (WPanel *panel)
733 int i;
734 int color; /* Color value of the line */
735 int items; /* Number of items */
737 items = llines (panel) * (panel->split ? 2 : 1);
739 for (i = 0; i < items; i++){
740 if (i+panel->top_file >= panel->count)
741 color = 0;
742 else {
743 color = 2 * (panel->dir.list [i+panel->top_file].f.marked);
744 color += (panel->selected==i+panel->top_file && panel->active);
746 repaint_file (panel, i+panel->top_file, 1, color, 0);
748 standend ();
751 static void
752 mini_info_separator (WPanel *panel)
754 if (!show_mini_info)
755 return;
757 standend ();
758 widget_move (&panel->widget, llines (panel) + 2, 1);
759 #ifdef HAVE_SLANG
760 attrset (NORMAL_COLOR);
761 hline (ACS_HLINE, panel->widget.cols - 2);
762 #else
763 hline ((slow_terminal ? '-' : ACS_HLINE) | NORMAL_COLOR,
764 panel->widget.cols - 2);
765 #endif /* !HAVE_SLANG */
768 static void
769 show_dir (WPanel *panel)
771 char *tmp;
773 set_colors (panel);
774 draw_double_box (panel->widget.parent,
775 panel->widget.y, panel->widget.x,
776 panel->widget.lines, panel->widget.cols);
778 #ifdef HAVE_SLANG
779 if (show_mini_info) {
780 SLsmg_draw_object (panel->widget.y + llines (panel) + 2,
781 panel->widget.x, SLSMG_LTEE_CHAR);
782 SLsmg_draw_object (panel->widget.y + llines (panel) + 2,
783 panel->widget.x + panel->widget.cols - 1,
784 SLSMG_RTEE_CHAR);
786 #endif /* HAVE_SLANG */
788 if (panel->active)
789 attrset (REVERSE_COLOR);
791 widget_move (&panel->widget, 0, 3);
792 addch (' ');
794 tmp = g_malloc (panel->widget.cols + 1);
795 tmp[panel->widget.cols] = '\0';
797 trim (strip_home_and_password (panel->cwd), tmp,
798 max (panel->widget.cols - 9, 0));
799 addstr (tmp);
800 g_free (tmp);
802 addch (' ');
803 widget_move (&panel->widget, 0, 1);
804 addstr ("<");
805 widget_move (&panel->widget, 0, panel->widget.cols - 2);
806 addstr (">");
807 widget_move (&panel->widget, 0, panel->widget.cols - 3);
808 addstr ("v");
810 if (panel->active)
811 standend ();
814 /* To be used only by long_frame and full_frame to adjust top_file */
815 static void
816 adjust_top_file (WPanel *panel)
818 int old_top = panel->top_file;
820 if (panel->selected - old_top > llines (panel))
821 panel->top_file = panel->selected;
822 if (old_top - panel->count > llines (panel))
823 panel->top_file = panel->count - llines (panel);
827 * Repaint everything that can change on the panel - title, entries and
828 * mini status. The rest of the frame and the mini status separator are
829 * not repainted.
831 static void
832 panel_update_contents (WPanel *panel)
834 show_dir (panel);
835 paint_dir (panel);
836 display_mini_info (panel);
837 panel->dirty = 0;
840 /* Repaint everything, including frame and separator */
841 static void
842 paint_panel (WPanel *panel)
844 paint_frame (panel);
845 panel_update_contents (panel);
846 mini_info_separator (panel);
850 * Repaint the contents of the panels without frames. To schedule panel
851 * for repainting, set panel->dirty to 1. There are many reasons why
852 * the panels need to be repainted, and this is a costly operation, so
853 * it's done once per event.
855 void
856 update_dirty_panels (void)
858 if (current_panel->dirty)
859 panel_update_contents (current_panel);
861 if ((get_other_type () == view_listing) && other_panel->dirty)
862 panel_update_contents (other_panel);
865 static void
866 do_select (WPanel *panel, int i)
868 if (i != panel->selected) {
869 panel->dirty = 1;
870 panel->selected = i;
871 panel->top_file = panel->selected - (panel->widget.lines - 2) / 2;
872 if (panel->top_file < 0)
873 panel->top_file = 0;
877 static inline void
878 do_try_to_select (WPanel *panel, const char *name)
880 int i;
881 char *subdir;
883 if (!name) {
884 do_select(panel, 0);
885 return;
888 /* We only want the last component of the directory,
889 * and from this only the name without suffix. */
890 subdir = vfs_strip_suffix_from_filename (x_basename(name));
892 /* Search that subdirectory, if found select it */
893 for (i = 0; i < panel->count; i++){
894 if (strcmp (subdir, panel->dir.list [i].fname) == 0) {
895 do_select (panel, i);
896 g_free (subdir);
897 return;
901 /* Try to select a file near the file that is missing */
902 if (panel->selected >= panel->count)
903 do_select (panel, panel->count-1);
904 g_free (subdir);
907 void
908 try_to_select (WPanel *panel, const char *name)
910 do_try_to_select (panel, name);
911 select_item (panel);
914 void
915 panel_update_cols (Widget *widget, int frame_size)
917 int cols, origin;
919 if (horizontal_split){
920 widget->cols = COLS;
921 return;
924 if (frame_size == frame_full){
925 cols = COLS;
926 origin = 0;
927 } else {
928 if (widget == get_panel_widget (0)){
929 cols = first_panel_size;
930 origin = 0;
931 } else {
932 cols = COLS-first_panel_size;
933 origin = first_panel_size;
937 widget->cols = cols;
938 widget->x = origin;
941 static char *
942 panel_save_name (WPanel *panel)
944 extern int saving_setup;
946 /* If the program is shuting down */
947 if ((midnight_shutdown && auto_save_setup) || saving_setup)
948 return g_strdup (panel->panel_name);
949 else
950 return g_strconcat ("Temporal:", panel->panel_name, (char *) NULL);
953 static void
954 panel_destroy (WPanel *p)
956 int i;
958 char *name = panel_save_name (p);
960 panel_save_setup (p, name);
961 panel_clean_dir (p);
963 /* save and clean history */
964 if (p->dir_history) {
965 history_put (p->hist_name, p->dir_history);
967 p->dir_history = g_list_first (p->dir_history);
968 g_list_foreach (p->dir_history, (GFunc) g_free, NULL);
969 g_list_free (p->dir_history);
972 g_free (p->hist_name);
974 delete_format (p->format);
975 delete_format (p->status_format);
977 g_free (p->user_format);
978 for (i = 0; i < LIST_TYPES; i++)
979 g_free (p->user_status_format[i]);
980 g_free (p->dir.list);
981 g_free (p->panel_name);
982 g_free (name);
985 static void
986 panel_format_modified (WPanel *panel)
988 panel->format_modified = 1;
991 /* Panel creation */
992 /* The parameter specifies the name of the panel for setup retieving */
993 WPanel *
994 panel_new (const char *panel_name)
996 WPanel *panel;
997 char *section;
998 int i, err;
1000 panel = g_new0 (WPanel, 1);
1002 /* No know sizes of the panel at startup */
1003 init_widget (&panel->widget, 0, 0, 0, 0, panel_callback, panel_event);
1005 /* We do not want the cursor */
1006 widget_want_cursor (panel->widget, 0);
1008 mc_get_current_wd (panel->cwd, sizeof (panel->cwd) - 2);
1009 strcpy (panel->lwd, ".");
1011 panel->hist_name = g_strconcat ("Dir Hist ", panel_name, (char *) NULL);
1012 panel->dir_history = history_get (panel->hist_name);
1013 directory_history_add (panel, panel->cwd);
1015 panel->dir.list = g_new (file_entry, MIN_FILES);
1016 panel->dir.size = MIN_FILES;
1017 panel->active = 0;
1018 panel->filter = 0;
1019 panel->split = 0;
1020 panel->top_file = 0;
1021 panel->selected = 0;
1022 panel->marked = 0;
1023 panel->total = 0;
1024 panel->reverse = 0;
1025 panel->dirty = 1;
1026 panel->searching = 0;
1027 panel->dirs_marked = 0;
1028 panel->is_panelized = 0;
1029 panel->format = 0;
1030 panel->status_format = 0;
1031 panel->format_modified = 1;
1033 panel->panel_name = g_strdup (panel_name);
1034 panel->user_format = g_strdup (DEFAULT_USER_FORMAT);
1036 for (i = 0; i < LIST_TYPES; i++)
1037 panel->user_status_format[i] = g_strdup (DEFAULT_USER_FORMAT);
1039 panel->search_buffer[0] = 0;
1040 panel->frame_size = frame_half;
1041 section = g_strconcat ("Temporal:", panel->panel_name, (char *) NULL);
1042 if (!profile_has_section (section, profile_name)) {
1043 g_free (section);
1044 section = g_strdup (panel->panel_name);
1046 panel_load_setup (panel, section);
1047 g_free (section);
1049 /* Load format strings */
1050 err = set_panel_formats (panel);
1051 if (err) {
1052 set_panel_formats (panel);
1055 /* Load the default format */
1056 panel->count =
1057 do_load_dir (panel->cwd, &panel->dir, panel->sort_type,
1058 panel->reverse, panel->case_sensitive,
1059 panel->exec_first, panel->filter);
1060 return panel;
1063 void
1064 panel_reload (WPanel *panel)
1066 struct stat current_stat;
1068 if (fast_reload && !stat (panel->cwd, &current_stat)
1069 && current_stat.st_ctime == panel->dir_stat.st_ctime
1070 && current_stat.st_mtime == panel->dir_stat.st_mtime)
1071 return;
1073 while (mc_chdir (panel->cwd) == -1) {
1074 char *last_slash;
1076 if (panel->cwd[0] == PATH_SEP && panel->cwd[1] == 0) {
1077 panel_clean_dir (panel);
1078 panel->count = set_zero_dir (&panel->dir);
1079 return;
1081 last_slash = strrchr (panel->cwd, PATH_SEP);
1082 if (!last_slash || last_slash == panel->cwd)
1083 strcpy (panel->cwd, PATH_SEP_STR);
1084 else
1085 *last_slash = 0;
1086 memset (&(panel->dir_stat), 0, sizeof (panel->dir_stat));
1087 show_dir (panel);
1090 panel->count =
1091 do_reload_dir (panel->cwd, &panel->dir, panel->sort_type,
1092 panel->count, panel->reverse, panel->case_sensitive,
1093 panel->exec_first, panel->filter);
1095 panel->dirty = 1;
1096 if (panel->selected >= panel->count)
1097 do_select (panel, panel->count - 1);
1099 recalculate_panel_summary (panel);
1102 static void
1103 paint_frame (WPanel *panel)
1105 int header_len;
1106 int spaces, extra;
1107 int side, width;
1109 const char *txt;
1110 if (!panel->split)
1111 adjust_top_file (panel);
1113 widget_erase (&panel->widget);
1114 show_dir (panel);
1116 widget_move (&panel->widget, 1, 1);
1118 for (side = 0; side <= panel->split; side++){
1119 format_e *format;
1121 if (side){
1122 attrset (NORMAL_COLOR);
1123 one_vline ();
1124 width = panel->widget.cols - panel->widget.cols/2 - 1;
1125 } else if (panel->split)
1126 width = panel->widget.cols/2 - 3;
1127 else
1128 width = panel->widget.cols - 2;
1130 for (format = panel->format; format; format = format->next){
1131 if (format->string_fn){
1132 txt = format->title;
1134 header_len = strlen (txt);
1135 if (header_len > format->field_len)
1136 header_len = format->field_len;
1138 attrset (MARKED_COLOR);
1139 spaces = (format->field_len - header_len) / 2;
1140 extra = (format->field_len - header_len) % 2;
1141 tty_printf ("%*s%.*s%*s", spaces, "",
1142 header_len, txt, spaces+extra, "");
1143 width -= 2 * spaces + extra + header_len;
1144 } else {
1145 attrset (NORMAL_COLOR);
1146 one_vline ();
1147 width --;
1148 continue;
1152 if (width > 0)
1153 tty_printf ("%*s", width, "");
1157 static const char *
1158 parse_panel_size (WPanel *panel, const char *format, int isstatus)
1160 int frame = frame_half;
1161 format = skip_separators (format);
1163 if (!strncmp (format, "full", 4)){
1164 frame = frame_full;
1165 format += 4;
1166 } else if (!strncmp (format, "half", 4)){
1167 frame = frame_half;
1168 format += 4;
1171 if (!isstatus){
1172 panel->frame_size = frame;
1173 panel->split = 0;
1176 /* Now, the optional column specifier */
1177 format = skip_separators (format);
1179 if (*format == '1' || *format == '2'){
1180 if (!isstatus)
1181 panel->split = *format == '2';
1182 format++;
1185 if (!isstatus)
1186 panel_update_cols (&(panel->widget), panel->frame_size);
1188 return skip_separators (format);
1191 /* Format is:
1193 all := panel_format? format
1194 panel_format := [full|half] [1|2]
1195 format := one_format_e
1196 | format , one_format_e
1198 one_format_e := just format.id [opt_size]
1199 just := [<=>]
1200 opt_size := : size [opt_expand]
1201 size := [0-9]+
1202 opt_expand := +
1206 static format_e *
1207 parse_display_format (WPanel *panel, const char *format, char **error, int isstatus, int *res_total_cols)
1209 format_e *darr, *old = 0, *home = 0; /* The formats we return */
1210 int total_cols = 0; /* Used columns by the format */
1211 int set_justify; /* flag: set justification mode? */
1212 int justify = 0; /* Which mode. */
1213 int items = 0; /* Number of items in the format */
1214 size_t i;
1216 static size_t i18n_timelength = 0; /* flag: check ?Time length at startup */
1218 *error = 0;
1220 if (i18n_timelength == 0) {
1221 i18n_timelength = i18n_checktimelength (); /* Musn't be 0 */
1223 for (i = 0; i < ELEMENTS(formats); i++)
1224 if (strcmp ("time", formats [i].id+1) == 0)
1225 formats [i].min_size = i18n_timelength;
1229 * This makes sure that the panel and mini status full/half mode
1230 * setting is equal
1232 format = parse_panel_size (panel, format, isstatus);
1234 while (*format){ /* format can be an empty string */
1235 int found = 0;
1237 darr = g_new (format_e, 1);
1239 /* I'm so ugly, don't look at me :-) */
1240 if (!home)
1241 home = old = darr;
1243 old->next = darr;
1244 darr->next = 0;
1245 old = darr;
1247 format = skip_separators (format);
1249 if (strchr ("<=>", *format)){
1250 set_justify = 1;
1251 switch (*format)
1253 case '<':
1254 justify = J_LEFT;
1255 break;
1256 case '=':
1257 justify = J_CENTER;
1258 break;
1259 case '>':
1260 default:
1261 justify = J_RIGHT;
1262 break;
1264 format = skip_separators (format+1);
1265 } else
1266 set_justify = 0;
1268 for (i = 0; i < ELEMENTS(formats); i++){
1269 int klen = strlen (formats [i].id);
1271 if (strncmp (format, formats [i].id, klen) != 0)
1272 continue;
1274 format += klen;
1276 if (formats [i].use_in_gui)
1277 items++;
1279 darr->requested_field_len = formats [i].min_size;
1280 darr->string_fn = formats [i].string_fn;
1281 if (formats [i].title [0])
1282 darr->title = _(formats [i].title);
1283 else
1284 darr->title = "";
1285 darr->id = formats [i].id;
1286 darr->expand = formats [i].expands;
1287 darr->just_mode = formats [i].default_just;
1289 if (set_justify) {
1290 if (IS_FIT(darr->just_mode))
1291 darr->just_mode = MAKE_FIT(justify);
1292 else
1293 darr->just_mode = justify;
1295 found = 1;
1297 format = skip_separators (format);
1299 /* If we have a size specifier */
1300 if (*format == ':'){
1301 int req_length;
1303 /* If the size was specified, we don't want
1304 * auto-expansion by default
1306 darr->expand = 0;
1307 format++;
1308 req_length = atoi (format);
1309 darr->requested_field_len = req_length;
1311 format = skip_numbers (format);
1313 /* Now, if they insist on expansion */
1314 if (*format == '+'){
1315 darr->expand = 1;
1316 format++;
1321 break;
1323 if (!found){
1324 char *tmp_format = g_strdup (format);
1326 int pos = min (8, strlen (format));
1327 delete_format (home);
1328 tmp_format [pos] = 0;
1329 *error = g_strconcat (_("Unknown tag on display format: "), tmp_format, (char *) NULL);
1330 g_free (tmp_format);
1331 return 0;
1333 total_cols += darr->requested_field_len;
1336 *res_total_cols = total_cols;
1337 return home;
1340 static format_e *
1341 use_display_format (WPanel *panel, const char *format, char **error, int isstatus)
1343 #define MAX_EXPAND 4
1344 int expand_top = 0; /* Max used element in expand */
1345 int usable_columns; /* Usable columns in the panel */
1346 int total_cols = 0;
1347 int i;
1348 format_e *darr, *home;
1350 if (!format)
1351 format = DEFAULT_USER_FORMAT;
1353 home = parse_display_format (panel, format, error, isstatus, &total_cols);
1355 if (*error)
1356 return 0;
1358 panel->dirty = 1;
1360 /* Status needn't to be split */
1361 usable_columns = ((panel->widget.cols-2)/((isstatus)
1363 : (panel->split+1))) - (!isstatus && panel->split);
1365 /* Look for the expandable fields and set field_len based on the requested field len */
1366 for (darr = home; darr && expand_top < MAX_EXPAND; darr = darr->next){
1367 darr->field_len = darr->requested_field_len;
1368 if (darr->expand)
1369 expand_top++;
1372 /* If we used more columns than the available columns, adjust that */
1373 if (total_cols > usable_columns){
1374 int pdif, dif = total_cols - usable_columns;
1376 while (dif){
1377 pdif = dif;
1378 for (darr = home; darr; darr = darr->next){
1379 if (dif && darr->field_len - 1){
1380 darr->field_len--;
1381 dif--;
1385 /* avoid endless loop if num fields > 40 */
1386 if (pdif == dif)
1387 break;
1389 total_cols = usable_columns; /* give up, the rest should be truncated */
1392 /* Expand the available space */
1393 if ((usable_columns > total_cols) && expand_top){
1394 int spaces = (usable_columns - total_cols) / expand_top;
1395 int extra = (usable_columns - total_cols) % expand_top;
1397 for (i = 0, darr = home; darr && (i < expand_top); darr = darr->next)
1398 if (darr->expand){
1399 darr->field_len += (spaces + ((i == 0) ? extra : 0));
1400 i++;
1403 return home;
1406 /* Switches the panel to the mode specified in the format */
1407 /* Seting up both format and status string. Return: 0 - on success; */
1408 /* 1 - format error; 2 - status error; 3 - errors in both formats. */
1410 set_panel_formats (WPanel *p)
1412 format_e *form;
1413 char *err;
1414 int retcode = 0;
1416 form = use_display_format (p, panel_format (p), &err, 0);
1418 if (err){
1419 g_free (err);
1420 retcode = 1;
1422 else {
1423 if (p->format)
1424 delete_format (p->format);
1426 p->format = form;
1429 if (show_mini_info){
1431 form = use_display_format (p, mini_status_format (p), &err, 1);
1433 if (err){
1434 g_free (err);
1435 retcode += 2;
1437 else {
1438 if (p->status_format)
1439 delete_format (p->status_format);
1441 p->status_format = form;
1445 panel_format_modified (p);
1446 panel_update_cols (&(p->widget), p->frame_size);
1448 if (retcode)
1449 message( 1, _("Warning" ), _( "User supplied format looks invalid, reverting to default." ) );
1450 if (retcode & 0x01){
1451 g_free (p->user_format);
1452 p->user_format = g_strdup (DEFAULT_USER_FORMAT);
1454 if (retcode & 0x02){
1455 g_free (p->user_status_format [p->list_type]);
1456 p->user_status_format [p->list_type] = g_strdup (DEFAULT_USER_FORMAT);
1459 return retcode;
1462 /* Given the panel->view_type returns the format string to be parsed */
1463 static const char *
1464 panel_format (WPanel *panel)
1466 switch (panel->list_type){
1468 case list_long:
1469 return "full perm space nlink space owner space group space size space mtime space name";
1471 case list_brief:
1472 return "half 2 type name";
1474 case list_user:
1475 return panel->user_format;
1477 default:
1478 case list_full:
1479 return "half type name | size | mtime";
1483 static const char *
1484 mini_status_format (WPanel *panel)
1486 if (panel->user_mini_status)
1487 return panel->user_status_format [panel->list_type];
1489 switch (panel->list_type){
1491 case list_long:
1492 return "full perm space nlink space owner space group space size space mtime space name";
1494 case list_brief:
1495 return "half type name space bsize space perm space";
1497 case list_full:
1498 return "half type name";
1500 default:
1501 case list_user:
1502 return panel->user_format;
1506 /* */
1507 /* Panel operation commands */
1508 /* */
1510 /* Returns the number of items in the given panel */
1511 static int
1512 ITEMS (WPanel *p)
1514 if (p->split)
1515 return llines (p) * 2;
1516 else
1517 return llines (p);
1520 /* Select current item and readjust the panel */
1521 void
1522 select_item (WPanel *panel)
1524 int items = ITEMS (panel);
1526 /* Although currently all over the code we set the selection and
1527 top file to decent values before calling select_item, I could
1528 forget it someday, so it's better to do the actual fitting here */
1530 if (panel->top_file < 0)
1531 panel->top_file = 0;
1533 if (panel->selected < 0)
1534 panel->selected = 0;
1536 if (panel->selected > panel->count - 1)
1537 panel->selected = panel->count - 1;
1539 if (panel->top_file > panel->count - 1)
1540 panel->top_file = panel->count - 1;
1542 if ((panel->count - panel->top_file) < items) {
1543 panel->top_file = panel->count - items;
1544 if (panel->top_file < 0)
1545 panel->top_file = 0;
1548 if (panel->selected < panel->top_file)
1549 panel->top_file = panel->selected;
1551 if ((panel->selected - panel->top_file) >= items)
1552 panel->top_file = panel->selected - items + 1;
1554 panel->dirty = 1;
1556 execute_hooks (select_file_hook);
1559 /* Clears all files in the panel, used only when one file was marked */
1560 void
1561 unmark_files (WPanel *panel)
1563 int i;
1565 if (!panel->marked)
1566 return;
1567 for (i = 0; i < panel->count; i++)
1568 file_mark (panel, i, 0);
1570 panel->dirs_marked = 0;
1571 panel->marked = 0;
1572 panel->total = 0;
1575 static void
1576 unselect_item (WPanel *panel)
1578 repaint_file (panel, panel->selected, 1, 2*selection (panel)->f.marked, 0);
1581 static void
1582 move_down (WPanel *panel)
1584 if (panel->selected+1 == panel->count)
1585 return;
1587 unselect_item (panel);
1588 panel->selected++;
1590 if (panel->selected - panel->top_file == ITEMS (panel) &&
1591 panel_scroll_pages){
1592 /* Scroll window half screen */
1593 panel->top_file += ITEMS (panel)/2;
1594 if (panel->top_file > panel->count - ITEMS (panel))
1595 panel->top_file = panel->count - ITEMS (panel);
1596 paint_dir (panel);
1597 select_item (panel);
1599 select_item (panel);
1602 static void
1603 move_up (WPanel *panel)
1605 if (panel->selected == 0)
1606 return;
1608 unselect_item (panel);
1609 panel->selected--;
1610 if (panel->selected < panel->top_file && panel_scroll_pages){
1611 /* Scroll window half screen */
1612 panel->top_file -= ITEMS (panel)/2;
1613 if (panel->top_file < 0) panel->top_file = 0;
1614 paint_dir (panel);
1616 select_item (panel);
1619 /* Changes the selection by lines (may be negative) */
1620 static void
1621 move_selection (WPanel *panel, int lines)
1623 int new_pos;
1624 int adjust = 0;
1626 new_pos = panel->selected + lines;
1627 if (new_pos >= panel->count)
1628 new_pos = panel->count-1;
1630 if (new_pos < 0)
1631 new_pos = 0;
1633 unselect_item (panel);
1634 panel->selected = new_pos;
1636 if (panel->selected - panel->top_file >= ITEMS (panel)){
1637 panel->top_file += lines;
1638 adjust = 1;
1641 if (panel->selected - panel->top_file < 0){
1642 panel->top_file += lines;
1643 adjust = 1;
1646 if (adjust){
1647 if (panel->top_file > panel->selected)
1648 panel->top_file = panel->selected;
1649 if (panel->top_file < 0)
1650 panel->top_file = 0;
1651 paint_dir (panel);
1653 select_item (panel);
1656 static cb_ret_t
1657 move_left (WPanel *panel, int c_code)
1659 if (panel->split) {
1660 move_selection (panel, -llines (panel));
1661 return MSG_HANDLED;
1662 } else
1663 return maybe_cd (1); /* cd .. */
1666 static int
1667 move_right (WPanel *panel, int c_code)
1669 if (panel->split) {
1670 move_selection (panel, llines (panel));
1671 return MSG_HANDLED;
1672 } else
1673 return maybe_cd (0); /* cd (selection) */
1676 static void
1677 prev_page (WPanel *panel)
1679 int items;
1681 if (!panel->selected && !panel->top_file)
1682 return;
1683 unselect_item (panel);
1684 items = ITEMS (panel);
1685 if (panel->top_file < items)
1686 items = panel->top_file;
1687 if (!items)
1688 panel->selected = 0;
1689 else
1690 panel->selected -= items;
1691 panel->top_file -= items;
1693 /* This keeps the selection in a reasonable place */
1694 if (panel->selected < 0)
1695 panel->selected = 0;
1696 if (panel->top_file < 0)
1697 panel->top_file = 0;
1698 select_item (panel);
1699 paint_dir (panel);
1702 static void
1703 ctrl_prev_page (WPanel *panel)
1705 do_cd ("..", cd_exact);
1708 static void
1709 next_page (WPanel *panel)
1711 int items;
1713 if (panel->selected == panel->count - 1)
1714 return;
1715 unselect_item (panel);
1716 items = ITEMS (panel);
1717 if (panel->top_file > panel->count - 2 * items)
1718 items = panel->count - items - panel->top_file;
1719 if (panel->top_file + items < 0)
1720 items = -panel->top_file;
1721 if (!items)
1722 panel->selected = panel->count - 1;
1723 else
1724 panel->selected += items;
1725 panel->top_file += items;
1727 /* This keeps the selection in it's relative position */
1728 if (panel->selected >= panel->count)
1729 panel->selected = panel->count - 1;
1730 if (panel->top_file >= panel->count)
1731 panel->top_file = panel->count - 1;
1732 select_item (panel);
1733 paint_dir (panel);
1736 static void
1737 ctrl_next_page (WPanel *panel)
1739 if ((S_ISDIR (selection (panel)->st.st_mode)
1740 || link_isdir (selection (panel)))) {
1741 do_cd (selection (panel)->fname, cd_exact);
1745 static void
1746 goto_top_file (WPanel *panel)
1748 unselect_item (panel);
1749 panel->selected = panel->top_file;
1750 select_item (panel);
1753 static void
1754 goto_middle_file (WPanel *panel)
1756 unselect_item (panel);
1757 panel->selected = panel->top_file + (ITEMS (panel)/2);
1758 if (panel->selected >= panel->count)
1759 panel->selected = panel->count - 1;
1760 select_item (panel);
1763 static void
1764 goto_bottom_file (WPanel *panel)
1766 unselect_item (panel);
1767 panel->selected = panel->top_file + ITEMS (panel)-1;
1768 if (panel->selected >= panel->count)
1769 panel->selected = panel->count - 1;
1770 select_item (panel);
1773 static void
1774 move_home (WPanel *panel)
1776 if (panel->selected == 0)
1777 return;
1778 unselect_item (panel);
1780 if (torben_fj_mode){
1781 int middle_pos = panel->top_file + (ITEMS (panel)/2);
1783 if (panel->selected > middle_pos){
1784 goto_middle_file (panel);
1785 return;
1787 if (panel->selected != panel->top_file){
1788 goto_top_file (panel);
1789 return;
1793 panel->top_file = 0;
1794 panel->selected = 0;
1796 paint_dir (panel);
1797 select_item (panel);
1800 static void
1801 move_end (WPanel *panel)
1803 if (panel->selected == panel->count-1)
1804 return;
1805 unselect_item (panel);
1806 if (torben_fj_mode){
1807 int middle_pos = panel->top_file + (ITEMS (panel)/2);
1809 if (panel->selected < middle_pos){
1810 goto_middle_file (panel);
1811 return;
1813 if (panel->selected != (panel->top_file + ITEMS(panel)-1)){
1814 goto_bottom_file (panel);
1815 return;
1819 panel->selected = panel->count-1;
1820 paint_dir (panel);
1821 select_item (panel);
1824 /* Recalculate the panels summary information, used e.g. when marked
1825 files might have been removed by an external command */
1826 void
1827 recalculate_panel_summary (WPanel *panel)
1829 int i;
1831 panel->marked = 0;
1832 panel->dirs_marked = 0;
1833 panel->total = 0;
1835 for (i = 0; i < panel->count; i++)
1836 if (panel->dir.list [i].f.marked){
1837 /* do_file_mark will return immediately if newmark == oldmark.
1838 So we have to first unmark it to get panel's summary information
1839 updated. (Norbert) */
1840 panel->dir.list [i].f.marked = 0;
1841 do_file_mark (panel, i, 1);
1845 /* This routine marks a file or a directory */
1846 void
1847 do_file_mark (WPanel *panel, int idx, int mark)
1849 if (panel->dir.list[idx].f.marked == mark)
1850 return;
1852 /* Only '..' can't be marked, '.' isn't visible */
1853 if (!strcmp (panel->dir.list[idx].fname, ".."))
1854 return;
1856 file_mark (panel, idx, mark);
1857 if (panel->dir.list[idx].f.marked) {
1858 panel->marked++;
1859 if (S_ISDIR (panel->dir.list[idx].st.st_mode)) {
1860 if (panel->dir.list[idx].f.dir_size_computed)
1861 panel->total += panel->dir.list[idx].st.st_size;
1862 panel->dirs_marked++;
1863 } else
1864 panel->total += panel->dir.list[idx].st.st_size;
1865 set_colors (panel);
1866 } else {
1867 if (S_ISDIR (panel->dir.list[idx].st.st_mode)) {
1868 if (panel->dir.list[idx].f.dir_size_computed)
1869 panel->total -= panel->dir.list[idx].st.st_size;
1870 panel->dirs_marked--;
1871 } else
1872 panel->total -= panel->dir.list[idx].st.st_size;
1873 panel->marked--;
1877 static void
1878 do_mark_file (WPanel *panel, int do_move)
1880 do_file_mark (panel, panel->selected,
1881 selection (panel)->f.marked ? 0 : 1);
1882 if (mark_moves_down && do_move)
1883 move_down (panel);
1886 static void
1887 mark_file (WPanel *panel)
1889 do_mark_file (panel, 1);
1892 /* Incremental search of a file name in the panel */
1893 static void
1894 do_search (WPanel *panel, int c_code)
1896 size_t l;
1897 int i;
1898 int wrapped = 0;
1899 int found;
1901 l = strlen (panel->search_buffer);
1902 if (c_code == KEY_BACKSPACE) {
1903 if (l)
1904 panel->search_buffer[--l] = '\0';
1905 } else {
1906 if (c_code && l < sizeof (panel->search_buffer)) {
1907 panel->search_buffer[l] = c_code;
1908 panel->search_buffer[l + 1] = 0;
1909 l++;
1913 found = 0;
1914 for (i = panel->selected; !wrapped || i != panel->selected; i++) {
1915 if (i >= panel->count) {
1916 i = 0;
1917 if (wrapped)
1918 break;
1919 wrapped = 1;
1921 if (panel->
1922 case_sensitive
1923 ? (strncmp (panel->dir.list[i].fname, panel->search_buffer, l)
1924 == 0) : (g_strncasecmp (panel->dir.list[i].fname,
1925 panel->search_buffer, l) == 0)) {
1926 unselect_item (panel);
1927 panel->selected = i;
1928 select_item (panel);
1929 found = 1;
1930 break;
1933 if (!found)
1934 panel->search_buffer[--l] = 0;
1936 paint_panel (panel);
1939 static void
1940 start_search (WPanel *panel)
1942 if (panel->searching){
1943 if (panel->selected+1 == panel->count)
1944 panel->selected = 0;
1945 else
1946 move_down (panel);
1947 do_search (panel, 0);
1948 } else {
1949 panel->searching = 1;
1950 panel->search_buffer [0] = 0;
1951 display_mini_info (panel);
1952 mc_refresh ();
1956 /* Return 1 if the Enter key has been processed, 0 otherwise */
1957 static int
1958 do_enter_on_file_entry (file_entry *fe)
1960 char *full_name;
1963 * Directory or link to directory - change directory.
1964 * Try the same for the entries on which mc_lstat() has failed.
1966 if (S_ISDIR (fe->st.st_mode) || link_isdir (fe)
1967 || (fe->st.st_mode == 0)) {
1968 if (!do_cd (fe->fname, cd_exact))
1969 message (1, MSG_ERROR, _("Cannot change directory"));
1970 return 1;
1973 /* Try associated command */
1974 if (regex_command (fe->fname, "Open", 0) != 0)
1975 return 1;
1977 /* Check if the file is executable */
1978 full_name = mhl_str_dir_plus_file (current_panel->cwd, fe->fname);
1979 if (!is_exe (fe->st.st_mode) || !if_link_is_exe (full_name, fe)) {
1980 g_free (full_name);
1981 return 0;
1983 g_free (full_name);
1985 if (confirm_execute) {
1986 if (query_dialog
1987 (_(" The Midnight Commander "),
1988 _(" Do you really want to execute? "), 0, 2, _("&Yes"),
1989 _("&No")) != 0)
1990 return 1;
1992 #ifdef USE_VFS
1993 if (!vfs_current_is_local ()) {
1994 char *tmp;
1995 int ret;
1997 tmp = mhl_str_dir_plus_file (vfs_get_current_dir (), fe->fname);
1998 ret = mc_setctl (tmp, VFS_SETCTL_RUN, NULL);
1999 g_free (tmp);
2000 /* We took action only if the dialog was shown or the execution
2001 * was successful */
2002 return confirm_execute || (ret == 0);
2004 #endif
2007 char *tmp = name_quote (fe->fname, 0);
2008 char *cmd = g_strconcat (".", PATH_SEP_STR, tmp, (char *) NULL);
2009 g_free (tmp);
2010 shell_execute (cmd, 0);
2011 g_free (cmd);
2014 return 1;
2017 static int
2018 do_enter (WPanel *panel)
2020 return do_enter_on_file_entry (selection (panel));
2023 static void
2024 chdir_other_panel (WPanel *panel)
2026 char *new_dir;
2027 char *sel_entry = NULL;
2029 if (get_other_type () != view_listing) {
2030 set_display_type (get_other_index (), view_listing);
2033 if (!S_ISDIR (panel->dir.list [panel->selected].st.st_mode)) {
2034 new_dir = mhl_str_dir_plus_file (panel->cwd, "..");
2035 sel_entry = strrchr(panel->cwd, PATH_SEP);
2036 } else
2037 new_dir = mhl_str_dir_plus_file (panel->cwd, panel->dir.list [panel->selected].fname);
2039 change_panel ();
2040 do_cd (new_dir, cd_exact);
2041 if (sel_entry)
2042 try_to_select (current_panel, sel_entry);
2043 change_panel ();
2045 move_down (panel);
2047 g_free (new_dir);
2051 * Make the current directory of the current panel also the current
2052 * directory of the other panel. Put the other panel to the listing
2053 * mode if needed. If the current panel is panelized, the other panel
2054 * doesn't become panelized.
2056 static void
2057 sync_other_panel (WPanel *panel)
2059 if (get_other_type () != view_listing) {
2060 set_display_type (get_other_index (), view_listing);
2063 do_panel_cd (other_panel, current_panel->cwd, cd_exact);
2065 /* try to select current filename on the other panel */
2066 if (!panel->is_panelized) {
2067 try_to_select (other_panel, selection (panel)->fname);
2071 static void
2072 chdir_to_readlink (WPanel *panel)
2074 char *new_dir;
2076 if (get_other_type () != view_listing)
2077 return;
2079 if (S_ISLNK (panel->dir.list [panel->selected].st.st_mode)) {
2080 char buffer [MC_MAXPATHLEN], *p;
2081 int i;
2082 struct stat st;
2084 i = readlink (selection (panel)->fname, buffer, MC_MAXPATHLEN - 1);
2085 if (i < 0)
2086 return;
2087 if (mc_stat (selection (panel)->fname, &st) < 0)
2088 return;
2089 buffer [i] = 0;
2090 if (!S_ISDIR (st.st_mode)) {
2091 p = strrchr (buffer, PATH_SEP);
2092 if (p && !p[1]) {
2093 *p = 0;
2094 p = strrchr (buffer, PATH_SEP);
2096 if (!p)
2097 return;
2098 p[1] = 0;
2100 if (*buffer == PATH_SEP)
2101 new_dir = g_strdup (buffer);
2102 else
2103 new_dir = mhl_str_dir_plus_file (panel->cwd, buffer);
2105 change_panel ();
2106 do_cd (new_dir, cd_exact);
2107 change_panel ();
2109 move_down (panel);
2111 g_free (new_dir);
2115 typedef void (*panel_key_callback) (WPanel *);
2116 typedef struct {
2117 int key_code;
2118 panel_key_callback fn;
2119 } panel_key_map;
2121 static void cmd_do_enter(WPanel *wp) { (void) do_enter(wp); }
2122 static void cmd_view_simple(WPanel *wp) { view_simple_cmd(); }
2123 static void cmd_edit_new(WPanel *wp) { edit_cmd_new(); }
2124 static void cmd_copy_local(WPanel *wp) { copy_cmd_local(); }
2125 static void cmd_rename_local(WPanel *wp) { ren_cmd_local(); }
2126 static void cmd_delete_local(WPanel *wp) { delete_cmd_local(); }
2127 static void cmd_select(WPanel *wp) { select_cmd(); }
2128 static void cmd_unselect(WPanel *wp) { unselect_cmd(); }
2129 static void cmd_reverse_selection(WPanel *wp) { reverse_selection_cmd(); }
2131 static const panel_key_map panel_keymap [] = {
2132 { KEY_DOWN, move_down },
2133 { KEY_UP, move_up },
2135 /* The action button :-) */
2136 { '\n', cmd_do_enter },
2137 { KEY_ENTER, cmd_do_enter },
2139 { KEY_IC, mark_file },
2140 { KEY_HOME, move_home },
2141 { KEY_A1, move_home },
2142 { ALT ('<'), move_home },
2143 { KEY_C1, move_end },
2144 { KEY_END, move_end },
2145 { ALT ('>'), move_end },
2146 { KEY_NPAGE, next_page },
2147 { KEY_PPAGE, prev_page },
2148 { KEY_NPAGE | KEY_M_CTRL, ctrl_next_page },
2149 { KEY_PPAGE | KEY_M_CTRL, ctrl_prev_page },
2151 /* To quickly move in the panel */
2152 { ALT('g'), goto_top_file },
2153 { ALT('r'), goto_middle_file }, /* M-r like emacs */
2154 { ALT('j'), goto_bottom_file },
2156 /* Emacs-like bindings */
2157 { XCTRL('v'), next_page }, /* C-v like emacs */
2158 { ALT('v'), prev_page }, /* M-v like emacs */
2159 { XCTRL('p'), move_up }, /* C-p like emacs */
2160 { XCTRL('n'), move_down }, /* C-n like emacs */
2161 { XCTRL('s'), start_search }, /* C-s like emacs */
2162 { ALT('s'), start_search }, /* M-s not like emacs */
2163 { XCTRL('t'), mark_file },
2164 { ALT('o'), chdir_other_panel },
2165 { ALT('i'), sync_other_panel },
2166 { ALT('l'), chdir_to_readlink },
2167 { ALT('H'), directory_history_list },
2168 { KEY_F(13), cmd_view_simple },
2169 { KEY_F(14), cmd_edit_new },
2170 { KEY_F(15), cmd_copy_local },
2171 { KEY_F(16), cmd_rename_local },
2172 { KEY_F(18), cmd_delete_local },
2173 { ALT('y'), directory_history_prev },
2174 { ALT('u'), directory_history_next },
2175 { ALT('+'), cmd_select },
2176 { KEY_KP_ADD, cmd_select },
2177 { ALT('\\'), cmd_unselect },
2178 { ALT('-'), cmd_unselect },
2179 { KEY_KP_SUBTRACT, cmd_unselect },
2180 { ALT('*'), cmd_reverse_selection },
2181 { KEY_KP_MULTIPLY, cmd_reverse_selection },
2182 { 0, 0 }
2185 static inline cb_ret_t
2186 panel_key (WPanel *panel, int key)
2188 int i;
2190 for (i = 0; panel_keymap[i].key_code; i++) {
2191 if (key == panel_keymap[i].key_code) {
2192 int old_searching = panel->searching;
2194 if (panel_keymap[i].fn != start_search)
2195 panel->searching = 0;
2197 (*panel_keymap[i].fn) (panel);
2199 if (panel->searching != old_searching)
2200 display_mini_info (panel);
2201 return MSG_HANDLED;
2204 if (torben_fj_mode && key == ALT ('h')) {
2205 goto_middle_file (panel);
2206 return MSG_HANDLED;
2209 /* We do not want to take a key press if nothing can be done with it */
2210 /* The command line widget may do something more useful */
2211 if (key == KEY_LEFT)
2212 return move_left (panel, key);
2214 if (key == KEY_RIGHT)
2215 return move_right (panel, key);
2217 if (is_abort_char (key)) {
2218 panel->searching = 0;
2219 display_mini_info (panel);
2220 return MSG_HANDLED;
2223 /* Do not eat characters not meant for the panel below ' ' (e.g. C-l). */
2224 if ((key >= ' ' && key <= 255) || key == KEY_BACKSPACE) {
2225 if (panel->searching) {
2226 do_search (panel, key);
2227 return MSG_HANDLED;
2230 if (!command_prompt) {
2231 start_search (panel);
2232 do_search (panel, key);
2233 return MSG_HANDLED;
2237 return MSG_NOT_HANDLED;
2240 static cb_ret_t
2241 panel_callback (Widget *w, widget_msg_t msg, int parm)
2243 WPanel *panel = (WPanel *) w;
2244 Dlg_head *h = panel->widget.parent;
2246 switch (msg) {
2247 case WIDGET_DRAW:
2248 paint_panel (panel);
2249 return MSG_HANDLED;
2251 case WIDGET_FOCUS:
2252 current_panel = panel;
2253 panel->active = 1;
2254 if (mc_chdir (panel->cwd) != 0) {
2255 char *cwd = strip_password (g_strdup (panel->cwd), 1);
2256 message (1, MSG_ERROR, _(" Cannot chdir to \"%s\" \n %s "),
2257 cwd, unix_error_string (errno));
2258 g_free(cwd);
2259 } else
2260 subshell_chdir (panel->cwd);
2262 update_xterm_title_path ();
2263 select_item (panel);
2264 show_dir (panel);
2265 paint_dir (panel);
2266 panel->dirty = 0;
2268 buttonbar_set_label (h, 1, _("Help"), help_cmd);
2269 buttonbar_set_label (h, 2, _("Menu"), user_file_menu_cmd);
2270 buttonbar_set_label (h, 3, _("View"), view_cmd);
2271 buttonbar_set_label (h, 4, _("Edit"), edit_cmd);
2272 buttonbar_set_label (h, 5, _("Copy"), copy_cmd);
2273 buttonbar_set_label (h, 6, _("RenMov"), ren_cmd);
2274 buttonbar_set_label (h, 7, _("Mkdir"), mkdir_cmd);
2275 buttonbar_set_label (h, 8, _("Delete"), delete_cmd);
2276 buttonbar_redraw (h);
2277 return MSG_HANDLED;
2279 case WIDGET_UNFOCUS:
2280 /* Janne: look at this for the multiple panel options */
2281 if (panel->searching){
2282 panel->searching = 0;
2283 display_mini_info (panel);
2285 panel->active = 0;
2286 show_dir (panel);
2287 unselect_item (panel);
2288 return MSG_HANDLED;
2290 case WIDGET_KEY:
2291 return panel_key (panel, parm);
2293 case WIDGET_DESTROY:
2294 panel_destroy (panel);
2295 return MSG_HANDLED;
2297 default:
2298 return default_proc (msg, parm);
2302 void
2303 file_mark (WPanel *panel, int index, int val)
2305 if (panel->dir.list[index].f.marked != val) {
2306 panel->dir.list[index].f.marked = val;
2307 panel->dirty = 1;
2311 /* */
2312 /* Panel mouse events support routines */
2313 /* */
2314 static int mouse_marking = 0;
2316 static void
2317 mouse_toggle_mark (WPanel *panel)
2319 do_mark_file (panel, 0);
2320 mouse_marking = selection (panel)->f.marked;
2323 static void
2324 mouse_set_mark (WPanel *panel)
2326 if (mouse_marking && !(selection (panel)->f.marked))
2327 do_mark_file (panel, 0);
2328 else if (!mouse_marking && (selection (panel)->f.marked))
2329 do_mark_file (panel, 0);
2332 static inline int
2333 mark_if_marking (WPanel *panel, Gpm_Event *event)
2335 if (event->buttons & GPM_B_RIGHT){
2336 if (event->type & GPM_DOWN)
2337 mouse_toggle_mark (panel);
2338 else
2339 mouse_set_mark (panel);
2340 return 1;
2342 return 0;
2346 * Mouse callback of the panel minus repainting.
2347 * If the event is redirected to the menu, *redir is set to 1.
2349 static int
2350 do_panel_event (Gpm_Event *event, WPanel *panel, int *redir)
2352 const int lines = llines (panel);
2354 int my_index;
2356 /* Mouse wheel events */
2357 if ((event->buttons & GPM_B_UP) && (event->type & GPM_DOWN)) {
2358 prev_page (panel);
2359 return MOU_NORMAL;
2361 if ((event->buttons & GPM_B_DOWN) && (event->type & GPM_DOWN)) {
2362 next_page (panel);
2363 return MOU_NORMAL;
2366 /* "<" button */
2367 if (event->type & GPM_DOWN && event->x == 2 && event->y == 1) {
2368 directory_history_prev (panel);
2369 return MOU_NORMAL;
2372 /* ">" button */
2373 if (event->type & GPM_DOWN && event->x == panel->widget.cols - 1
2374 && event->y == 1) {
2375 directory_history_next (panel);
2376 return MOU_NORMAL;
2379 /* "v" button */
2380 if (event->type & GPM_DOWN && event->x == panel->widget.cols - 2
2381 && event->y == 1) {
2382 directory_history_list (panel);
2383 return MOU_NORMAL;
2386 /* rest of the upper frame, the menu is invisible - call menu */
2387 if (event->type & GPM_DOWN && event->y == 1 && !menubar_visible) {
2388 *redir = 1;
2389 event->x += panel->widget.x;
2390 return (*(the_menubar->widget.mouse)) (event, the_menubar);
2393 event->y -= 2;
2394 if ((event->type & (GPM_DOWN | GPM_DRAG))) {
2396 if (!dlg_widget_active (panel))
2397 change_panel ();
2399 if (event->y <= 0) {
2400 mark_if_marking (panel, event);
2401 if (mouse_move_pages)
2402 prev_page (panel);
2403 else
2404 move_up (panel);
2405 return MOU_REPEAT;
2408 if (!((panel->top_file + event->y <= panel->count)
2409 && event->y <= lines)) {
2410 mark_if_marking (panel, event);
2411 if (mouse_move_pages)
2412 next_page (panel);
2413 else
2414 move_down (panel);
2415 return MOU_REPEAT;
2417 my_index = panel->top_file + event->y - 1;
2418 if (panel->split) {
2419 if (event->x > ((panel->widget.cols - 2) / 2))
2420 my_index += llines (panel);
2423 if (my_index >= panel->count)
2424 my_index = panel->count - 1;
2426 if (my_index != panel->selected) {
2427 unselect_item (panel);
2428 panel->selected = my_index;
2429 select_item (panel);
2432 /* This one is new */
2433 mark_if_marking (panel, event);
2435 } else if ((event->type & (GPM_UP | GPM_DOUBLE)) ==
2436 (GPM_UP | GPM_DOUBLE)) {
2437 if (event->y > 0 && event->y <= lines)
2438 do_enter (panel);
2440 return MOU_NORMAL;
2443 /* Mouse callback of the panel */
2444 static int
2445 panel_event (Gpm_Event *event, void *data)
2447 WPanel *panel = data;
2448 int ret;
2449 int redir = 0;
2451 ret = do_panel_event (event, panel, &redir);
2452 if (!redir)
2453 panel_update_contents (panel);
2455 return ret;
2458 void
2459 panel_re_sort (WPanel *panel)
2461 char *filename;
2462 int i;
2464 if (panel == NULL)
2465 return;
2467 filename = g_strdup (selection (panel)->fname);
2468 unselect_item (panel);
2469 do_sort (&panel->dir, panel->sort_type, panel->count-1, panel->reverse,
2470 panel->case_sensitive, panel->exec_first);
2471 panel->selected = -1;
2472 for (i = panel->count; i; i--){
2473 if (!strcmp (panel->dir.list [i-1].fname, filename)){
2474 panel->selected = i-1;
2475 break;
2478 g_free (filename);
2479 panel->top_file = panel->selected - ITEMS (panel)/2;
2480 if (panel->top_file < 0)
2481 panel->top_file = 0;
2482 select_item (panel);
2483 panel->dirty = 1;
2486 void
2487 panel_set_sort_order (WPanel *panel, sortfn *sort_order)
2489 if (sort_order == 0)
2490 return;
2492 panel->sort_type = sort_order;
2494 /* The directory is already sorted, we have to load the unsorted stuff */
2495 if (sort_order == (sortfn *) unsorted){
2496 char *current_file;
2498 current_file = g_strdup (panel->dir.list [panel->selected].fname);
2499 panel_reload (panel);
2500 try_to_select (panel, current_file);
2501 g_free (current_file);
2503 panel_re_sort (panel);