Manual pages: mcmcdiffmceditmcview

root/lib/filehighlight/get-color.c

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

DEFINITIONS

This source file includes following definitions.
  1. mc_fhl_is_file
  2. mc_fhl_is_file_exec
  3. mc_fhl_is_dir
  4. mc_fhl_is_link
  5. mc_fhl_is_hlink
  6. mc_fhl_is_link_to_dir
  7. mc_fhl_is_stale_link
  8. mc_fhl_is_device_char
  9. mc_fhl_is_device_block
  10. mc_fhl_is_special_socket
  11. mc_fhl_is_special_fifo
  12. mc_fhl_is_special_door
  13. mc_fhl_is_special
  14. mc_fhl_get_color_filetype
  15. mc_fhl_get_color_regexp
  16. mc_fhl_get_color

   1 /*
   2    File highlight plugin.
   3    Interface functions. get color pair index for highlighted file.
   4 
   5    Copyright (C) 2009-2025
   6    Free Software Foundation, Inc.
   7 
   8    Written by:
   9    Slava Zanko <slavazanko@gmail.com>, 2009.
  10 
  11    This file is part of the Midnight Commander.
  12 
  13    The Midnight Commander is free software: you can redistribute it
  14    and/or modify it under the terms of the GNU General Public License as
  15    published by the Free Software Foundation, either version 3 of the License,
  16    or (at your option) any later version.
  17 
  18    The Midnight Commander is distributed in the hope that it will be useful,
  19    but WITHOUT ANY WARRANTY; without even the implied warranty of
  20    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21    GNU General Public License for more details.
  22 
  23    You should have received a copy of the GNU General Public License
  24    along with this program.  If not, see <https://www.gnu.org/licenses/>.
  25  */
  26 
  27 #include <config.h>
  28 #include <string.h>
  29 
  30 #include "lib/global.h"
  31 #include "lib/skin.h"
  32 #include "lib/util.h"  // is_exe()
  33 #include "lib/filehighlight.h"
  34 #include "internal.h"
  35 
  36 /*** global variables ****************************************************************************/
  37 
  38 /*** file scope macro definitions ****************************************************************/
  39 
  40 /*** file scope type declarations ****************************************************************/
  41 
  42 /*** forward declarations (file scope functions) *************************************************/
  43 
  44 /*** file scope variables ************************************************************************/
  45 
  46 /* --------------------------------------------------------------------------------------------- */
  47 /*** file scope functions ************************************************************************/
  48 /* --------------------------------------------------------------------------------------------- */
  49 
  50 /*inline functions */
  51 inline static gboolean
  52 mc_fhl_is_file (const file_entry_t *fe)
     /* [previous][next][first][last][top][bottom][index][help]  */
  53 {
  54 #if HAVE_S_ISREG == 0
  55     (void) fe;
  56 #endif
  57     return S_ISREG (fe->st.st_mode);
  58 }
  59 
  60 /* --------------------------------------------------------------------------------------------- */
  61 
  62 inline static gboolean
  63 mc_fhl_is_file_exec (const file_entry_t *fe)
     /* [previous][next][first][last][top][bottom][index][help]  */
  64 {
  65     return is_exe (fe->st.st_mode);
  66 }
  67 
  68 /* --------------------------------------------------------------------------------------------- */
  69 
  70 inline static gboolean
  71 mc_fhl_is_dir (const file_entry_t *fe)
     /* [previous][next][first][last][top][bottom][index][help]  */
  72 {
  73 #if HAVE_S_ISDIR == 0
  74     (void) fe;
  75 #endif
  76     return S_ISDIR (fe->st.st_mode);
  77 }
  78 
  79 /* --------------------------------------------------------------------------------------------- */
  80 
  81 inline static gboolean
  82 mc_fhl_is_link (const file_entry_t *fe)
     /* [previous][next][first][last][top][bottom][index][help]  */
  83 {
  84 #if HAVE_S_ISLNK == 0
  85     (void) fe;
  86 #endif
  87     return S_ISLNK (fe->st.st_mode);
  88 }
  89 
  90 /* --------------------------------------------------------------------------------------------- */
  91 
  92 inline static gboolean
  93 mc_fhl_is_hlink (const file_entry_t *fe)
     /* [previous][next][first][last][top][bottom][index][help]  */
  94 {
  95     return (fe->st.st_nlink > 1);
  96 }
  97 
  98 /* --------------------------------------------------------------------------------------------- */
  99 
 100 inline static gboolean
 101 mc_fhl_is_link_to_dir (const file_entry_t *fe)
     /* [previous][next][first][last][top][bottom][index][help]  */
 102 {
 103     return mc_fhl_is_link (fe) && fe->f.link_to_dir != 0;
 104 }
 105 
 106 /* --------------------------------------------------------------------------------------------- */
 107 
 108 inline static gboolean
 109 mc_fhl_is_stale_link (const file_entry_t *fe)
     /* [previous][next][first][last][top][bottom][index][help]  */
 110 {
 111     return mc_fhl_is_link (fe) ? (fe->f.stale_link != 0) : !mc_fhl_is_file (fe);
 112 }
 113 
 114 /* --------------------------------------------------------------------------------------------- */
 115 
 116 inline static gboolean
 117 mc_fhl_is_device_char (const file_entry_t *fe)
     /* [previous][next][first][last][top][bottom][index][help]  */
 118 {
 119 #if HAVE_S_ISCHR == 0
 120     (void) fe;
 121 #endif
 122     return S_ISCHR (fe->st.st_mode);
 123 }
 124 
 125 /* --------------------------------------------------------------------------------------------- */
 126 
 127 inline static gboolean
 128 mc_fhl_is_device_block (const file_entry_t *fe)
     /* [previous][next][first][last][top][bottom][index][help]  */
 129 {
 130 #if HAVE_S_ISBLK == 0
 131     (void) fe;
 132 #endif
 133     return S_ISBLK (fe->st.st_mode);
 134 }
 135 
 136 /* --------------------------------------------------------------------------------------------- */
 137 
 138 inline static gboolean
 139 mc_fhl_is_special_socket (const file_entry_t *fe)
     /* [previous][next][first][last][top][bottom][index][help]  */
 140 {
 141 #if HAVE_S_ISSOCK == 0
 142     (void) fe;
 143 #endif
 144     return S_ISSOCK (fe->st.st_mode);
 145 }
 146 
 147 /* --------------------------------------------------------------------------------------------- */
 148 
 149 inline static gboolean
 150 mc_fhl_is_special_fifo (const file_entry_t *fe)
     /* [previous][next][first][last][top][bottom][index][help]  */
 151 {
 152 #if HAVE_S_ISFIFO == 0
 153     (void) fe;
 154 #endif
 155     return S_ISFIFO (fe->st.st_mode);
 156 }
 157 
 158 /* --------------------------------------------------------------------------------------------- */
 159 
 160 inline static gboolean
 161 mc_fhl_is_special_door (const file_entry_t *fe)
     /* [previous][next][first][last][top][bottom][index][help]  */
 162 {
 163 #if HAVE_S_ISDOOR == 0
 164     (void) fe;
 165 #endif
 166     return S_ISDOOR (fe->st.st_mode);
 167 }
 168 
 169 /* --------------------------------------------------------------------------------------------- */
 170 
 171 inline static gboolean
 172 mc_fhl_is_special (const file_entry_t *fe)
     /* [previous][next][first][last][top][bottom][index][help]  */
 173 {
 174     return (mc_fhl_is_special_socket (fe) || mc_fhl_is_special_fifo (fe)
 175             || mc_fhl_is_special_door (fe));
 176 }
 177 
 178 /* --------------------------------------------------------------------------------------------- */
 179 
 180 static int
 181 mc_fhl_get_color_filetype (const mc_fhl_filter_t *mc_filter, const mc_fhl_t *fhl,
     /* [previous][next][first][last][top][bottom][index][help]  */
 182                            const file_entry_t *fe)
 183 {
 184     gboolean my_color = FALSE;
 185 
 186     (void) fhl;
 187 
 188     switch (mc_filter->file_type)
 189     {
 190     case MC_FLHGH_FTYPE_T_FILE:
 191         if (mc_fhl_is_file (fe))
 192             my_color = TRUE;
 193         break;
 194     case MC_FLHGH_FTYPE_T_FILE_EXE:
 195         if (mc_fhl_is_file (fe) && mc_fhl_is_file_exec (fe))
 196             my_color = TRUE;
 197         break;
 198     case MC_FLHGH_FTYPE_T_DIR:
 199         if (mc_fhl_is_dir (fe) || mc_fhl_is_link_to_dir (fe))
 200             my_color = TRUE;
 201         break;
 202     case MC_FLHGH_FTYPE_T_LINK_DIR:
 203         if (mc_fhl_is_link_to_dir (fe))
 204             my_color = TRUE;
 205         break;
 206     case MC_FLHGH_FTYPE_T_LINK:
 207         if (mc_fhl_is_link (fe) || mc_fhl_is_hlink (fe))
 208             my_color = TRUE;
 209         break;
 210     case MC_FLHGH_FTYPE_T_HARDLINK:
 211         if (mc_fhl_is_hlink (fe))
 212             my_color = TRUE;
 213         break;
 214     case MC_FLHGH_FTYPE_T_SYMLINK:
 215         if (mc_fhl_is_link (fe))
 216             my_color = TRUE;
 217         break;
 218     case MC_FLHGH_FTYPE_T_STALE_LINK:
 219         if (mc_fhl_is_stale_link (fe))
 220             my_color = TRUE;
 221         break;
 222     case MC_FLHGH_FTYPE_T_DEVICE:
 223         if (mc_fhl_is_device_char (fe) || mc_fhl_is_device_block (fe))
 224             my_color = TRUE;
 225         break;
 226     case MC_FLHGH_FTYPE_T_DEVICE_BLOCK:
 227         if (mc_fhl_is_device_block (fe))
 228             my_color = TRUE;
 229         break;
 230     case MC_FLHGH_FTYPE_T_DEVICE_CHAR:
 231         if (mc_fhl_is_device_char (fe))
 232             my_color = TRUE;
 233         break;
 234     case MC_FLHGH_FTYPE_T_SPECIAL:
 235         if (mc_fhl_is_special (fe))
 236             my_color = TRUE;
 237         break;
 238     case MC_FLHGH_FTYPE_T_SPECIAL_SOCKET:
 239         if (mc_fhl_is_special_socket (fe))
 240             my_color = TRUE;
 241         break;
 242     case MC_FLHGH_FTYPE_T_SPECIAL_FIFO:
 243         if (mc_fhl_is_special_fifo (fe))
 244             my_color = TRUE;
 245         break;
 246     case MC_FLHGH_FTYPE_T_SPECIAL_DOOR:
 247         if (mc_fhl_is_special_door (fe))
 248             my_color = TRUE;
 249         break;
 250     default:
 251         break;
 252     }
 253 
 254     return my_color ? mc_filter->color_pair_index : -1;
 255 }
 256 
 257 /* --------------------------------------------------------------------------------------------- */
 258 
 259 static int
 260 mc_fhl_get_color_regexp (const mc_fhl_filter_t *mc_filter, const mc_fhl_t *fhl,
     /* [previous][next][first][last][top][bottom][index][help]  */
 261                          const file_entry_t *fe)
 262 {
 263     (void) fhl;
 264 
 265     if (mc_filter->search_condition == NULL)
 266         return -1;
 267 
 268     if (mc_search_run (mc_filter->search_condition, fe->fname->str, 0, fe->fname->len, NULL))
 269         return mc_filter->color_pair_index;
 270 
 271     return -1;
 272 }
 273 
 274 /* --------------------------------------------------------------------------------------------- */
 275 /*** public functions ****************************************************************************/
 276 /* --------------------------------------------------------------------------------------------- */
 277 
 278 int
 279 mc_fhl_get_color (const mc_fhl_t *fhl, const file_entry_t *fe)
     /* [previous][next][first][last][top][bottom][index][help]  */
 280 {
 281     guint i;
 282     int ret;
 283 
 284     if (fhl == NULL)
 285         return NORMAL_COLOR;
 286 
 287     for (i = 0; i < fhl->filters->len; i++)
 288     {
 289         mc_fhl_filter_t *mc_filter;
 290 
 291         mc_filter = (mc_fhl_filter_t *) g_ptr_array_index (fhl->filters, i);
 292         switch (mc_filter->type)
 293         {
 294         case MC_FLHGH_T_FTYPE:
 295             ret = mc_fhl_get_color_filetype (mc_filter, fhl, fe);
 296             if (ret > 0)
 297                 return -ret;
 298             break;
 299         case MC_FLHGH_T_EXT:
 300         case MC_FLHGH_T_FREGEXP:
 301             ret = mc_fhl_get_color_regexp (mc_filter, fhl, fe);
 302             if (ret > 0)
 303                 return -ret;
 304             break;
 305         default:
 306             break;
 307         }
 308     }
 309     return NORMAL_COLOR;
 310 }
 311 
 312 /* --------------------------------------------------------------------------------------------- */

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