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

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