Manual pages: mcmcdiffmceditmcview

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

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