root/src/filemanager/panel.c

/* [previous][next][first][last][top][bottom][index][help]  */

DEFINITIONS

This source file includes following definitions.
  1. panelized_descr_new
  2. panelized_descr_free
  3. set_colors
  4. format_item_free
  5. panel_lines
  6. add_permission_string
  7. string_file_name
  8. ilog10
  9. format_device_number
  10. string_file_size
  11. string_file_size_brief
  12. string_file_type
  13. string_file_mtime
  14. string_file_atime
  15. string_file_ctime
  16. string_file_permission
  17. string_file_perm_octal
  18. string_file_nlinks
  19. string_inode
  20. string_file_nuid
  21. string_file_ngid
  22. string_file_owner
  23. string_file_group
  24. string_marked
  25. string_space
  26. string_dot
  27. file_compute_color
  28. panel_items
  29. format_file
  30. repaint_file
  31. repaint_status
  32. display_mini_info
  33. paint_dir
  34. display_total_marked_size
  35. mini_info_separator
  36. show_free_space
  37. panel_correct_path_to_show
  38. panel_get_encoding_info_str
  39. show_dir
  40. adjust_top_file
  41. panel_save_name
  42. directory_history_add
  43. panel_load_history
  44. panel_save_history
  45. panel_destroy
  46. panel_paint_sort_info
  47. panel_get_title_without_hotkey
  48. panel_print_header
  49. parse_panel_size
  50. parse_display_format
  51. use_display_format
  52. panel_format
  53. mini_status_format
  54. cd_up_dir
  55. maybe_cd
  56. force_maybe_cd
  57. unselect_item
  58. panel_select_ext_cmd
  59. panel_set_current
  60. panel_current_at_half
  61. move_down
  62. move_up
  63. panel_move_current
  64. move_left
  65. move_right
  66. prev_page
  67. goto_parent_dir
  68. next_page
  69. goto_child_dir
  70. goto_top_file
  71. goto_middle_file
  72. goto_bottom_file
  73. move_home
  74. move_end
  75. do_mark_file
  76. mark_file
  77. mark_file_up
  78. mark_file_down
  79. mark_file_right
  80. mark_file_left
  81. panel_select_unselect_files_dialog
  82. panel_select_unselect_files
  83. panel_select_files
  84. panel_unselect_files
  85. panel_select_invert_files
  86. panel_do_set_filter
  87. do_search
  88. start_search
  89. stop_search
  90. do_enter_on_file_entry
  91. do_enter
  92. panel_cycle_listing_format
  93. chdir_other_panel
  94. panel_sync_other
  95. chdir_to_readlink
  96. panel_get_format_field_index_by_name
  97. panel_get_sortable_field_by_format
  98. panel_toggle_sort_order_prev
  99. panel_toggle_sort_order_next
  100. panel_select_sort_order
  101. panel_content_scroll_left
  102. panel_content_scroll_right
  103. panel_set_sort_type_by_id
  104. get_parent_dir_name
  105. panel_do_cd_int
  106. directory_history_next
  107. directory_history_prev
  108. directory_history_list
  109. panel_execute_cmd
  110. panel_key
  111. panel_callback
  112. mouse_toggle_mark
  113. mouse_set_mark
  114. mark_if_marking
  115. mouse_sort_col
  116. panel_mouse_is_on_item
  117. panel_mouse_callback
  118. reload_panelized
  119. update_one_panel_widget
  120. update_one_panel
  121. event_update_panels
  122. panel_save_current_file_to_clip_file
  123. panel_recursive_cd_to_parent
  124. panel_dir_list_callback
  125. panel_current_entry
  126. panel_set_current_by_name
  127. panel_clean_dir
  128. panel_set_cwd
  129. panel_set_lwd
  130. panel_sized_empty_new
  131. panel_sized_with_dir_new
  132. panel_reload
  133. set_panel_formats
  134. panel_set_filter
  135. select_item
  136. unmark_files
  137. recalculate_panel_summary
  138. do_file_mark
  139. panel_do_cd
  140. file_mark
  141. panel_find_marked_file
  142. panel_get_marked_file
  143. panel_re_sort
  144. panel_set_sort_order
  145. panel_change_encoding
  146. remove_encoding_from_path
  147. update_panels
  148. panel_get_num_of_sortable_fields
  149. panel_get_sortable_fields
  150. panel_get_field_by_id
  151. panel_get_field_by_title_hotkey
  152. panel_get_field_by_title
  153. panel_get_num_of_user_possible_fields
  154. panel_get_user_possible_fields
  155. panel_panelize_cd
  156. panel_panelize_change_root
  157. panel_panelize_absolutize_if_needed
  158. panel_panelize_save
  159. panel_init
  160. panel_deinit
  161. panel_cd

   1 /*
   2    Panel managing.
   3 
   4    Copyright (C) 1994-2025
   5    Free Software Foundation, Inc.
   6 
   7    Written by:
   8    Miguel de Icaza, 1995
   9    Timur Bakeyev, 1997, 1999
  10    Slava Zanko <slavazanko@gmail.com>, 2013
  11    Andrew Borodin <aborodin@vmail.ru>, 2013-2023
  12 
  13    This file is part of the Midnight Commander.
  14 
  15    The Midnight Commander is free software: you can redistribute it
  16    and/or modify it under the terms of the GNU General Public License as
  17    published by the Free Software Foundation, either version 3 of the License,
  18    or (at your option) any later version.
  19 
  20    The Midnight Commander is distributed in the hope that it will be useful,
  21    but WITHOUT ANY WARRANTY; without even the implied warranty of
  22    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  23    GNU General Public License for more details.
  24 
  25    You should have received a copy of the GNU General Public License
  26    along with this program.  If not, see <https://www.gnu.org/licenses/>.
  27  */
  28 
  29 /** \file panel.c
  30  *  \brief Source: panel managin module
  31  */
  32 
  33 #include <config.h>
  34 
  35 #include <stdio.h>
  36 #include <stdlib.h>
  37 #include <string.h>
  38 
  39 #include "lib/global.h"
  40 
  41 #include "lib/tty/tty.h"
  42 #include "lib/tty/key.h"  // XCTRL and ALT macros
  43 #include "lib/skin.h"
  44 #include "lib/strutil.h"
  45 #include "lib/mcconfig.h"
  46 #include "lib/vfs/vfs.h"
  47 #include "lib/unixcompat.h"
  48 #include "lib/search.h"
  49 #include "lib/timefmt.h"  // file_date()
  50 #include "lib/util.h"
  51 #include "lib/widget.h"
  52 #ifdef HAVE_CHARSET
  53 #    include "lib/charsets.h"  // get_codepage_id ()
  54 #endif
  55 #include "lib/event.h"
  56 
  57 #include "src/setup.h"  // For loading/saving panel options
  58 #include "src/execute.h"
  59 #ifdef HAVE_CHARSET
  60 #    include "src/selcodepage.h"  // select_charset (), SELECT_CHARSET_NO_TRANSLATE
  61 #endif
  62 #include "src/keymap.h"  // global_keymap_t
  63 #include "src/history.h"
  64 #ifdef ENABLE_SUBSHELL
  65 #    include "src/subshell/subshell.h"  // subshell_chdir()
  66 #endif
  67 
  68 #include "src/usermenu.h"
  69 
  70 #include "dir.h"
  71 #include "boxes.h"
  72 #include "tree.h"
  73 #include "ext.h"     // regexp_command
  74 #include "layout.h"  // Most layout variables are here
  75 #include "cmd.h"
  76 #include "command.h"  // cmdline
  77 #include "filemanager.h"
  78 #include "mountlist.h"  // my_statfs
  79 #include "cd.h"         // cd_error_message()
  80 
  81 #include "panel.h"
  82 
  83 /*** global variables ****************************************************************************/
  84 
  85 /* The hook list for the select file function */
  86 hook_t *select_file_hook = NULL;
  87 
  88 mc_fhl_t *mc_filehighlight = NULL;
  89 
  90 /*** file scope macro definitions ****************************************************************/
  91 
  92 typedef enum
  93 {
  94     FATTR_NORMAL = 0,
  95     FATTR_CURRENT,
  96     FATTR_MARKED,
  97     FATTR_MARKED_CURRENT,
  98     FATTR_STATUS
  99 } file_attr_t;
 100 
 101 #define DEFAULT_USER_FORMAT "half type name | size | perm"
 102 
 103 /* select/unselect dialog results */
 104 #define SELECT_RESET ((mc_search_t *) (-1))
 105 #define SELECT_ERROR ((mc_search_t *) (-2))
 106 
 107 /* mouse position relative to file list */
 108 #define MOUSE_UPPER_FILE_LIST (-1)
 109 #define MOUSE_BELOW_FILE_LIST (-2)
 110 #define MOUSE_AFTER_LAST_FILE (-3)
 111 
 112 /*** file scope type declarations ****************************************************************/
 113 
 114 typedef enum
 115 {
 116     MARK_DONT_MOVE = 0,
 117     MARK_DOWN = 1,
 118     MARK_FORCE_DOWN = 2,
 119     MARK_FORCE_UP = 3
 120 } mark_act_t;
 121 
 122 /*
 123  * This describes a format item.  The parse_display_format routine parses
 124  * the user specified format and creates a linked list of format_item_t structures.
 125  */
 126 typedef struct format_item_t
 127 {
 128     int requested_field_len;
 129     int field_len;
 130     align_crt_t just_mode;
 131     gboolean expand;
 132     const char *(*string_fn) (const file_entry_t *fe, int len);
 133     char *title;
 134     const char *id;
 135 } format_item_t;
 136 
 137 /* File name scroll states */
 138 typedef enum
 139 {
 140     FILENAME_NOSCROLL = 1,
 141     FILENAME_SCROLL_LEFT = 2,
 142     FILENAME_SCROLL_RIGHT = 4
 143 } filename_scroll_flag_t;
 144 
 145 /*** forward declarations (file scope functions) *************************************************/
 146 
 147 static const char *string_file_name (const file_entry_t *fe, int len);
 148 static const char *string_file_size (const file_entry_t *fe, int len);
 149 static const char *string_file_size_brief (const file_entry_t *fe, int len);
 150 static const char *string_file_type (const file_entry_t *fe, int len);
 151 static const char *string_file_mtime (const file_entry_t *fe, int len);
 152 static const char *string_file_atime (const file_entry_t *fe, int len);
 153 static const char *string_file_ctime (const file_entry_t *fe, int len);
 154 static const char *string_file_permission (const file_entry_t *fe, int len);
 155 static const char *string_file_perm_octal (const file_entry_t *fe, int len);
 156 static const char *string_file_nlinks (const file_entry_t *fe, int len);
 157 static const char *string_inode (const file_entry_t *fe, int len);
 158 static const char *string_file_nuid (const file_entry_t *fe, int len);
 159 static const char *string_file_ngid (const file_entry_t *fe, int len);
 160 static const char *string_file_owner (const file_entry_t *fe, int len);
 161 static const char *string_file_group (const file_entry_t *fe, int len);
 162 static const char *string_marked (const file_entry_t *fe, int len);
 163 static const char *string_space (const file_entry_t *fe, int len);
 164 static const char *string_dot (const file_entry_t *fe, int len);
 165 
 166 /*** file scope variables ************************************************************************/
 167 
 168 static panel_field_t panel_fields[] = {
 169     { "unsorted", 12, TRUE, J_LEFT_FIT,
 170       // TRANSLATORS: one single character to represent 'unsorted' sort mode
 171       // TRANSLATORS: no need to translate 'sort', it's just a context prefix
 172       N_ ("sort|u"), N_ ("&Unsorted"), TRUE, FALSE, string_file_name, (GCompareFunc) unsorted },
 173     { "name", 12, TRUE, J_LEFT_FIT,
 174       // TRANSLATORS: one single character to represent 'name' sort mode
 175       // TRANSLATORS: no need to translate 'sort', it's just a context prefix
 176       N_ ("sort|n"), N_ ("&Name"), TRUE, TRUE, string_file_name, (GCompareFunc) sort_name },
 177     { "version", 12, TRUE, J_LEFT_FIT,
 178       // TRANSLATORS: one single character to represent 'version' sort mode
 179       // TRANSLATORS: no need to translate 'sort', it's just a context prefix
 180       N_ ("sort|v"), N_ ("&Version"), TRUE, FALSE, string_file_name, (GCompareFunc) sort_vers },
 181     { "extension", 12, TRUE, J_LEFT_FIT,
 182       // TRANSLATORS: one single character to represent 'extension' sort mode
 183       // TRANSLATORS: no need to translate 'sort', it's just a context prefix
 184       N_ ("sort|e"), N_ ("E&xtension"), TRUE, FALSE,
 185       string_file_name,  // TODO: string_file_ext
 186       (GCompareFunc) sort_ext },
 187     { "size", 7, FALSE, J_RIGHT,
 188       // TRANSLATORS: one single character to represent 'size' sort mode
 189       // TRANSLATORS: no need to translate 'sort', it's just a context prefix
 190       N_ ("sort|s"), N_ ("&Size"), TRUE, TRUE, string_file_size, (GCompareFunc) sort_size },
 191     { "bsize", 7, FALSE, J_RIGHT, "", N_ ("Block Size"), FALSE, FALSE, string_file_size_brief,
 192       (GCompareFunc) sort_size },
 193     { "type", 1, FALSE, J_LEFT, "", "", FALSE, TRUE, string_file_type, NULL },
 194     { "mtime", 12, FALSE, J_RIGHT,
 195       // TRANSLATORS: one single character to represent 'Modify time' sort mode
 196       // TRANSLATORS: no need to translate 'sort', it's just a context prefix
 197       N_ ("sort|m"), N_ ("&Modify time"), TRUE, TRUE, string_file_mtime, (GCompareFunc) sort_time },
 198     { "atime", 12, FALSE, J_RIGHT,
 199       // TRANSLATORS: one single character to represent 'Access time' sort mode
 200       // TRANSLATORS: no need to translate 'sort', it's just a context prefix
 201       N_ ("sort|a"), N_ ("&Access time"), TRUE, TRUE, string_file_atime,
 202       (GCompareFunc) sort_atime },
 203     { "ctime", 12, FALSE, J_RIGHT,
 204       // TRANSLATORS: one single character to represent 'Change time' sort mode
 205       // TRANSLATORS: no need to translate 'sort', it's just a context prefix
 206       N_ ("sort|h"), N_ ("C&hange time"), TRUE, TRUE, string_file_ctime,
 207       (GCompareFunc) sort_ctime },
 208     { "perm", 10, FALSE, J_LEFT, "", N_ ("Permission"), FALSE, TRUE, string_file_permission, NULL },
 209     { "mode", 6, FALSE, J_RIGHT, "", N_ ("Perm"), FALSE, TRUE, string_file_perm_octal, NULL },
 210     { "nlink", 2, FALSE, J_RIGHT, "", N_ ("Nl"), FALSE, TRUE, string_file_nlinks, NULL },
 211     { "inode", 5, FALSE, J_RIGHT,
 212       // TRANSLATORS: one single character to represent 'inode' sort mode
 213       // TRANSLATORS: no need to translate 'sort', it's just a context prefix
 214       N_ ("sort|i"), N_ ("&Inode"), TRUE, TRUE, string_inode, (GCompareFunc) sort_inode },
 215     { "nuid", 5, FALSE, J_RIGHT, "", N_ ("UID"), FALSE, FALSE, string_file_nuid, NULL },
 216     { "ngid", 5, FALSE, J_RIGHT, "", N_ ("GID"), FALSE, FALSE, string_file_ngid, NULL },
 217     { "owner", 8, FALSE, J_LEFT_FIT, "", N_ ("Owner"), FALSE, TRUE, string_file_owner, NULL },
 218     { "group", 8, FALSE, J_LEFT_FIT, "", N_ ("Group"), FALSE, TRUE, string_file_group, NULL },
 219     { "mark", 1, FALSE, J_RIGHT, "", " ", FALSE, TRUE, string_marked, NULL },
 220     { "|", 1, FALSE, J_RIGHT, "", " ", FALSE, TRUE, NULL, NULL },
 221     { "space", 1, FALSE, J_RIGHT, "", " ", FALSE, TRUE, string_space, NULL },
 222     { "dot", 1, FALSE, J_RIGHT, "", " ", FALSE, FALSE, string_dot, NULL },
 223     { NULL, 0, FALSE, J_RIGHT, NULL, NULL, FALSE, FALSE, NULL, NULL },
 224 };
 225 
 226 static char *panel_sort_up_char = NULL;
 227 static char *panel_sort_down_char = NULL;
 228 static int panel_sort_up_indicator_width = 0;
 229 static int panel_sort_down_indicator_width = 0;
 230 
 231 static char *panel_hiddenfiles_show_char = NULL;
 232 static char *panel_hiddenfiles_hide_char = NULL;
 233 static char *panel_history_prev_item_char = NULL;
 234 static char *panel_history_next_item_char = NULL;
 235 static char *panel_history_show_list_char = NULL;
 236 static char *panel_filename_scroll_left_char = NULL;
 237 static char *panel_filename_scroll_right_char = NULL;
 238 
 239 /* Panel that selection started */
 240 static WPanel *mouse_mark_panel = NULL;
 241 
 242 static gboolean mouse_marking = FALSE;
 243 static int state_mark = 0;
 244 
 245 static GString *string_file_name_buffer;
 246 
 247 /* --------------------------------------------------------------------------------------------- */
 248 /*** file scope functions ************************************************************************/
 249 /* --------------------------------------------------------------------------------------------- */
 250 
 251 static panelized_descr_t *
 252 panelized_descr_new (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 253 {
 254     panelized_descr_t *p;
 255 
 256     p = g_new0 (panelized_descr_t, 1);
 257     p->list.len = -1;
 258 
 259     return p;
 260 }
 261 
 262 /* --------------------------------------------------------------------------------------------- */
 263 
 264 static void
 265 panelized_descr_free (panelized_descr_t *p)
     /* [previous][next][first][last][top][bottom][index][help]  */
 266 {
 267     if (p != NULL)
 268     {
 269         dir_list_free_list (&p->list);
 270         vfs_path_free (p->root_vpath, TRUE);
 271         g_free (p);
 272     }
 273 }
 274 
 275 /* --------------------------------------------------------------------------------------------- */
 276 
 277 static void
 278 set_colors (const WPanel *panel)
     /* [previous][next][first][last][top][bottom][index][help]  */
 279 {
 280     (void) panel;
 281 
 282     tty_set_normal_attrs ();
 283     tty_setcolor (NORMAL_COLOR);
 284 }
 285 
 286 /* --------------------------------------------------------------------------------------------- */
 287 /** Delete format_item_t object */
 288 
 289 static void
 290 format_item_free (format_item_t *format)
     /* [previous][next][first][last][top][bottom][index][help]  */
 291 {
 292     g_free (format->title);
 293     g_free (format);
 294 }
 295 
 296 /* --------------------------------------------------------------------------------------------- */
 297 /** Extract the number of available lines in a panel */
 298 
 299 static int
 300 panel_lines (const WPanel *p)
     /* [previous][next][first][last][top][bottom][index][help]  */
 301 {
 302     // 3 lines are: top frame, column header, button frame
 303     return (CONST_WIDGET (p)->rect.lines - 3 - (panels_options.show_mini_info ? 2 : 0));
 304 }
 305 
 306 /* --------------------------------------------------------------------------------------------- */
 307 /** This code relies on the default justification!!! */
 308 
 309 static void
 310 add_permission_string (const char *dest, int width, file_entry_t *fe, file_attr_t attr, int color,
     /* [previous][next][first][last][top][bottom][index][help]  */
 311                        gboolean is_octal)
 312 {
 313     int i, r, l;
 314 
 315     l = get_user_permissions (&fe->st);
 316 
 317     if (is_octal)
 318     {
 319         // Place of the access bit in octal mode
 320         l = width + l - 3;
 321         r = l + 1;
 322     }
 323     else
 324     {
 325         // The same to the triplet in string mode
 326         l = l * 3 + 1;
 327         r = l + 3;
 328     }
 329 
 330     for (i = 0; i < width; i++)
 331     {
 332         if (i >= l && i < r)
 333         {
 334             if (attr == FATTR_CURRENT || attr == FATTR_MARKED_CURRENT)
 335                 tty_setcolor (MARKED_SELECTED_COLOR);
 336             else
 337                 tty_setcolor (MARKED_COLOR);
 338         }
 339         else if (color >= 0)
 340             tty_setcolor (color);
 341         else
 342             tty_lowlevel_setcolor (-color);
 343 
 344         tty_print_char (dest[i]);
 345     }
 346 }
 347 
 348 /* --------------------------------------------------------------------------------------------- */
 349 /** String representations of various file attributes name */
 350 
 351 static const char *
 352 string_file_name (const file_entry_t *fe, int len)
     /* [previous][next][first][last][top][bottom][index][help]  */
 353 {
 354     (void) len;
 355 
 356     mc_g_string_copy (string_file_name_buffer, fe->fname);
 357 
 358     return string_file_name_buffer->str;
 359 }
 360 
 361 /* --------------------------------------------------------------------------------------------- */
 362 
 363 static unsigned int
 364 ilog10 (dev_t n)
     /* [previous][next][first][last][top][bottom][index][help]  */
 365 {
 366     unsigned int digits = 0;
 367 
 368     do
 369     {
 370         digits++;
 371         n /= 10;
 372     }
 373     while (n != 0);
 374 
 375     return digits;
 376 }
 377 
 378 /* --------------------------------------------------------------------------------------------- */
 379 
 380 static void
 381 format_device_number (char *buf, size_t bufsize, dev_t dev)
     /* [previous][next][first][last][top][bottom][index][help]  */
 382 {
 383     dev_t major_dev, minor_dev;
 384     unsigned int major_digits, minor_digits;
 385 
 386     major_dev = major (dev);
 387     major_digits = ilog10 (major_dev);
 388 
 389     minor_dev = minor (dev);
 390     minor_digits = ilog10 (minor_dev);
 391 
 392     g_assert (bufsize >= 1);
 393 
 394     if (major_digits + 1 + minor_digits + 1 <= bufsize)
 395         g_snprintf (buf, bufsize, "%lu,%lu", (unsigned long) major_dev, (unsigned long) minor_dev);
 396     else
 397         g_strlcpy (buf, _ ("[dev]"), bufsize);
 398 }
 399 
 400 /* --------------------------------------------------------------------------------------------- */
 401 /** size */
 402 
 403 static const char *
 404 string_file_size (const file_entry_t *fe, int len)
     /* [previous][next][first][last][top][bottom][index][help]  */
 405 {
 406     static char buffer[BUF_TINY];
 407 
 408     // Don't ever show size of ".." since we don't calculate it
 409     if (DIR_IS_DOTDOT (fe->fname->str))
 410         return _ ("UP--DIR");
 411 
 412 #ifdef HAVE_STRUCT_STAT_ST_RDEV
 413     if (S_ISBLK (fe->st.st_mode) || S_ISCHR (fe->st.st_mode))
 414         format_device_number (buffer, len + 1, fe->st.st_rdev);
 415     else
 416 #endif
 417         size_trunc_len (buffer, (unsigned int) len, fe->st.st_size, 0, panels_options.kilobyte_si);
 418 
 419     return buffer;
 420 }
 421 
 422 /* --------------------------------------------------------------------------------------------- */
 423 /** bsize */
 424 
 425 static const char *
 426 string_file_size_brief (const file_entry_t *fe, int len)
     /* [previous][next][first][last][top][bottom][index][help]  */
 427 {
 428     if (S_ISLNK (fe->st.st_mode) && !link_isdir (fe))
 429         return _ ("SYMLINK");
 430 
 431     if ((S_ISDIR (fe->st.st_mode) || link_isdir (fe)) && !DIR_IS_DOTDOT (fe->fname->str))
 432         return _ ("SUB-DIR");
 433 
 434     return string_file_size (fe, len);
 435 }
 436 
 437 /* --------------------------------------------------------------------------------------------- */
 438 /** This functions return a string representation of a file entry type */
 439 
 440 static const char *
 441 string_file_type (const file_entry_t *fe, int len)
     /* [previous][next][first][last][top][bottom][index][help]  */
 442 {
 443     static char buffer[2];
 444 
 445     (void) len;
 446 
 447     if (S_ISDIR (fe->st.st_mode))
 448         buffer[0] = PATH_SEP;
 449     else if (S_ISLNK (fe->st.st_mode))
 450     {
 451         if (link_isdir (fe))
 452             buffer[0] = '~';
 453         else if (fe->f.stale_link != 0)
 454             buffer[0] = '!';
 455         else
 456             buffer[0] = '@';
 457     }
 458     else if (S_ISCHR (fe->st.st_mode))
 459         buffer[0] = '-';
 460     else if (S_ISSOCK (fe->st.st_mode))
 461         buffer[0] = '=';
 462     else if (S_ISDOOR (fe->st.st_mode))
 463         buffer[0] = '>';
 464     else if (S_ISBLK (fe->st.st_mode))
 465         buffer[0] = '+';
 466     else if (S_ISFIFO (fe->st.st_mode))
 467         buffer[0] = '|';
 468     else if (S_ISNAM (fe->st.st_mode))
 469         buffer[0] = '#';
 470     else if (!S_ISREG (fe->st.st_mode))
 471         buffer[0] = '?';  // non-regular of unknown kind
 472     else if (is_exe (fe->st.st_mode))
 473         buffer[0] = '*';
 474     else
 475         buffer[0] = ' ';
 476     buffer[1] = '\0';
 477     return buffer;
 478 }
 479 
 480 /* --------------------------------------------------------------------------------------------- */
 481 /** mtime */
 482 
 483 static const char *
 484 string_file_mtime (const file_entry_t *fe, int len)
     /* [previous][next][first][last][top][bottom][index][help]  */
 485 {
 486     (void) len;
 487 
 488     return file_date (fe->st.st_mtime);
 489 }
 490 
 491 /* --------------------------------------------------------------------------------------------- */
 492 /** atime */
 493 
 494 static const char *
 495 string_file_atime (const file_entry_t *fe, int len)
     /* [previous][next][first][last][top][bottom][index][help]  */
 496 {
 497     (void) len;
 498 
 499     return file_date (fe->st.st_atime);
 500 }
 501 
 502 /* --------------------------------------------------------------------------------------------- */
 503 /** ctime */
 504 
 505 static const char *
 506 string_file_ctime (const file_entry_t *fe, int len)
     /* [previous][next][first][last][top][bottom][index][help]  */
 507 {
 508     (void) len;
 509 
 510     return file_date (fe->st.st_ctime);
 511 }
 512 
 513 /* --------------------------------------------------------------------------------------------- */
 514 /** perm */
 515 
 516 static const char *
 517 string_file_permission (const file_entry_t *fe, int len)
     /* [previous][next][first][last][top][bottom][index][help]  */
 518 {
 519     (void) len;
 520 
 521     return string_perm (fe->st.st_mode);
 522 }
 523 
 524 /* --------------------------------------------------------------------------------------------- */
 525 /** mode */
 526 
 527 static const char *
 528 string_file_perm_octal (const file_entry_t *fe, int len)
     /* [previous][next][first][last][top][bottom][index][help]  */
 529 {
 530     static char buffer[10];
 531 
 532     (void) len;
 533 
 534     g_snprintf (buffer, sizeof (buffer), "0%06lo", (unsigned long) fe->st.st_mode);
 535     return buffer;
 536 }
 537 
 538 /* --------------------------------------------------------------------------------------------- */
 539 /** nlink */
 540 
 541 static const char *
 542 string_file_nlinks (const file_entry_t *fe, int len)
     /* [previous][next][first][last][top][bottom][index][help]  */
 543 {
 544     static char buffer[BUF_TINY];
 545 
 546     (void) len;
 547 
 548     g_snprintf (buffer, sizeof (buffer), "%16d", (int) fe->st.st_nlink);
 549     return buffer;
 550 }
 551 
 552 /* --------------------------------------------------------------------------------------------- */
 553 /** inode */
 554 
 555 static const char *
 556 string_inode (const file_entry_t *fe, int len)
     /* [previous][next][first][last][top][bottom][index][help]  */
 557 {
 558     static char buffer[10];
 559 
 560     (void) len;
 561 
 562     g_snprintf (buffer, sizeof (buffer), "%lu", (unsigned long) fe->st.st_ino);
 563     return buffer;
 564 }
 565 
 566 /* --------------------------------------------------------------------------------------------- */
 567 /** nuid */
 568 
 569 static const char *
 570 string_file_nuid (const file_entry_t *fe, int len)
     /* [previous][next][first][last][top][bottom][index][help]  */
 571 {
 572     static char buffer[10];
 573 
 574     (void) len;
 575 
 576     g_snprintf (buffer, sizeof (buffer), "%lu", (unsigned long) fe->st.st_uid);
 577     return buffer;
 578 }
 579 
 580 /* --------------------------------------------------------------------------------------------- */
 581 /** ngid */
 582 
 583 static const char *
 584 string_file_ngid (const file_entry_t *fe, int len)
     /* [previous][next][first][last][top][bottom][index][help]  */
 585 {
 586     static char buffer[10];
 587 
 588     (void) len;
 589 
 590     g_snprintf (buffer, sizeof (buffer), "%lu", (unsigned long) fe->st.st_gid);
 591     return buffer;
 592 }
 593 
 594 /* --------------------------------------------------------------------------------------------- */
 595 /** owner */
 596 
 597 static const char *
 598 string_file_owner (const file_entry_t *fe, int len)
     /* [previous][next][first][last][top][bottom][index][help]  */
 599 {
 600     (void) len;
 601 
 602     return get_owner (fe->st.st_uid);
 603 }
 604 
 605 /* --------------------------------------------------------------------------------------------- */
 606 /** group */
 607 
 608 static const char *
 609 string_file_group (const file_entry_t *fe, int len)
     /* [previous][next][first][last][top][bottom][index][help]  */
 610 {
 611     (void) len;
 612 
 613     return get_group (fe->st.st_gid);
 614 }
 615 
 616 /* --------------------------------------------------------------------------------------------- */
 617 /** mark */
 618 
 619 static const char *
 620 string_marked (const file_entry_t *fe, int len)
     /* [previous][next][first][last][top][bottom][index][help]  */
 621 {
 622     (void) len;
 623 
 624     return fe->f.marked != 0 ? "*" : " ";
 625 }
 626 
 627 /* --------------------------------------------------------------------------------------------- */
 628 /** space */
 629 
 630 static const char *
 631 string_space (const file_entry_t *fe, int len)
     /* [previous][next][first][last][top][bottom][index][help]  */
 632 {
 633     (void) fe;
 634     (void) len;
 635 
 636     return " ";
 637 }
 638 
 639 /* --------------------------------------------------------------------------------------------- */
 640 /** dot */
 641 
 642 static const char *
 643 string_dot (const file_entry_t *fe, int len)
     /* [previous][next][first][last][top][bottom][index][help]  */
 644 {
 645     (void) fe;
 646     (void) len;
 647 
 648     return ".";
 649 }
 650 
 651 /* --------------------------------------------------------------------------------------------- */
 652 
 653 static int
 654 file_compute_color (const file_attr_t attr, file_entry_t *fe)
     /* [previous][next][first][last][top][bottom][index][help]  */
 655 {
 656     switch (attr)
 657     {
 658     case FATTR_CURRENT:
 659         return (SELECTED_COLOR);
 660     case FATTR_MARKED:
 661         return (MARKED_COLOR);
 662     case FATTR_MARKED_CURRENT:
 663         return (MARKED_SELECTED_COLOR);
 664     case FATTR_STATUS:
 665         return (NORMAL_COLOR);
 666     case FATTR_NORMAL:
 667     default:
 668         if (!panels_options.filetype_mode)
 669             return (NORMAL_COLOR);
 670     }
 671 
 672     return mc_fhl_get_color (mc_filehighlight, fe);
 673 }
 674 
 675 /* --------------------------------------------------------------------------------------------- */
 676 /** Returns the number of items in the given panel */
 677 
 678 static int
 679 panel_items (const WPanel *p)
     /* [previous][next][first][last][top][bottom][index][help]  */
 680 {
 681     return panel_lines (p) * p->list_cols;
 682 }
 683 
 684 /* --------------------------------------------------------------------------------------------- */
 685 /** Formats the file number file_index of panel in the buffer dest */
 686 
 687 static filename_scroll_flag_t
 688 format_file (WPanel *panel, int file_index, int width, file_attr_t attr, gboolean isstatus,
     /* [previous][next][first][last][top][bottom][index][help]  */
 689              int *field_length)
 690 {
 691     int color = NORMAL_COLOR;
 692     int length = 0;
 693     GSList *format, *home;
 694     file_entry_t *fe = NULL;
 695     filename_scroll_flag_t res = FILENAME_NOSCROLL;
 696 
 697     *field_length = 0;
 698 
 699     if (panel->dir.len != 0 && file_index < panel->dir.len)
 700     {
 701         fe = &panel->dir.list[file_index];
 702         color = file_compute_color (attr, fe);
 703     }
 704 
 705     home = isstatus ? panel->status_format : panel->format;
 706 
 707     for (format = home; format != NULL && length != width; format = g_slist_next (format))
 708     {
 709         format_item_t *fi = (format_item_t *) format->data;
 710 
 711         if (fi->string_fn != NULL)
 712         {
 713             const char *txt = " ";
 714             int len, perm = 0;
 715             const char *prepared_text;
 716             int name_offset = 0;
 717 
 718             if (fe != NULL)
 719                 txt = fi->string_fn (fe, fi->field_len);
 720 
 721             len = fi->field_len;
 722             if (len + length > width)
 723                 len = width - length;
 724             if (len <= 0)
 725                 break;
 726 
 727             if (!isstatus && panel->content_shift > -1 && strcmp (fi->id, "name") == 0)
 728             {
 729                 int str_len;
 730                 int i;
 731 
 732                 *field_length = len + 1;
 733 
 734                 str_len = str_length (txt);
 735                 i = MAX (0, str_len - len);
 736                 panel->max_shift = MAX (panel->max_shift, i);
 737                 i = MIN (panel->content_shift, i);
 738 
 739                 if (i > -1)
 740                 {
 741                     name_offset = str_offset_to_pos (txt, i);
 742                     if (str_len > len)
 743                     {
 744                         res = FILENAME_SCROLL_LEFT;
 745                         if (str_length (txt + name_offset) > len)
 746                             res |= FILENAME_SCROLL_RIGHT;
 747                     }
 748                 }
 749             }
 750 
 751             if (panels_options.permission_mode)
 752             {
 753                 if (strcmp (fi->id, "perm") == 0)
 754                     perm = 1;
 755                 else if (strcmp (fi->id, "mode") == 0)
 756                     perm = 2;
 757             }
 758 
 759             if (color >= 0)
 760                 tty_setcolor (color);
 761             else
 762                 tty_lowlevel_setcolor (-color);
 763 
 764             if (!isstatus && panel->content_shift > -1)
 765                 prepared_text = str_fit_to_term (txt + name_offset, len, HIDE_FIT (fi->just_mode));
 766             else
 767                 prepared_text = str_fit_to_term (txt, len, fi->just_mode);
 768 
 769             if (perm != 0 && fe != NULL)
 770                 add_permission_string (prepared_text, fi->field_len, fe, attr, color, perm != 1);
 771             else
 772                 tty_print_string (prepared_text);
 773 
 774             length += len;
 775         }
 776         else
 777         {
 778             if (attr == FATTR_CURRENT || attr == FATTR_MARKED_CURRENT)
 779                 tty_setcolor (SELECTED_COLOR);
 780             else
 781                 tty_setcolor (NORMAL_COLOR);
 782             tty_print_one_vline (TRUE);
 783             length++;
 784         }
 785     }
 786 
 787     if (length < width)
 788     {
 789         int y, x;
 790 
 791         tty_getyx (&y, &x);
 792         tty_draw_hline (y, x, ' ', width - length);
 793     }
 794 
 795     return res;
 796 }
 797 
 798 /* --------------------------------------------------------------------------------------------- */
 799 
 800 static void
 801 repaint_file (WPanel *panel, int file_index, file_attr_t attr)
     /* [previous][next][first][last][top][bottom][index][help]  */
 802 {
 803     Widget *w = WIDGET (panel);
 804 
 805     int nth_column = 0;
 806     int width;
 807     int offset = 0;
 808     filename_scroll_flag_t ret_frm;
 809     int ypos = 0;
 810     gboolean panel_is_split;
 811     int fln = 0;
 812 
 813     panel_is_split = panel->list_cols > 1;
 814     width = w->rect.cols - 2;
 815 
 816     if (panel_is_split)
 817     {
 818         nth_column = (file_index - panel->top) / panel_lines (panel);
 819         width /= panel->list_cols;
 820 
 821         offset = width * nth_column;
 822 
 823         if (nth_column + 1 >= panel->list_cols)
 824             width = w->rect.cols - offset - 2;
 825     }
 826 
 827     // Nothing to paint
 828     if (width <= 0)
 829         return;
 830 
 831     ypos = file_index - panel->top;
 832 
 833     if (panel_is_split)
 834         ypos %= panel_lines (panel);
 835 
 836     ypos += 2;  // top frame and header
 837     widget_gotoyx (w, ypos, offset + 1);
 838 
 839     ret_frm = format_file (panel, file_index, width, attr, FALSE, &fln);
 840 
 841     if (panel_is_split && nth_column + 1 < panel->list_cols)
 842     {
 843         tty_setcolor (NORMAL_COLOR);
 844         tty_print_one_vline (TRUE);
 845     }
 846 
 847     if (ret_frm != FILENAME_NOSCROLL)
 848     {
 849         if (!panel_is_split && fln > 0)
 850         {
 851             if (panel->list_format != list_long)
 852                 width = fln;
 853             else
 854             {
 855                 offset = width - fln + 1;
 856                 width = fln - 1;
 857             }
 858         }
 859 
 860         widget_gotoyx (w, ypos, offset);
 861         tty_setcolor (NORMAL_COLOR);
 862         tty_print_string (panel_filename_scroll_left_char);
 863 
 864         if ((ret_frm & FILENAME_SCROLL_RIGHT) != 0)
 865         {
 866             offset += width;
 867             if (nth_column + 1 >= panel->list_cols)
 868                 offset++;
 869 
 870             widget_gotoyx (w, ypos, offset);
 871             tty_setcolor (NORMAL_COLOR);
 872             tty_print_string (panel_filename_scroll_right_char);
 873         }
 874     }
 875 }
 876 
 877 /* --------------------------------------------------------------------------------------------- */
 878 
 879 static void
 880 repaint_status (WPanel *panel)
     /* [previous][next][first][last][top][bottom][index][help]  */
 881 {
 882     int width;
 883 
 884     width = WIDGET (panel)->rect.cols - 2;
 885     if (width > 0)
 886     {
 887         int fln = 0;
 888 
 889         (void) format_file (panel, panel->current, width, FATTR_STATUS, TRUE, &fln);
 890     }
 891 }
 892 
 893 /* --------------------------------------------------------------------------------------------- */
 894 
 895 static void
 896 display_mini_info (WPanel *panel)
     /* [previous][next][first][last][top][bottom][index][help]  */
 897 {
 898     Widget *w = WIDGET (panel);
 899     const file_entry_t *fe;
 900 
 901     if (!panels_options.show_mini_info)
 902         return;
 903 
 904     widget_gotoyx (w, panel_lines (panel) + 3, 1);
 905 
 906     if (panel->quick_search.active)
 907     {
 908         tty_setcolor (INPUT_COLOR);
 909         tty_print_char ('/');
 910         tty_print_string (
 911             str_fit_to_term (panel->quick_search.buffer->str, w->rect.cols - 3, J_LEFT));
 912         return;
 913     }
 914 
 915     // Status resolves links and show them
 916     set_colors (panel);
 917 
 918     fe = panel_current_entry (panel);
 919 
 920     if (fe == NULL)
 921         // NULL is in case of filter that doesn't match anything
 922         repaint_status (panel);
 923     else if (S_ISLNK (fe->st.st_mode))
 924     {
 925         char link_target[MC_MAXPATHLEN];
 926         vfs_path_t *lc_link_vpath;
 927         int len;
 928 
 929         lc_link_vpath = vfs_path_append_new (panel->cwd_vpath, fe->fname->str, (char *) NULL);
 930         len = mc_readlink (lc_link_vpath, link_target, MC_MAXPATHLEN - 1);
 931         vfs_path_free (lc_link_vpath, TRUE);
 932         if (len > 0)
 933         {
 934             link_target[len] = 0;
 935             tty_print_string ("-> ");
 936             tty_print_string (str_fit_to_term (link_target, w->rect.cols - 5, J_LEFT_FIT));
 937         }
 938         else
 939             tty_print_string (str_fit_to_term (_ ("<readlink failed>"), w->rect.cols - 2, J_LEFT));
 940     }
 941     else if (DIR_IS_DOTDOT (fe->fname->str))
 942     {
 943         /* FIXME:
 944          * while loading directory (dir_list_load() and dir_list_reload()),
 945          * the actual stat info about ".." directory isn't got;
 946          * so just don't display incorrect info about ".." directory */
 947         tty_print_string (str_fit_to_term (_ ("UP--DIR"), w->rect.cols - 2, J_LEFT));
 948     }
 949     else
 950         // Default behavior
 951         repaint_status (panel);
 952 }
 953 
 954 /* --------------------------------------------------------------------------------------------- */
 955 
 956 static void
 957 paint_dir (WPanel *panel)
     /* [previous][next][first][last][top][bottom][index][help]  */
 958 {
 959     int i;
 960     int items;  // Number of items
 961 
 962     items = panel_items (panel);
 963     // reset max len of filename because we have the new max length for the new file list
 964     panel->max_shift = -1;
 965 
 966     for (i = 0; i < items; i++)
 967     {
 968         file_attr_t attr = FATTR_NORMAL;  // Color value of the line
 969         int n;
 970         gboolean marked;
 971 
 972         n = i + panel->top;
 973         marked = (panel->dir.list[n].f.marked != 0);
 974 
 975         if (n < panel->dir.len)
 976         {
 977             if (panel->current == n && panel->active)
 978                 attr = marked ? FATTR_MARKED_CURRENT : FATTR_CURRENT;
 979             else if (marked)
 980                 attr = FATTR_MARKED;
 981         }
 982 
 983         repaint_file (panel, n, attr);
 984     }
 985 
 986     tty_set_normal_attrs ();
 987 }
 988 
 989 /* --------------------------------------------------------------------------------------------- */
 990 
 991 static void
 992 display_total_marked_size (const WPanel *panel, int y, int x, gboolean size_only)
     /* [previous][next][first][last][top][bottom][index][help]  */
 993 {
 994     const Widget *w = CONST_WIDGET (panel);
 995 
 996     char buffer[BUF_SMALL], b_bytes[BUF_SMALL];
 997     const char *buf;
 998     int cols;
 999 
1000     if (panel->marked <= 0)
1001         return;
1002 
1003     buf = size_only ? b_bytes : buffer;
1004     cols = w->rect.cols - 2;
1005 
1006     g_strlcpy (b_bytes, size_trunc_sep (panel->total, panels_options.kilobyte_si),
1007                sizeof (b_bytes));
1008 
1009     if (!size_only)
1010         g_snprintf (buffer, sizeof (buffer),
1011                     ngettext ("%s in %d file", "%s in %d files", panel->marked), b_bytes,
1012                     panel->marked);
1013 
1014     // don't forget spaces around buffer content
1015     buf = str_trunc (buf, cols - 4);
1016 
1017     if (x < 0)
1018         // center in panel
1019         x = (w->rect.cols - str_term_width1 (buf)) / 2 - 1;
1020 
1021     /*
1022      * y == panel_lines (panel) + 2  for mini_info_separator
1023      * y == w->lines - 1             for panel bottom frame
1024      */
1025     widget_gotoyx (w, y, x);
1026     tty_setcolor (MARKED_COLOR);
1027     tty_printf (" %s ", buf);
1028 }
1029 
1030 /* --------------------------------------------------------------------------------------------- */
1031 
1032 static void
1033 mini_info_separator (const WPanel *panel)
     /* [previous][next][first][last][top][bottom][index][help]  */
1034 {
1035     if (panels_options.show_mini_info)
1036     {
1037         const Widget *w = CONST_WIDGET (panel);
1038         int y;
1039 
1040         y = panel_lines (panel) + 2;
1041 
1042         tty_setcolor (NORMAL_COLOR);
1043         tty_draw_hline (w->rect.y + y, w->rect.x + 1, ACS_HLINE, w->rect.cols - 2);
1044         /* Status displays total marked size.
1045          * Centered in panel, full format. */
1046         display_total_marked_size (panel, y, -1, FALSE);
1047     }
1048 }
1049 
1050 /* --------------------------------------------------------------------------------------------- */
1051 
1052 static void
1053 show_free_space (const WPanel *panel)
     /* [previous][next][first][last][top][bottom][index][help]  */
1054 {
1055     // Used to figure out how many free space we have
1056     static struct my_statfs myfs_stats;
1057     // Old current working directory for displaying free space
1058     static char *old_cwd = NULL;
1059 
1060     // Don't try to stat non-local fs
1061     if (!vfs_file_is_local (panel->cwd_vpath) || !free_space)
1062         return;
1063 
1064     if (old_cwd == NULL || strcmp (old_cwd, vfs_path_as_str (panel->cwd_vpath)) != 0)
1065     {
1066         char rpath[PATH_MAX];
1067 
1068         init_my_statfs ();
1069         g_free (old_cwd);
1070         old_cwd = g_strdup (vfs_path_as_str (panel->cwd_vpath));
1071 
1072         if (mc_realpath (old_cwd, rpath) == NULL)
1073             return;
1074 
1075         my_statfs (&myfs_stats, rpath);
1076     }
1077 
1078     if (myfs_stats.avail != 0 || myfs_stats.total != 0)
1079     {
1080         const Widget *w = CONST_WIDGET (panel);
1081         char buffer1[6], buffer2[6], tmp[BUF_SMALL];
1082 
1083         size_trunc_len (buffer1, sizeof (buffer1) - 1, myfs_stats.avail, 1,
1084                         panels_options.kilobyte_si);
1085         size_trunc_len (buffer2, sizeof (buffer2) - 1, myfs_stats.total, 1,
1086                         panels_options.kilobyte_si);
1087         g_snprintf (tmp, sizeof (tmp), " %s / %s (%d%%) ", buffer1, buffer2,
1088                     myfs_stats.total == 0
1089                         ? 0
1090                         : (int) (100 * (long double) myfs_stats.avail / myfs_stats.total));
1091         widget_gotoyx (w, w->rect.lines - 1, w->rect.cols - 2 - (int) strlen (tmp));
1092         tty_setcolor (NORMAL_COLOR);
1093         tty_print_string (tmp);
1094     }
1095 }
1096 
1097 /* --------------------------------------------------------------------------------------------- */
1098 /**
1099  * Prepare path string for showing in panel's header.
1100  * Passwords will removed, also home dir will replaced by ~
1101  *
1102  * @param panel WPanel object
1103  *
1104  * @return newly allocated string.
1105  */
1106 
1107 static char *
1108 panel_correct_path_to_show (const WPanel *panel)
     /* [previous][next][first][last][top][bottom][index][help]  */
1109 {
1110     vfs_path_t *last_vpath;
1111     const vfs_path_element_t *path_element;
1112     char *return_path;
1113     int elements_count;
1114 
1115     elements_count = vfs_path_elements_count (panel->cwd_vpath);
1116 
1117     // get last path element
1118     path_element = vfs_path_element_clone (vfs_path_get_by_index (panel->cwd_vpath, -1));
1119 
1120     if (elements_count > 1
1121         && (strcmp (path_element->class->name, "cpiofs") == 0
1122             || strcmp (path_element->class->name, "extfs") == 0
1123             || strcmp (path_element->class->name, "tarfs") == 0))
1124     {
1125         const char *archive_name;
1126         const vfs_path_element_t *prev_path_element;
1127 
1128         // get previous path element for catching archive name
1129         prev_path_element = vfs_path_get_by_index (panel->cwd_vpath, -2);
1130         archive_name = strrchr (prev_path_element->path, PATH_SEP);
1131         if (archive_name != NULL)
1132             last_vpath = vfs_path_from_str_flags (archive_name + 1, VPF_NO_CANON);
1133         else
1134         {
1135             last_vpath = vfs_path_from_str_flags (prev_path_element->path, VPF_NO_CANON);
1136             last_vpath->relative = TRUE;
1137         }
1138     }
1139     else
1140         last_vpath = vfs_path_new (TRUE);
1141 
1142     vfs_path_add_element (last_vpath, path_element);
1143     return_path = vfs_path_to_str_flags (last_vpath, 0,
1144                                          VPF_STRIP_HOME | VPF_STRIP_PASSWORD | VPF_HIDE_CHARSET);
1145     vfs_path_free (last_vpath, TRUE);
1146 
1147     return return_path;
1148 }
1149 
1150 /* --------------------------------------------------------------------------------------------- */
1151 /**
1152  * Get Current path element encoding
1153  *
1154  * @param panel WPanel object
1155  *
1156  * @return newly allocated string or NULL if path charset is same as system charset
1157  */
1158 
1159 #ifdef HAVE_CHARSET
1160 static char *
1161 panel_get_encoding_info_str (const WPanel *panel)
     /* [previous][next][first][last][top][bottom][index][help]  */
1162 {
1163     char *ret_str = NULL;
1164     const vfs_path_element_t *path_element;
1165 
1166     path_element = vfs_path_get_by_index (panel->cwd_vpath, -1);
1167     if (path_element->encoding != NULL)
1168         ret_str = g_strdup_printf ("[%s]", path_element->encoding);
1169 
1170     return ret_str;
1171 }
1172 #endif
1173 
1174 /* --------------------------------------------------------------------------------------------- */
1175 
1176 static void
1177 show_dir (const WPanel *panel)
     /* [previous][next][first][last][top][bottom][index][help]  */
1178 {
1179     const Widget *w = CONST_WIDGET (panel);
1180     gchar *tmp;
1181 
1182     set_colors (panel);
1183     tty_draw_box (w->rect.y, w->rect.x, w->rect.lines, w->rect.cols, FALSE);
1184 
1185     if (panels_options.show_mini_info)
1186     {
1187         int y;
1188 
1189         y = panel_lines (panel) + 2;
1190 
1191         widget_gotoyx (w, y, 0);
1192         tty_print_alt_char (ACS_LTEE, FALSE);
1193         widget_gotoyx (w, y, w->rect.cols - 1);
1194         tty_print_alt_char (ACS_RTEE, FALSE);
1195     }
1196 
1197     widget_gotoyx (w, 0, 1);
1198     tty_print_string (panel_history_prev_item_char);
1199 
1200     tmp = panels_options.show_dot_files ? panel_hiddenfiles_show_char : panel_hiddenfiles_hide_char;
1201     tmp = g_strdup_printf ("%s[%s]%s", tmp, panel_history_show_list_char,
1202                            panel_history_next_item_char);
1203 
1204     widget_gotoyx (w, 0, w->rect.cols - 6);
1205     tty_print_string (tmp);
1206 
1207     g_free (tmp);
1208 
1209     widget_gotoyx (w, 0, 3);
1210 
1211     if (panel->is_panelized)
1212         tty_printf (" %s ", _ ("Panelize"));
1213 #ifdef HAVE_CHARSET
1214     else
1215     {
1216         tmp = panel_get_encoding_info_str (panel);
1217         if (tmp != NULL)
1218         {
1219             tty_printf ("%s", tmp);
1220             widget_gotoyx (w, 0, 3 + strlen (tmp));
1221             g_free (tmp);
1222         }
1223     }
1224 #endif
1225 
1226     if (panel->active)
1227         tty_setcolor (REVERSE_COLOR);
1228 
1229     tmp = panel_correct_path_to_show (panel);
1230     tty_printf (" %s ", str_term_trim (tmp, MIN (MAX (w->rect.cols - 12, 0), w->rect.cols)));
1231     g_free (tmp);
1232 
1233     if (!panels_options.show_mini_info)
1234     {
1235         if (panel->marked == 0)
1236         {
1237             const file_entry_t *fe;
1238 
1239             fe = panel_current_entry (panel);
1240 
1241             // Show size of current file in the bottom of panel
1242             if (fe != NULL && S_ISREG (fe->st.st_mode))
1243             {
1244                 char buffer[BUF_SMALL];
1245 
1246                 g_snprintf (buffer, sizeof (buffer), " %s ",
1247                             size_trunc_sep (fe->st.st_size, panels_options.kilobyte_si));
1248                 tty_setcolor (NORMAL_COLOR);
1249                 widget_gotoyx (w, w->rect.lines - 1, 4);
1250                 tty_print_string (buffer);
1251             }
1252         }
1253         else
1254         {
1255             /* Show total size of marked files
1256              * In the bottom of panel, display size only. */
1257             display_total_marked_size (panel, w->rect.lines - 1, 2, TRUE);
1258         }
1259     }
1260 
1261     show_free_space (panel);
1262 
1263     if (panel->active)
1264         tty_set_normal_attrs ();
1265 }
1266 
1267 /* --------------------------------------------------------------------------------------------- */
1268 
1269 static void
1270 adjust_top_file (WPanel *panel)
     /* [previous][next][first][last][top][bottom][index][help]  */
1271 {
1272     int items;
1273 
1274     items = panel_items (panel);
1275 
1276     if (panel->dir.len <= items || panel->current < 0)
1277     {
1278         // If all files fit, show them all.
1279         // panel->current < 0 is in case of filter that doesn't match anything, keep it.
1280         panel->top = 0;
1281     }
1282     else
1283     {
1284         int i;
1285 
1286         // Update panel->current to avoid out of range in panel->dir.list[panel->current].
1287         panel->current = CLAMP (panel->current, 0, panel->dir.len - 1);
1288 
1289         /* top_file has to be in the range [current-items+1, current] so that
1290            the current file is visible.
1291            top_file should be in the range [0, count-items] so that there's
1292            no empty space wasted.
1293            Within these ranges, adjust it by as little as possible. */
1294 
1295         if (panel->top < 0)
1296             panel->top = 0;
1297 
1298         i = panel->current - items + 1;
1299         if (panel->top < i)
1300             panel->top = i;
1301 
1302         i = panel->dir.len - items;
1303         if (panel->top > i)
1304             panel->top = i;
1305 
1306         if (panel->top > panel->current)
1307             panel->top = panel->current;
1308     }
1309 }
1310 
1311 /* --------------------------------------------------------------------------------------------- */
1312 /** add "#enc:encoding" to end of path */
1313 /* if path ends width a previous #enc:, only encoding is changed no additional
1314  * #enc: is appended
1315  * return new string
1316  */
1317 
1318 static char *
1319 panel_save_name (WPanel *panel)
     /* [previous][next][first][last][top][bottom][index][help]  */
1320 {
1321     // If the program is shutting down
1322     if ((mc_global.midnight_shutdown && auto_save_setup) || saving_setup)
1323         return g_strdup (panel->name);
1324 
1325     return g_strconcat ("Temporal:", panel->name, (char *) NULL);
1326 }
1327 
1328 /* --------------------------------------------------------------------------------------------- */
1329 
1330 static void
1331 directory_history_add (WPanel *panel, const vfs_path_t *vpath)
     /* [previous][next][first][last][top][bottom][index][help]  */
1332 {
1333     char *tmp;
1334 
1335     tmp = vfs_path_to_str_flags (vpath, 0, VPF_STRIP_PASSWORD);
1336     panel->dir_history.list = list_append_unique (panel->dir_history.list, tmp);
1337     panel->dir_history.current = panel->dir_history.list;
1338 }
1339 
1340 /* --------------------------------------------------------------------------------------------- */
1341 
1342 /* "history_load" event handler */
1343 static gboolean
1344 panel_load_history (const gchar *event_group_name, const gchar *event_name, gpointer init_data,
     /* [previous][next][first][last][top][bottom][index][help]  */
1345                     gpointer data)
1346 {
1347     WPanel *p = PANEL (init_data);
1348     ev_history_load_save_t *ev = (ev_history_load_save_t *) data;
1349 
1350     (void) event_group_name;
1351     (void) event_name;
1352 
1353     if (ev->receiver == NULL || ev->receiver == WIDGET (p))
1354     {
1355         if (ev->cfg != NULL)
1356             p->dir_history.list = mc_config_history_load (ev->cfg, p->dir_history.name);
1357         else
1358             p->dir_history.list = mc_config_history_get (p->dir_history.name);
1359 
1360         directory_history_add (p, p->cwd_vpath);
1361     }
1362 
1363     return TRUE;
1364 }
1365 
1366 /* --------------------------------------------------------------------------------------------- */
1367 
1368 /* "history_save" event handler */
1369 static gboolean
1370 panel_save_history (const gchar *event_group_name, const gchar *event_name, gpointer init_data,
     /* [previous][next][first][last][top][bottom][index][help]  */
1371                     gpointer data)
1372 {
1373     WPanel *p = PANEL (init_data);
1374 
1375     (void) event_group_name;
1376     (void) event_name;
1377 
1378     if (p->dir_history.list != NULL)
1379     {
1380         ev_history_load_save_t *ev = (ev_history_load_save_t *) data;
1381 
1382         mc_config_history_save (ev->cfg, p->dir_history.name, p->dir_history.list);
1383     }
1384 
1385     return TRUE;
1386 }
1387 
1388 /* --------------------------------------------------------------------------------------------- */
1389 
1390 static void
1391 panel_destroy (WPanel *p)
     /* [previous][next][first][last][top][bottom][index][help]  */
1392 {
1393     size_t i;
1394 
1395     if (panels_options.auto_save_setup)
1396     {
1397         char *name;
1398 
1399         name = panel_save_name (p);
1400         panel_save_setup (p, name);
1401         g_free (name);
1402     }
1403 
1404     panel_clean_dir (p);
1405 
1406     // clean history
1407     if (p->dir_history.list != NULL)
1408     {
1409         // directory history is already saved before this moment
1410         p->dir_history.list = g_list_first (p->dir_history.list);
1411         g_list_free_full (p->dir_history.list, g_free);
1412     }
1413     g_free (p->dir_history.name);
1414 
1415     file_filter_clear (&p->filter);
1416 
1417     g_slist_free_full (p->format, (GDestroyNotify) format_item_free);
1418     g_slist_free_full (p->status_format, (GDestroyNotify) format_item_free);
1419 
1420     g_free (p->user_format);
1421     for (i = 0; i < LIST_FORMATS; i++)
1422         g_free (p->user_status_format[i]);
1423 
1424     g_free (p->name);
1425 
1426     panelized_descr_free (p->panelized_descr);
1427 
1428     g_string_free (p->quick_search.buffer, TRUE);
1429     g_string_free (p->quick_search.prev_buffer, TRUE);
1430 
1431     vfs_path_free (p->lwd_vpath, TRUE);
1432     vfs_path_free (p->cwd_vpath, TRUE);
1433 }
1434 
1435 /* --------------------------------------------------------------------------------------------- */
1436 
1437 static void
1438 panel_paint_sort_info (const WPanel *panel)
     /* [previous][next][first][last][top][bottom][index][help]  */
1439 {
1440     if (*panel->sort_field->hotkey != '\0')
1441     {
1442         char str[BUF_TINY];
1443 
1444         if (panel->sort_info.reverse)
1445         {
1446             g_snprintf (str, sizeof (str), "%s%s", panel_sort_up_char,
1447                         Q_ (panel->sort_field->hotkey));
1448             panel_sort_up_indicator_width = str_term_width1 (str);
1449         }
1450         else
1451         {
1452             g_snprintf (str, sizeof (str), "%s%s", panel_sort_down_char,
1453                         Q_ (panel->sort_field->hotkey));
1454             panel_sort_down_indicator_width = str_term_width1 (str);
1455         }
1456         widget_gotoyx (panel, 1, 1);
1457         tty_print_string (str);
1458     }
1459 }
1460 
1461 /* --------------------------------------------------------------------------------------------- */
1462 
1463 static const char *
1464 panel_get_title_without_hotkey (const char *title)
     /* [previous][next][first][last][top][bottom][index][help]  */
1465 {
1466     static char translated_title[BUF_TINY];
1467 
1468     if (title == NULL || title[0] == '\0')
1469         translated_title[0] = '\0';
1470     else
1471     {
1472         char *hkey;
1473 
1474         g_snprintf (translated_title, sizeof (translated_title), "%s", _ (title));
1475 
1476         hkey = strchr (translated_title, '&');
1477         if (hkey != NULL && hkey[1] != '\0')
1478             memmove (hkey, hkey + 1, strlen (hkey));
1479     }
1480 
1481     return translated_title;
1482 }
1483 
1484 /* --------------------------------------------------------------------------------------------- */
1485 
1486 static void
1487 panel_print_header (const WPanel *panel)
     /* [previous][next][first][last][top][bottom][index][help]  */
1488 {
1489     const Widget *w = CONST_WIDGET (panel);
1490 
1491     int y, x;
1492     int i;
1493     GString *format_txt;
1494 
1495     widget_gotoyx (w, 1, 1);
1496     tty_getyx (&y, &x);
1497     tty_setcolor (NORMAL_COLOR);
1498     tty_draw_hline (y, x, ' ', w->rect.cols - 2);
1499 
1500     format_txt = g_string_new ("");
1501 
1502     for (i = 0; i < panel->list_cols; i++)
1503     {
1504         GSList *format;
1505 
1506         for (format = panel->format; format != NULL; format = g_slist_next (format))
1507         {
1508             format_item_t *fi = (format_item_t *) format->data;
1509 
1510             if (fi->string_fn != NULL)
1511             {
1512                 g_string_set_size (format_txt, 0);
1513 
1514                 if (panel->list_format == list_long && strcmp (fi->id, panel->sort_field->id) == 0)
1515                     g_string_append (format_txt,
1516                                      panel->sort_info.reverse ? panel_sort_up_char
1517                                                               : panel_sort_down_char);
1518 
1519                 g_string_append (format_txt, fi->title);
1520 
1521                 if (panel->filter.handler != NULL && strcmp (fi->id, "name") == 0)
1522                 {
1523                     g_string_append (format_txt, " [");
1524                     g_string_append (format_txt, panel->filter.value);
1525                     g_string_append (format_txt, "]");
1526                 }
1527 
1528                 tty_setcolor (HEADER_COLOR);
1529                 tty_print_string (str_fit_to_term (format_txt->str, fi->field_len, J_CENTER_LEFT));
1530             }
1531             else
1532             {
1533                 tty_setcolor (NORMAL_COLOR);
1534                 tty_print_one_vline (TRUE);
1535             }
1536         }
1537 
1538         if (i < panel->list_cols - 1)
1539         {
1540             tty_setcolor (NORMAL_COLOR);
1541             tty_print_one_vline (TRUE);
1542         }
1543     }
1544 
1545     g_string_free (format_txt, TRUE);
1546 
1547     if (panel->list_format != list_long)
1548         panel_paint_sort_info (panel);
1549 }
1550 
1551 /* --------------------------------------------------------------------------------------------- */
1552 
1553 static const char *
1554 parse_panel_size (WPanel *panel, const char *format, gboolean isstatus)
     /* [previous][next][first][last][top][bottom][index][help]  */
1555 {
1556     panel_display_t frame = frame_half;
1557 
1558     format = skip_separators (format);
1559 
1560     if (strncmp (format, "full", 4) == 0)
1561     {
1562         frame = frame_full;
1563         format += 4;
1564     }
1565     else if (strncmp (format, "half", 4) == 0)
1566     {
1567         frame = frame_half;
1568         format += 4;
1569     }
1570 
1571     if (!isstatus)
1572     {
1573         panel->frame_size = frame;
1574         panel->list_cols = 1;
1575     }
1576 
1577     // Now, the optional column specifier
1578     format = skip_separators (format);
1579 
1580     if (g_ascii_isdigit (*format))
1581     {
1582         if (!isstatus)
1583         {
1584             panel->list_cols = g_ascii_digit_value (*format);
1585             if (panel->list_cols < 1)
1586                 panel->list_cols = 1;
1587         }
1588 
1589         format++;
1590     }
1591 
1592     if (!isstatus)
1593         panel_update_cols (WIDGET (panel), panel->frame_size);
1594 
1595     return skip_separators (format);
1596 }
1597 
1598 /* --------------------------------------------------------------------------------------------- */
1599 
1600 /* Format is:
1601 
1602    all              := panel_format? format
1603    panel_format     := [full|half] [1-9]
1604    format           := one_format_item_t
1605                      | format , one_format_item_t
1606 
1607    one_format_item_t := just format.id [opt_size]
1608    just              := [<=>]
1609    opt_size          := : size [opt_expand]
1610    size              := [0-9]+
1611    opt_expand        := +
1612 
1613  */
1614 
1615 static GSList *
1616 parse_display_format (WPanel *panel, const char *format, char **error, gboolean isstatus,
     /* [previous][next][first][last][top][bottom][index][help]  */
1617                       int *res_total_cols)
1618 {
1619     GSList *home = NULL;  // The formats we return
1620     int total_cols = 0;   // Used columns by the format
1621     size_t i;
1622 
1623     static size_t i18n_timelength = 0;  // flag: check ?Time length at startup
1624 
1625     *error = NULL;
1626 
1627     if (i18n_timelength == 0)
1628     {
1629         i18n_timelength = i18n_checktimelength ();  // Mustn't be 0
1630 
1631         for (i = 0; panel_fields[i].id != NULL; i++)
1632             if (strcmp ("time", panel_fields[i].id + 1) == 0)
1633                 panel_fields[i].min_size = i18n_timelength;
1634     }
1635 
1636     /*
1637      * This makes sure that the panel and mini status full/half mode
1638      * setting is equal
1639      */
1640     format = parse_panel_size (panel, format, isstatus);
1641 
1642     while (*format != '\0')
1643     {  // format can be an empty string
1644         format_item_t *darr;
1645         align_crt_t justify;          // Which mode.
1646         gboolean set_justify = TRUE;  // flag: set justification mode?
1647         gboolean found = FALSE;
1648         size_t klen = 0;
1649 
1650         darr = g_new0 (format_item_t, 1);
1651         home = g_slist_append (home, darr);
1652 
1653         format = skip_separators (format);
1654 
1655         switch (*format)
1656         {
1657         case '<':
1658             justify = J_LEFT;
1659             format = skip_separators (format + 1);
1660             break;
1661         case '=':
1662             justify = J_CENTER;
1663             format = skip_separators (format + 1);
1664             break;
1665         case '>':
1666             justify = J_RIGHT;
1667             format = skip_separators (format + 1);
1668             break;
1669         default:
1670             justify = J_LEFT;
1671             set_justify = FALSE;
1672             break;
1673         }
1674 
1675         for (i = 0; !found && panel_fields[i].id != NULL; i++)
1676         {
1677             klen = strlen (panel_fields[i].id);
1678             found = strncmp (format, panel_fields[i].id, klen) == 0;
1679         }
1680 
1681         if (found)
1682         {
1683             i--;
1684             format += klen;
1685 
1686             darr->requested_field_len = panel_fields[i].min_size;
1687             darr->string_fn = panel_fields[i].string_fn;
1688             darr->title = g_strdup (panel_get_title_without_hotkey (panel_fields[i].title_hotkey));
1689             darr->id = panel_fields[i].id;
1690             darr->expand = panel_fields[i].expands;
1691             darr->just_mode = panel_fields[i].default_just;
1692 
1693             if (set_justify)
1694             {
1695                 if (IS_FIT (darr->just_mode))
1696                     darr->just_mode = MAKE_FIT (justify);
1697                 else
1698                     darr->just_mode = justify;
1699             }
1700 
1701             format = skip_separators (format);
1702 
1703             // If we have a size specifier
1704             if (*format == ':')
1705             {
1706                 int req_length;
1707 
1708                 /* If the size was specified, we don't want
1709                  * auto-expansion by default
1710                  */
1711                 darr->expand = FALSE;
1712                 format++;
1713                 req_length = atoi (format);
1714                 darr->requested_field_len = req_length;
1715 
1716                 format = skip_numbers (format);
1717 
1718                 // Now, if they insist on expansion
1719                 if (*format == '+')
1720                 {
1721                     darr->expand = TRUE;
1722                     format++;
1723                 }
1724             }
1725         }
1726         else
1727         {
1728             size_t pos;
1729             char *tmp_format;
1730 
1731             pos = strlen (format);
1732             if (pos > 8)
1733                 pos = 8;
1734 
1735             tmp_format = g_strndup (format, pos);
1736             g_slist_free_full (home, (GDestroyNotify) format_item_free);
1737             *error =
1738                 g_strconcat (_ ("Unknown tag on display format:"), " ", tmp_format, (char *) NULL);
1739             g_free (tmp_format);
1740 
1741             return NULL;
1742         }
1743 
1744         total_cols += darr->requested_field_len;
1745     }
1746 
1747     *res_total_cols = total_cols;
1748     return home;
1749 }
1750 
1751 /* --------------------------------------------------------------------------------------------- */
1752 
1753 static GSList *
1754 use_display_format (WPanel *panel, const char *format, char **error, gboolean isstatus)
     /* [previous][next][first][last][top][bottom][index][help]  */
1755 {
1756 #define MAX_EXPAND 4
1757     int expand_top = 0;  // Max used element in expand
1758     int usable_columns;  // Usable columns in the panel
1759     int total_cols = 0;
1760     GSList *darr, *home;
1761 
1762     if (format == NULL)
1763         format = DEFAULT_USER_FORMAT;
1764 
1765     home = parse_display_format (panel, format, error, isstatus, &total_cols);
1766 
1767     if (*error != NULL)
1768         return NULL;
1769 
1770     panel->dirty = TRUE;
1771 
1772     usable_columns = WIDGET (panel)->rect.cols - 2;
1773     // Status needn't to be split
1774     if (!isstatus)
1775     {
1776         usable_columns /= panel->list_cols;
1777         if (panel->list_cols > 1)
1778             usable_columns--;
1779     }
1780 
1781     // Look for the expandable fields and set field_len based on the requested field len
1782     for (darr = home; darr != NULL && expand_top < MAX_EXPAND; darr = g_slist_next (darr))
1783     {
1784         format_item_t *fi = (format_item_t *) darr->data;
1785 
1786         fi->field_len = fi->requested_field_len;
1787         if (fi->expand)
1788             expand_top++;
1789     }
1790 
1791     // If we used more columns than the available columns, adjust that
1792     if (total_cols > usable_columns)
1793     {
1794         int dif;
1795         int pdif = 0;
1796 
1797         dif = total_cols - usable_columns;
1798 
1799         while (dif != 0 && pdif != dif)
1800         {
1801             pdif = dif;
1802 
1803             for (darr = home; darr != NULL; darr = g_slist_next (darr))
1804             {
1805                 format_item_t *fi = (format_item_t *) darr->data;
1806 
1807                 if (dif != 0 && fi->field_len != 1)
1808                 {
1809                     fi->field_len--;
1810                     dif--;
1811                 }
1812             }
1813         }
1814 
1815         total_cols = usable_columns;  // give up, the rest should be truncated
1816     }
1817 
1818     // Expand the available space
1819     if (usable_columns > total_cols && expand_top != 0)
1820     {
1821         int i;
1822         int spaces;
1823 
1824         spaces = (usable_columns - total_cols) / expand_top;
1825 
1826         for (i = 0, darr = home; darr != NULL && i < expand_top; darr = g_slist_next (darr))
1827         {
1828             format_item_t *fi = (format_item_t *) darr->data;
1829 
1830             if (fi->expand)
1831             {
1832                 fi->field_len += spaces;
1833                 if (i == 0)
1834                     fi->field_len += (usable_columns - total_cols) % expand_top;
1835                 i++;
1836             }
1837         }
1838     }
1839 
1840     return home;
1841 }
1842 
1843 /* --------------------------------------------------------------------------------------------- */
1844 /** Given the panel->view_type returns the format string to be parsed */
1845 
1846 static const char *
1847 panel_format (WPanel *panel)
     /* [previous][next][first][last][top][bottom][index][help]  */
1848 {
1849     switch (panel->list_format)
1850     {
1851     case list_long:
1852         return "full perm space nlink space owner space group space size space mtime space name";
1853 
1854     case list_brief:
1855     {
1856         static char format[BUF_TINY];
1857         int brief_cols = panel->brief_cols;
1858 
1859         if (brief_cols < 1)
1860             brief_cols = 2;
1861 
1862         if (brief_cols > 9)
1863             brief_cols = 9;
1864 
1865         g_snprintf (format, sizeof (format), "half %d type name", brief_cols);
1866         return format;
1867     }
1868 
1869     case list_user:
1870         return panel->user_format;
1871 
1872     default:
1873     case list_full:
1874         return "half type name | size | mtime";
1875     }
1876 }
1877 
1878 /* --------------------------------------------------------------------------------------------- */
1879 
1880 static const char *
1881 mini_status_format (WPanel *panel)
     /* [previous][next][first][last][top][bottom][index][help]  */
1882 {
1883     if (panel->user_mini_status)
1884         return panel->user_status_format[panel->list_format];
1885 
1886     switch (panel->list_format)
1887     {
1888     case list_long:
1889         return "full perm space nlink space owner space group space size space mtime space name";
1890 
1891     case list_brief:
1892         return "half type name space bsize space perm space";
1893 
1894     case list_full:
1895         return "half type name";
1896 
1897     default:
1898     case list_user:
1899         return panel->user_format;
1900     }
1901 }
1902 
1903 /*                          */
1904 /* Panel operation commands */
1905 /*                          */
1906 
1907 /* --------------------------------------------------------------------------------------------- */
1908 
1909 static void
1910 cd_up_dir (WPanel *panel)
     /* [previous][next][first][last][top][bottom][index][help]  */
1911 {
1912     vfs_path_t *up_dir;
1913 
1914     up_dir = vfs_path_from_str ("..");
1915     panel_cd (panel, up_dir, cd_exact);
1916     vfs_path_free (up_dir, TRUE);
1917 }
1918 
1919 /* --------------------------------------------------------------------------------------------- */
1920 /** Used to emulate Lynx's entering leaving a directory with the arrow keys */
1921 
1922 static cb_ret_t
1923 maybe_cd (WPanel *panel, gboolean move_up_dir)
     /* [previous][next][first][last][top][bottom][index][help]  */
1924 {
1925     if (panels_options.navigate_with_arrows && input_is_empty (cmdline))
1926     {
1927         const file_entry_t *fe;
1928 
1929         if (move_up_dir)
1930         {
1931             cd_up_dir (panel);
1932             return MSG_HANDLED;
1933         }
1934 
1935         fe = panel_current_entry (panel);
1936 
1937         if (fe != NULL && (S_ISDIR (fe->st.st_mode) || link_isdir (fe)))
1938         {
1939             vfs_path_t *vpath;
1940 
1941             vpath = vfs_path_from_str (fe->fname->str);
1942             panel_cd (panel, vpath, cd_exact);
1943             vfs_path_free (vpath, TRUE);
1944             return MSG_HANDLED;
1945         }
1946     }
1947 
1948     return MSG_NOT_HANDLED;
1949 }
1950 
1951 /* --------------------------------------------------------------------------------------------- */
1952 
1953 /* if command line is empty then do 'cd ..' */
1954 static cb_ret_t
1955 force_maybe_cd (WPanel *panel)
     /* [previous][next][first][last][top][bottom][index][help]  */
1956 {
1957     if (input_is_empty (cmdline))
1958     {
1959         cd_up_dir (panel);
1960         return MSG_HANDLED;
1961     }
1962 
1963     return MSG_NOT_HANDLED;
1964 }
1965 
1966 /* --------------------------------------------------------------------------------------------- */
1967 
1968 static void
1969 unselect_item (WPanel *panel)
     /* [previous][next][first][last][top][bottom][index][help]  */
1970 {
1971     const file_entry_t *fe;
1972 
1973     fe = panel_current_entry (panel);
1974     repaint_file (panel, panel->current,
1975                   fe != NULL && fe->f.marked != 0 ? FATTR_MARKED : FATTR_NORMAL);
1976 }
1977 
1978 /* --------------------------------------------------------------------------------------------- */
1979 /** Select/unselect all the files like a current file by extension */
1980 
1981 static void
1982 panel_select_ext_cmd (WPanel *panel)
     /* [previous][next][first][last][top][bottom][index][help]  */
1983 {
1984     const file_entry_t *fe;
1985     gboolean do_select;
1986     char *reg_exp, *cur_file_ext;
1987     mc_search_t *search;
1988     int i;
1989 
1990     fe = panel_current_entry (panel);
1991     if (fe == NULL)
1992         return;
1993 
1994     do_select = (fe->f.marked == 0);
1995 
1996     cur_file_ext = str_regex_escape (extension (fe->fname->str));
1997     if (cur_file_ext[0] != '\0')
1998         reg_exp = g_strconcat ("^.*\\.", cur_file_ext, "$", (char *) NULL);
1999     else
2000         reg_exp = g_strdup ("^[^\\.]+$");
2001 
2002     g_free (cur_file_ext);
2003 
2004     search = mc_search_new (reg_exp, NULL);
2005     search->search_type = MC_SEARCH_T_REGEX;
2006     search->is_case_sensitive = FALSE;
2007 
2008     for (i = 0; i < panel->dir.len; i++)
2009     {
2010         fe = &panel->dir.list[i];
2011 
2012         if (DIR_IS_DOTDOT (fe->fname->str) || S_ISDIR (fe->st.st_mode))
2013             continue;
2014 
2015         if (!mc_search_run (search, fe->fname->str, 0, fe->fname->len, NULL))
2016             continue;
2017 
2018         do_file_mark (panel, i, do_select ? 1 : 0);
2019     }
2020 
2021     mc_search_free (search);
2022     g_free (reg_exp);
2023 }
2024 
2025 /* --------------------------------------------------------------------------------------------- */
2026 
2027 static void
2028 panel_set_current (WPanel *panel, int i)
     /* [previous][next][first][last][top][bottom][index][help]  */
2029 {
2030     if (i != panel->current)
2031     {
2032         panel->dirty = TRUE;
2033         panel->current = i;
2034         panel->top = panel->current - (WIDGET (panel)->rect.lines - 2) / 2;
2035         if (panel->top < 0)
2036             panel->top = 0;
2037     }
2038 }
2039 
2040 /* --------------------------------------------------------------------------------------------- */
2041 
2042 static int
2043 panel_current_at_half (const WPanel *panel)
     /* [previous][next][first][last][top][bottom][index][help]  */
2044 {
2045     int lines, top;
2046 
2047     lines = panel_lines (panel);
2048 
2049     // define top file of column
2050     top = panel->top;
2051     if (panel->list_cols > 1)
2052         top += lines * ((panel->current - top) / lines);
2053 
2054     return (panel->current - top - lines / 2);
2055 }
2056 
2057 /* --------------------------------------------------------------------------------------------- */
2058 
2059 static void
2060 move_down (WPanel *panel)
     /* [previous][next][first][last][top][bottom][index][help]  */
2061 {
2062     int items;
2063 
2064     if (panel->dir.len == 0)
2065         return;
2066 
2067     if (panel->current >= panel->dir.len - 1)
2068     {
2069         panel->current = panel->dir.len - 1;
2070         return;
2071     }
2072 
2073     unselect_item (panel);
2074     panel->current++;
2075 
2076     items = panel_items (panel);
2077 
2078     if (panels_options.scroll_pages && panel->current - panel->top == items)
2079     {
2080         // Scroll window half screen
2081         panel->top += items / 2;
2082         if (panel->top > panel->dir.len - items)
2083             panel->top = panel->dir.len - items;
2084         paint_dir (panel);
2085     }
2086     else if (panels_options.scroll_center && panel_current_at_half (panel) > 0)
2087     {
2088         // Scroll window when cursor is halfway down
2089         panel->top++;
2090         if (panel->top > panel->dir.len - items)
2091             panel->top = panel->dir.len - items;
2092     }
2093     select_item (panel);
2094 }
2095 
2096 /* --------------------------------------------------------------------------------------------- */
2097 
2098 static void
2099 move_up (WPanel *panel)
     /* [previous][next][first][last][top][bottom][index][help]  */
2100 {
2101     if (panel->dir.len == 0 || panel->current <= 0)
2102         return;
2103 
2104     unselect_item (panel);
2105     panel->current--;
2106 
2107     if (panels_options.scroll_pages && panel->current < panel->top)
2108     {
2109         // Scroll window half screen
2110         panel->top -= panel_items (panel) / 2;
2111         if (panel->top < 0)
2112             panel->top = 0;
2113         paint_dir (panel);
2114     }
2115     else if (panels_options.scroll_center && panel_current_at_half (panel) < 0)
2116     {
2117         // Scroll window when cursor is halfway up
2118         panel->top--;
2119         if (panel->top < 0)
2120             panel->top = 0;
2121     }
2122     select_item (panel);
2123 }
2124 
2125 /* --------------------------------------------------------------------------------------------- */
2126 /** Changes the current by lines (may be negative) */
2127 
2128 static void
2129 panel_move_current (WPanel *panel, int lines)
     /* [previous][next][first][last][top][bottom][index][help]  */
2130 {
2131     int new_pos;
2132     gboolean adjust = FALSE;
2133 
2134     if (panel->dir.len == 0 || panel->current < 0)
2135         return;
2136 
2137     new_pos = panel->current + lines;
2138     if (new_pos >= panel->dir.len)
2139         new_pos = panel->dir.len - 1;
2140 
2141     if (new_pos < 0)
2142         new_pos = 0;
2143 
2144     unselect_item (panel);
2145     panel->current = new_pos;
2146 
2147     if (panel->current - panel->top >= panel_items (panel))
2148     {
2149         panel->top += lines;
2150         adjust = TRUE;
2151     }
2152 
2153     if (panel->current - panel->top < 0)
2154     {
2155         panel->top += lines;
2156         adjust = TRUE;
2157     }
2158 
2159     if (adjust)
2160     {
2161         if (panel->top > panel->current)
2162             panel->top = panel->current;
2163         if (panel->top < 0)
2164             panel->top = 0;
2165         paint_dir (panel);
2166     }
2167     select_item (panel);
2168 }
2169 
2170 /* --------------------------------------------------------------------------------------------- */
2171 
2172 static cb_ret_t
2173 move_left (WPanel *panel)
     /* [previous][next][first][last][top][bottom][index][help]  */
2174 {
2175     if (panel->list_cols > 1)
2176     {
2177         panel_move_current (panel, -panel_lines (panel));
2178         return MSG_HANDLED;
2179     }
2180 
2181     return maybe_cd (panel, TRUE);  // cd ..
2182 }
2183 
2184 /* --------------------------------------------------------------------------------------------- */
2185 
2186 static cb_ret_t
2187 move_right (WPanel *panel)
     /* [previous][next][first][last][top][bottom][index][help]  */
2188 {
2189     if (panel->list_cols > 1)
2190     {
2191         panel_move_current (panel, panel_lines (panel));
2192         return MSG_HANDLED;
2193     }
2194 
2195     return maybe_cd (panel, FALSE);  // cd (current)
2196 }
2197 
2198 /* --------------------------------------------------------------------------------------------- */
2199 
2200 static void
2201 prev_page (WPanel *panel)
     /* [previous][next][first][last][top][bottom][index][help]  */
2202 {
2203     int items;
2204 
2205     if (panel->dir.len == 0 || panel->current < 0 || (panel->current == 0 && panel->top == 0))
2206         return;
2207 
2208     unselect_item (panel);
2209     items = panel_items (panel);
2210     if (panel->top < items)
2211         items = panel->top;
2212     if (items == 0)
2213         panel->current = 0;
2214     else
2215         panel->current -= items;
2216     panel->top -= items;
2217 
2218     select_item (panel);
2219     paint_dir (panel);
2220 }
2221 
2222 /* --------------------------------------------------------------------------------------------- */
2223 
2224 static void
2225 goto_parent_dir (WPanel *panel)
     /* [previous][next][first][last][top][bottom][index][help]  */
2226 {
2227     if (!panel->is_panelized)
2228         cd_up_dir (panel);
2229     else
2230     {
2231         const file_entry_t *fe;
2232         GString *fname;
2233         const char *bname;
2234         vfs_path_t *dname_vpath;
2235 
2236         fe = panel_current_entry (panel);
2237         if (fe == NULL)
2238             return;
2239 
2240         fname = fe->fname;
2241 
2242         if (g_path_is_absolute (fname->str))
2243             fname = mc_g_string_dup (fname);
2244         else
2245         {
2246             char *fname2;
2247 
2248             fname2 = mc_build_filename (vfs_path_as_str (panel->panelized_descr->root_vpath),
2249                                         fname->str, (char *) NULL);
2250 
2251             fname = g_string_new_take (fname2);
2252         }
2253 
2254         bname = x_basename (fname->str);
2255 
2256         if (bname == fname->str)
2257             dname_vpath = vfs_path_from_str (".");
2258         else
2259         {
2260             g_string_truncate (fname, bname - fname->str);
2261             dname_vpath = vfs_path_from_str (fname->str);
2262         }
2263 
2264         panel_cd (panel, dname_vpath, cd_exact);
2265         panel_set_current_by_name (panel, bname);
2266 
2267         vfs_path_free (dname_vpath, TRUE);
2268         g_string_free (fname, TRUE);
2269     }
2270 }
2271 
2272 /* --------------------------------------------------------------------------------------------- */
2273 
2274 static void
2275 next_page (WPanel *panel)
     /* [previous][next][first][last][top][bottom][index][help]  */
2276 {
2277     int items;
2278 
2279     if (panel->dir.len == 0 || panel->current < 0 || panel->current == panel->dir.len - 1)
2280         return;
2281 
2282     unselect_item (panel);
2283     items = panel_items (panel);
2284     if (panel->top > panel->dir.len - 2 * items)
2285         items = panel->dir.len - items - panel->top;
2286     if (panel->top + items < 0)
2287         items = -panel->top;
2288     if (items == 0)
2289         panel->current = panel->dir.len - 1;
2290     else
2291         panel->current += items;
2292     panel->top += items;
2293 
2294     select_item (panel);
2295     paint_dir (panel);
2296 }
2297 
2298 /* --------------------------------------------------------------------------------------------- */
2299 
2300 static void
2301 goto_child_dir (WPanel *panel)
     /* [previous][next][first][last][top][bottom][index][help]  */
2302 {
2303     const file_entry_t *fe;
2304 
2305     fe = panel_current_entry (panel);
2306 
2307     if (fe != NULL && (S_ISDIR (fe->st.st_mode) || link_isdir (fe)))
2308     {
2309         vfs_path_t *vpath;
2310 
2311         vpath = vfs_path_from_str (fe->fname->str);
2312         panel_cd (panel, vpath, cd_exact);
2313         vfs_path_free (vpath, TRUE);
2314     }
2315 }
2316 
2317 /* --------------------------------------------------------------------------------------------- */
2318 
2319 static void
2320 goto_top_file (WPanel *panel)
     /* [previous][next][first][last][top][bottom][index][help]  */
2321 {
2322     if (panel->dir.len == 0 || panel->current < 0)
2323         return;
2324 
2325     unselect_item (panel);
2326     panel->current = panel->top;
2327     select_item (panel);
2328 }
2329 
2330 /* --------------------------------------------------------------------------------------------- */
2331 
2332 static void
2333 goto_middle_file (WPanel *panel)
     /* [previous][next][first][last][top][bottom][index][help]  */
2334 {
2335     if (panel->dir.len == 0 || panel->current < 0)
2336         return;
2337 
2338     unselect_item (panel);
2339     panel->current = panel->top + panel_items (panel) / 2;
2340     select_item (panel);
2341 }
2342 
2343 /* --------------------------------------------------------------------------------------------- */
2344 
2345 static void
2346 goto_bottom_file (WPanel *panel)
     /* [previous][next][first][last][top][bottom][index][help]  */
2347 {
2348     if (panel->dir.len == 0 || panel->current < 0)
2349         return;
2350 
2351     unselect_item (panel);
2352     panel->current = panel->top + panel_items (panel) - 1;
2353     select_item (panel);
2354 }
2355 
2356 /* --------------------------------------------------------------------------------------------- */
2357 
2358 static void
2359 move_home (WPanel *panel)
     /* [previous][next][first][last][top][bottom][index][help]  */
2360 {
2361     if (panel->dir.len == 0 || panel->current <= 0)
2362         return;
2363 
2364     unselect_item (panel);
2365 
2366     if (panels_options.torben_fj_mode)
2367     {
2368         int middle_pos;
2369 
2370         middle_pos = panel->top + panel_items (panel) / 2;
2371 
2372         if (panel->current > middle_pos)
2373         {
2374             goto_middle_file (panel);
2375             return;
2376         }
2377         if (panel->current != panel->top)
2378         {
2379             goto_top_file (panel);
2380             return;
2381         }
2382     }
2383 
2384     panel->top = 0;
2385     panel->current = 0;
2386 
2387     paint_dir (panel);
2388     select_item (panel);
2389 }
2390 
2391 /* --------------------------------------------------------------------------------------------- */
2392 
2393 static void
2394 move_end (WPanel *panel)
     /* [previous][next][first][last][top][bottom][index][help]  */
2395 {
2396     if (panel->dir.len == 0 || panel->current < 0 || panel->current == panel->dir.len - 1)
2397         return;
2398 
2399     unselect_item (panel);
2400 
2401     if (panels_options.torben_fj_mode)
2402     {
2403         int items, middle_pos;
2404 
2405         items = panel_items (panel);
2406         middle_pos = panel->top + items / 2;
2407 
2408         if (panel->current < middle_pos)
2409         {
2410             goto_middle_file (panel);
2411             return;
2412         }
2413         if (panel->current != panel->top + items - 1)
2414         {
2415             goto_bottom_file (panel);
2416             return;
2417         }
2418     }
2419 
2420     panel->current = panel->dir.len - 1;
2421     paint_dir (panel);
2422     select_item (panel);
2423 }
2424 
2425 /* --------------------------------------------------------------------------------------------- */
2426 
2427 static void
2428 do_mark_file (WPanel *panel, mark_act_t do_move)
     /* [previous][next][first][last][top][bottom][index][help]  */
2429 {
2430     const file_entry_t *fe;
2431 
2432     fe = panel_current_entry (panel);
2433     if (fe == NULL)
2434         return;
2435 
2436     do_file_mark (panel, panel->current, fe->f.marked ? 0 : 1);
2437 
2438     if ((panels_options.mark_moves_down && do_move == MARK_DOWN) || do_move == MARK_FORCE_DOWN)
2439         move_down (panel);
2440     else if (do_move == MARK_FORCE_UP)
2441         move_up (panel);
2442 }
2443 
2444 /* --------------------------------------------------------------------------------------------- */
2445 
2446 static inline void
2447 mark_file (WPanel *panel)
     /* [previous][next][first][last][top][bottom][index][help]  */
2448 {
2449     do_mark_file (panel, MARK_DOWN);
2450 }
2451 
2452 /* --------------------------------------------------------------------------------------------- */
2453 
2454 static inline void
2455 mark_file_up (WPanel *panel)
     /* [previous][next][first][last][top][bottom][index][help]  */
2456 {
2457     do_mark_file (panel, MARK_FORCE_UP);
2458 }
2459 
2460 /* --------------------------------------------------------------------------------------------- */
2461 
2462 static inline void
2463 mark_file_down (WPanel *panel)
     /* [previous][next][first][last][top][bottom][index][help]  */
2464 {
2465     do_mark_file (panel, MARK_FORCE_DOWN);
2466 }
2467 
2468 /* --------------------------------------------------------------------------------------------- */
2469 
2470 static void
2471 mark_file_right (WPanel *panel)
     /* [previous][next][first][last][top][bottom][index][help]  */
2472 {
2473     int lines;
2474 
2475     if (state_mark < 0)
2476     {
2477         const file_entry_t *fe;
2478 
2479         fe = panel_current_entry (panel);
2480         if (fe == NULL)
2481             return;
2482 
2483         state_mark = fe->f.marked ? 0 : 1;
2484     }
2485 
2486     lines = panel_lines (panel);
2487     lines = MIN (lines, panel->dir.len - panel->current - 1);
2488     for (; lines != 0; lines--)
2489     {
2490         do_file_mark (panel, panel->current, state_mark);
2491         move_down (panel);
2492     }
2493     do_file_mark (panel, panel->current, state_mark);
2494 }
2495 
2496 /* --------------------------------------------------------------------------------------------- */
2497 
2498 static void
2499 mark_file_left (WPanel *panel)
     /* [previous][next][first][last][top][bottom][index][help]  */
2500 {
2501     int lines;
2502 
2503     if (state_mark < 0)
2504     {
2505         const file_entry_t *fe;
2506 
2507         fe = panel_current_entry (panel);
2508         if (fe == NULL)
2509             return;
2510 
2511         state_mark = fe->f.marked ? 0 : 1;
2512     }
2513 
2514     lines = panel_lines (panel);
2515     lines = MIN (lines, panel->current + 1);
2516     for (; lines != 0; lines--)
2517     {
2518         do_file_mark (panel, panel->current, state_mark);
2519         move_up (panel);
2520     }
2521     do_file_mark (panel, panel->current, state_mark);
2522 }
2523 
2524 /* --------------------------------------------------------------------------------------------- */
2525 
2526 static mc_search_t *
2527 panel_select_unselect_files_dialog (select_flags_t *flags, const char *title,
     /* [previous][next][first][last][top][bottom][index][help]  */
2528                                     const char *history_name, const char *help_section, char **str)
2529 {
2530     gboolean files_only = (*flags & SELECT_FILES_ONLY) != 0;
2531     gboolean case_sens = (*flags & SELECT_MATCH_CASE) != 0;
2532     gboolean shell_patterns = (*flags & SELECT_SHELL_PATTERNS) != 0;
2533 
2534     char *reg_exp;
2535     mc_search_t *search;
2536 
2537     quick_widget_t quick_widgets[] = {
2538         // clang-format off
2539         QUICK_INPUT (INPUT_LAST_TEXT, history_name, &reg_exp, NULL,
2540                      FALSE, FALSE, INPUT_COMPLETE_FILENAMES),
2541         QUICK_START_COLUMNS,
2542             QUICK_CHECKBOX (N_ ("&Files only"), &files_only, NULL),
2543             QUICK_CHECKBOX (N_ ("&Using shell patterns"), &shell_patterns, NULL),
2544         QUICK_NEXT_COLUMN,
2545             QUICK_CHECKBOX (N_ ("&Case sensitive"), &case_sens, NULL),
2546         QUICK_STOP_COLUMNS,
2547         QUICK_END,
2548         // clang-format on
2549     };
2550 
2551     WRect r = { -1, -1, 0, 50 };
2552 
2553     quick_dialog_t qdlg = {
2554         .rect = r,
2555         .title = title,
2556         .help = help_section,
2557         .widgets = quick_widgets,
2558         .callback = NULL,
2559         .mouse_callback = NULL,
2560     };
2561 
2562     if (quick_dialog (&qdlg) == B_CANCEL)
2563         return NULL;
2564 
2565     if (*reg_exp == '\0')
2566     {
2567         g_free (reg_exp);
2568         if (str != NULL)
2569             *str = NULL;
2570         return SELECT_RESET;
2571     }
2572 
2573     search = mc_search_new (reg_exp, NULL);
2574     search->search_type = shell_patterns ? MC_SEARCH_T_GLOB : MC_SEARCH_T_REGEX;
2575     search->is_entire_line = TRUE;
2576     search->is_case_sensitive = case_sens;
2577 
2578     if (str != NULL)
2579         *str = reg_exp;
2580     else
2581         g_free (reg_exp);
2582 
2583     if (!mc_search_prepare (search))
2584     {
2585         message (D_ERROR, MSG_ERROR, _ ("Malformed regular expression"));
2586         mc_search_free (search);
2587         return SELECT_ERROR;
2588     }
2589 
2590     // result flags
2591     *flags = 0;
2592     if (case_sens)
2593         *flags |= SELECT_MATCH_CASE;
2594     if (files_only)
2595         *flags |= SELECT_FILES_ONLY;
2596     if (shell_patterns)
2597         *flags |= SELECT_SHELL_PATTERNS;
2598 
2599     return search;
2600 }
2601 
2602 /* --------------------------------------------------------------------------------------------- */
2603 
2604 static void
2605 panel_select_unselect_files (WPanel *panel, const char *title, const char *history_name,
     /* [previous][next][first][last][top][bottom][index][help]  */
2606                              const char *help_section, gboolean do_select)
2607 {
2608     mc_search_t *search;
2609     gboolean files_only;
2610     int i;
2611 
2612     search = panel_select_unselect_files_dialog (&panels_options.select_flags, title, history_name,
2613                                                  help_section, NULL);
2614     if (search == NULL || search == SELECT_RESET || search == SELECT_ERROR)
2615         return;
2616 
2617     files_only = (panels_options.select_flags & SELECT_FILES_ONLY) != 0;
2618 
2619     for (i = 0; i < panel->dir.len; i++)
2620     {
2621         if (DIR_IS_DOTDOT (panel->dir.list[i].fname->str))
2622             continue;
2623         if (S_ISDIR (panel->dir.list[i].st.st_mode) && files_only)
2624             continue;
2625 
2626         if (mc_search_run (search, panel->dir.list[i].fname->str, 0, panel->dir.list[i].fname->len,
2627                            NULL))
2628             do_file_mark (panel, i, do_select ? 1 : 0);
2629     }
2630 
2631     mc_search_free (search);
2632 }
2633 
2634 /* --------------------------------------------------------------------------------------------- */
2635 
2636 static void
2637 panel_select_files (WPanel *panel)
     /* [previous][next][first][last][top][bottom][index][help]  */
2638 {
2639     panel_select_unselect_files (panel, _ ("Select"), MC_HISTORY_FM_PANEL_SELECT,
2640                                  "[Select/Unselect Files]", TRUE);
2641 }
2642 
2643 /* --------------------------------------------------------------------------------------------- */
2644 
2645 static void
2646 panel_unselect_files (WPanel *panel)
     /* [previous][next][first][last][top][bottom][index][help]  */
2647 {
2648     panel_select_unselect_files (panel, _ ("Unselect"), MC_HISTORY_FM_PANEL_UNSELECT,
2649                                  "[Select/Unselect Files]", FALSE);
2650 }
2651 
2652 /* --------------------------------------------------------------------------------------------- */
2653 
2654 static void
2655 panel_select_invert_files (WPanel *panel)
     /* [previous][next][first][last][top][bottom][index][help]  */
2656 {
2657     int i;
2658 
2659     for (i = 0; i < panel->dir.len; i++)
2660     {
2661         file_entry_t *file = &panel->dir.list[i];
2662 
2663         if (!panels_options.reverse_files_only || !S_ISDIR (file->st.st_mode))
2664             do_file_mark (panel, i, file->f.marked ? 0 : 1);
2665     }
2666 }
2667 
2668 /* --------------------------------------------------------------------------------------------- */
2669 
2670 static void
2671 panel_do_set_filter (WPanel *panel)
     /* [previous][next][first][last][top][bottom][index][help]  */
2672 {
2673     file_filter_t ff = {
2674         .value = NULL,
2675         .handler = NULL,
2676         .flags = panel->filter.flags,
2677     };
2678 
2679     ff.handler = panel_select_unselect_files_dialog (
2680         &ff.flags, _ ("Filter"), MC_HISTORY_FM_PANEL_FILTER, "[Filter...]", &ff.value);
2681 
2682     if (ff.handler == NULL || ff.handler == SELECT_ERROR)
2683         return;
2684 
2685     if (ff.handler == SELECT_RESET)
2686         ff.handler = NULL;
2687 
2688     panel_set_filter (panel, &ff);
2689 }
2690 
2691 /* --------------------------------------------------------------------------------------------- */
2692 /** Incremental search of a file name in the panel.
2693  * @param panel instance of WPanel structure
2694  * @param c_code key code
2695  */
2696 
2697 static void
2698 do_search (WPanel *panel, int c_code)
     /* [previous][next][first][last][top][bottom][index][help]  */
2699 {
2700     int curr;
2701     int i;
2702     gboolean wrapped = FALSE;
2703     char *act;
2704     mc_search_t *search;
2705     char *reg_exp, *esc_str;
2706     gboolean is_found = FALSE;
2707 
2708     if (c_code == KEY_BACKSPACE)
2709     {
2710         if (panel->quick_search.buffer->len != 0)
2711         {
2712             act = panel->quick_search.buffer->str + panel->quick_search.buffer->len;
2713             str_prev_noncomb_char (&act, panel->quick_search.buffer->str);
2714             g_string_set_size (panel->quick_search.buffer, act - panel->quick_search.buffer->str);
2715         }
2716         panel->quick_search.chpoint = 0;
2717     }
2718     else
2719     {
2720         if (c_code != 0 && (gsize) panel->quick_search.chpoint < sizeof (panel->quick_search.ch))
2721         {
2722             panel->quick_search.ch[panel->quick_search.chpoint] = c_code;
2723             panel->quick_search.chpoint++;
2724         }
2725 
2726         if (panel->quick_search.chpoint > 0)
2727         {
2728             switch (str_is_valid_char (panel->quick_search.ch, panel->quick_search.chpoint))
2729             {
2730             case -2:
2731                 return;
2732             case -1:
2733                 panel->quick_search.chpoint = 0;
2734                 return;
2735             default:
2736                 g_string_append_len (panel->quick_search.buffer, panel->quick_search.ch,
2737                                      panel->quick_search.chpoint);
2738                 panel->quick_search.chpoint = 0;
2739             }
2740         }
2741     }
2742 
2743     reg_exp = g_strdup_printf ("%s*", panel->quick_search.buffer->str);
2744     esc_str = str_escape (reg_exp, -1, ",|\\{}[]", TRUE);
2745     search = mc_search_new (esc_str, NULL);
2746     search->search_type = MC_SEARCH_T_GLOB;
2747     search->is_entire_line = TRUE;
2748 
2749     switch (panels_options.qsearch_mode)
2750     {
2751     case QSEARCH_CASE_SENSITIVE:
2752         search->is_case_sensitive = TRUE;
2753         break;
2754     case QSEARCH_CASE_INSENSITIVE:
2755         search->is_case_sensitive = FALSE;
2756         break;
2757     default:
2758         search->is_case_sensitive = panel->sort_info.case_sensitive;
2759         break;
2760     }
2761 
2762     curr = panel->current;
2763 
2764     for (i = panel->current; !wrapped || i != panel->current; i++)
2765     {
2766         if (i >= panel->dir.len)
2767         {
2768             i = 0;
2769             if (wrapped)
2770                 break;
2771             wrapped = TRUE;
2772         }
2773         if (mc_search_run (search, panel->dir.list[i].fname->str, 0, panel->dir.list[i].fname->len,
2774                            NULL))
2775         {
2776             curr = i;
2777             is_found = TRUE;
2778             break;
2779         }
2780     }
2781     if (is_found)
2782     {
2783         unselect_item (panel);
2784         panel->current = curr;
2785         select_item (panel);
2786         widget_draw (WIDGET (panel));
2787     }
2788     else if (c_code != KEY_BACKSPACE)
2789     {
2790         act = panel->quick_search.buffer->str + panel->quick_search.buffer->len;
2791         str_prev_noncomb_char (&act, panel->quick_search.buffer->str);
2792         g_string_set_size (panel->quick_search.buffer, act - panel->quick_search.buffer->str);
2793     }
2794     mc_search_free (search);
2795     g_free (reg_exp);
2796     g_free (esc_str);
2797 }
2798 
2799 /* --------------------------------------------------------------------------------------------- */
2800 /** Start new search.
2801  * @param panel instance of WPanel structure
2802  */
2803 
2804 static void
2805 start_search (WPanel *panel)
     /* [previous][next][first][last][top][bottom][index][help]  */
2806 {
2807     if (panel->quick_search.active)
2808     {
2809         if (panel->current == panel->dir.len - 1)
2810             panel->current = 0;
2811         else
2812             move_down (panel);
2813 
2814         /* in case if there was no search string we need to recall
2815            previous string, with which we ended previous searching */
2816         if (panel->quick_search.buffer->len == 0)
2817             mc_g_string_copy (panel->quick_search.buffer, panel->quick_search.prev_buffer);
2818 
2819         do_search (panel, 0);
2820     }
2821     else if (panel->dir.len != 0)
2822     {
2823         panel->quick_search.active = TRUE;
2824         g_string_set_size (panel->quick_search.buffer, 0);
2825         panel->quick_search.ch[0] = '\0';
2826         panel->quick_search.chpoint = 0;
2827         display_mini_info (panel);
2828     }
2829 }
2830 
2831 /* --------------------------------------------------------------------------------------------- */
2832 
2833 static void
2834 stop_search (WPanel *panel)
     /* [previous][next][first][last][top][bottom][index][help]  */
2835 {
2836     if (!panel->quick_search.active)
2837         return;
2838 
2839     panel->quick_search.active = FALSE;
2840 
2841     /* if user overrdied search string, we need to store it
2842        to the quick_search.prev_buffer */
2843     if (panel->quick_search.buffer->len != 0)
2844         mc_g_string_copy (panel->quick_search.prev_buffer, panel->quick_search.buffer);
2845 
2846     display_mini_info (panel);
2847 }
2848 
2849 /* --------------------------------------------------------------------------------------------- */
2850 /** Return TRUE if the Enter key has been processed, FALSE otherwise */
2851 
2852 static gboolean
2853 do_enter_on_file_entry (WPanel *panel, const file_entry_t *fe)
     /* [previous][next][first][last][top][bottom][index][help]  */
2854 {
2855     const char *fname = fe->fname->str;
2856     char *fname_quoted;
2857     vfs_path_t *full_name_vpath;
2858     gboolean ok;
2859 
2860     /*
2861      * Directory or link to directory - change directory.
2862      * Try the same for the entries on which mc_lstat() has failed.
2863      */
2864     if (S_ISDIR (fe->st.st_mode) || link_isdir (fe) || (fe->st.st_mode == 0))
2865     {
2866         vfs_path_t *fname_vpath;
2867 
2868         fname_vpath = vfs_path_from_str (fname);
2869         if (!panel_cd (panel, fname_vpath, cd_exact))
2870             cd_error_message (fname);
2871         vfs_path_free (fname_vpath, TRUE);
2872         return TRUE;
2873     }
2874 
2875     full_name_vpath = vfs_path_append_new (panel->cwd_vpath, fname, (char *) NULL);
2876 
2877     // Try associated command
2878     ok = regex_command (full_name_vpath, "Open") != 0;
2879     vfs_path_free (full_name_vpath, TRUE);
2880     if (ok)
2881         return TRUE;
2882 
2883     // Check if the file is executable
2884     full_name_vpath = vfs_path_append_new (panel->cwd_vpath, fname, (char *) NULL);
2885     ok = (is_exe (fe->st.st_mode) && if_link_is_exe (full_name_vpath, fe));
2886     vfs_path_free (full_name_vpath, TRUE);
2887     if (!ok)
2888         return FALSE;
2889 
2890     if (confirm_execute
2891         && query_dialog (_ ("The Midnight Commander"), _ ("Do you really want to execute?"),
2892                          D_NORMAL, 2, _ ("&Yes"), _ ("&No"))
2893             != 0)
2894         return TRUE;
2895 
2896     if (!vfs_current_is_local ())
2897     {
2898         int ret;
2899         vfs_path_t *tmp_vpath;
2900 
2901         tmp_vpath = vfs_path_append_new (vfs_get_raw_current_dir (), fname, (char *) NULL);
2902         ret = mc_setctl (tmp_vpath, VFS_SETCTL_RUN, NULL);
2903         vfs_path_free (tmp_vpath, TRUE);
2904         // We took action only if the dialog was shown or the execution was successful
2905         return confirm_execute || (ret == 0);
2906     }
2907 
2908     fname_quoted = name_quote (fname, FALSE);
2909     if (fname_quoted != NULL)
2910     {
2911         char *cmd;
2912 
2913         cmd = g_strconcat ("." PATH_SEP_STR, fname_quoted, (char *) NULL);
2914         g_free (fname_quoted);
2915 
2916         shell_execute (cmd, 0);
2917         g_free (cmd);
2918     }
2919 
2920 #ifdef HAVE_CHARSET
2921     mc_global.source_codepage = default_source_codepage;
2922 #endif
2923 
2924     return TRUE;
2925 }
2926 
2927 /* --------------------------------------------------------------------------------------------- */
2928 
2929 static inline gboolean
2930 do_enter (WPanel *panel)
     /* [previous][next][first][last][top][bottom][index][help]  */
2931 {
2932     const file_entry_t *fe;
2933 
2934     fe = panel_current_entry (panel);
2935 
2936     return (fe == NULL ? FALSE : do_enter_on_file_entry (panel, fe));
2937 }
2938 
2939 /* --------------------------------------------------------------------------------------------- */
2940 
2941 static void
2942 panel_cycle_listing_format (WPanel *panel)
     /* [previous][next][first][last][top][bottom][index][help]  */
2943 {
2944     panel->list_format = (panel->list_format + 1) % LIST_FORMATS;
2945 
2946     if (set_panel_formats (panel) == 0)
2947         do_refresh ();
2948 }
2949 
2950 /* --------------------------------------------------------------------------------------------- */
2951 
2952 static void
2953 chdir_other_panel (WPanel *panel)
     /* [previous][next][first][last][top][bottom][index][help]  */
2954 {
2955     const file_entry_t *entry;
2956     vfs_path_t *new_dir_vpath;
2957     char *curr_entry = NULL;
2958     WPanel *p;
2959 
2960     entry = panel_current_entry (panel);
2961     if (entry == NULL)
2962         return;
2963 
2964     if (get_other_type () != view_listing)
2965         create_panel (get_other_index (), view_listing);
2966 
2967     if (S_ISDIR (entry->st.st_mode) || link_isdir (entry))
2968         new_dir_vpath = vfs_path_append_new (panel->cwd_vpath, entry->fname->str, (char *) NULL);
2969     else
2970     {
2971         new_dir_vpath = vfs_path_append_new (panel->cwd_vpath, "..", (char *) NULL);
2972         curr_entry = strrchr (vfs_path_get_last_path_str (panel->cwd_vpath), PATH_SEP);
2973     }
2974 
2975     p = change_panel ();
2976     panel_cd (p, new_dir_vpath, cd_exact);
2977     vfs_path_free (new_dir_vpath, TRUE);
2978 
2979     if (curr_entry != NULL)
2980         panel_set_current_by_name (p, curr_entry);
2981     (void) change_panel ();
2982 
2983     move_down (panel);
2984 }
2985 
2986 /* --------------------------------------------------------------------------------------------- */
2987 /**
2988  * Make the current directory of the current panel also the current
2989  * directory of the other panel.  Put the other panel to the listing
2990  * mode if needed.  If the current panel is panelized, the other panel
2991  * doesn't become panelized.
2992  */
2993 
2994 static void
2995 panel_sync_other (const WPanel *panel)
     /* [previous][next][first][last][top][bottom][index][help]  */
2996 {
2997     if (get_other_type () != view_listing)
2998         create_panel (get_other_index (), view_listing);
2999 
3000     panel_do_cd (other_panel, panel->cwd_vpath, cd_exact);
3001 
3002     // try to set current filename on the other panel
3003     if (!panel->is_panelized)
3004     {
3005         const file_entry_t *fe;
3006 
3007         fe = panel_current_entry (panel);
3008         if (fe != NULL)
3009             panel_set_current_by_name (other_panel, fe->fname->str);
3010     }
3011 }
3012 
3013 /* --------------------------------------------------------------------------------------------- */
3014 
3015 static void
3016 chdir_to_readlink (WPanel *panel)
     /* [previous][next][first][last][top][bottom][index][help]  */
3017 {
3018     const file_entry_t *fe;
3019     vfs_path_t *new_dir_vpath;
3020     char buffer[MC_MAXPATHLEN];
3021     int i;
3022     struct stat st;
3023     vfs_path_t *panel_fname_vpath;
3024     gboolean ok;
3025     WPanel *cpanel;
3026 
3027     if (get_other_type () != view_listing)
3028         return;
3029 
3030     fe = panel_current_entry (panel);
3031 
3032     if (fe == NULL || !S_ISLNK (fe->st.st_mode))
3033         return;
3034 
3035     i = readlink (fe->fname->str, buffer, MC_MAXPATHLEN - 1);
3036     if (i < 0)
3037         return;
3038 
3039     panel_fname_vpath = vfs_path_from_str (fe->fname->str);
3040     ok = (mc_stat (panel_fname_vpath, &st) >= 0);
3041     vfs_path_free (panel_fname_vpath, TRUE);
3042     if (!ok)
3043         return;
3044 
3045     buffer[i] = '\0';
3046     if (!S_ISDIR (st.st_mode))
3047     {
3048         char *p;
3049 
3050         p = strrchr (buffer, PATH_SEP);
3051         if (p != NULL && p[1] == '\0')
3052         {
3053             *p = '\0';
3054             p = strrchr (buffer, PATH_SEP);
3055         }
3056         if (p == NULL)
3057             return;
3058 
3059         p[1] = '\0';
3060     }
3061     if (IS_PATH_SEP (*buffer))
3062         new_dir_vpath = vfs_path_from_str (buffer);
3063     else
3064         new_dir_vpath = vfs_path_append_new (panel->cwd_vpath, buffer, (char *) NULL);
3065 
3066     cpanel = change_panel ();
3067     panel_cd (cpanel, new_dir_vpath, cd_exact);
3068     vfs_path_free (new_dir_vpath, TRUE);
3069     (void) change_panel ();
3070 
3071     move_down (panel);
3072 }
3073 
3074 /* --------------------------------------------------------------------------------------------- */
3075 /**
3076    function return 0 if not found and REAL_INDEX+1 if found
3077  */
3078 
3079 static gsize
3080 panel_get_format_field_index_by_name (const WPanel *panel, const char *name)
     /* [previous][next][first][last][top][bottom][index][help]  */
3081 {
3082     GSList *format;
3083     gsize lc_index;
3084 
3085     for (lc_index = 1, format = panel->format;
3086          format != NULL && strcmp (((format_item_t *) format->data)->title, name) != 0;
3087          format = g_slist_next (format), lc_index++)
3088         ;
3089 
3090     if (format == NULL)
3091         lc_index = 0;
3092 
3093     return lc_index;
3094 }
3095 
3096 /* --------------------------------------------------------------------------------------------- */
3097 
3098 static const panel_field_t *
3099 panel_get_sortable_field_by_format (const WPanel *panel, gsize lc_index)
     /* [previous][next][first][last][top][bottom][index][help]  */
3100 {
3101     const panel_field_t *pfield;
3102     const format_item_t *format;
3103 
3104     format = (const format_item_t *) g_slist_nth_data (panel->format, lc_index);
3105     if (format == NULL)
3106         return NULL;
3107 
3108     pfield = panel_get_field_by_title (format->title);
3109     if (pfield == NULL)
3110         return NULL;
3111     if (pfield->sort_routine == NULL)
3112         return NULL;
3113     return pfield;
3114 }
3115 
3116 /* --------------------------------------------------------------------------------------------- */
3117 
3118 static void
3119 panel_toggle_sort_order_prev (WPanel *panel)
     /* [previous][next][first][last][top][bottom][index][help]  */
3120 {
3121     gsize lc_index, i;
3122     const char *title;
3123     const panel_field_t *pfield = NULL;
3124 
3125     title = panel_get_title_without_hotkey (panel->sort_field->title_hotkey);
3126     lc_index = panel_get_format_field_index_by_name (panel, title);
3127 
3128     if (lc_index > 1)
3129     {
3130         // search for prev sortable column in panel format
3131         for (i = lc_index - 1;
3132              i != 0 && (pfield = panel_get_sortable_field_by_format (panel, i - 1)) == NULL; i--)
3133             ;
3134     }
3135 
3136     if (pfield == NULL)
3137     {
3138         // Sortable field not found. Try to search in each array
3139         for (i = g_slist_length (panel->format);
3140              i != 0 && (pfield = panel_get_sortable_field_by_format (panel, i - 1)) == NULL; i--)
3141             ;
3142     }
3143 
3144     if (pfield != NULL)
3145     {
3146         panel->sort_field = pfield;
3147         panel_set_sort_order (panel, pfield);
3148     }
3149 }
3150 
3151 /* --------------------------------------------------------------------------------------------- */
3152 
3153 static void
3154 panel_toggle_sort_order_next (WPanel *panel)
     /* [previous][next][first][last][top][bottom][index][help]  */
3155 {
3156     gsize lc_index, i;
3157     const panel_field_t *pfield = NULL;
3158     gsize format_field_count;
3159     const char *title;
3160 
3161     format_field_count = g_slist_length (panel->format);
3162     title = panel_get_title_without_hotkey (panel->sort_field->title_hotkey);
3163     lc_index = panel_get_format_field_index_by_name (panel, title);
3164 
3165     if (lc_index != 0 && lc_index != format_field_count)
3166     {
3167         // search for prev sortable column in panel format
3168         for (i = lc_index; i != format_field_count
3169              && (pfield = panel_get_sortable_field_by_format (panel, i)) == NULL;
3170              i++)
3171             ;
3172     }
3173 
3174     if (pfield == NULL)
3175     {
3176         // Sortable field not found. Try to search in each array
3177         for (i = 0; i != format_field_count
3178              && (pfield = panel_get_sortable_field_by_format (panel, i)) == NULL;
3179              i++)
3180             ;
3181     }
3182 
3183     if (pfield != NULL)
3184     {
3185         panel->sort_field = pfield;
3186         panel_set_sort_order (panel, pfield);
3187     }
3188 }
3189 
3190 /* --------------------------------------------------------------------------------------------- */
3191 
3192 static void
3193 panel_select_sort_order (WPanel *panel)
     /* [previous][next][first][last][top][bottom][index][help]  */
3194 {
3195     const panel_field_t *sort_order;
3196 
3197     sort_order = sort_box (&panel->sort_info, panel->sort_field);
3198     if (sort_order != NULL)
3199     {
3200         panel->sort_field = sort_order;
3201         panel_set_sort_order (panel, sort_order);
3202     }
3203 }
3204 
3205 /* --------------------------------------------------------------------------------------------- */
3206 
3207 /**
3208  * panel_content_scroll_left:
3209  * @param panel the pointer to the panel on which we operate
3210  *
3211  * scroll long filename to the left (decrement scroll pointer)
3212  *
3213  */
3214 
3215 static void
3216 panel_content_scroll_left (WPanel *panel)
     /* [previous][next][first][last][top][bottom][index][help]  */
3217 {
3218     if (panel->content_shift > -1)
3219     {
3220         if (panel->content_shift > panel->max_shift)
3221             panel->content_shift = panel->max_shift;
3222 
3223         panel->content_shift--;
3224         show_dir (panel);
3225         paint_dir (panel);
3226     }
3227 }
3228 
3229 /* --------------------------------------------------------------------------------------------- */
3230 /**
3231  * panel_content_scroll_right:
3232  * @param panel the pointer to the panel on which we operate
3233  *
3234  * scroll long filename to the right (increment scroll pointer)
3235  *
3236  */
3237 
3238 static void
3239 panel_content_scroll_right (WPanel *panel)
     /* [previous][next][first][last][top][bottom][index][help]  */
3240 {
3241     if (panel->content_shift < 0 || panel->content_shift < panel->max_shift)
3242     {
3243         panel->content_shift++;
3244         show_dir (panel);
3245         paint_dir (panel);
3246     }
3247 }
3248 
3249 /* --------------------------------------------------------------------------------------------- */
3250 
3251 static void
3252 panel_set_sort_type_by_id (WPanel *panel, const char *name)
     /* [previous][next][first][last][top][bottom][index][help]  */
3253 {
3254     if (strcmp (panel->sort_field->id, name) == 0)
3255         panel->sort_info.reverse = !panel->sort_info.reverse;
3256     else
3257     {
3258         const panel_field_t *sort_order;
3259 
3260         sort_order = panel_get_field_by_id (name);
3261         if (sort_order == NULL)
3262             return;
3263 
3264         panel->sort_field = sort_order;
3265     }
3266 
3267     panel_set_sort_order (panel, panel->sort_field);
3268 }
3269 
3270 /* --------------------------------------------------------------------------------------------- */
3271 /**
3272  *  If we moved to the parent directory move the 'current' pointer to
3273  *  the old directory name; If we leave VFS dir, remove FS specificator.
3274  *
3275  *  You do _NOT_ want to add any vfs aware code here. <pavel@ucw.cz>
3276  */
3277 
3278 static const char *
3279 get_parent_dir_name (const vfs_path_t *cwd_vpath, const vfs_path_t *lwd_vpath)
     /* [previous][next][first][last][top][bottom][index][help]  */
3280 {
3281     size_t llen, clen;
3282     const char *p, *lwd;
3283 
3284     llen = vfs_path_len (lwd_vpath);
3285     clen = vfs_path_len (cwd_vpath);
3286 
3287     if (llen <= clen)
3288         return NULL;
3289 
3290     lwd = vfs_path_as_str (lwd_vpath);
3291 
3292     p = g_strrstr (lwd, VFS_PATH_URL_DELIMITER);
3293 
3294     if (p == NULL)
3295     {
3296         const char *cwd;
3297 
3298         cwd = vfs_path_as_str (cwd_vpath);
3299 
3300         p = strrchr (lwd, PATH_SEP);
3301 
3302         if (p != NULL && strncmp (cwd, lwd, (size_t) (p - lwd)) == 0
3303             && (clen == (size_t) (p - lwd) || (p == lwd && IS_PATH_SEP (cwd[0]) && cwd[1] == '\0')))
3304             return (p + 1);
3305 
3306         return NULL;
3307     }
3308 
3309     // skip VFS prefix
3310     while (--p > lwd && !IS_PATH_SEP (*p))
3311         ;
3312     // get last component
3313     while (--p > lwd && !IS_PATH_SEP (*p))
3314         ;
3315 
3316     // return last component
3317     return (p != lwd || IS_PATH_SEP (*p)) ? p + 1 : p;
3318 }
3319 
3320 /* --------------------------------------------------------------------------------------------- */
3321 /**
3322  * Changes the current directory of the panel.
3323  * Don't record change in the directory history.
3324  */
3325 
3326 static gboolean
3327 panel_do_cd_int (WPanel *panel, const vfs_path_t *new_dir_vpath, enum cd_enum cd_type)
     /* [previous][next][first][last][top][bottom][index][help]  */
3328 {
3329     vfs_path_t *olddir_vpath;
3330 
3331     // Convert *new_path to a suitable pathname, handle ~user
3332     if (cd_type == cd_parse_command)
3333     {
3334         const vfs_path_element_t *element;
3335 
3336         element = vfs_path_get_by_index (new_dir_vpath, 0);
3337         if (strcmp (element->path, "-") == 0)
3338             new_dir_vpath = panel->lwd_vpath;
3339     }
3340 
3341     if (mc_chdir (new_dir_vpath) == -1)
3342         return FALSE;
3343 
3344     // Success: save previous directory, shutdown status of previous dir
3345     olddir_vpath = vfs_path_clone (panel->cwd_vpath);
3346     panel_set_lwd (panel, panel->cwd_vpath);
3347     input_complete_free (cmdline);
3348 
3349     vfs_path_free (panel->cwd_vpath, TRUE);
3350     vfs_setup_cwd ();
3351     panel->cwd_vpath = vfs_path_clone (vfs_get_raw_current_dir ());
3352 
3353     vfs_release_path (olddir_vpath);
3354 
3355 #ifdef ENABLE_SUBSHELL
3356     subshell_chdir (panel->cwd_vpath);
3357 #endif
3358 
3359     // Reload current panel
3360     panel_clean_dir (panel);
3361 
3362     if (!dir_list_load (&panel->dir, panel->cwd_vpath, panel->sort_field->sort_routine,
3363                         &panel->sort_info, &panel->filter))
3364         message (D_ERROR, MSG_ERROR, _ ("Cannot read directory contents"));
3365 
3366     if (panel->dir.len == 0)
3367         panel_set_current (panel, -1);
3368 
3369     panel_set_current_by_name (panel, get_parent_dir_name (panel->cwd_vpath, olddir_vpath));
3370 
3371     load_hint (FALSE);
3372     panel->dirty = TRUE;
3373     update_xterm_title_path ();
3374     update_terminal_cwd ();
3375 
3376     vfs_path_free (olddir_vpath, TRUE);
3377 
3378     return TRUE;
3379 }
3380 
3381 /* --------------------------------------------------------------------------------------------- */
3382 
3383 static void
3384 directory_history_next (WPanel *panel)
     /* [previous][next][first][last][top][bottom][index][help]  */
3385 {
3386     gboolean ok;
3387 
3388     do
3389     {
3390         GList *next;
3391 
3392         ok = TRUE;
3393         next = g_list_next (panel->dir_history.current);
3394         if (next != NULL)
3395         {
3396             vfs_path_t *data_vpath;
3397 
3398             data_vpath = vfs_path_from_str ((char *) next->data);
3399             ok = panel_do_cd_int (panel, data_vpath, cd_exact);
3400             vfs_path_free (data_vpath, TRUE);
3401             panel->dir_history.current = next;
3402         }
3403         // skip directories that present in history but absent in file system
3404     }
3405     while (!ok);
3406 }
3407 
3408 /* --------------------------------------------------------------------------------------------- */
3409 
3410 static void
3411 directory_history_prev (WPanel *panel)
     /* [previous][next][first][last][top][bottom][index][help]  */
3412 {
3413     gboolean ok;
3414 
3415     do
3416     {
3417         GList *prev;
3418 
3419         ok = TRUE;
3420         prev = g_list_previous (panel->dir_history.current);
3421         if (prev != NULL)
3422         {
3423             vfs_path_t *data_vpath;
3424 
3425             data_vpath = vfs_path_from_str ((char *) prev->data);
3426             ok = panel_do_cd_int (panel, data_vpath, cd_exact);
3427             vfs_path_free (data_vpath, TRUE);
3428             panel->dir_history.current = prev;
3429         }
3430         // skip directories that present in history but absent in file system
3431     }
3432     while (!ok);
3433 }
3434 
3435 /* --------------------------------------------------------------------------------------------- */
3436 
3437 static void
3438 directory_history_list (WPanel *panel)
     /* [previous][next][first][last][top][bottom][index][help]  */
3439 {
3440     history_descriptor_t hd;
3441     gboolean ok = FALSE;
3442     size_t pos;
3443 
3444     pos = g_list_position (panel->dir_history.current, panel->dir_history.list);
3445 
3446     history_descriptor_init (&hd, WIDGET (panel)->rect.y, WIDGET (panel)->rect.x,
3447                              panel->dir_history.list, (int) pos);
3448     history_show (&hd);
3449 
3450     panel->dir_history.list = hd.list;
3451     if (hd.text != NULL)
3452     {
3453         vfs_path_t *s_vpath;
3454 
3455         s_vpath = vfs_path_from_str (hd.text);
3456         ok = panel_do_cd_int (panel, s_vpath, cd_exact);
3457         if (ok)
3458             directory_history_add (panel, panel->cwd_vpath);
3459         else
3460             cd_error_message (hd.text);
3461         vfs_path_free (s_vpath, TRUE);
3462         g_free (hd.text);
3463     }
3464 
3465     if (!ok)
3466     {
3467         /* Since history is fully modified in history_show(), panel->dir_history actually
3468          * points to the invalid place. Try restore current position here. */
3469 
3470         size_t i;
3471 
3472         panel->dir_history.current = panel->dir_history.list;
3473 
3474         for (i = 0; i <= pos; i++)
3475         {
3476             GList *prev;
3477 
3478             prev = g_list_previous (panel->dir_history.current);
3479             if (prev == NULL)
3480                 break;
3481 
3482             panel->dir_history.current = prev;
3483         }
3484     }
3485 }
3486 
3487 /* --------------------------------------------------------------------------------------------- */
3488 
3489 static cb_ret_t
3490 panel_execute_cmd (WPanel *panel, long command)
     /* [previous][next][first][last][top][bottom][index][help]  */
3491 {
3492     int res = MSG_HANDLED;
3493 
3494     if (command != CK_Search)
3495         stop_search (panel);
3496 
3497     switch (command)
3498     {
3499     case CK_Up:
3500     case CK_Down:
3501     case CK_Left:
3502     case CK_Right:
3503     case CK_Bottom:
3504     case CK_Top:
3505     case CK_PageDown:
3506     case CK_PageUp:
3507         // reset state of marks flag
3508         state_mark = -1;
3509         break;
3510     default:
3511         break;
3512     }
3513 
3514     switch (command)
3515     {
3516     case CK_CycleListingFormat:
3517         panel_cycle_listing_format (panel);
3518         break;
3519     case CK_PanelOtherCd:
3520         chdir_other_panel (panel);
3521         break;
3522     case CK_PanelOtherCdLink:
3523         chdir_to_readlink (panel);
3524         break;
3525     case CK_CopySingle:
3526         copy_cmd_local (panel);
3527         break;
3528     case CK_DeleteSingle:
3529         delete_cmd_local (panel);
3530         break;
3531     case CK_Enter:
3532         do_enter (panel);
3533         break;
3534     case CK_ViewRaw:
3535         view_raw_cmd (panel);
3536         break;
3537     case CK_EditNew:
3538         edit_cmd_new ();
3539         break;
3540     case CK_MoveSingle:
3541         rename_cmd_local (panel);
3542         break;
3543     case CK_SelectInvert:
3544         panel_select_invert_files (panel);
3545         break;
3546     case CK_Select:
3547         panel_select_files (panel);
3548         break;
3549     case CK_SelectExt:
3550         panel_select_ext_cmd (panel);
3551         break;
3552     case CK_Unselect:
3553         panel_unselect_files (panel);
3554         break;
3555     case CK_Filter:
3556         panel_do_set_filter (panel);
3557         break;
3558     case CK_PageDown:
3559         next_page (panel);
3560         break;
3561     case CK_PageUp:
3562         prev_page (panel);
3563         break;
3564     case CK_CdChild:
3565         goto_child_dir (panel);
3566         break;
3567     case CK_CdParent:
3568         goto_parent_dir (panel);
3569         break;
3570     case CK_History:
3571         directory_history_list (panel);
3572         break;
3573     case CK_HistoryNext:
3574         directory_history_next (panel);
3575         break;
3576     case CK_HistoryPrev:
3577         directory_history_prev (panel);
3578         break;
3579     case CK_BottomOnScreen:
3580         goto_bottom_file (panel);
3581         break;
3582     case CK_MiddleOnScreen:
3583         goto_middle_file (panel);
3584         break;
3585     case CK_TopOnScreen:
3586         goto_top_file (panel);
3587         break;
3588     case CK_Mark:
3589         mark_file (panel);
3590         break;
3591     case CK_MarkUp:
3592         mark_file_up (panel);
3593         break;
3594     case CK_MarkDown:
3595         mark_file_down (panel);
3596         break;
3597     case CK_MarkLeft:
3598         mark_file_left (panel);
3599         break;
3600     case CK_MarkRight:
3601         mark_file_right (panel);
3602         break;
3603     case CK_CdParentSmart:
3604         res = force_maybe_cd (panel);
3605         break;
3606     case CK_Up:
3607         move_up (panel);
3608         break;
3609     case CK_Down:
3610         move_down (panel);
3611         break;
3612     case CK_Left:
3613         res = move_left (panel);
3614         break;
3615     case CK_Right:
3616         res = move_right (panel);
3617         break;
3618     case CK_Bottom:
3619         move_end (panel);
3620         break;
3621     case CK_Top:
3622         move_home (panel);
3623         break;
3624 #ifdef HAVE_CHARSET
3625     case CK_SelectCodepage:
3626         panel_change_encoding (panel);
3627         break;
3628 #endif
3629     case CK_ScrollLeft:
3630         panel_content_scroll_left (panel);
3631         break;
3632     case CK_ScrollRight:
3633         panel_content_scroll_right (panel);
3634         break;
3635     case CK_Search:
3636         start_search (panel);
3637         break;
3638     case CK_SearchStop:
3639         break;
3640     case CK_PanelOtherSync:
3641         panel_sync_other (panel);
3642         break;
3643     case CK_Sort:
3644         panel_select_sort_order (panel);
3645         break;
3646     case CK_SortPrev:
3647         panel_toggle_sort_order_prev (panel);
3648         break;
3649     case CK_SortNext:
3650         panel_toggle_sort_order_next (panel);
3651         break;
3652     case CK_SortReverse:
3653         panel->sort_info.reverse = !panel->sort_info.reverse;
3654         panel_set_sort_order (panel, panel->sort_field);
3655         break;
3656     case CK_SortByName:
3657         panel_set_sort_type_by_id (panel, "name");
3658         break;
3659     case CK_SortByExt:
3660         panel_set_sort_type_by_id (panel, "extension");
3661         break;
3662     case CK_SortBySize:
3663         panel_set_sort_type_by_id (panel, "size");
3664         break;
3665     case CK_SortByMTime:
3666         panel_set_sort_type_by_id (panel, "mtime");
3667         break;
3668     default:
3669         res = MSG_NOT_HANDLED;
3670         break;
3671     }
3672 
3673     return res;
3674 }
3675 
3676 /* --------------------------------------------------------------------------------------------- */
3677 
3678 static cb_ret_t
3679 panel_key (WPanel *panel, int key)
     /* [previous][next][first][last][top][bottom][index][help]  */
3680 {
3681     long command;
3682 
3683     if (is_abort_char (key))
3684     {
3685         stop_search (panel);
3686         return MSG_HANDLED;
3687     }
3688 
3689     if (panel->quick_search.active && ((key >= ' ' && key <= 255) || key == KEY_BACKSPACE))
3690     {
3691         do_search (panel, key);
3692         return MSG_HANDLED;
3693     }
3694 
3695     command = widget_lookup_key (WIDGET (panel), key);
3696     if (command != CK_IgnoreKey)
3697         return panel_execute_cmd (panel, command);
3698 
3699     if (panels_options.torben_fj_mode && key == ALT ('h'))
3700     {
3701         goto_middle_file (panel);
3702         return MSG_HANDLED;
3703     }
3704 
3705     if (!command_prompt && ((key >= ' ' && key <= 255) || key == KEY_BACKSPACE))
3706     {
3707         start_search (panel);
3708         do_search (panel, key);
3709         return MSG_HANDLED;
3710     }
3711 
3712     return MSG_NOT_HANDLED;
3713 }
3714 
3715 /* --------------------------------------------------------------------------------------------- */
3716 
3717 static cb_ret_t
3718 panel_callback (Widget *w, Widget *sender, widget_msg_t msg, int parm, void *data)
     /* [previous][next][first][last][top][bottom][index][help]  */
3719 {
3720     WPanel *panel = PANEL (w);
3721     WDialog *h = DIALOG (w->owner);
3722     WButtonBar *bb;
3723 
3724     switch (msg)
3725     {
3726     case MSG_INIT:
3727         // subscribe to "history_load" event
3728         mc_event_add (h->event_group, MCEVENT_HISTORY_LOAD, panel_load_history, w, NULL);
3729         // subscribe to "history_save" event
3730         mc_event_add (h->event_group, MCEVENT_HISTORY_SAVE, panel_save_history, w, NULL);
3731         return MSG_HANDLED;
3732 
3733     case MSG_DRAW:
3734         // Repaint everything, including frame and separator
3735         widget_erase (w);
3736         show_dir (panel);
3737         panel_print_header (panel);
3738         adjust_top_file (panel);
3739         paint_dir (panel);
3740         mini_info_separator (panel);
3741         display_mini_info (panel);
3742         panel->dirty = FALSE;
3743         return MSG_HANDLED;
3744 
3745     case MSG_FOCUS:
3746         state_mark = -1;
3747         current_panel = panel;
3748         panel->active = TRUE;
3749 
3750         if (mc_chdir (panel->cwd_vpath) != 0)
3751         {
3752             char *cwd;
3753 
3754             cwd = vfs_path_to_str_flags (panel->cwd_vpath, 0, VPF_STRIP_PASSWORD);
3755             cd_error_message (cwd);
3756             g_free (cwd);
3757         }
3758 #ifdef ENABLE_SUBSHELL
3759         else
3760             subshell_chdir (panel->cwd_vpath);
3761 #endif
3762 
3763         update_xterm_title_path ();
3764         update_terminal_cwd ();
3765         select_item (panel);
3766 
3767         bb = buttonbar_find (h);
3768         midnight_set_buttonbar (bb);
3769         return MSG_HANDLED;
3770 
3771     case MSG_UNFOCUS:
3772         // Janne: look at this for the multiple panel options
3773         stop_search (panel);
3774         panel->active = FALSE;
3775         unselect_item (panel);
3776         return MSG_HANDLED;
3777 
3778     case MSG_KEY:
3779         return panel_key (panel, parm);
3780 
3781     case MSG_ACTION:
3782         return panel_execute_cmd (panel, parm);
3783 
3784     case MSG_DESTROY:
3785         vfs_stamp_path (panel->cwd_vpath);
3786         // unsubscribe from "history_load" event
3787         mc_event_del (h->event_group, MCEVENT_HISTORY_LOAD, panel_load_history, w);
3788         // unsubscribe from "history_save" event
3789         mc_event_del (h->event_group, MCEVENT_HISTORY_SAVE, panel_save_history, w);
3790         panel_destroy (panel);
3791         free_my_statfs ();
3792         return MSG_HANDLED;
3793 
3794     default:
3795         return widget_default_callback (w, sender, msg, parm, data);
3796     }
3797 }
3798 
3799 /* --------------------------------------------------------------------------------------------- */
3800 /*                                     */
3801 /* Panel mouse events support routines */
3802 /*                                     */
3803 
3804 static void
3805 mouse_toggle_mark (WPanel *panel)
     /* [previous][next][first][last][top][bottom][index][help]  */
3806 {
3807     const file_entry_t *fe;
3808 
3809     fe = panel_current_entry (panel);
3810     if (fe != NULL)
3811     {
3812         do_mark_file (panel, MARK_DONT_MOVE);
3813         mouse_marking = fe->f.marked != 0;
3814         mouse_mark_panel = current_panel;
3815     }
3816 }
3817 
3818 /* --------------------------------------------------------------------------------------------- */
3819 
3820 static void
3821 mouse_set_mark (WPanel *panel)
     /* [previous][next][first][last][top][bottom][index][help]  */
3822 {
3823     if (mouse_mark_panel == panel)
3824     {
3825         const file_entry_t *fe;
3826 
3827         fe = panel_current_entry (panel);
3828         if (fe != NULL)
3829         {
3830             if (mouse_marking && fe->f.marked == 0)
3831                 do_mark_file (panel, MARK_DONT_MOVE);
3832             else if (!mouse_marking && fe->f.marked != 0)
3833                 do_mark_file (panel, MARK_DONT_MOVE);
3834         }
3835     }
3836 }
3837 
3838 /* --------------------------------------------------------------------------------------------- */
3839 
3840 static void
3841 mark_if_marking (WPanel *panel, const mouse_event_t *event, int previous_current)
     /* [previous][next][first][last][top][bottom][index][help]  */
3842 {
3843     if ((event->buttons & GPM_B_RIGHT) == 0)
3844         return;
3845 
3846     if (event->msg == MSG_MOUSE_DOWN)
3847         mouse_toggle_mark (panel);
3848     else
3849     {
3850         int pcurr, curr1, curr2;
3851 
3852         pcurr = panel->current;
3853         curr1 = MIN (previous_current, panel->current);
3854         curr2 = MAX (previous_current, panel->current);
3855 
3856         for (; curr1 <= curr2; curr1++)
3857         {
3858             panel->current = curr1;
3859             mouse_set_mark (panel);
3860         }
3861 
3862         panel->current = pcurr;
3863     }
3864 }
3865 
3866 /* --------------------------------------------------------------------------------------------- */
3867 /** Determine which column was clicked, and sort the panel on
3868  * that column, or reverse sort on that column if already
3869  * sorted on that column.
3870  */
3871 
3872 static void
3873 mouse_sort_col (WPanel *panel, int x)
     /* [previous][next][first][last][top][bottom][index][help]  */
3874 {
3875     int i = 0;
3876     GSList *format;
3877     const char *lc_sort_name = NULL;
3878     panel_field_t *col_sort_format = NULL;
3879 
3880     const int sort_indicator_width =
3881         panel->sort_info.reverse ? panel_sort_up_indicator_width : panel_sort_down_indicator_width;
3882 
3883     // Clicking the sort order indicator itself should reverse the current sort order
3884     if (x >= 2 && x < 2 + sort_indicator_width)
3885     {
3886         panel->sort_info.reverse = !panel->sort_info.reverse;
3887         panel_set_sort_order (panel, panel->sort_field);
3888         return;
3889     }
3890 
3891     for (format = panel->format; format != NULL; format = g_slist_next (format))
3892     {
3893         format_item_t *fi = (format_item_t *) format->data;
3894 
3895         i += fi->field_len;
3896         if (x < i + 1)
3897         {
3898             // found column
3899             lc_sort_name = fi->title;
3900             break;
3901         }
3902     }
3903 
3904     if (lc_sort_name == NULL)
3905         return;
3906 
3907     for (i = 0; panel_fields[i].id != NULL; i++)
3908     {
3909         const char *title;
3910 
3911         title = panel_get_title_without_hotkey (panel_fields[i].title_hotkey);
3912         if (panel_fields[i].sort_routine != NULL && strcmp (title, lc_sort_name) == 0)
3913         {
3914             col_sort_format = &panel_fields[i];
3915             break;
3916         }
3917     }
3918 
3919     if (col_sort_format != NULL)
3920     {
3921         if (panel->sort_field == col_sort_format)
3922             // reverse the sort if clicked column is already the sorted column
3923             panel->sort_info.reverse = !panel->sort_info.reverse;
3924         else
3925             // new sort is forced to be ascending
3926             panel->sort_info.reverse = FALSE;
3927 
3928         panel_set_sort_order (panel, col_sort_format);
3929     }
3930 }
3931 
3932 /* --------------------------------------------------------------------------------------------- */
3933 
3934 static int
3935 panel_mouse_is_on_item (const WPanel *panel, int y, int x)
     /* [previous][next][first][last][top][bottom][index][help]  */
3936 {
3937     int lines, col_width, col;
3938 
3939     if (y < 0)
3940         return MOUSE_UPPER_FILE_LIST;
3941 
3942     lines = panel_lines (panel);
3943     if (y >= lines)
3944         return MOUSE_BELOW_FILE_LIST;
3945 
3946     col_width = (CONST_WIDGET (panel)->rect.cols - 2) / panel->list_cols;
3947     // column where mouse is
3948     col = x / col_width;
3949 
3950     y += panel->top + lines * col;
3951 
3952     // are we below or in the next column of last file?
3953     if (y >= panel->dir.len)
3954         return MOUSE_AFTER_LAST_FILE;
3955 
3956     // we are on item of the file file; return an index to select a file
3957     return y;
3958 }
3959 
3960 /* --------------------------------------------------------------------------------------------- */
3961 
3962 static void
3963 panel_mouse_callback (Widget *w, mouse_msg_t msg, mouse_event_t *event)
     /* [previous][next][first][last][top][bottom][index][help]  */
3964 {
3965     WPanel *panel = PANEL (w);
3966     gboolean is_active;
3967 
3968     is_active = widget_is_active (w);
3969 
3970     switch (msg)
3971     {
3972     case MSG_MOUSE_DOWN:
3973         if (event->y == 0)
3974         {
3975             // top frame
3976             if (event->x == 1)
3977                 // "<" button
3978                 directory_history_prev (panel);
3979             else if (event->x == w->rect.cols - 2)
3980                 // ">" button
3981                 directory_history_next (panel);
3982             else if (event->x >= w->rect.cols - 5 && event->x <= w->rect.cols - 3)
3983                 // "^" button
3984                 directory_history_list (panel);
3985             else if (event->x == w->rect.cols - 6)
3986                 // "." button show/hide hidden files
3987                 send_message (filemanager, NULL, MSG_ACTION, CK_ShowHidden, NULL);
3988             else
3989             {
3990                 // no other events on 1st line, return MOU_UNHANDLED
3991                 event->result.abort = TRUE;
3992                 // avoid extra panel redraw
3993                 panel->dirty = FALSE;
3994             }
3995             break;
3996         }
3997 
3998         if (event->y == 1)
3999         {
4000             // sort on clicked column
4001             mouse_sort_col (panel, event->x + 1);
4002             break;
4003         }
4004 
4005         if (!is_active)
4006             (void) change_panel ();
4007         MC_FALLTHROUGH;
4008 
4009     case MSG_MOUSE_DRAG:
4010     {
4011         int my_index;
4012         int previous_current;
4013 
4014         my_index = panel_mouse_is_on_item (panel, event->y - 2, event->x);
4015         previous_current = panel->current;
4016 
4017         switch (my_index)
4018         {
4019         case MOUSE_UPPER_FILE_LIST:
4020             move_up (panel);
4021             mark_if_marking (panel, event, previous_current);
4022             break;
4023 
4024         case MOUSE_BELOW_FILE_LIST:
4025             move_down (panel);
4026             mark_if_marking (panel, event, previous_current);
4027             break;
4028 
4029         case MOUSE_AFTER_LAST_FILE:
4030             break;  // do nothing
4031 
4032         default:
4033             if (my_index != panel->current)
4034             {
4035                 unselect_item (panel);
4036                 panel->current = my_index;
4037                 select_item (panel);
4038             }
4039 
4040             mark_if_marking (panel, event, previous_current);
4041             break;
4042         }
4043     }
4044     break;
4045 
4046     case MSG_MOUSE_UP:
4047         break;
4048 
4049     case MSG_MOUSE_CLICK:
4050         if ((event->count & GPM_DOUBLE) != 0 && (event->buttons & GPM_B_LEFT) != 0
4051             && panel_mouse_is_on_item (panel, event->y - 2, event->x) >= 0)
4052             do_enter (panel);
4053         break;
4054 
4055     case MSG_MOUSE_MOVE:
4056         break;
4057 
4058     case MSG_MOUSE_SCROLL_UP:
4059         if (is_active)
4060         {
4061             if (panels_options.mouse_move_pages && panel->top > 0)
4062                 prev_page (panel);
4063             else  // We are in first page
4064                 move_up (panel);
4065         }
4066         break;
4067 
4068     case MSG_MOUSE_SCROLL_DOWN:
4069         if (is_active)
4070         {
4071             if (panels_options.mouse_move_pages
4072                 && panel->top + panel_items (panel) < panel->dir.len)
4073                 next_page (panel);
4074             else  // We are in last page
4075                 move_down (panel);
4076         }
4077         break;
4078 
4079     default:
4080         break;
4081     }
4082 
4083     if (panel->dirty)
4084         widget_draw (w);
4085 }
4086 
4087 /* --------------------------------------------------------------------------------------------- */
4088 
4089 static void
4090 reload_panelized (WPanel *panel)
     /* [previous][next][first][last][top][bottom][index][help]  */
4091 {
4092     int i, j;
4093     dir_list *list = &panel->dir;
4094 
4095     // refresh current VFS directory required for vfs_path_from_str()
4096     (void) mc_chdir (panel->cwd_vpath);
4097 
4098     for (i = 0, j = 0; i < list->len; i++)
4099     {
4100         vfs_path_t *vpath;
4101 
4102         vpath = vfs_path_from_str (list->list[i].fname->str);
4103         if (mc_lstat (vpath, &list->list[i].st) != 0)
4104             g_string_free (list->list[i].fname, TRUE);
4105         else
4106         {
4107             if (j != i)
4108                 list->list[j] = list->list[i];
4109             j++;
4110         }
4111         vfs_path_free (vpath, TRUE);
4112     }
4113     if (j == 0)
4114         dir_list_init (list);
4115     else
4116         list->len = j;
4117 
4118     recalculate_panel_summary (panel);
4119 
4120     if (panel != current_panel)
4121         (void) mc_chdir (current_panel->cwd_vpath);
4122 }
4123 
4124 /* --------------------------------------------------------------------------------------------- */
4125 
4126 static void
4127 update_one_panel_widget (WPanel *panel, panel_update_flags_t flags, const char *current_file)
     /* [previous][next][first][last][top][bottom][index][help]  */
4128 {
4129     gboolean free_pointer;
4130     char *my_current_file = NULL;
4131 
4132     if ((flags & UP_RELOAD) != 0)
4133     {
4134         panel->is_panelized = FALSE;
4135         mc_setctl (panel->cwd_vpath, VFS_SETCTL_FLUSH, NULL);
4136         memset (&(panel->dir_stat), 0, sizeof (panel->dir_stat));
4137     }
4138 
4139     // If current_file == -1 (an invalid pointer) then preserve current
4140     free_pointer = current_file == UP_KEEPSEL;
4141 
4142     if (free_pointer)
4143     {
4144         const file_entry_t *fe;
4145 
4146         fe = panel_current_entry (panel);
4147         if (fe != NULL)
4148             my_current_file = g_strndup (fe->fname->str, fe->fname->len);
4149         current_file = my_current_file;
4150     }
4151 
4152     if (panel->is_panelized)
4153         reload_panelized (panel);
4154     else
4155         panel_reload (panel);
4156 
4157     panel_set_current_by_name (panel, current_file);
4158     panel->dirty = TRUE;
4159 
4160     if (free_pointer)
4161         g_free (my_current_file);
4162 }
4163 
4164 /* --------------------------------------------------------------------------------------------- */
4165 
4166 static void
4167 update_one_panel (int which, panel_update_flags_t flags, const char *current_file)
     /* [previous][next][first][last][top][bottom][index][help]  */
4168 {
4169     if (get_panel_type (which) == view_listing)
4170     {
4171         WPanel *panel;
4172 
4173         panel = PANEL (get_panel_widget (which));
4174         if (panel->is_panelized)
4175             flags &= ~UP_RELOAD;
4176         update_one_panel_widget (panel, flags, current_file);
4177     }
4178 }
4179 
4180 /* --------------------------------------------------------------------------------------------- */
4181 
4182 /* event callback */
4183 static gboolean
4184 event_update_panels (const gchar *event_group_name, const gchar *event_name, gpointer init_data,
     /* [previous][next][first][last][top][bottom][index][help]  */
4185                      gpointer data)
4186 {
4187     (void) event_group_name;
4188     (void) event_name;
4189     (void) init_data;
4190     (void) data;
4191 
4192     update_panels (UP_RELOAD, UP_KEEPSEL);
4193 
4194     return TRUE;
4195 }
4196 
4197 /* --------------------------------------------------------------------------------------------- */
4198 
4199 /* event callback */
4200 static gboolean
4201 panel_save_current_file_to_clip_file (const gchar *event_group_name, const gchar *event_name,
     /* [previous][next][first][last][top][bottom][index][help]  */
4202                                       gpointer init_data, gpointer data)
4203 {
4204     (void) event_group_name;
4205     (void) event_name;
4206     (void) init_data;
4207     (void) data;
4208 
4209     if (current_panel->marked == 0)
4210     {
4211         const file_entry_t *fe;
4212 
4213         fe = panel_current_entry (current_panel);
4214         if (fe != NULL)
4215             mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_text_to_file",
4216                             (gpointer) fe->fname->str);
4217     }
4218     else
4219     {
4220         int i;
4221         gboolean first = TRUE;
4222         char *flist = NULL;
4223 
4224         for (i = 0; i < current_panel->dir.len; i++)
4225         {
4226             const file_entry_t *fe = &current_panel->dir.list[i];
4227 
4228             if (fe->f.marked != 0)
4229             {  // Skip the unmarked ones
4230                 if (first)
4231                 {
4232                     flist = g_strndup (fe->fname->str, fe->fname->len);
4233                     first = FALSE;
4234                 }
4235                 else
4236                 {
4237                     // Add empty lines after the file
4238                     char *tmp;
4239 
4240                     tmp = g_strconcat (flist, "\n", fe->fname->str, (char *) NULL);
4241                     g_free (flist);
4242                     flist = tmp;
4243                 }
4244             }
4245         }
4246 
4247         mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_text_to_file", (gpointer) flist);
4248         g_free (flist);
4249     }
4250     return TRUE;
4251 }
4252 
4253 /* --------------------------------------------------------------------------------------------- */
4254 
4255 static vfs_path_t *
4256 panel_recursive_cd_to_parent (const vfs_path_t *vpath)
     /* [previous][next][first][last][top][bottom][index][help]  */
4257 {
4258     vfs_path_t *cwd_vpath;
4259 
4260     cwd_vpath = vfs_path_clone (vpath);
4261 
4262     while (mc_chdir (cwd_vpath) < 0)
4263     {
4264         const char *panel_cwd_path;
4265         vfs_path_t *tmp_vpath;
4266 
4267         // check if path contains only '/'
4268         panel_cwd_path = vfs_path_as_str (cwd_vpath);
4269         if (panel_cwd_path != NULL && IS_PATH_SEP (panel_cwd_path[0]) && panel_cwd_path[1] == '\0')
4270         {
4271             vfs_path_free (cwd_vpath, TRUE);
4272             return NULL;
4273         }
4274 
4275         tmp_vpath = vfs_path_vtokens_get (cwd_vpath, 0, -1);
4276         vfs_path_free (cwd_vpath, TRUE);
4277         cwd_vpath =
4278             vfs_path_build_filename (PATH_SEP_STR, vfs_path_as_str (tmp_vpath), (char *) NULL);
4279         vfs_path_free (tmp_vpath, TRUE);
4280     }
4281 
4282     return cwd_vpath;
4283 }
4284 
4285 /* --------------------------------------------------------------------------------------------- */
4286 
4287 static void
4288 panel_dir_list_callback (dir_list_cb_state_t state, void *data)
     /* [previous][next][first][last][top][bottom][index][help]  */
4289 {
4290     static int count = 0;
4291 
4292     (void) data;
4293 
4294     switch (state)
4295     {
4296     case DIR_OPEN:
4297         count = 0;
4298         break;
4299 
4300     case DIR_READ:
4301         count++;
4302         if ((count & 15) == 0)
4303             rotate_dash (TRUE);
4304         break;
4305 
4306     case DIR_CLOSE:
4307         rotate_dash (FALSE);
4308         break;
4309 
4310     default:
4311         g_assert_not_reached ();
4312     }
4313 }
4314 
4315 /* --------------------------------------------------------------------------------------------- */
4316 /*** public functions ****************************************************************************/
4317 /* --------------------------------------------------------------------------------------------- */
4318 
4319 file_entry_t *
4320 panel_current_entry (const WPanel *panel)
     /* [previous][next][first][last][top][bottom][index][help]  */
4321 {
4322     file_entry_t *fe;
4323 
4324     if (panel->dir.len == 0 || panel->current < 0 || panel->current >= panel->dir.len)
4325         return NULL;
4326 
4327     fe = &(panel->dir.list[panel->current]);
4328 
4329     return fe->fname == NULL ? NULL : fe;
4330 }
4331 
4332 /* --------------------------------------------------------------------------------------------- */
4333 
4334 void
4335 panel_set_current_by_name (WPanel *panel, const char *name)
     /* [previous][next][first][last][top][bottom][index][help]  */
4336 {
4337     int i;
4338     char *subdir;
4339 
4340     if (panel->dir.len == 0)
4341     {
4342         panel_set_current (panel, -1);
4343         return;
4344     }
4345 
4346     if (name == NULL)
4347     {
4348         panel_set_current (panel, 0);
4349         return;
4350     }
4351 
4352     /* We only want the last component of the directory,
4353      * and from this only the name without suffix.
4354      * Cut prefix if the panel is not panelized */
4355     if (panel->is_panelized)
4356         subdir = vfs_strip_suffix_from_filename (name);
4357     else
4358         subdir = vfs_strip_suffix_from_filename (x_basename (name));
4359 
4360     /* Search that subdir or filename without prefix (if not panelized panel),
4361        make it current if found */
4362     for (i = 0; i < panel->dir.len; i++)
4363         if (strcmp (subdir, panel->dir.list[i].fname->str) == 0)
4364         {
4365             panel_set_current (panel, i);
4366             g_free (subdir);
4367             return;
4368         }
4369 
4370     // Make current near the filee that is missing
4371     if (panel->current >= panel->dir.len)
4372         panel_set_current (panel, panel->dir.len - 1);
4373     g_free (subdir);
4374 
4375     select_item (panel);
4376 }
4377 
4378 /* --------------------------------------------------------------------------------------------- */
4379 
4380 void
4381 panel_clean_dir (WPanel *panel)
     /* [previous][next][first][last][top][bottom][index][help]  */
4382 {
4383     panel->top = 0;
4384     panel->current = 0;
4385     panel->marked = 0;
4386     panel->dirs_marked = 0;
4387     panel->total = 0;
4388     panel->quick_search.active = FALSE;
4389     panel->is_panelized = FALSE;
4390     panel->dirty = TRUE;
4391     panel->content_shift = -1;
4392     panel->max_shift = -1;
4393 
4394     dir_list_free_list (&panel->dir);
4395 }
4396 
4397 /* --------------------------------------------------------------------------------------------- */
4398 /**
4399  * Set Up panel's current dir object
4400  *
4401  * @param panel panel object
4402  * @param path_str string contain path
4403  */
4404 
4405 void
4406 panel_set_cwd (WPanel *panel, const vfs_path_t *vpath)
     /* [previous][next][first][last][top][bottom][index][help]  */
4407 {
4408     if (vpath != panel->cwd_vpath)  // check if new vpath is not the panel->cwd_vpath object
4409     {
4410         vfs_path_free (panel->cwd_vpath, TRUE);
4411         panel->cwd_vpath = vfs_path_clone (vpath);
4412     }
4413 }
4414 
4415 /* --------------------------------------------------------------------------------------------- */
4416 /**
4417  * Set Up panel's last working dir object
4418  *
4419  * @param panel panel object
4420  * @param path_str string contain path
4421  */
4422 
4423 void
4424 panel_set_lwd (WPanel *panel, const vfs_path_t *vpath)
     /* [previous][next][first][last][top][bottom][index][help]  */
4425 {
4426     if (vpath != panel->lwd_vpath)  // check if new vpath is not the panel->lwd_vpath object
4427     {
4428         vfs_path_free (panel->lwd_vpath, TRUE);
4429         panel->lwd_vpath = vfs_path_clone (vpath);
4430     }
4431 }
4432 
4433 /* --------------------------------------------------------------------------------------------- */
4434 /**
4435  * Creatie an empty panel with specified size.
4436  *
4437  * @param panel_name name of panel for setup receiving
4438  * @param r panel areaa
4439  *
4440  * @return new instance of WPanel
4441  */
4442 
4443 WPanel *
4444 panel_sized_empty_new (const char *panel_name, const WRect *r)
     /* [previous][next][first][last][top][bottom][index][help]  */
4445 {
4446     WPanel *panel;
4447     Widget *w;
4448     char *section;
4449     int i, err;
4450 
4451     panel = g_new0 (WPanel, 1);
4452     w = WIDGET (panel);
4453     widget_init (w, r, panel_callback, panel_mouse_callback);
4454     w->options |= WOP_SELECTABLE | WOP_TOP_SELECT;
4455     w->keymap = panel_map;
4456 
4457     panel->dir.size = DIR_LIST_MIN_SIZE;
4458     panel->dir.list = g_new (file_entry_t, panel->dir.size);
4459     panel->dir.len = 0;
4460     panel->dir.callback = panel_dir_list_callback;
4461 
4462     panel->list_cols = 1;
4463     panel->brief_cols = 2;
4464     panel->dirty = TRUE;
4465     panel->content_shift = -1;
4466     panel->max_shift = -1;
4467 
4468     panel->list_format = list_full;
4469     panel->user_format = g_strdup (DEFAULT_USER_FORMAT);
4470 
4471     panel->filter.flags = FILE_FILTER_DEFAULT_FLAGS;
4472 
4473     for (i = 0; i < LIST_FORMATS; i++)
4474         panel->user_status_format[i] = g_strdup (DEFAULT_USER_FORMAT);
4475 
4476 #ifdef HAVE_CHARSET
4477     panel->codepage = SELECT_CHARSET_NO_TRANSLATE;
4478 #endif
4479 
4480     panel->frame_size = frame_half;
4481 
4482     panel->quick_search.buffer = g_string_sized_new (MC_MAXFILENAMELEN);
4483     panel->quick_search.prev_buffer = g_string_sized_new (MC_MAXFILENAMELEN);
4484 
4485     panel->name = g_strdup (panel_name);
4486     panel->dir_history.name = g_strconcat ("Dir Hist ", panel->name, (char *) NULL);
4487     // directories history will be get later
4488 
4489     section = g_strconcat ("Temporal:", panel->name, (char *) NULL);
4490     if (!mc_config_has_group (mc_global.main_config, section))
4491     {
4492         g_free (section);
4493         section = g_strdup (panel->name);
4494     }
4495     panel_load_setup (panel, section);
4496     g_free (section);
4497 
4498     if (panel->filter.value != NULL)
4499     {
4500         gboolean case_sens = (panel->filter.flags & SELECT_MATCH_CASE) != 0;
4501         gboolean shell_patterns = (panel->filter.flags & SELECT_SHELL_PATTERNS) != 0;
4502 
4503         panel->filter.handler = mc_search_new (panel->filter.value, NULL);
4504         panel->filter.handler->search_type = shell_patterns ? MC_SEARCH_T_GLOB : MC_SEARCH_T_REGEX;
4505         panel->filter.handler->is_entire_line = TRUE;
4506         panel->filter.handler->is_case_sensitive = case_sens;
4507 
4508         // FIXME: silent check -- do not display an error message
4509         if (!mc_search_prepare (panel->filter.handler))
4510             file_filter_clear (&panel->filter);
4511     }
4512 
4513     // Load format strings
4514     err = set_panel_formats (panel);
4515     if (err != 0)
4516         set_panel_formats (panel);
4517 
4518     return panel;
4519 }
4520 
4521 /* --------------------------------------------------------------------------------------------- */
4522 /**
4523  * Panel creation for specified size and directory.
4524  *
4525  * @param panel_name name of panel for setup retrieving
4526  * @param r panel areaa
4527  * @param vpath working panel directory. If NULL then current directory is used
4528  *
4529  * @return new instance of WPanel
4530  */
4531 
4532 WPanel *
4533 panel_sized_with_dir_new (const char *panel_name, const WRect *r, const vfs_path_t *vpath)
     /* [previous][next][first][last][top][bottom][index][help]  */
4534 {
4535     WPanel *panel;
4536     char *curdir = NULL;
4537 #ifdef HAVE_CHARSET
4538     const vfs_path_element_t *path_element;
4539 #endif
4540 
4541     panel = panel_sized_empty_new (panel_name, r);
4542 
4543     if (vpath != NULL)
4544     {
4545         curdir = vfs_get_cwd ();
4546         panel_set_cwd (panel, vpath);
4547     }
4548     else
4549     {
4550         vfs_setup_cwd ();
4551         panel->cwd_vpath = vfs_path_clone (vfs_get_raw_current_dir ());
4552     }
4553 
4554     panel_set_lwd (panel, vfs_get_raw_current_dir ());
4555 
4556 #ifdef HAVE_CHARSET
4557     path_element = vfs_path_get_by_index (panel->cwd_vpath, -1);
4558     if (path_element->encoding != NULL)
4559         panel->codepage = get_codepage_index (path_element->encoding);
4560 #endif
4561 
4562     if (mc_chdir (panel->cwd_vpath) != 0)
4563     {
4564 #ifdef HAVE_CHARSET
4565         panel->codepage = SELECT_CHARSET_NO_TRANSLATE;
4566 #endif
4567         vfs_setup_cwd ();
4568         vfs_path_free (panel->cwd_vpath, TRUE);
4569         panel->cwd_vpath = vfs_path_clone (vfs_get_raw_current_dir ());
4570     }
4571 
4572     // Load the default format
4573     if (!dir_list_load (&panel->dir, panel->cwd_vpath, panel->sort_field->sort_routine,
4574                         &panel->sort_info, &panel->filter))
4575         message (D_ERROR, MSG_ERROR, _ ("Cannot read directory contents"));
4576 
4577     if (panel->dir.len == 0)
4578         panel_set_current (panel, -1);
4579 
4580     // Restore old right path
4581     if (curdir != NULL)
4582     {
4583         vfs_path_t *tmp_vpath;
4584         int err;
4585 
4586         tmp_vpath = vfs_path_from_str (curdir);
4587         mc_chdir (tmp_vpath);
4588         vfs_path_free (tmp_vpath, TRUE);
4589         (void) err;
4590     }
4591     g_free (curdir);
4592 
4593     return panel;
4594 }
4595 
4596 /* --------------------------------------------------------------------------------------------- */
4597 
4598 void
4599 panel_reload (WPanel *panel)
     /* [previous][next][first][last][top][bottom][index][help]  */
4600 {
4601     struct stat current_stat;
4602     vfs_path_t *cwd_vpath;
4603 
4604     if (panels_options.fast_reload && stat (vfs_path_as_str (panel->cwd_vpath), &current_stat) == 0
4605         && current_stat.st_ctime == panel->dir_stat.st_ctime
4606         && current_stat.st_mtime == panel->dir_stat.st_mtime)
4607         return;
4608 
4609     cwd_vpath = panel_recursive_cd_to_parent (panel->cwd_vpath);
4610     vfs_path_free (panel->cwd_vpath, TRUE);
4611 
4612     if (cwd_vpath == NULL)
4613     {
4614         panel->cwd_vpath = vfs_path_from_str (PATH_SEP_STR);
4615         panel_clean_dir (panel);
4616         dir_list_init (&panel->dir);
4617         return;
4618     }
4619 
4620     panel->cwd_vpath = cwd_vpath;
4621     memset (&(panel->dir_stat), 0, sizeof (panel->dir_stat));
4622     show_dir (panel);
4623 
4624     if (!dir_list_reload (&panel->dir, panel->cwd_vpath, panel->sort_field->sort_routine,
4625                           &panel->sort_info, &panel->filter))
4626         message (D_ERROR, MSG_ERROR, _ ("Cannot read directory contents"));
4627 
4628     panel->dirty = TRUE;
4629 
4630     if (panel->dir.len == 0)
4631         panel_set_current (panel, -1);
4632     else if (panel->current >= panel->dir.len)
4633         panel_set_current (panel, panel->dir.len - 1);
4634 
4635     recalculate_panel_summary (panel);
4636 }
4637 
4638 /* --------------------------------------------------------------------------------------------- */
4639 /* Switches the panel to the mode specified in the format           */
4640 /* Setting up both format and status string. Return: 0 - on success; */
4641 /* 1 - format error; 2 - status error; 3 - errors in both formats.  */
4642 
4643 int
4644 set_panel_formats (WPanel *p)
     /* [previous][next][first][last][top][bottom][index][help]  */
4645 {
4646     GSList *form;
4647     char *err = NULL;
4648     int retcode = 0;
4649 
4650     form = use_display_format (p, panel_format (p), &err, FALSE);
4651 
4652     if (err != NULL)
4653     {
4654         g_free (err);
4655         retcode = 1;
4656     }
4657     else
4658     {
4659         g_slist_free_full (p->format, (GDestroyNotify) format_item_free);
4660         p->format = form;
4661     }
4662 
4663     if (panels_options.show_mini_info)
4664     {
4665         form = use_display_format (p, mini_status_format (p), &err, TRUE);
4666 
4667         if (err != NULL)
4668         {
4669             g_free (err);
4670             retcode += 2;
4671         }
4672         else
4673         {
4674             g_slist_free_full (p->status_format, (GDestroyNotify) format_item_free);
4675             p->status_format = form;
4676         }
4677     }
4678 
4679     panel_update_cols (WIDGET (p), p->frame_size);
4680 
4681     if (retcode != 0)
4682         message (D_ERROR, _ ("Warning"),
4683                  _ ("User supplied format looks invalid, reverting to default."));
4684     if ((retcode & 0x01) != 0)
4685     {
4686         g_free (p->user_format);
4687         p->user_format = g_strdup (DEFAULT_USER_FORMAT);
4688     }
4689     if ((retcode & 0x02) != 0)
4690     {
4691         g_free (p->user_status_format[p->list_format]);
4692         p->user_status_format[p->list_format] = g_strdup (DEFAULT_USER_FORMAT);
4693     }
4694 
4695     return retcode;
4696 }
4697 
4698 /* --------------------------------------------------------------------------------------------- */
4699 
4700 void
4701 panel_set_filter (WPanel *panel, const file_filter_t *filter)
     /* [previous][next][first][last][top][bottom][index][help]  */
4702 {
4703     MC_PTR_FREE (panel->filter.value);
4704     mc_search_free (panel->filter.handler);
4705     panel->filter.handler = NULL;
4706 
4707     // NULL to clear filter
4708     if (filter != NULL)
4709         panel->filter = *filter;
4710 
4711     reread_cmd ();
4712 }
4713 
4714 /* --------------------------------------------------------------------------------------------- */
4715 
4716 /* Select current item and readjust the panel */
4717 void
4718 select_item (WPanel *panel)
     /* [previous][next][first][last][top][bottom][index][help]  */
4719 {
4720     adjust_top_file (panel);
4721 
4722     panel->dirty = TRUE;
4723 
4724     execute_hooks (select_file_hook);
4725 }
4726 
4727 /* --------------------------------------------------------------------------------------------- */
4728 /** Clears all files in the panel, used only when one file was marked */
4729 void
4730 unmark_files (WPanel *panel)
     /* [previous][next][first][last][top][bottom][index][help]  */
4731 {
4732     if (panel->marked != 0)
4733     {
4734         int i;
4735 
4736         for (i = 0; i < panel->dir.len; i++)
4737             file_mark (panel, i, 0);
4738 
4739         panel->dirs_marked = 0;
4740         panel->marked = 0;
4741         panel->total = 0;
4742     }
4743 }
4744 
4745 /* --------------------------------------------------------------------------------------------- */
4746 /** Recalculate the panels summary information, used e.g. when marked
4747    files might have been removed by an external command */
4748 
4749 void
4750 recalculate_panel_summary (WPanel *panel)
     /* [previous][next][first][last][top][bottom][index][help]  */
4751 {
4752     int i;
4753 
4754     panel->marked = 0;
4755     panel->dirs_marked = 0;
4756     panel->total = 0;
4757 
4758     for (i = 0; i < panel->dir.len; i++)
4759         if (panel->dir.list[i].f.marked != 0)
4760         {
4761             /* do_file_mark will return immediately if newmark == oldmark.
4762                So we have to first unmark it to get panel's summary information
4763                updated. (Norbert) */
4764             panel->dir.list[i].f.marked = 0;
4765             do_file_mark (panel, i, 1);
4766         }
4767 }
4768 
4769 /* --------------------------------------------------------------------------------------------- */
4770 /** This routine marks a file or a directory */
4771 
4772 void
4773 do_file_mark (WPanel *panel, int idx, int mark)
     /* [previous][next][first][last][top][bottom][index][help]  */
4774 {
4775     if (panel->dir.list[idx].f.marked == mark)
4776         return;
4777 
4778     // Only '..' can't be marked, '.' isn't visible
4779     if (DIR_IS_DOTDOT (panel->dir.list[idx].fname->str))
4780         return;
4781 
4782     file_mark (panel, idx, mark);
4783     if (panel->dir.list[idx].f.marked != 0)
4784     {
4785         panel->marked++;
4786 
4787         if (S_ISDIR (panel->dir.list[idx].st.st_mode))
4788         {
4789             if (panel->dir.list[idx].f.dir_size_computed != 0)
4790                 panel->total += (uintmax_t) panel->dir.list[idx].st.st_size;
4791             panel->dirs_marked++;
4792         }
4793         else
4794             panel->total += (uintmax_t) panel->dir.list[idx].st.st_size;
4795 
4796         set_colors (panel);
4797     }
4798     else
4799     {
4800         if (S_ISDIR (panel->dir.list[idx].st.st_mode))
4801         {
4802             if (panel->dir.list[idx].f.dir_size_computed != 0)
4803                 panel->total -= (uintmax_t) panel->dir.list[idx].st.st_size;
4804             panel->dirs_marked--;
4805         }
4806         else
4807             panel->total -= (uintmax_t) panel->dir.list[idx].st.st_size;
4808 
4809         panel->marked--;
4810     }
4811 }
4812 
4813 /* --------------------------------------------------------------------------------------------- */
4814 /**
4815  * Changes the current directory of the panel.
4816  * Record change in the directory history.
4817  */
4818 gboolean
4819 panel_do_cd (WPanel *panel, const vfs_path_t *new_dir_vpath, enum cd_enum cd_type)
     /* [previous][next][first][last][top][bottom][index][help]  */
4820 {
4821     gboolean r;
4822 
4823     r = panel_do_cd_int (panel, new_dir_vpath, cd_type);
4824     if (r)
4825         directory_history_add (panel, panel->cwd_vpath);
4826     return r;
4827 }
4828 
4829 /* --------------------------------------------------------------------------------------------- */
4830 
4831 void
4832 file_mark (WPanel *panel, int lc_index, int val)
     /* [previous][next][first][last][top][bottom][index][help]  */
4833 {
4834     if (panel->dir.list[lc_index].f.marked != val)
4835     {
4836         panel->dir.list[lc_index].f.marked = val;
4837         panel->dirty = TRUE;
4838     }
4839 }
4840 
4841 /* --------------------------------------------------------------------------------------------- */
4842 /**
4843  * Find marked file starting from the given position.
4844  *
4845  * @param panel WPanel object
4846  * @param curent_file a staring position to get current or search next marked file
4847  *
4848  * @return pointer to the name of find file or NULL if no file found or @current_file is out of
4849  * range
4850  */
4851 
4852 const GString *
4853 panel_find_marked_file (const WPanel *panel, int *current_file)
     /* [previous][next][first][last][top][bottom][index][help]  */
4854 {
4855     while (panel->dir.list[*current_file].f.marked == 0 && *current_file < panel->dir.len)
4856         (*current_file)++;
4857 
4858     return (*current_file >= panel->dir.len ? NULL : panel->dir.list[*current_file].fname);
4859 }
4860 
4861 /* --------------------------------------------------------------------------------------------- */
4862 /**
4863  * Get marked file clsest to the given position.
4864  *
4865  * @param panel WPanel object
4866  * @param curent_file a staring position to get current or closest next marked file. If there are
4867  *                    no marked files in @panel, @panel->current is used.
4868  *
4869  * @return pointer to the name of find file or NULL if no file found or @current_file is out of
4870  * range.
4871  */
4872 
4873 const GString *
4874 panel_get_marked_file (const WPanel *panel, int *current_file)
     /* [previous][next][first][last][top][bottom][index][help]  */
4875 {
4876     const file_entry_t *fe;
4877 
4878     if (panel->marked != 0)
4879         return panel_find_marked_file (panel, current_file);
4880 
4881     fe = panel_current_entry (panel);
4882 
4883     return (fe == NULL ? NULL : fe->fname);
4884 }
4885 
4886 /* --------------------------------------------------------------------------------------------- */
4887 
4888 void
4889 panel_re_sort (WPanel *panel)
     /* [previous][next][first][last][top][bottom][index][help]  */
4890 {
4891     char *filename;
4892     const file_entry_t *fe;
4893     int i;
4894 
4895     if (panel == NULL)
4896         return;
4897 
4898     fe = panel_current_entry (panel);
4899     if (fe == NULL)
4900         return;
4901 
4902     filename = g_strndup (fe->fname->str, fe->fname->len);
4903     unselect_item (panel);
4904     dir_list_sort (&panel->dir, panel->sort_field->sort_routine, &panel->sort_info);
4905     panel->current = -1;
4906 
4907     for (i = panel->dir.len; i != 0; i--)
4908         if (strcmp (panel->dir.list[i - 1].fname->str, filename) == 0)
4909         {
4910             panel->current = i - 1;
4911             break;
4912         }
4913 
4914     g_free (filename);
4915     panel->top = panel->current - panel_items (panel) / 2;
4916     select_item (panel);
4917     panel->dirty = TRUE;
4918 }
4919 
4920 /* --------------------------------------------------------------------------------------------- */
4921 
4922 void
4923 panel_set_sort_order (WPanel *panel, const panel_field_t *sort_order)
     /* [previous][next][first][last][top][bottom][index][help]  */
4924 {
4925     if (sort_order == NULL)
4926         return;
4927 
4928     panel->sort_field = sort_order;
4929 
4930     // The directory is already sorted, we have to load the unsorted stuff
4931     if (sort_order->sort_routine == (GCompareFunc) unsorted)
4932     {
4933         const file_entry_t *fe;
4934         char *current_file = NULL;
4935 
4936         fe = panel_current_entry (panel);
4937         if (fe != NULL)
4938             current_file = g_strndup (fe->fname->str, fe->fname->len);
4939         panel_reload (panel);
4940         panel_set_current_by_name (panel, current_file);
4941         g_free (current_file);
4942     }
4943     panel_re_sort (panel);
4944 }
4945 
4946 /* --------------------------------------------------------------------------------------------- */
4947 
4948 #ifdef HAVE_CHARSET
4949 
4950 /**
4951  * Change panel encoding.
4952  * @param panel WPanel object
4953  */
4954 
4955 void
4956 panel_change_encoding (WPanel *panel)
     /* [previous][next][first][last][top][bottom][index][help]  */
4957 {
4958     const char *encoding = NULL;
4959     char *errmsg;
4960     int r;
4961 
4962     r = select_charset (-1, -1, panel->codepage, FALSE);
4963 
4964     if (r == SELECT_CHARSET_CANCEL)
4965         return;  // Cancel
4966 
4967     panel->codepage = r;
4968 
4969     if (panel->codepage == SELECT_CHARSET_NO_TRANSLATE)
4970     {
4971         // No translation
4972         vfs_path_t *cd_path_vpath;
4973 
4974         g_free (init_translation_table (mc_global.display_codepage, mc_global.display_codepage));
4975         cd_path_vpath = remove_encoding_from_path (panel->cwd_vpath);
4976         panel_do_cd (panel, cd_path_vpath, cd_parse_command);
4977         show_dir (panel);
4978         vfs_path_free (cd_path_vpath, TRUE);
4979         return;
4980     }
4981 
4982     errmsg = init_translation_table (panel->codepage, mc_global.display_codepage);
4983     if (errmsg != NULL)
4984     {
4985         message (D_ERROR, MSG_ERROR, "%s", errmsg);
4986         g_free (errmsg);
4987         return;
4988     }
4989 
4990     encoding = get_codepage_id (panel->codepage);
4991     if (encoding != NULL)
4992     {
4993         vfs_path_change_encoding (panel->cwd_vpath, encoding);
4994 
4995         if (!panel_do_cd (panel, panel->cwd_vpath, cd_parse_command))
4996             cd_error_message (vfs_path_as_str (panel->cwd_vpath));
4997     }
4998 }
4999 
5000 /* --------------------------------------------------------------------------------------------- */
5001 
5002 /**
5003  * Remove encode info from last path element.
5004  *
5005  */
5006 vfs_path_t *
5007 remove_encoding_from_path (const vfs_path_t *vpath)
     /* [previous][next][first][last][top][bottom][index][help]  */
5008 {
5009     vfs_path_t *ret_vpath;
5010     GString *tmp_conv;
5011     int indx;
5012 
5013     ret_vpath = vfs_path_new (FALSE);
5014 
5015     tmp_conv = g_string_new ("");
5016 
5017     for (indx = 0; indx < vfs_path_elements_count (vpath); indx++)
5018     {
5019         GIConv converter;
5020         vfs_path_element_t *path_element;
5021 
5022         path_element = vfs_path_element_clone (vfs_path_get_by_index (vpath, indx));
5023 
5024         if (path_element->encoding == NULL)
5025         {
5026             vfs_path_add_element (ret_vpath, path_element);
5027             continue;
5028         }
5029 
5030         converter = str_crt_conv_to (path_element->encoding);
5031         if (converter == INVALID_CONV)
5032         {
5033             vfs_path_add_element (ret_vpath, path_element);
5034             continue;
5035         }
5036 
5037         MC_PTR_FREE (path_element->encoding);
5038 
5039         str_vfs_convert_from (converter, path_element->path, tmp_conv);
5040 
5041         g_free (path_element->path);
5042         path_element->path = g_strndup (tmp_conv->str, tmp_conv->len);
5043 
5044         g_string_set_size (tmp_conv, 0);
5045 
5046         str_close_conv (converter);
5047         str_close_conv (path_element->dir.converter);
5048         path_element->dir.converter = INVALID_CONV;
5049         vfs_path_add_element (ret_vpath, path_element);
5050     }
5051     g_string_free (tmp_conv, TRUE);
5052     return ret_vpath;
5053 }
5054 #endif
5055 
5056 /* --------------------------------------------------------------------------------------------- */
5057 
5058 /**
5059  * This routine reloads the directory in both panels. It tries to
5060  * select current_file in current_panel and other_file in other_panel.
5061  * If current_file == -1 then it automatically sets current_file and
5062  * other_file to the current files in the panels.
5063  *
5064  * If flags has the UP_ONLY_CURRENT bit toggled on, then it
5065  * will not reload the other panel.
5066  *
5067  * @param flags for reload panel
5068  * @param current_file name of the current file
5069  */
5070 
5071 void
5072 update_panels (panel_update_flags_t flags, const char *current_file)
     /* [previous][next][first][last][top][bottom][index][help]  */
5073 {
5074     WPanel *panel;
5075 
5076     // first, update other panel...
5077     if ((flags & UP_ONLY_CURRENT) == 0)
5078         update_one_panel (get_other_index (), flags, UP_KEEPSEL);
5079     // ...then current one
5080     update_one_panel (get_current_index (), flags, current_file);
5081 
5082     if (get_current_type () == view_listing)
5083         panel = PANEL (get_panel_widget (get_current_index ()));
5084     else
5085         panel = PANEL (get_panel_widget (get_other_index ()));
5086 
5087     if (!panel->is_panelized)
5088         (void) mc_chdir (panel->cwd_vpath);
5089 }
5090 
5091 /* --------------------------------------------------------------------------------------------- */
5092 
5093 gsize
5094 panel_get_num_of_sortable_fields (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
5095 {
5096     gsize ret = 0, lc_index;
5097 
5098     for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
5099         if (panel_fields[lc_index].is_user_choice)
5100             ret++;
5101     return ret;
5102 }
5103 
5104 /* --------------------------------------------------------------------------------------------- */
5105 
5106 char **
5107 panel_get_sortable_fields (gsize *array_size)
     /* [previous][next][first][last][top][bottom][index][help]  */
5108 {
5109     char **ret;
5110     gsize lc_index, i;
5111 
5112     lc_index = panel_get_num_of_sortable_fields ();
5113 
5114     ret = g_try_new0 (char *, lc_index + 1);
5115     if (ret == NULL)
5116         return NULL;
5117 
5118     if (array_size != NULL)
5119         *array_size = lc_index;
5120 
5121     lc_index = 0;
5122 
5123     for (i = 0; panel_fields[i].id != NULL; i++)
5124         if (panel_fields[i].is_user_choice)
5125             ret[lc_index++] = g_strdup (_ (panel_fields[i].title_hotkey));
5126 
5127     return ret;
5128 }
5129 
5130 /* --------------------------------------------------------------------------------------------- */
5131 
5132 const panel_field_t *
5133 panel_get_field_by_id (const char *name)
     /* [previous][next][first][last][top][bottom][index][help]  */
5134 {
5135     gsize lc_index;
5136 
5137     for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
5138         if (panel_fields[lc_index].id != NULL && strcmp (name, panel_fields[lc_index].id) == 0)
5139             return &panel_fields[lc_index];
5140 
5141     return NULL;
5142 }
5143 
5144 /* --------------------------------------------------------------------------------------------- */
5145 
5146 const panel_field_t *
5147 panel_get_field_by_title_hotkey (const char *name)
     /* [previous][next][first][last][top][bottom][index][help]  */
5148 {
5149     gsize lc_index;
5150 
5151     for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
5152         if (panel_fields[lc_index].title_hotkey != NULL
5153             && strcmp (name, _ (panel_fields[lc_index].title_hotkey)) == 0)
5154             return &panel_fields[lc_index];
5155 
5156     return NULL;
5157 }
5158 
5159 /* --------------------------------------------------------------------------------------------- */
5160 
5161 const panel_field_t *
5162 panel_get_field_by_title (const char *name)
     /* [previous][next][first][last][top][bottom][index][help]  */
5163 {
5164     gsize lc_index;
5165 
5166     for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
5167     {
5168         const char *title;
5169 
5170         title = panel_get_title_without_hotkey (panel_fields[lc_index].title_hotkey);
5171         if (strcmp (title, name) == 0)
5172             return &panel_fields[lc_index];
5173     }
5174 
5175     return NULL;
5176 }
5177 
5178 /* --------------------------------------------------------------------------------------------- */
5179 
5180 gsize
5181 panel_get_num_of_user_possible_fields (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
5182 {
5183     gsize ret = 0, lc_index;
5184 
5185     for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
5186         if (panel_fields[lc_index].use_in_user_format)
5187             ret++;
5188 
5189     return ret;
5190 }
5191 
5192 /* --------------------------------------------------------------------------------------------- */
5193 
5194 char **
5195 panel_get_user_possible_fields (gsize *array_size)
     /* [previous][next][first][last][top][bottom][index][help]  */
5196 {
5197     char **ret;
5198     gsize lc_index, i;
5199 
5200     lc_index = panel_get_num_of_user_possible_fields ();
5201 
5202     ret = g_try_new0 (char *, lc_index + 1);
5203     if (ret == NULL)
5204         return NULL;
5205 
5206     if (array_size != NULL)
5207         *array_size = lc_index;
5208 
5209     lc_index = 0;
5210 
5211     for (i = 0; panel_fields[i].id != NULL; i++)
5212         if (panel_fields[i].use_in_user_format)
5213             ret[lc_index++] = g_strdup (_ (panel_fields[i].title_hotkey));
5214 
5215     return ret;
5216 }
5217 
5218 /* --------------------------------------------------------------------------------------------- */
5219 
5220 void
5221 panel_panelize_cd (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
5222 {
5223     WPanel *panel;
5224     int i;
5225     dir_list *list;
5226     panelized_descr_t *pdescr;
5227     dir_list *plist;
5228     gboolean panelized_same;
5229 
5230     if (!SELECTED_IS_PANEL)
5231         create_panel (MENU_PANEL_IDX, view_listing);
5232 
5233     panel = PANEL (get_panel_widget (MENU_PANEL_IDX));
5234 
5235     dir_list_clean (&panel->dir);
5236 
5237     if (panel->panelized_descr == NULL)
5238         panel->panelized_descr = panelized_descr_new ();
5239 
5240     pdescr = panel->panelized_descr;
5241     plist = &pdescr->list;
5242 
5243     if (pdescr->root_vpath == NULL)
5244         panel_panelize_change_root (panel, panel->cwd_vpath);
5245 
5246     if (plist->len < 1)
5247         dir_list_init (plist);
5248     else if (plist->len > panel->dir.size)
5249         dir_list_grow (&panel->dir, plist->len - panel->dir.size);
5250 
5251     list = &panel->dir;
5252     list->len = plist->len;
5253 
5254     panelized_same = vfs_path_equal (pdescr->root_vpath, panel->cwd_vpath);
5255 
5256     for (i = 0; i < plist->len; i++)
5257     {
5258         if (panelized_same || DIR_IS_DOTDOT (plist->list[i].fname->str))
5259             list->list[i].fname = mc_g_string_dup (plist->list[i].fname);
5260         else
5261         {
5262             vfs_path_t *tmp_vpath;
5263 
5264             tmp_vpath =
5265                 vfs_path_append_new (pdescr->root_vpath, plist->list[i].fname->str, (char *) NULL);
5266             list->list[i].fname = g_string_new_take (vfs_path_free (tmp_vpath, FALSE));
5267         }
5268         list->list[i].f.link_to_dir = plist->list[i].f.link_to_dir;
5269         list->list[i].f.stale_link = plist->list[i].f.stale_link;
5270         list->list[i].f.dir_size_computed = plist->list[i].f.dir_size_computed;
5271         list->list[i].f.marked = plist->list[i].f.marked;
5272         list->list[i].st = plist->list[i].st;
5273         list->list[i].name_sort_key = plist->list[i].name_sort_key;
5274         list->list[i].extension_sort_key = plist->list[i].extension_sort_key;
5275     }
5276 
5277     panel->is_panelized = TRUE;
5278     panel_panelize_absolutize_if_needed (panel);
5279 
5280     panel_set_current_by_name (panel, NULL);
5281 }
5282 
5283 /* --------------------------------------------------------------------------------------------- */
5284 
5285 /**
5286  * Change root directory of panelized content.
5287  * @param panel file panel
5288  * @param new_root new path
5289  */
5290 void
5291 panel_panelize_change_root (WPanel *panel, const vfs_path_t *new_root)
     /* [previous][next][first][last][top][bottom][index][help]  */
5292 {
5293     if (panel->panelized_descr == NULL)
5294         panel->panelized_descr = panelized_descr_new ();
5295     else
5296         vfs_path_free (panel->panelized_descr->root_vpath, TRUE);
5297 
5298     panel->panelized_descr->root_vpath = vfs_path_clone (new_root);
5299 }
5300 
5301 /* --------------------------------------------------------------------------------------------- */
5302 
5303 /**
5304  * Conditionally switches a panel's directory to "/" (root).
5305  *
5306  * If a panelized panel's listing contain absolute paths, this function
5307  * sets the panel's directory to "/". Otherwise it does nothing.
5308  *
5309  * Rationale:
5310  *
5311  * This makes tokenized strings like "%d/%p" work. This also makes other
5312  * places work where such naive concatenation is done in code (e.g., when
5313  * pressing ctrl+shift+enter, for CK_PutCurrentFullSelected).
5314  *
5315  * When to call:
5316  *
5317  * You should always call this function after you populate the listing
5318  * of a panelized panel.
5319  */
5320 void
5321 panel_panelize_absolutize_if_needed (WPanel *panel)
     /* [previous][next][first][last][top][bottom][index][help]  */
5322 {
5323     const dir_list *const list = &panel->dir;
5324 
5325     /* Note: We don't support mixing of absolute and relative paths, which is
5326      * why it's ok for us to check only the 1st entry. */
5327     if (list->len > 1 && g_path_is_absolute (list->list[1].fname->str))
5328     {
5329         vfs_path_t *root;
5330 
5331         root = vfs_path_from_str (PATH_SEP_STR);
5332         panel_set_cwd (panel, root);
5333         if (panel == current_panel)
5334             mc_chdir (root);
5335         vfs_path_free (root, TRUE);
5336     }
5337 }
5338 
5339 /* --------------------------------------------------------------------------------------------- */
5340 
5341 void
5342 panel_panelize_save (WPanel *panel)
     /* [previous][next][first][last][top][bottom][index][help]  */
5343 {
5344     int i;
5345     dir_list *list = &panel->dir;
5346     dir_list *plist;
5347 
5348     panel_panelize_change_root (panel, panel->cwd_vpath);
5349 
5350     plist = &panel->panelized_descr->list;
5351 
5352     if (plist->len > 0)
5353         dir_list_clean (plist);
5354     if (panel->dir.len == 0)
5355         return;
5356 
5357     if (panel->dir.len > plist->size)
5358         dir_list_grow (plist, panel->dir.len - plist->size);
5359     plist->len = panel->dir.len;
5360 
5361     for (i = 0; i < panel->dir.len; i++)
5362     {
5363         plist->list[i].fname = mc_g_string_dup (list->list[i].fname);
5364         plist->list[i].f.link_to_dir = list->list[i].f.link_to_dir;
5365         plist->list[i].f.stale_link = list->list[i].f.stale_link;
5366         plist->list[i].f.dir_size_computed = list->list[i].f.dir_size_computed;
5367         plist->list[i].f.marked = list->list[i].f.marked;
5368         plist->list[i].st = list->list[i].st;
5369         plist->list[i].name_sort_key = list->list[i].name_sort_key;
5370         plist->list[i].extension_sort_key = list->list[i].extension_sort_key;
5371     }
5372 }
5373 
5374 /* --------------------------------------------------------------------------------------------- */
5375 
5376 void
5377 panel_init (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
5378 {
5379     panel_sort_up_char = mc_skin_get ("widget-panel", "sort-up-char", "'");
5380     panel_sort_down_char = mc_skin_get ("widget-panel", "sort-down-char", ".");
5381     panel_hiddenfiles_show_char = mc_skin_get ("widget-panel", "hiddenfiles-show-char", ".");
5382     panel_hiddenfiles_hide_char = mc_skin_get ("widget-panel", "hiddenfiles-hide-char", ".");
5383     panel_history_prev_item_char = mc_skin_get ("widget-panel", "history-prev-item-char", "<");
5384     panel_history_next_item_char = mc_skin_get ("widget-panel", "history-next-item-char", ">");
5385     panel_history_show_list_char = mc_skin_get ("widget-panel", "history-show-list-char", "^");
5386     panel_filename_scroll_left_char =
5387         mc_skin_get ("widget-panel", "filename-scroll-left-char", "{");
5388     panel_filename_scroll_right_char =
5389         mc_skin_get ("widget-panel", "filename-scroll-right-char", "}");
5390 
5391     string_file_name_buffer = g_string_sized_new (MC_MAXFILENAMELEN);
5392 
5393     mc_event_add (MCEVENT_GROUP_FILEMANAGER, "update_panels", event_update_panels, NULL, NULL);
5394     mc_event_add (MCEVENT_GROUP_FILEMANAGER, "panel_save_current_file_to_clip_file",
5395                   panel_save_current_file_to_clip_file, NULL, NULL);
5396 }
5397 
5398 /* --------------------------------------------------------------------------------------------- */
5399 
5400 void
5401 panel_deinit (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
5402 {
5403     g_free (panel_sort_up_char);
5404     g_free (panel_sort_down_char);
5405     g_free (panel_hiddenfiles_show_char);
5406     g_free (panel_hiddenfiles_hide_char);
5407     g_free (panel_history_prev_item_char);
5408     g_free (panel_history_next_item_char);
5409     g_free (panel_history_show_list_char);
5410     g_free (panel_filename_scroll_left_char);
5411     g_free (panel_filename_scroll_right_char);
5412     g_string_free (string_file_name_buffer, TRUE);
5413 }
5414 
5415 /* --------------------------------------------------------------------------------------------- */
5416 
5417 gboolean
5418 panel_cd (WPanel *panel, const vfs_path_t *new_dir_vpath, enum cd_enum exact)
     /* [previous][next][first][last][top][bottom][index][help]  */
5419 {
5420     gboolean res;
5421     const vfs_path_t *_new_dir_vpath = new_dir_vpath;
5422 
5423     if (panel->is_panelized)
5424     {
5425         size_t new_vpath_len;
5426 
5427         new_vpath_len = vfs_path_len (new_dir_vpath);
5428         if (vfs_path_equal_len (new_dir_vpath, panel->panelized_descr->root_vpath, new_vpath_len))
5429             _new_dir_vpath = panel->panelized_descr->root_vpath;
5430     }
5431 
5432     res = panel_do_cd (panel, _new_dir_vpath, exact);
5433 
5434 #ifdef HAVE_CHARSET
5435     if (res)
5436     {
5437         const vfs_path_element_t *path_element;
5438 
5439         path_element = vfs_path_get_by_index (panel->cwd_vpath, -1);
5440         if (path_element->encoding != NULL)
5441             panel->codepage = get_codepage_index (path_element->encoding);
5442         else
5443             panel->codepage = SELECT_CHARSET_NO_TRANSLATE;
5444     }
5445 #endif
5446 
5447     return res;
5448 }
5449 
5450 /* --------------------------------------------------------------------------------------------- */

/* [previous][next][first][last][top][bottom][index][help]  */