* vfs.c (vfs_init) [!WITH_MCFS]: Don't register mcfs.
[midnight-commander.git] / src / screen.c
blobcb126536429b5f71ed2c476978dc00baf9a5a152
1 /* Panel managing.
2 Copyright (C) 1994, 1995 Miguel de Icaza.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 Written by: 1995 Miguel de Icaza
15 1997, 1999 Timur Bakeyev
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21 #include <config.h>
22 #include <string.h>
23 #include <stdio.h>
24 #include <errno.h>
25 #ifdef HAVE_UNISTD_H
26 # include <unistd.h> /* For chdir(), readlink() and getwd()/getcwd() */
27 #endif
29 #include "global.h"
30 #include "tty.h"
31 #include "dir.h"
32 #include "panel.h"
33 #include "color.h"
34 #include "tree.h"
35 #include "win.h"
36 #include "main.h"
37 #include "ext.h" /* regexp_command */
38 #include "mouse.h" /* For Gpm_Event */
39 #include "layout.h" /* Most layout variables are here */
40 #include "dialog.h" /* for message (...) */
41 #include "cmd.h"
42 #include "key.h" /* XCTRL and ALT macros */
43 #include "setup.h" /* For loading/saving panel options */
44 #include "user.h"
45 #include "profile.h"
46 #include "widget.h"
47 #include "../vfs/vfs.h"
49 #ifdef _OS_NT
50 # include "drive.h"
51 #endif
53 #define ELEMENTS(arr) ( sizeof(arr) / sizeof((arr)[0]) )
55 /* If true, show the mini-info on the panel */
56 int show_mini_info = 1;
58 /* If true, then use stat() on the cwd to determine directory changes */
59 int fast_reload = 0;
61 /* If true, use some usability hacks by Torben */
62 int torben_fj_mode = 0;
64 /* If true, up/down keys scroll the pane listing by pages */
65 int panel_scroll_pages = 1;
67 /* If 1, we use permission hilighting */
68 int permission_mode = 0;
70 /* If 1 - then add per file type hilighting */
71 int filetype_mode = 1;
73 /* If we have an info panel, this points to it */
74 WPanel *the_info_panel = 0;
76 /* The hook list for the select file function */
77 Hook *select_file_hook = 0;
79 static int panel_callback (Dlg_head *h, WPanel *p, int Msg, int Par);
80 static int panel_event (Gpm_Event *event, WPanel *panel);
81 static void paint_frame (WPanel *panel);
82 static char *panel_format (WPanel *panel);
83 static char *mini_status_format (WPanel *panel);
85 #define x_adjust_top_file(p)
86 #define x_reset_sort_labels(x)
88 /* This macro extracts the number of available lines in a panel */
89 #define llines(p) (p->widget.lines-3 - (show_mini_info ? 2 : 0))
91 #ifdef PORT_NOT_FOCUS_SELECT_ITEM
92 # define focus_select_item(x)
93 #else
94 # define focus_select_item(x) select_item(x)
95 #endif
97 #ifdef PORT_NOT_UNFOCUS_UNSELECT_ITEM
98 # define unfocus_unselect_item(x)
99 #else
100 # define unfocus_unselect_item(x) unselect_item(x)
101 #endif
103 #define x_create_panel(x,y,z) 1;
104 #define x_panel_load_index(p,x)
105 #define x_panel_select_item(a,b,c)
106 #define x_panel_destroy(p)
108 static void
109 set_colors (WPanel *panel)
111 standend ();
112 if (hascolors)
113 attrset (NORMAL_COLOR);
116 #ifndef ICONS_PER_ROW
117 # define ICONS_PER_ROW(x) 1
118 #endif
120 /* Delete format string, it is a linked list */
121 static void
122 delete_format (format_e *format)
124 format_e *next;
126 while (format){
127 next = format->next;
128 g_free (format);
129 format = next;
133 /* This code relies on the default justification!!! */
134 static void
135 add_permission_string (char *dest, int width, file_entry *fe, int attr, int color, int is_octal)
137 int i, r, l;
139 l = get_user_permissions (&fe->buf);
141 if (is_octal){
142 /* Place of the access bit in octal mode */
143 l = width + l - 3;
144 r = l + 1;
145 } else {
146 /* The same to the triplet in string mode */
147 l = l * 3 + 1;
148 r = l + 3;
151 for(i = 0; i < width; i++){
152 if (i >= l && i < r){
153 if (attr == SELECTED || attr == MARKED_SELECTED)
154 attrset (MARKED_SELECTED_COLOR);
155 else
156 attrset (MARKED_COLOR);
157 } else
158 attrset (color);
160 addch (dest[i]);
164 /* String representations of various file attributes */
165 /* name */
166 static char *
167 string_file_name (file_entry *fe, int len)
169 return fe->fname;
172 /* size */
173 static char *
174 string_file_size (file_entry *fe, int len)
176 static char buffer [BUF_TINY];
178 /* Don't ever show size of ".." since we don't calculate it */
179 if (!strcmp (fe->fname, "..")) {
180 strcpy (buffer, _("UP--DIR"));
181 return buffer;
184 #ifdef HAVE_ST_RDEV
185 if (S_ISBLK (fe->buf.st_mode) || S_ISCHR (fe->buf.st_mode))
186 g_snprintf (buffer, sizeof (buffer), "%3d,%3d",
187 (int) ((fe->buf.st_rdev >> 8) & 0xff),
188 (int) (fe->buf.st_rdev & 0xff));
189 else
190 #endif
192 size_trunc_len (buffer, len, fe->buf.st_size, 0);
194 return buffer;
197 /* bsize */
198 static char *
199 string_file_size_brief (file_entry *fe, int len)
201 static char buffer [BUF_TINY];
203 if (S_ISLNK (fe->buf.st_mode) && !fe->f.link_to_dir) {
204 strcpy (buffer, _("SYMLINK"));
205 return buffer;
208 if ((S_ISDIR (fe->buf.st_mode) || fe->f.link_to_dir) && strcmp (fe->fname, "..")) {
209 strcpy (buffer, _("SUB-DIR"));
210 return buffer;
213 return string_file_size (fe, len);
216 /* This functions return a string representation of a file entry */
217 /* type */
218 static char *
219 string_file_type (file_entry *fe, int len)
221 static char buffer [2];
223 if (S_ISDIR (fe->buf.st_mode))
224 buffer [0] = PATH_SEP;
225 else if (S_ISLNK (fe->buf.st_mode)) {
226 if (fe->f.link_to_dir)
227 buffer [0] = '~';
228 else if (fe->f.stalled_link)
229 buffer [0] = '!';
230 else
231 buffer [0] = '@';
232 } else if (S_ISCHR (fe->buf.st_mode))
233 buffer [0] = '-';
234 #ifdef S_ISSOCK
235 else if (S_ISSOCK (fe->buf.st_mode))
236 buffer [0] = '=';
237 #endif
238 #ifdef S_ISDOOR
239 else if (S_ISDOOR (fe->buf.st_mode))
240 buffer [0] = '>';
241 #endif
242 else if (S_ISBLK (fe->buf.st_mode))
243 buffer [0] = '+';
244 else if (S_ISFIFO (fe->buf.st_mode))
245 buffer [0] = '|';
246 else if (is_exe (fe->buf.st_mode))
247 buffer [0] = '*';
248 else
249 buffer [0] = ' ';
250 buffer [1] = '\0';
251 return buffer;
254 /* mtime */
255 static char *
256 string_file_mtime (file_entry *fe, int len)
258 #ifdef PORT_STATIC_IN_STRING_FILE_XTIME
259 static char timebuf [MAX_I18NTIMELENGTH + 1];
261 return strcpy (timebuf, file_date (fe->buf.st_mtime));
262 #else
263 return file_date (fe->buf.st_mtime);
264 #endif
267 /* atime */
268 static char *
269 string_file_atime (file_entry *fe, int len)
271 #ifdef PORT_STATIC_IN_STRING_FILE_XTIME
272 static char timebuf [MAX_I18NTIMELENGTH + 1];
274 return strcpy (timebuf, file_date (fe->buf.st_atime));
275 #else
276 return file_date (fe->buf.st_atime);
277 #endif
280 /* ctime */
281 static char *
282 string_file_ctime (file_entry *fe, int len)
284 #ifdef PORT_STATIC_IN_STRING_FILE_XTIME
285 static char timebuf [MAX_I18NTIMELENGTH + 1];
287 return strcpy (timebuf, file_date (fe->buf.st_ctime));
288 #else
289 return file_date (fe->buf.st_ctime);
290 #endif
293 /* perm */
294 static char *
295 string_file_permission (file_entry *fe, int len)
297 return string_perm (fe->buf.st_mode);
300 /* mode */
301 static char *
302 string_file_perm_octal (file_entry *fe, int len)
304 static char buffer [10];
306 g_snprintf (buffer, sizeof (buffer), "0%06o", fe->buf.st_mode);
307 return buffer;
310 /* nlink */
311 static char *
312 string_file_nlinks (file_entry *fe, int len)
314 static char buffer [BUF_TINY];
316 g_snprintf (buffer, sizeof (buffer), "%16d", fe->buf.st_nlink);
317 return buffer;
320 /* inode */
321 static char *
322 string_inode (file_entry *fe, int len)
324 static char buffer [10];
326 g_snprintf (buffer, sizeof (buffer), "%lu", (unsigned long) fe->buf.st_ino);
327 return buffer;
330 /* nuid */
331 static char *
332 string_file_nuid (file_entry *fe, int len)
334 static char buffer [10];
336 g_snprintf (buffer, sizeof (buffer), "%d", fe->buf.st_uid);
337 return buffer;
340 /* ngid */
341 static char *
342 string_file_ngid (file_entry *fe, int len)
344 static char buffer [10];
346 g_snprintf (buffer, sizeof (buffer), "%d", fe->buf.st_gid);
347 return buffer;
350 /* owner */
351 static char *
352 string_file_owner (file_entry *fe, int len)
354 return get_owner (fe->buf.st_uid);
357 /* group */
358 static char *
359 string_file_group (file_entry *fe, int len)
361 return get_group (fe->buf.st_gid);
364 /* mark */
365 static char *
366 string_marked (file_entry *fe, int len)
368 return fe->f.marked ? "*" : " ";
371 /* space */
372 static char *
373 string_space (file_entry *fe, int len)
375 return " ";
378 /* dot */
379 static char *
380 string_dot (file_entry *fe, int len)
382 return ".";
385 #define GT 1
387 static struct {
388 char *id;
389 int min_size;
390 int expands;
391 int default_just;
392 char *title;
393 int use_in_gui;
394 char *(*string_fn)(file_entry *, int);
395 sortfn *sort_routine;
396 } formats [] = {
397 { "name", 12, 1, J_LEFT_FIT, N_("Name"), 1, string_file_name, (sortfn *) sort_name },
398 { "size", 7, 0, J_RIGHT, N_("Size"), 1, string_file_size, (sortfn *) sort_size },
399 { "bsize", 7, 0, J_RIGHT, N_("Size"), 1, string_file_size_brief, (sortfn *) sort_size },
400 { "type", GT, 0, J_LEFT, "", 2, string_file_type, (sortfn *) sort_type },
401 { "mtime", 12, 0, J_RIGHT, N_("MTime"), 1, string_file_mtime, (sortfn *) sort_time },
402 { "atime", 12, 0, J_RIGHT, N_("ATime"), 1, string_file_atime, (sortfn *) sort_atime },
403 { "ctime", 12, 0, J_RIGHT, N_("CTime"), 1, string_file_ctime, (sortfn *) sort_ctime },
404 { "perm", 10, 0, J_LEFT, N_("Permission"),1,string_file_permission, NULL },
405 { "mode", 6, 0, J_RIGHT, N_("Perm"), 1, string_file_perm_octal, NULL },
406 { "nlink", 2, 0, J_RIGHT, N_("Nl"), 1, string_file_nlinks, (sortfn *) sort_links },
407 { "inode", 5, 0, J_RIGHT, N_("Inode"), 1, string_inode, (sortfn *) sort_inode },
408 { "nuid", 5, 0, J_RIGHT, N_("UID"), 1, string_file_nuid, (sortfn *) sort_nuid },
409 { "ngid", 5, 0, J_RIGHT, N_("GID"), 1, string_file_ngid, (sortfn *) sort_ngid },
410 { "owner", 8, 0, J_LEFT_FIT, N_("Owner"), 1, string_file_owner, (sortfn *) sort_owner },
411 { "group", 8, 0, J_LEFT_FIT, N_("Group"), 1, string_file_group, (sortfn *) sort_group },
412 { "mark", 1, 0, J_RIGHT, " ", 1, string_marked, NULL },
413 { "|", 1, 0, J_RIGHT, " ", 0, NULL, NULL },
414 { "space", 1, 0, J_RIGHT, " ", 0, string_space, NULL },
415 { "dot", 1, 0, J_RIGHT, " ", 0, string_dot, NULL },
418 static char *
419 to_buffer (char *dest, int just_mode, int len, char *txt)
421 int txtlen = strlen (txt);
422 int still, over;
424 /* Fill buffer with spaces */
425 memset (dest, ' ', len);
427 still = (over=(txtlen > len)) ? (txtlen - len) : (len - txtlen);
429 switch (HIDE_FIT(just_mode)){
430 case J_LEFT:
431 still = 0;
432 break;
433 case J_CENTER:
434 still /= 2;
435 break;
436 case J_RIGHT:
437 default:
438 break;
441 if (over){
442 if (IS_FIT(just_mode))
443 strcpy (dest, name_trunc(txt, len));
444 else
445 strncpy (dest, txt+still, len);
446 } else
447 strncpy (dest+still, txt, txtlen);
449 dest[len] = '\0';
451 return (dest + len);
454 static int
455 file_compute_color (int attr, file_entry *fe)
457 switch (attr){
458 case SELECTED:
459 return (SELECTED_COLOR);
460 case MARKED:
461 return (MARKED_COLOR);
462 case MARKED_SELECTED:
463 return (MARKED_SELECTED_COLOR);
464 case STATUS:
465 return (NORMAL_COLOR);
466 case NORMAL:
467 default:
468 if (!filetype_mode)
469 return (NORMAL_COLOR);
472 /* if filetype_mode == true */
473 if (S_ISDIR (fe->buf.st_mode))
474 return (DIRECTORY_COLOR);
475 else if (S_ISLNK (fe->buf.st_mode)){
476 if (fe->f.link_to_dir)
477 return (DIRECTORY_COLOR);
478 else if (fe->f.stalled_link)
479 return (STALLED_LINK_COLOR);
480 else
481 return (LINK_COLOR);
482 } else if (S_ISSOCK (fe->buf.st_mode))
483 return (SPECIAL_COLOR);
484 else if (S_ISCHR (fe->buf.st_mode))
485 return (DEVICE_COLOR);
486 else if (S_ISBLK (fe->buf.st_mode))
487 return (DEVICE_COLOR);
488 else if (S_ISFIFO (fe->buf.st_mode))
489 return (SPECIAL_COLOR);
490 else if (is_exe (fe->buf.st_mode))
491 return (EXECUTABLE_COLOR);
492 else if (fe->fname && (!strcmp (fe->fname, "core") || !strcmp (extension(fe->fname), "core")))
493 return (CORE_COLOR);
495 return (NORMAL_COLOR); /* just for safeness */
498 /* Formats the file number file_index of panel in the buffer dest */
499 static void
500 format_file (char *dest, int limit, WPanel *panel, int file_index, int width, int attr, int isstatus)
502 int color, length, empty_line;
503 char *txt;
504 char *old_pos;
505 char *cdest = dest;
506 format_e *format, *home;
507 file_entry *fe;
509 length = 0;
510 empty_line = (file_index >= panel->count);
511 home = (isstatus) ? panel->status_format : panel->format;
512 fe = &panel->dir.list [file_index];
514 if (!empty_line)
515 color = file_compute_color (attr, fe);
516 else
517 color = NORMAL_COLOR;
519 for (format = home; format; format = format->next){
521 if (length == width)
522 break;
524 if (format->string_fn){
525 int len;
527 if (empty_line)
528 txt = " ";
529 else
530 txt = (*format->string_fn)(fe, format->field_len);
532 old_pos = cdest;
534 len = format->field_len;
535 if (len + length > width)
536 len = width - length;
537 if (len + (cdest - dest) > limit)
538 len = limit - (cdest - dest);
539 if (len <= 0)
540 break;
541 cdest = to_buffer (cdest, format->just_mode, len, txt);
542 length += len;
544 attrset (color);
546 if (permission_mode && !strcmp(format->id, "perm"))
547 add_permission_string (old_pos, format->field_len, fe, attr, color, 0);
548 else if (permission_mode && !strcmp(format->id, "mode"))
549 add_permission_string (old_pos, format->field_len, fe, attr, color, 1);
550 else
551 addstr (old_pos);
553 } else {
554 if (attr == SELECTED || attr == MARKED_SELECTED)
555 attrset (SELECTED_COLOR);
556 else
557 attrset (NORMAL_COLOR);
558 one_vline ();
559 length++;
563 if (length < width){
564 int still = width - length;
565 while (still--)
566 addch (' ');
570 static void
571 repaint_file (WPanel *panel, int file_index, int mv, int attr, int isstatus)
573 int second_column = 0;
574 int width, offset;
575 char buffer [BUF_MEDIUM];
577 offset = 0;
578 if (!isstatus && panel->split){
580 second_column = (file_index - panel->top_file) / llines (panel);
581 width = (panel->widget.cols - 2)/2 - 1;
583 if (second_column){
584 offset = 1 + width;
585 width = (panel->widget.cols-2) - (panel->widget.cols-2)/2 - 1;
587 } else
588 width = (panel->widget.cols - 2);
590 /* Nothing to paint */
591 if (width <= 0)
592 return;
594 if (mv){
595 if (!isstatus && panel->split){
596 widget_move (&panel->widget,
597 (file_index - panel->top_file) %
598 llines (panel) + 2,
599 (offset + 1));
600 } else
601 widget_move (&panel->widget, file_index - panel->top_file + 2, 1);
604 format_file (buffer, sizeof(buffer), panel, file_index, width, attr, isstatus);
606 if (!isstatus && panel->split){
607 if (second_column)
608 addch (' ');
609 else {
610 attrset (NORMAL_COLOR);
611 one_vline ();
616 void
617 display_mini_info (WPanel *panel)
619 if (!show_mini_info)
620 return;
622 widget_move (&panel->widget, llines (panel)+3, 1);
624 if (panel->searching){
625 attrset (INPUT_COLOR);
626 printw ("/%-*s", panel->widget.cols-3, panel->search_buffer);
627 attrset (NORMAL_COLOR);
628 return;
631 /* Status displays total marked size */
632 if (panel->marked){
633 char buffer [BUF_SMALL];
634 char *p = " %-*s";
635 int cols = panel->widget.cols-2;
637 attrset (MARKED_COLOR);
638 printw ("%*s", cols, " ");
639 widget_move (&panel->widget, llines (panel)+3, 1);
640 /* FIXME: use ngettext() here when gettext-0.10.35 becomes history */
641 g_snprintf (buffer, sizeof (buffer), (panel->marked == 1) ?
642 _("%s bytes in %d file") : _("%s bytes in %d files"),
643 size_trunc_sep (panel->total), panel->marked);
644 if (strlen (buffer) > cols-2){
645 buffer [cols] = 0;
646 p += 2;
647 } else
648 cols -= 2;
649 printw (p, cols, buffer);
650 return;
653 /* Status resolves links and show them */
654 set_colors (panel);
655 #ifndef OS2_NT
656 if (S_ISLNK (panel->dir.list [panel->selected].buf.st_mode)){
657 char *link, link_target [MC_MAXPATHLEN];
658 int len;
660 link = concat_dir_and_file (panel->cwd, panel->dir.list [panel->selected].fname);
661 len = mc_readlink (link, link_target, MC_MAXPATHLEN);
662 g_free (link);
663 if (len > 0){
664 link_target[len] = 0;
665 printw ("-> %-*s", panel->widget.cols - 5,
666 name_trunc (link_target, panel->widget.cols - 5));
667 } else
668 addstr (_("<readlink failed>"));
669 return;
671 #endif
672 /* Default behaviour */
673 repaint_file (panel, panel->selected, 0, STATUS, 1);
674 return;
677 static void
678 paint_dir (WPanel *panel)
680 int i;
681 int color; /* Color value of the line */
682 int items; /* Number of items */
684 items = llines (panel) * (panel->split ? 2 : 1);
686 for (i = 0; i < items; i++){
687 if (i+panel->top_file >= panel->count)
688 color = 0;
689 else {
690 color = 2 * (panel->dir.list [i+panel->top_file].f.marked);
691 color += (panel->selected==i+panel->top_file && panel->active);
693 repaint_file (panel, i+panel->top_file, 1, color, 0);
695 standend ();
696 panel->dirty = 0;
699 static void
700 mini_info_separator (WPanel *panel)
702 if (!show_mini_info)
703 return;
705 standend ();
706 widget_move (&panel->widget, llines (panel)+2, 1);
707 #ifdef HAVE_SLANG
708 attrset (NORMAL_COLOR);
709 hline (ACS_HLINE, panel->widget.cols-2);
710 #else
711 hline ((slow_terminal ? '-' : ACS_HLINE) | NORMAL_COLOR,
712 panel->widget.cols-2);
713 #endif /* !HAVE_SLANG */
716 static void
717 show_dir (WPanel *panel)
719 char tmp [200];
721 set_colors (panel);
722 draw_double_box (panel->widget.parent,
723 panel->widget.y, panel->widget.x,
724 panel->widget.lines, panel->widget.cols);
726 #ifdef HAVE_SLANG
727 if (show_mini_info) {
728 #ifdef linux_unicode
729 if (SLtt_Unicode) {
730 SLsmg_draw_unicode (panel->widget.y + llines (panel) + 2,
731 panel->widget.x, SLUNI_DSLTEE_CHAR);
732 SLsmg_draw_unicode (panel->widget.y + llines (panel) + 2,
733 panel->widget.x + panel->widget.cols - 1, SLUNI_DSRTEE_CHAR);
734 } else
735 #endif /* linux_unicode */
737 SLsmg_draw_object (panel->widget.y + llines (panel) + 2,
738 panel->widget.x, SLSMG_LTEE_CHAR);
739 SLsmg_draw_object (panel->widget.y + llines (panel) + 2,
740 panel->widget.x + panel->widget.cols - 1, SLSMG_RTEE_CHAR);
743 #endif /* HAVE_SLANG */
745 if (panel->active)
746 attrset (REVERSE_COLOR);
748 widget_move (&panel->widget, 0, 3);
750 trim (strip_home_and_password (panel->cwd), tmp, panel->widget.cols-7);
751 addstr (tmp);
752 widget_move (&panel->widget, 0, 1);
753 addstr ("<");
754 widget_move (&panel->widget, 0, panel->widget.cols-2);
755 addstr (">");
756 widget_move (&panel->widget, 0, panel->widget.cols-3);
757 addstr ("v");
759 if (panel->active)
760 standend ();
763 /* To be used only by long_frame and full_frame to adjust top_file */
764 static void
765 adjust_top_file (WPanel *panel)
767 int old_top = panel->top_file;
769 if (panel->selected - old_top > llines (panel))
770 panel->top_file = panel->selected;
771 if (old_top - panel->count > llines (panel))
772 panel->top_file = panel->count - llines (panel);
775 /* Repaints the information that changes after a command */
776 void
777 panel_update_contents (WPanel *panel)
779 show_dir (panel);
780 paint_dir (panel);
781 display_mini_info (panel);
784 void
785 paint_panel (WPanel *panel)
787 paint_frame (panel);
788 panel_update_contents (panel);
789 mini_info_separator (panel);
792 static void
793 do_select (WPanel *panel, int i)
795 if (i != panel->selected){
796 panel->selected = i;
797 panel->top_file = panel->selected - (panel->widget.lines-2)/2;
798 if (panel->top_file < 0)
799 panel->top_file = 0;
800 x_adjust_top_file (panel);
804 void
805 Xtry_to_select (WPanel *panel, char *name)
807 int i;
808 char *subdir;
810 if (!name) {
811 do_select(panel, 0);
812 return;
815 /* We only want the last component of the directory */
816 subdir = x_basename (name);
818 subdir = vfs_strip_suffix_from_filename (subdir);
820 /* Search that subdirectory, if found select it */
821 for (i = 0; i < panel->count; i++){
822 if (strcmp (subdir, panel->dir.list [i].fname) == 0) {
823 do_select (panel, i);
824 g_free (subdir);
825 return;
829 /* Try to select a file near the file that is missing */
830 if (panel->selected >= panel->count)
831 do_select (panel, panel->count-1);
832 g_free (subdir);
835 void
836 panel_update_cols (Widget *widget, int frame_size)
838 int cols, origin;
840 if (horizontal_split){
841 widget->cols = COLS;
842 return;
845 if (frame_size == frame_full){
846 cols = COLS;
847 origin = 0;
848 } else {
849 if (widget == get_panel_widget (0)){
850 cols = first_panel_size;
851 origin = 0;
852 } else {
853 cols = COLS-first_panel_size;
854 origin = first_panel_size;
858 widget->cols = cols;
859 widget->x = origin;
862 static char *
863 panel_save_name (WPanel *panel)
865 extern int saving_setup;
867 /* If the program is shuting down */
868 if ((midnight_shutdown && auto_save_setup) || saving_setup)
869 return g_strdup (panel->panel_name);
870 else
871 return g_strconcat ("Temporal:", panel->panel_name, NULL);
874 static void
875 panel_destroy (WPanel *p)
877 int i;
879 char *name = panel_save_name (p);
881 panel_save_setup (p, name);
882 x_panel_destroy (p);
883 panel_clean_dir (p);
885 /* save and clean history */
886 if (p->dir_history){
887 Hist *current, *old;
888 history_put (p->hist_name, p->dir_history);
889 current = p->dir_history;
890 while (current->next)
891 current = current->next;
892 while (current){
893 old = current;
894 current = current->prev;
895 g_free (old->text);
896 g_free (old);
899 g_free (p->hist_name);
901 delete_format (p->format);
902 delete_format (p->status_format);
904 g_free (p->user_format);
905 for (i = 0; i < LIST_TYPES; i++)
906 g_free (p->user_status_format [i]);
907 g_free (p->dir.list);
908 g_free (p->panel_name);
909 g_free (name);
912 static void
913 panel_format_modified (WPanel *panel)
915 panel->format_modified = 1;
916 x_reset_sort_labels (panel);
919 /* Panel creation */
920 /* The parameter specifies the name of the panel for setup retieving */
921 WPanel *
922 panel_new (const char *panel_name)
924 WPanel *panel;
925 char *section;
926 int i, err;
928 panel = g_new0 (WPanel, 1);
930 /* No know sizes of the panel at startup */
931 init_widget (&panel->widget, 0, 0, 0, 0, (callback_fn)
932 panel_callback, (destroy_fn) panel_destroy,
933 (mouse_h) panel_event, NULL);
935 /* We do not want the cursor */
936 widget_want_cursor (panel->widget, 0);
938 mc_get_current_wd (panel->cwd, sizeof (panel->cwd)-2);
939 strcpy (panel->lwd, ".");
941 panel->hist_name = g_strconcat ("Dir Hist ", panel_name, NULL);
942 panel->dir_history = history_get (panel->hist_name);
943 directory_history_add (panel, panel->cwd);
945 panel->dir.list = g_new (file_entry, MIN_FILES);
946 panel->dir.size = MIN_FILES;
947 panel->active = 0;
948 panel->filter = 0;
949 panel->split = 0;
950 panel->top_file = 0;
951 panel->selected = 0;
952 panel->marked = 0;
953 panel->total = 0;
954 panel->reverse = 0;
955 panel->dirty = 1;
956 panel->searching = 0;
957 panel->dirs_marked = 0;
958 panel->is_panelized = 0;
959 panel->format = 0;
960 panel->status_format = 0;
961 panel->format_modified = 1;
963 panel->panel_name = g_strdup (panel_name);
964 panel->user_format = g_strdup (DEFAULT_USER_FORMAT);
966 for(i = 0; i < LIST_TYPES; i++)
967 panel->user_status_format [i] = g_strdup (DEFAULT_USER_FORMAT);
969 panel->search_buffer [0] = 0;
970 panel->frame_size = frame_half;
971 section = g_strconcat ("Temporal:", panel->panel_name, NULL);
972 if (!profile_has_section (section, profile_name)){
973 g_free (section);
974 section = g_strdup (panel->panel_name);
976 panel_load_setup (panel, section);
977 g_free (section);
979 /* Load format strings */
980 err = set_panel_formats (panel);
981 if (err){
982 set_panel_formats (panel);
985 /* Load the default format */
986 panel->count = do_load_dir (&panel->dir, panel->sort_type,
987 panel->reverse, panel->case_sensitive, panel->filter);
988 return panel;
991 void
992 panel_reload (WPanel *panel)
994 struct stat current_stat;
996 if (fast_reload
997 && !stat (panel->cwd, &current_stat)
998 && current_stat.st_ctime == panel->dir_stat.st_ctime
999 && current_stat.st_mtime == panel->dir_stat.st_mtime)
1000 return;
1002 while (mc_chdir (panel->cwd) == -1){
1003 char *last_slash;
1005 if (panel->cwd [0] == PATH_SEP && panel->cwd [1] == 0){
1006 panel_clean_dir (panel);
1007 panel->count = set_zero_dir (&panel->dir);
1008 return;
1010 last_slash = strrchr (panel->cwd, PATH_SEP);
1011 if (!last_slash || last_slash == panel->cwd)
1012 strcpy (panel->cwd, PATH_SEP_STR);
1013 else
1014 *last_slash = 0;
1015 memset (&(panel->dir_stat), 0, sizeof (panel->dir_stat));
1016 show_dir (panel);
1019 panel->count = do_reload_dir (&panel->dir, panel->sort_type, panel->count,
1020 panel->reverse, panel->case_sensitive, panel->filter);
1022 panel->dirty = 1;
1023 if (panel->selected >= panel->count)
1024 do_select (panel, panel->count-1);
1026 recalculate_panel_summary (panel);
1029 static void
1030 paint_frame (WPanel *panel)
1032 int header_len;
1033 int spaces, extra;
1034 int side, width;
1036 char *txt, buffer[30]; /*Hope that this is enough ;-) */
1037 if (!panel->split)
1038 adjust_top_file (panel);
1040 widget_erase (&panel->widget);
1041 show_dir (panel);
1043 widget_move (&panel->widget, 1, 1);
1045 for (side = 0; side <= panel->split; side++){
1046 format_e *format;
1048 if (side){
1049 attrset (NORMAL_COLOR);
1050 one_vline ();
1051 width = panel->widget.cols - panel->widget.cols/2 - 1;
1052 } else if (panel->split)
1053 width = panel->widget.cols/2 - 3;
1054 else
1055 width = panel->widget.cols - 2;
1057 for (format = panel->format; format; format = format->next){
1058 if (format->string_fn){
1059 txt = format->title;
1061 header_len = strlen (txt);
1062 if (header_len > format->field_len){
1063 strcpy (buffer, txt);
1064 txt = buffer;
1065 txt [format->field_len] = 0;
1066 header_len = strlen (txt);
1069 attrset (MARKED_COLOR);
1070 spaces = (format->field_len - header_len) / 2;
1071 extra = (format->field_len - header_len) % 2;
1072 printw ("%*s%-s%*s", spaces, "",
1073 txt, spaces+extra, "");
1074 width -= 2 * spaces + extra + header_len;
1075 } else {
1076 attrset (NORMAL_COLOR);
1077 one_vline ();
1078 width --;
1079 continue;
1083 if (width > 0)
1084 printw ("%*s", width, "");
1088 static char *
1089 parse_panel_size (WPanel *panel, char *format, int isstatus)
1091 int frame = frame_half;
1092 format = skip_separators (format);
1094 if (!strncmp (format, "full", 4)){
1095 frame = frame_full;
1096 format += 4;
1097 } else if (!strncmp (format, "half", 4)){
1098 frame = frame_half;
1099 format += 4;
1102 if (!isstatus){
1103 panel->frame_size = frame;
1104 panel->split = 0;
1107 /* Now, the optional column specifier */
1108 format = skip_separators (format);
1110 if (*format == '1' || *format == '2'){
1111 if (!isstatus)
1112 panel->split = *format == '2';
1113 format++;
1116 if (!isstatus)
1117 panel_update_cols (&(panel->widget), panel->frame_size);
1119 return skip_separators (format);
1122 /* Format is:
1124 all := panel_format? format
1125 panel_format := [full|half] [1|2]
1126 format := one_format_e
1127 | format , one_format_e
1129 one_format_e := just format.id [opt_size]
1130 just := [<=>]
1131 opt_size := : size [opt_expand]
1132 size := [0-9]+
1133 opt_expand := +
1137 static format_e *
1138 parse_display_format (WPanel *panel, char *format, char **error, int isstatus, int *res_total_cols)
1140 format_e *darr, *old = 0, *home = 0; /* The formats we return */
1141 int total_cols = 0; /* Used columns by the format */
1142 int set_justify; /* flag: set justification mode? */
1143 int justify = 0; /* Which mode. */
1144 int items = 0; /* Number of items in the format */
1145 int i;
1147 static size_t i18n_timelength = 0; /* flag: check ?Time length at startup */
1149 *error = 0;
1151 if (i18n_timelength == 0) {
1152 i18n_timelength = i18n_checktimelength (); /* Musn't be 0 */
1154 for (i = 0; i < ELEMENTS(formats); i++)
1155 if (strcmp ("time", formats [i].id+1) == 0)
1156 formats [i].min_size = i18n_timelength;
1160 * This makes sure that the panel and mini status full/half mode
1161 * setting is equal
1163 format = parse_panel_size (panel, format, isstatus);
1165 while (*format){ /* format can be an empty string */
1166 int found = 0;
1168 darr = g_new (format_e, 1);
1170 /* I'm so ugly, don't look at me :-) */
1171 if (!home)
1172 home = old = darr;
1174 old->next = darr;
1175 darr->next = 0;
1176 old = darr;
1178 format = skip_separators (format);
1180 if (strchr ("<=>", *format)){
1181 set_justify = 1;
1182 switch (*format)
1184 case '<':
1185 justify = J_LEFT;
1186 break;
1187 case '=':
1188 justify = J_CENTER;
1189 break;
1190 case '>':
1191 default:
1192 justify = J_RIGHT;
1193 break;
1195 format = skip_separators (format+1);
1196 } else
1197 set_justify = 0;
1199 for (i = 0; i < ELEMENTS(formats); i++){
1200 int klen = strlen (formats [i].id);
1202 if (strncmp (format, formats [i].id, klen) != 0)
1203 continue;
1205 format += klen;
1207 if (formats [i].use_in_gui)
1208 items++;
1210 darr->use_in_gui = formats [i].use_in_gui;
1211 darr->requested_field_len = formats [i].min_size;
1212 darr->string_fn = formats [i].string_fn;
1213 if (formats [i].title [0])
1214 darr->title = _(formats [i].title);
1215 else
1216 darr->title = "";
1217 darr->id = formats [i].id;
1218 darr->expand = formats [i].expands;
1219 darr->just_mode = formats [i].default_just;
1221 if (set_justify) {
1222 if (IS_FIT(darr->just_mode))
1223 darr->just_mode = MAKE_FIT(justify);
1224 else
1225 darr->just_mode = justify;
1227 found = 1;
1229 format = skip_separators (format);
1231 /* If we have a size specifier */
1232 if (*format == ':'){
1233 int req_length;
1235 /* If the size was specified, we don't want
1236 * auto-expansion by default
1238 darr->expand = 0;
1239 format++;
1240 req_length = atoi (format);
1241 darr->requested_field_len = req_length;
1243 format = skip_numbers (format);
1245 /* Now, if they insist on expansion */
1246 if (*format == '+'){
1247 darr->expand = 1;
1248 format++;
1253 break;
1255 if (!found){
1256 char old_char;
1258 int pos = min (8, strlen (format));
1259 delete_format (home);
1260 old_char = format [pos];
1261 format [pos] = 0;
1262 *error = g_strconcat (_("Unknown tag on display format: "), format, NULL);
1263 format [pos] = old_char;
1264 return 0;
1266 total_cols += darr->requested_field_len;
1269 *res_total_cols = total_cols;
1270 if (home)
1271 home->items = items;
1272 return home;
1275 static format_e *
1276 use_display_format (WPanel *panel, char *format, char **error, int isstatus)
1278 #define MAX_EXPAND 4
1279 int expand_top = 0; /* Max used element in expand */
1280 int usable_columns; /* Usable columns in the panel */
1281 int total_cols;
1282 char *expand_list [MAX_EXPAND]; /* Expand at most 4 fields. */
1283 int i;
1284 format_e *darr, *home;
1286 if (!format)
1287 format = DEFAULT_USER_FORMAT;
1289 home = parse_display_format (panel, format, error, isstatus, &total_cols);
1291 if (*error)
1292 return 0;
1294 /* Status needn't to be split */
1295 usable_columns = ((panel->widget.cols-2)/((isstatus)
1297 : (panel->split+1))) - (!isstatus && panel->split);
1299 /* Clean expand list */
1300 for (i = 0; i < MAX_EXPAND; i++)
1301 expand_list [i] = '\0';
1304 /* Look for the expandable fields and set field_len based on the requested field len */
1305 for (darr = home; darr && expand_top < MAX_EXPAND; darr = darr->next){
1306 darr->field_len = darr->requested_field_len;
1307 if (darr->expand)
1308 expand_list [expand_top++] = darr->id;
1311 /* If we used more columns than the available columns, adjust that */
1312 if (total_cols > usable_columns){
1313 int pdif, dif = total_cols - usable_columns;
1315 while (dif){
1316 pdif = dif;
1317 for (darr = home; darr; darr = darr->next){
1318 if (dif && darr->field_len - 1){
1319 darr->field_len--;
1320 dif--;
1324 /* avoid endless loop if num fields > 40 */
1325 if (pdif == dif)
1326 break;
1328 total_cols = usable_columns; /* give up, the rest should be truncated */
1331 /* Expand the available space */
1332 if ((usable_columns > total_cols) && expand_top){
1333 int spaces = (usable_columns - total_cols) / expand_top;
1334 int extra = (usable_columns - total_cols) % expand_top;
1336 for (i = 0, darr = home; darr && (i < expand_top); darr = darr->next)
1337 if (darr->expand){
1338 darr->field_len += (spaces + ((i == 0) ? extra : 0));
1339 i++;
1342 return home;
1345 /* Switches the panel to the mode specified in the format */
1346 /* Seting up both format and status string. Return: 0 - on success; */
1347 /* 1 - format error; 2 - status error; 3 - errors in both formats. */
1349 set_panel_formats (WPanel *p)
1351 format_e *form;
1352 char *err;
1353 int retcode = 0;
1355 form = use_display_format (p, panel_format (p), &err, 0);
1357 if (err){
1358 g_free (err);
1359 retcode = 1;
1361 else {
1362 if (p->format)
1363 delete_format (p->format);
1365 p->format = form;
1368 if (show_mini_info){
1370 form = use_display_format (p, mini_status_format (p), &err, 1);
1372 if (err){
1373 g_free (err);
1374 retcode += 2;
1376 else {
1377 if (p->status_format)
1378 delete_format (p->status_format);
1380 p->status_format = form;
1384 panel_format_modified (p);
1385 panel_update_cols (&(p->widget), p->frame_size);
1387 if (retcode)
1388 message( 1, _(" Warning " ), _( "User suplied format looks invalid, reverting to default." ) );
1389 if (retcode & 0x01){
1390 g_free (p->user_format);
1391 p->user_format = g_strdup (DEFAULT_USER_FORMAT);
1393 if (retcode & 0x02){
1394 g_free (p->user_status_format [p->list_type]);
1395 p->user_status_format [p->list_type] = g_strdup (DEFAULT_USER_FORMAT);
1398 return retcode;
1401 /* Given the panel->view_type returns the format string to be parsed */
1402 static char *
1403 panel_format (WPanel *panel)
1405 switch (panel->list_type){
1407 case list_long:
1408 return "full perm,space,nlink,space,owner,space,group,space,size,space,mtime,space,name";
1410 case list_brief:
1411 return "half 2,type,name";
1413 case list_user:
1414 return panel->user_format;
1416 default:
1417 case list_full:
1418 return "half type,name,|,size,|,mtime";
1422 static char *
1423 mini_status_format (WPanel *panel)
1425 if (panel->user_mini_status)
1426 return panel->user_status_format [panel->list_type];
1428 switch (panel->list_type){
1430 case list_long:
1431 return "full perm,space,nlink,space,owner,space,group,space,size,space,mtime,space,name";
1433 case list_brief:
1434 return "half type,name,space,bsize,space,perm,space";
1436 case list_full:
1437 return "half type,name";
1439 default:
1440 case list_user:
1441 return panel->user_format;
1445 /* */
1446 /* Panel operation commands */
1447 /* */
1449 /* Returns the number of items in the given panel */
1450 static int
1451 ITEMS (WPanel *p)
1453 if (p->split)
1454 return llines (p) * 2;
1455 else
1456 return llines (p);
1459 /* This function sets redisplays the selection */
1460 void
1461 select_item (WPanel *panel)
1463 int repaint = 0;
1464 int items = ITEMS (panel);
1466 /* Although currently all over the code we set the selection and
1467 top file to decent values before calling select_item, I could
1468 forget it someday, so it's better to do the actual fitting here */
1470 if (panel->top_file < 0){
1471 repaint = 1;
1472 panel->top_file = 0;
1475 if (panel->selected < 0)
1476 panel->selected = 0;
1478 if (panel->selected > panel->count-1)
1479 panel->selected = panel->count - 1;
1481 if (panel->top_file > panel->count-1){
1482 repaint = 1;
1483 panel->top_file = panel->count-1;
1486 if ((panel->count - panel->top_file) < items){
1487 repaint = 1;
1488 panel->top_file = panel->count - items;
1489 if (panel->top_file < 0)
1490 panel->top_file = 0;
1493 if (panel->selected < panel->top_file){
1494 repaint = 1;
1495 panel->top_file = panel->selected;
1498 if ((panel->selected - panel->top_file) >= items){
1499 repaint = 1;
1500 panel->top_file = panel->selected - items + 1;
1503 if (repaint)
1504 paint_panel (panel);
1505 else
1506 repaint_file (panel, panel->selected, 1, 2*selection (panel)->f.marked+1, 0);
1508 display_mini_info (panel);
1510 execute_hooks (select_file_hook);
1513 /* Clears all files in the panel, used only when one file was marked */
1514 void
1515 unmark_files (WPanel *panel)
1517 int i;
1519 if (!panel->marked)
1520 return;
1521 for (i = 0; i < panel->count; i++)
1522 file_mark (panel, i, 0);
1524 panel->dirs_marked = 0;
1525 panel->marked = 0;
1526 panel->total = 0;
1529 static void
1530 unselect_item (WPanel *panel)
1532 repaint_file (panel, panel->selected, 1, 2*selection (panel)->f.marked, 0);
1535 static void
1536 do_move_down (WPanel *panel)
1538 if (panel->selected+1 == panel->count)
1539 return;
1541 unselect_item (panel);
1542 panel->selected++;
1544 if (panel->selected - panel->top_file == ITEMS (panel) &&
1545 panel_scroll_pages){
1546 /* Scroll window half screen */
1547 panel->top_file += ITEMS (panel)/2;
1548 if (panel->top_file > panel->count - ITEMS (panel))
1549 panel->top_file = panel->count - ITEMS (panel);
1550 paint_dir (panel);
1551 select_item (panel);
1553 select_item (panel);
1556 static void
1557 do_move_up (WPanel *panel)
1559 if (panel->selected == 0)
1560 return;
1562 unselect_item (panel);
1563 panel->selected--;
1564 if (panel->selected < panel->top_file && panel_scroll_pages){
1565 /* Scroll window half screen */
1566 panel->top_file -= ITEMS (panel)/2;
1567 if (panel->top_file < 0) panel->top_file = 0;
1568 paint_dir (panel);
1570 select_item (panel);
1573 static void
1574 move_rel (WPanel *panel, int rel)
1576 unselect_item (panel);
1578 if (rel < 0){
1579 if (panel->selected + rel < 0)
1580 panel->selected = 0;
1581 else
1582 panel->selected = panel->selected + rel;
1583 } else {
1584 if (panel->selected + rel >= panel->count)
1585 panel->selected = panel->count - 1;
1586 else
1587 panel->selected = panel->selected + rel;
1589 select_item (panel);
1592 /* */
1593 /* Panel key binded commands */
1594 /* */
1595 static void
1596 move_up (WPanel *panel)
1598 if (panel->list_type == list_icons){
1599 move_rel (panel, -ICONS_PER_ROW (panel));
1600 } else
1601 do_move_up (panel);
1604 static void
1605 move_down (WPanel *panel)
1607 if (panel->list_type == list_icons){
1608 move_rel (panel, ICONS_PER_ROW (panel));
1609 } else
1610 do_move_down (panel);
1613 /* Changes the selection by lines (may be negative) */
1614 static void
1615 move_selection (WPanel *panel, int lines)
1617 int new_pos;
1618 int adjust = 0;
1620 new_pos = panel->selected + lines;
1621 if (new_pos >= panel->count)
1622 new_pos = panel->count-1;
1624 if (new_pos < 0)
1625 new_pos = 0;
1627 unselect_item (panel);
1628 panel->selected = new_pos;
1630 if (panel->selected - panel->top_file >= ITEMS (panel)){
1631 panel->top_file += lines;
1632 adjust = 1;
1635 if (panel->selected - panel->top_file < 0){
1636 panel->top_file += lines;
1637 adjust = 1;
1640 if (adjust){
1641 if (panel->top_file > panel->selected)
1642 panel->top_file = panel->selected;
1643 if (panel->top_file < 0)
1644 panel->top_file = 0;
1645 paint_dir (panel);
1647 select_item (panel);
1650 static int
1651 move_left (WPanel *panel, int c_code)
1653 if (panel->list_type == list_icons){
1654 do_move_up (panel);
1655 return 1;
1656 } else {
1657 if (panel->split){
1658 move_selection (panel, -llines (panel));
1659 return 1;
1660 } else
1661 return maybe_cd (c_code, 0);
1665 static int
1666 move_right (WPanel *panel, int c_code)
1668 if (panel->list_type == list_icons){
1669 do_move_down (panel);
1670 return 1;
1671 } else {
1672 if (panel->split){
1673 move_selection (panel, llines (panel));
1674 return 1;
1675 } else
1676 return maybe_cd (c_code, 1);
1680 static void
1681 prev_page (WPanel *panel)
1683 int items;
1685 if (!panel->selected && !panel->top_file)
1686 return;
1687 unselect_item (panel);
1688 items = ITEMS (panel);
1689 if (panel->top_file < items)
1690 items = panel->top_file;
1691 if (!items)
1692 panel->selected = 0;
1693 else
1694 panel->selected -= items;
1695 panel->top_file -= items;
1697 /* This keeps the selection in a reasonable place */
1698 if (panel->selected < 0)
1699 panel->selected = 0;
1700 if (panel->top_file < 0)
1701 panel->top_file = 0;
1702 x_adjust_top_file (panel);
1703 select_item (panel);
1704 paint_dir (panel);
1707 static void
1708 prev_page_key (WPanel *panel)
1710 if (ctrl_pressed ()){
1711 do_cd ("..", cd_exact);
1712 } else {
1713 prev_page (panel);
1717 static void
1718 next_page (WPanel *panel)
1720 int items;
1722 if (panel->selected == panel->count - 1)
1723 return;
1724 unselect_item (panel);
1725 items = ITEMS (panel);
1726 if (panel->top_file > panel->count - 2 * items)
1727 items = panel->count - items - panel->top_file;
1728 if (panel->top_file + items < 0)
1729 items = - panel->top_file;
1730 if (!items)
1731 panel->selected = panel->count - 1;
1732 else
1733 panel->selected += items;
1734 panel->top_file += items;
1736 /* This keeps the selection in it's relative position */
1737 if (panel->selected >= panel->count)
1738 panel->selected = panel->count - 1;
1739 if (panel->top_file >= panel->count)
1740 panel->top_file = panel->count - 1;
1741 x_adjust_top_file (panel);
1742 select_item (panel);
1743 paint_dir (panel);
1746 static void next_page_key (WPanel *panel)
1748 if (ctrl_pressed() &&
1749 (S_ISDIR(selection (panel)->buf.st_mode) ||
1750 link_isdir (selection (panel)))) {
1751 do_cd (selection (panel)->fname, cd_exact);
1752 } else {
1753 next_page (panel);
1757 static void
1758 goto_top_file (WPanel *panel)
1760 unselect_item (panel);
1761 panel->selected = panel->top_file;
1762 select_item (panel);
1765 static void
1766 goto_middle_file (WPanel *panel)
1768 unselect_item (panel);
1769 panel->selected = panel->top_file + (ITEMS (panel)/2);
1770 if (panel->selected >= panel->count)
1771 panel->selected = panel->count - 1;
1772 select_item (panel);
1775 static void
1776 goto_bottom_file (WPanel *panel)
1778 unselect_item (panel);
1779 panel->selected = panel->top_file + ITEMS (panel)-1;
1780 if (panel->selected >= panel->count)
1781 panel->selected = panel->count - 1;
1782 select_item (panel);
1785 static void
1786 move_home (WPanel *panel)
1788 if (panel->selected == 0)
1789 return;
1790 unselect_item (panel);
1792 if (torben_fj_mode){
1793 int middle_pos = panel->top_file + (ITEMS (panel)/2);
1795 if (panel->selected > middle_pos){
1796 goto_middle_file (panel);
1797 return;
1799 if (panel->selected != panel->top_file){
1800 goto_top_file (panel);
1801 return;
1805 panel->top_file = 0;
1806 panel->selected = 0;
1808 paint_dir (panel);
1809 select_item (panel);
1812 static void
1813 move_end (WPanel *panel)
1815 if (panel->selected == panel->count-1)
1816 return;
1817 unselect_item (panel);
1818 if (torben_fj_mode){
1819 int middle_pos = panel->top_file + (ITEMS (panel)/2);
1821 if (panel->selected < middle_pos){
1822 goto_middle_file (panel);
1823 return;
1825 if (panel->selected != (panel->top_file + ITEMS(panel)-1)){
1826 goto_bottom_file (panel);
1827 return;
1831 panel->selected = panel->count-1;
1832 paint_dir (panel);
1833 select_item (panel);
1836 /* Recalculate the panels summary information, used e.g. when marked
1837 files might have been removed by an external command */
1838 void
1839 recalculate_panel_summary (WPanel *panel)
1841 int i;
1843 panel->marked = 0;
1844 panel->dirs_marked = 0;
1845 panel->total = 0;
1847 for (i = 0; i < panel->count; i++)
1848 if (panel->dir.list [i].f.marked){
1849 /* do_file_mark will return immediately if newmark == oldmark.
1850 So we have to first unmark it to get panel's summary information
1851 updated. (Norbert) */
1852 panel->dir.list [i].f.marked = 0;
1853 do_file_mark (panel, i, 1);
1857 /* This routine marks a file or a directory */
1858 void
1859 do_file_mark (WPanel *panel, int idx, int mark)
1861 if (panel->dir.list [idx].f.marked == mark)
1862 return;
1864 * Only '..' can't be marked, '.' isn't visible.
1866 if (strcmp (panel->dir.list [idx].fname, "..")){
1867 file_mark (panel, idx, mark);
1868 if (panel->dir.list [idx].f.marked){
1869 panel->marked++;
1870 if (S_ISDIR (panel->dir.list [idx].buf.st_mode)) {
1871 if (panel->dir.list [idx].f.dir_size_computed)
1872 panel->total += panel->dir.list [idx].buf.st_size;
1873 panel->dirs_marked++;
1874 } else
1875 panel->total += panel->dir.list [idx].buf.st_size;
1876 set_colors (panel);
1877 } else {
1878 if (S_ISDIR(panel->dir.list [idx].buf.st_mode)) {
1879 if (panel->dir.list [idx].f.dir_size_computed)
1880 panel->total -= panel->dir.list [idx].buf.st_size;
1881 panel->dirs_marked--;
1882 } else
1883 panel->total -= panel->dir.list [idx].buf.st_size;
1884 panel->marked--;
1889 static void
1890 do_mark_file (WPanel *panel, int do_move)
1892 int idx = panel->selected;
1894 do_file_mark (panel, idx, selection (panel)->f.marked ? 0 : 1);
1895 repaint_file (panel, idx, 1, 2*panel->dir.list [idx].f.marked+1, 0);
1897 if (mark_moves_down && do_move)
1898 move_down (panel);
1899 display_mini_info (panel);
1902 static void
1903 mark_file (WPanel *panel)
1905 do_mark_file (panel, 1);
1908 /* Incremental search of a file name in the panel */
1909 static void
1910 do_search (WPanel *panel, int c_code)
1912 int l, i;
1913 int wrapped = 0;
1914 int found;
1916 l = strlen (panel->search_buffer);
1917 if (l && (c_code == 8 || c_code == 0177 || c_code == KEY_BACKSPACE))
1918 panel->search_buffer [--l] = 0;
1919 else {
1920 if (c_code && l < sizeof (panel->search_buffer)){
1921 panel->search_buffer [l] = c_code;
1922 panel->search_buffer [l+1] = 0;
1923 l++;
1927 found = 0;
1928 for (i = panel->selected; !wrapped || i != panel->selected; i++){
1929 if (i >= panel->count){
1930 i = 0;
1931 if (wrapped)
1932 break;
1933 wrapped = 1;
1935 if (STRNCOMP (panel->dir.list [i].fname, panel->search_buffer, l) == 0){
1936 unselect_item (panel);
1937 panel->selected = i;
1938 select_item (panel);
1939 found = 1;
1940 break;
1943 if (!found)
1944 panel->search_buffer [--l] = 0;
1946 paint_panel (panel);
1949 static void
1950 start_search (WPanel *panel)
1952 if (panel->searching){
1953 if (panel->selected+1 == panel->count)
1954 panel->selected = 0;
1955 else
1956 do_move_down (panel);
1957 do_search (panel, 0);
1958 } else {
1959 panel->searching = 1;
1960 panel->search_buffer [0] = 0;
1961 display_mini_info (panel);
1962 mc_refresh ();
1966 static int
1967 do_enter_on_file_entry (file_entry *fe)
1969 char *full_name;
1971 if (S_ISDIR (fe->buf.st_mode) || link_isdir (fe)) {
1972 do_cd (fe->fname, cd_exact);
1973 return 1;
1974 } else {
1975 full_name = concat_dir_and_file (cpanel->cwd, fe->fname);
1976 if (is_exe (fe->buf.st_mode) && if_link_is_exe (full_name, fe)) {
1977 g_free (full_name);
1979 #ifdef USE_VFS
1980 if (vfs_current_is_local ())
1981 #endif
1983 char *tmp = name_quote (fe->fname, 0);
1984 char *cmd = g_strconcat (".", PATH_SEP_STR, tmp, NULL);
1985 g_free (tmp);
1986 if (!confirm_execute || (query_dialog (_(" The Midnight Commander "),
1987 _(" Do you really want to execute? "),
1988 0, 2, _("&Yes"), _("&No")) == 0))
1989 execute (cmd);
1990 g_free (cmd);
1992 #ifdef USE_VFS
1993 else {
1994 /* if (vfs_current_is_extfs ()) - I see no reason why
1995 filesystems other than extfs could not implement same
1996 call... -- pavel@ucw.cz*/
1997 char *tmp;
1999 tmp = concat_dir_and_file (vfs_get_current_dir(), fe->fname);
2000 if (!mc_setctl (tmp, MCCTL_EXTFS_RUN, NULL))
2001 message (1, _(" Warning "), _(" No action taken "));
2003 g_free (tmp);
2005 #endif /* USE_VFS */
2006 return 1;
2007 } else {
2008 char *p;
2010 g_free (full_name);
2012 p = regex_command (fe->fname, "Open", NULL, 0);
2013 if (p && (strcmp (p, "Success") == 0))
2014 return 1;
2015 else
2016 return 0;
2021 static int
2022 do_enter (WPanel *panel)
2024 return do_enter_on_file_entry (selection (panel));
2027 static void
2028 chdir_other_panel (WPanel *panel)
2030 char *new_dir;
2032 if (get_other_type () != view_listing)
2033 return;
2035 if (!S_ISDIR (panel->dir.list [panel->selected].buf.st_mode))
2036 new_dir = concat_dir_and_file (panel->cwd, "..");
2037 else
2038 new_dir = concat_dir_and_file (panel->cwd, panel->dir.list [panel->selected].fname);
2040 change_panel ();
2041 do_cd (new_dir, cd_exact);
2042 change_panel ();
2044 move_down (panel);
2046 g_free (new_dir);
2049 static void
2050 chdir_to_readlink (WPanel *panel)
2052 char *new_dir;
2054 if (get_other_type () != view_listing)
2055 return;
2057 if (S_ISLNK (panel->dir.list [panel->selected].buf.st_mode)) {
2058 char buffer [MC_MAXPATHLEN], *p;
2059 int i;
2060 struct stat mybuf;
2062 i = readlink (selection (panel)->fname, buffer, MC_MAXPATHLEN);
2063 if (i < 0)
2064 return;
2065 if (mc_stat (selection (panel)->fname, &mybuf) < 0)
2066 return;
2067 buffer [i] = 0;
2068 if (!S_ISDIR (mybuf.st_mode)) {
2069 p = strrchr (buffer, PATH_SEP);
2070 if (p && !p[1]) {
2071 *p = 0;
2072 p = strrchr (buffer, PATH_SEP);
2074 if (!p)
2075 return;
2076 p[1] = 0;
2078 if (*buffer == PATH_SEP)
2079 new_dir = g_strdup (buffer);
2080 else
2081 new_dir = concat_dir_and_file (panel->cwd, buffer);
2083 change_panel ();
2084 do_cd (new_dir, cd_exact);
2085 change_panel ();
2087 move_down (panel);
2089 g_free (new_dir);
2093 static const key_map panel_keymap [] = {
2094 { KEY_DOWN, move_down },
2095 { KEY_UP, move_up },
2097 /* The action button :-) */
2098 { '\n', (key_callback) do_enter },
2099 { KEY_ENTER, (key_callback) do_enter },
2101 { KEY_IC, mark_file },
2102 { KEY_HOME, move_home },
2103 { KEY_C1, move_end },
2104 { KEY_END, move_end },
2105 { KEY_A1, move_home },
2106 { KEY_NPAGE, next_page_key },
2107 { KEY_PPAGE, prev_page_key },
2109 /* To quickly move in the panel */
2110 { ALT('g'), goto_top_file },
2111 { ALT('r'), goto_middle_file }, /* M-r like emacs */
2112 { ALT('j'), goto_bottom_file },
2114 #ifdef OS2_NT
2115 { ALT(KEY_F(11)), drive_cmd_a },
2116 { ALT(KEY_F(12)), drive_cmd_b },
2117 { ALT('d'), drive_chg },
2118 #endif
2120 /* Emacs-like bindings */
2121 { XCTRL('v'), next_page }, /* C-v like emacs */
2122 { ALT('v'), prev_page }, /* M-v like emacs */
2123 { XCTRL('p'), move_up }, /* C-p like emacs */
2124 { XCTRL('n'), move_down }, /* C-n like emacs */
2125 { XCTRL('s'), start_search }, /* C-s like emacs */
2126 { ALT('s'), start_search }, /* M-s not like emacs */
2127 { XCTRL('t'), mark_file },
2128 { ALT('o'), chdir_other_panel },
2129 { ALT('l'), chdir_to_readlink },
2130 { ALT('H'), directory_history_list },
2131 { KEY_F(13), view_simple_cmd },
2132 { KEY_F(14), edit_cmd_new },
2133 { ALT('y'), directory_history_prev },
2134 { ALT('u'), directory_history_next },
2135 { ALT('+'), select_cmd_panel },
2136 { KEY_KP_ADD, select_cmd_panel },
2137 { ALT('\\'), unselect_cmd_panel },
2138 { ALT('-'), unselect_cmd_panel },
2139 { KEY_KP_SUBTRACT, unselect_cmd_panel },
2140 { ALT('*'), reverse_selection_cmd_panel },
2141 { KEY_KP_MULTIPLY, reverse_selection_cmd_panel },
2142 { 0, 0 }
2145 static inline int
2146 panel_key (WPanel *panel, int key)
2148 int i;
2150 for (i = 0; panel_keymap [i].key_code; i++){
2151 if (key == panel_keymap [i].key_code){
2152 if (panel_keymap [i].fn != start_search)
2153 panel->searching = 0;
2154 (*panel_keymap [i].fn)(panel);
2155 return 1;
2158 if (torben_fj_mode && key == ALT('h')) {
2159 goto_middle_file (panel);
2160 return 1;
2163 /* We do not want to take a key press if nothing can be done with it */
2164 /* The command line widget may do something more usefull */
2165 if (key == KEY_LEFT)
2166 return move_left (panel, key);
2168 if (key == KEY_RIGHT)
2169 return move_right (panel, key);
2171 if (is_abort_char (key)) {
2172 panel->searching = 0;
2173 display_mini_info (panel);
2174 return 1;
2177 /* Do not eat characters not meant for the panel below ' ' (e.g. C-l). */
2178 if ((key >= ' '&& key <= 255) || key == 8 || key == KEY_BACKSPACE) {
2179 if (panel->searching){
2180 do_search (panel, key);
2181 return 1;
2184 if (!command_prompt)
2186 start_search (panel);
2187 do_search (panel, key);
2188 return 1;
2192 return 0;
2195 void user_file_menu_cmd (void) {
2196 user_menu_cmd (NULL);
2199 static int
2200 panel_callback (Dlg_head *h, WPanel *panel, int msg, int par)
2202 switch (msg){
2203 case WIDGET_INIT:
2204 return 1;
2206 case WIDGET_DRAW:
2207 paint_panel (panel);
2208 break;
2210 case WIDGET_FOCUS:
2211 current_panel = panel;
2212 panel->active = 1;
2213 if (mc_chdir (panel->cwd) != 0){
2214 message (1, MSG_ERROR, _(" Cannot chdir to \"%s\" \n %s "),
2215 panel->cwd, unix_error_string (errno));
2216 } else
2217 subshell_chdir (panel->cwd);
2219 show_dir (panel);
2220 focus_select_item (panel);
2222 define_label (h, (Widget *)panel, 1, _("Help"), help_cmd);
2223 define_label (h, (Widget *)panel, 2, _("Menu"), user_file_menu_cmd);
2224 define_label (h, (Widget *)panel, 3, _("View"), view_panel_cmd);
2225 define_label (h, (Widget *)panel, 4, _("Edit"), edit_panel_cmd);
2226 define_label (h, (Widget *)panel, 5, _("Copy"), copy_cmd);
2227 define_label (h, (Widget *)panel, 6, _("RenMov"), ren_cmd);
2228 define_label (h, (Widget *)panel, 7, _("Mkdir"), mkdir_panel_cmd);
2229 define_label (h, (Widget *)panel, 8, _("Delete"), delete_cmd);
2230 redraw_labels (h, (Widget *)panel);
2232 /* Chain behaviour */
2233 default_proc (h, WIDGET_FOCUS, par);
2234 return 1;
2236 case WIDGET_UNFOCUS:
2237 /* Janne: look at this for the multiple panel options */
2238 if (panel->searching){
2239 panel->searching = 0;
2240 display_mini_info (panel);
2242 panel->active = 0;
2243 show_dir (panel);
2244 unselect_item (panel);
2245 return 1;
2247 case WIDGET_KEY:
2248 return panel_key (panel, par);
2249 break;
2251 return default_proc (h, msg, par);
2254 void
2255 file_mark (WPanel *panel, int index, int val)
2257 panel->dir.list [index].f.marked = val;
2258 x_panel_select_item (panel, index, val);
2261 /* */
2262 /* Panel mouse events support routines */
2263 /* */
2264 static int mouse_marking = 0;
2266 static void
2267 mouse_toggle_mark (WPanel *panel)
2269 do_mark_file (panel, 0);
2270 mouse_marking = selection (panel)->f.marked;
2273 static void
2274 mouse_set_mark (WPanel *panel)
2276 if (mouse_marking && !(selection (panel)->f.marked))
2277 do_mark_file (panel, 0);
2278 else if (!mouse_marking && (selection (panel)->f.marked))
2279 do_mark_file (panel, 0);
2282 static inline int
2283 mark_if_marking (WPanel *panel, Gpm_Event *event)
2285 if (event->buttons & GPM_B_RIGHT){
2286 if (event->type & GPM_DOWN)
2287 mouse_toggle_mark (panel);
2288 else
2289 mouse_set_mark (panel);
2290 return 1;
2292 return 0;
2295 static int
2296 panel_event (Gpm_Event *event, WPanel *panel)
2298 const int lines = llines (panel);
2300 int my_index;
2302 if (event->type & GPM_DOWN && event->x == 1 + 1 && event->y == 0 + 1) {
2303 directory_history_prev (panel);
2304 return MOU_NORMAL;
2307 if (event->type & GPM_DOWN && event->x == panel->widget.cols - 2 + 1 && event->y == 0 + 1) {
2308 directory_history_next (panel);
2309 return MOU_NORMAL;
2312 if (event->type & GPM_DOWN && event->x == panel->widget.cols - 3 + 1 && event->y == 0 + 1) {
2313 directory_history_list (panel);
2314 return MOU_NORMAL;
2317 event->y -= 2;
2318 if ((event->type & (GPM_DOWN|GPM_DRAG))){
2320 if (panel != (WPanel *) current_dlg->current->widget)
2321 change_panel ();
2323 if (event->y <= 0){
2324 mark_if_marking (panel, event);
2325 if (mouse_move_pages)
2326 prev_page (panel);
2327 else
2328 move_up (panel);
2329 return MOU_REPEAT;
2332 if (!((panel->top_file + event->y <= panel->count) &&
2333 event->y <= lines)){
2334 mark_if_marking (panel, event);
2335 if (mouse_move_pages)
2336 next_page (panel);
2337 else
2338 move_down (panel);
2339 return MOU_REPEAT;
2341 my_index = panel->top_file + event->y - 1;
2342 if (panel->split){
2343 if (event->x > ((panel->widget.cols-2)/2))
2344 my_index += llines (panel);
2347 if (my_index >= panel->count)
2348 my_index = panel->count - 1;
2350 if (my_index != panel->selected){
2351 unselect_item (panel);
2352 panel->selected = my_index;
2353 select_item (panel);
2356 /* This one is new */
2357 mark_if_marking (panel, event);
2359 } else if ((event->type & (GPM_UP|GPM_DOUBLE)) == (GPM_UP|GPM_DOUBLE)){
2360 if (event->y > 0 && event->y <= lines)
2361 do_enter (panel);
2363 return MOU_NORMAL;
2366 void
2367 panel_update_marks (WPanel *panel)
2371 void
2372 panel_re_sort (WPanel *panel)
2374 char *filename;
2375 int i;
2377 if (panel == NULL)
2378 return;
2380 filename = g_strdup (selection (panel)->fname);
2381 unselect_item (panel);
2382 do_sort (&panel->dir, panel->sort_type, panel->count-1, panel->reverse, panel->case_sensitive);
2383 panel->selected = -1;
2384 for (i = panel->count; i; i--){
2385 if (!strcmp (panel->dir.list [i-1].fname, filename)){
2386 panel->selected = i-1;
2387 break;
2390 g_free (filename);
2391 panel->top_file = panel->selected - ITEMS (panel)/2;
2392 if (panel->top_file < 0)
2393 panel->top_file = 0;
2394 select_item (panel);
2395 panel_update_contents (panel);
2398 void
2399 panel_set_sort_order (WPanel *panel, sortfn *sort_order)
2401 if (sort_order == 0)
2402 return;
2404 panel->sort_type = sort_order;
2406 /* The directory is already sorted, we have to load the unsorted stuff */
2407 if (sort_order == (sortfn *) unsorted){
2408 char *current_file;
2410 current_file = g_strdup (panel->dir.list [panel->selected].fname);
2411 panel_reload (panel);
2412 try_to_select (panel, current_file);
2413 g_free (current_file);
2415 panel_re_sort (panel);