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_dir
  40. adjust_top_file
  41. panel_save_name
  42. directory_history_add
  43. panel_load_history
  44. panel_save_history
  45. panel_destroy
  46. panel_paint_sort_info
  47. panel_get_title_without_hotkey
  48. panel_print_header
  49. parse_panel_size
  50. parse_display_format
  51. use_display_format
  52. panel_format
  53. mini_status_format
  54. cd_up_dir
  55. maybe_cd
  56. force_maybe_cd
  57. unselect_item
  58. panel_select_ext_cmd
  59. panel_set_current
  60. panel_current_at_half
  61. move_down
  62. move_up
  63. panel_move_current
  64. move_left
  65. move_right
  66. prev_page
  67. goto_parent_dir
  68. next_page
  69. goto_child_dir
  70. goto_top_file
  71. goto_middle_file
  72. goto_bottom_file
  73. move_home
  74. move_end
  75. do_mark_file
  76. mark_file
  77. mark_file_up
  78. mark_file_down
  79. mark_file_right
  80. mark_file_left
  81. panel_select_unselect_files_dialog
  82. panel_select_unselect_files
  83. panel_select_files
  84. panel_unselect_files
  85. panel_select_invert_files
  86. panel_do_set_filter
  87. do_search
  88. start_search
  89. stop_search
  90. do_enter_on_file_entry
  91. do_enter
  92. panel_cycle_listing_format
  93. chdir_other_panel
  94. panel_sync_other
  95. chdir_to_readlink
  96. panel_get_format_field_index_by_name
  97. panel_get_sortable_field_by_format
  98. panel_toggle_sort_order_prev
  99. panel_toggle_sort_order_next
  100. panel_select_sort_order
  101. panel_content_scroll_left
  102. panel_content_scroll_right
  103. panel_content_scroll_home
  104. panel_content_scroll_end
  105. panel_set_sort_type_by_id
  106. get_parent_dir_name
  107. panel_do_cd_int
  108. directory_history_next
  109. directory_history_prev
  110. directory_history_list
  111. panel_execute_cmd
  112. panel_key
  113. panel_callback
  114. mouse_toggle_mark
  115. mouse_set_mark
  116. mark_if_marking
  117. mouse_sort_col
  118. panel_mouse_is_on_item
  119. panel_mouse_callback
  120. reload_panelized
  121. update_one_panel_widget
  122. update_one_panel
  123. event_update_panels
  124. panel_save_current_file_to_clip_file
  125. panel_recursive_cd_to_parent
  126. panel_dir_list_callback
  127. panel_current_entry
  128. panel_set_current_by_name
  129. panel_clean_dir
  130. panel_set_cwd
  131. panel_set_lwd
  132. panel_sized_empty_new
  133. panel_sized_with_dir_new
  134. panel_reload
  135. set_panel_formats
  136. panel_set_filter
  137. select_item
  138. unmark_files
  139. recalculate_panel_summary
  140. do_file_mark
  141. panel_do_cd
  142. file_mark
  143. panel_find_marked_file
  144. panel_get_marked_file
  145. panel_re_sort
  146. panel_set_sort_order
  147. panel_change_encoding
  148. remove_encoding_from_path
  149. update_panels
  150. panel_get_num_of_sortable_fields
  151. panel_get_sortable_fields
  152. panel_get_field_by_id
  153. panel_get_field_by_title_hotkey
  154. panel_get_field_by_title
  155. panel_get_num_of_user_possible_fields
  156. panel_get_user_possible_fields
  157. panel_panelize_cd
  158. panel_panelize_change_root
  159. panel_panelize_absolutize_if_needed
  160. panel_panelize_save
  161. panel_init
  162. panel_deinit
  163. panel_cd

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

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