Merge branch '4562_mcedit_macros_paste'
[midnight-commander.git] / lib / filehighlight / get-color.c
blob503a097b17c17053fa7462c229f1409800316766
1 /*
2 File highlight plugin.
3 Interface functions. get color pair index for highlighted file.
5 Copyright (C) 2009-2024
6 Free Software Foundation, Inc.
8 Written by:
9 Slava Zanko <slavazanko@gmail.com>, 2009.
11 This file is part of the Midnight Commander.
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.
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.
23 You should have received a copy of the GNU General Public License
24 along with this program. If not, see <http://www.gnu.org/licenses/>.
27 #include <config.h>
28 #include <string.h>
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"
36 /*** global variables ****************************************************************************/
38 /*** file scope macro definitions ****************************************************************/
40 /*** file scope type declarations ****************************************************************/
42 /*** forward declarations (file scope functions) *************************************************/
44 /*** file scope variables ************************************************************************/
46 /* --------------------------------------------------------------------------------------------- */
47 /*** file scope functions ************************************************************************/
48 /* --------------------------------------------------------------------------------------------- */
50 /*inline functions */
51 inline static gboolean
52 mc_fhl_is_file (const file_entry_t *fe)
54 #if HAVE_S_ISREG == 0
55 (void) fe;
56 #endif
57 return S_ISREG (fe->st.st_mode);
60 /* --------------------------------------------------------------------------------------------- */
62 inline static gboolean
63 mc_fhl_is_file_exec (const file_entry_t *fe)
65 return is_exe (fe->st.st_mode);
68 /* --------------------------------------------------------------------------------------------- */
70 inline static gboolean
71 mc_fhl_is_dir (const file_entry_t *fe)
73 #if HAVE_S_ISDIR == 0
74 (void) fe;
75 #endif
76 return S_ISDIR (fe->st.st_mode);
79 /* --------------------------------------------------------------------------------------------- */
81 inline static gboolean
82 mc_fhl_is_link (const file_entry_t *fe)
84 #if HAVE_S_ISLNK == 0
85 (void) fe;
86 #endif
87 return S_ISLNK (fe->st.st_mode);
90 /* --------------------------------------------------------------------------------------------- */
92 inline static gboolean
93 mc_fhl_is_hlink (const file_entry_t *fe)
95 return (fe->st.st_nlink > 1);
98 /* --------------------------------------------------------------------------------------------- */
100 inline static gboolean
101 mc_fhl_is_link_to_dir (const file_entry_t *fe)
103 return mc_fhl_is_link (fe) && fe->f.link_to_dir != 0;
106 /* --------------------------------------------------------------------------------------------- */
108 inline static gboolean
109 mc_fhl_is_stale_link (const file_entry_t *fe)
111 return mc_fhl_is_link (fe) ? (fe->f.stale_link != 0) : !mc_fhl_is_file (fe);
114 /* --------------------------------------------------------------------------------------------- */
116 inline static gboolean
117 mc_fhl_is_device_char (const file_entry_t *fe)
119 #if HAVE_S_ISCHR == 0
120 (void) fe;
121 #endif
122 return S_ISCHR (fe->st.st_mode);
125 /* --------------------------------------------------------------------------------------------- */
127 inline static gboolean
128 mc_fhl_is_device_block (const file_entry_t *fe)
130 #if HAVE_S_ISBLK == 0
131 (void) fe;
132 #endif
133 return S_ISBLK (fe->st.st_mode);
136 /* --------------------------------------------------------------------------------------------- */
138 inline static gboolean
139 mc_fhl_is_special_socket (const file_entry_t *fe)
141 #if HAVE_S_ISSOCK == 0
142 (void) fe;
143 #endif
144 return S_ISSOCK (fe->st.st_mode);
147 /* --------------------------------------------------------------------------------------------- */
149 inline static gboolean
150 mc_fhl_is_special_fifo (const file_entry_t *fe)
152 #if HAVE_S_ISFIFO == 0
153 (void) fe;
154 #endif
155 return S_ISFIFO (fe->st.st_mode);
158 /* --------------------------------------------------------------------------------------------- */
160 inline static gboolean
161 mc_fhl_is_special_door (const file_entry_t *fe)
163 #if HAVE_S_ISDOOR == 0
164 (void) fe;
165 #endif
166 return S_ISDOOR (fe->st.st_mode);
169 /* --------------------------------------------------------------------------------------------- */
171 inline static gboolean
172 mc_fhl_is_special (const file_entry_t *fe)
174 return
175 (mc_fhl_is_special_socket (fe) || mc_fhl_is_special_fifo (fe)
176 || mc_fhl_is_special_door (fe));
179 /* --------------------------------------------------------------------------------------------- */
181 static int
182 mc_fhl_get_color_filetype (const mc_fhl_filter_t *mc_filter, const mc_fhl_t *fhl,
183 const file_entry_t *fe)
185 gboolean my_color = FALSE;
187 (void) fhl;
189 switch (mc_filter->file_type)
191 case MC_FLHGH_FTYPE_T_FILE:
192 if (mc_fhl_is_file (fe))
193 my_color = TRUE;
194 break;
195 case MC_FLHGH_FTYPE_T_FILE_EXE:
196 if (mc_fhl_is_file (fe) && mc_fhl_is_file_exec (fe))
197 my_color = TRUE;
198 break;
199 case MC_FLHGH_FTYPE_T_DIR:
200 if (mc_fhl_is_dir (fe) || mc_fhl_is_link_to_dir (fe))
201 my_color = TRUE;
202 break;
203 case MC_FLHGH_FTYPE_T_LINK_DIR:
204 if (mc_fhl_is_link_to_dir (fe))
205 my_color = TRUE;
206 break;
207 case MC_FLHGH_FTYPE_T_LINK:
208 if (mc_fhl_is_link (fe) || mc_fhl_is_hlink (fe))
209 my_color = TRUE;
210 break;
211 case MC_FLHGH_FTYPE_T_HARDLINK:
212 if (mc_fhl_is_hlink (fe))
213 my_color = TRUE;
214 break;
215 case MC_FLHGH_FTYPE_T_SYMLINK:
216 if (mc_fhl_is_link (fe))
217 my_color = TRUE;
218 break;
219 case MC_FLHGH_FTYPE_T_STALE_LINK:
220 if (mc_fhl_is_stale_link (fe))
221 my_color = TRUE;
222 break;
223 case MC_FLHGH_FTYPE_T_DEVICE:
224 if (mc_fhl_is_device_char (fe) || mc_fhl_is_device_block (fe))
225 my_color = TRUE;
226 break;
227 case MC_FLHGH_FTYPE_T_DEVICE_BLOCK:
228 if (mc_fhl_is_device_block (fe))
229 my_color = TRUE;
230 break;
231 case MC_FLHGH_FTYPE_T_DEVICE_CHAR:
232 if (mc_fhl_is_device_char (fe))
233 my_color = TRUE;
234 break;
235 case MC_FLHGH_FTYPE_T_SPECIAL:
236 if (mc_fhl_is_special (fe))
237 my_color = TRUE;
238 break;
239 case MC_FLHGH_FTYPE_T_SPECIAL_SOCKET:
240 if (mc_fhl_is_special_socket (fe))
241 my_color = TRUE;
242 break;
243 case MC_FLHGH_FTYPE_T_SPECIAL_FIFO:
244 if (mc_fhl_is_special_fifo (fe))
245 my_color = TRUE;
246 break;
247 case MC_FLHGH_FTYPE_T_SPECIAL_DOOR:
248 if (mc_fhl_is_special_door (fe))
249 my_color = TRUE;
250 break;
251 default:
252 break;
255 return my_color ? mc_filter->color_pair_index : -1;
258 /* --------------------------------------------------------------------------------------------- */
260 static int
261 mc_fhl_get_color_regexp (const mc_fhl_filter_t *mc_filter, const mc_fhl_t *fhl,
262 const file_entry_t *fe)
264 (void) fhl;
266 if (mc_filter->search_condition == NULL)
267 return -1;
269 if (mc_search_run (mc_filter->search_condition, fe->fname->str, 0, fe->fname->len, NULL))
270 return mc_filter->color_pair_index;
272 return -1;
275 /* --------------------------------------------------------------------------------------------- */
276 /*** public functions ****************************************************************************/
277 /* --------------------------------------------------------------------------------------------- */
280 mc_fhl_get_color (const mc_fhl_t *fhl, const file_entry_t *fe)
282 guint i;
283 int ret;
285 if (fhl == NULL)
286 return NORMAL_COLOR;
288 for (i = 0; i < fhl->filters->len; i++)
290 mc_fhl_filter_t *mc_filter;
292 mc_filter = (mc_fhl_filter_t *) g_ptr_array_index (fhl->filters, i);
293 switch (mc_filter->type)
295 case MC_FLHGH_T_FTYPE:
296 ret = mc_fhl_get_color_filetype (mc_filter, fhl, fe);
297 if (ret > 0)
298 return -ret;
299 break;
300 case MC_FLHGH_T_EXT:
301 case MC_FLHGH_T_FREGEXP:
302 ret = mc_fhl_get_color_regexp (mc_filter, fhl, fe);
303 if (ret > 0)
304 return -ret;
305 break;
306 default:
307 break;
310 return NORMAL_COLOR;
313 /* --------------------------------------------------------------------------------------------- */