Ticket #1649: Prepare for prerelease mc-4.7.0-pre3 (code cleanup)
[midnight-commander.git] / src / filehighlight / get-color.c
bloba070895ede21857ae8dcb3a1029e8141393876f9
1 /*
2 File highlight plugin.
3 Interface functions. get color pair index for highlighted file.
5 Copyright (C) 2009 The Free Software Foundation, Inc.
7 Written by:
8 Slava Zanko <slavazanko@gmail.com>, 2009.
10 This file is part of the Midnight Commander.
12 The Midnight Commander is free software; you can redistribute it
13 and/or modify it under the terms of the GNU General Public License as
14 published by the Free Software Foundation; either version 2 of the
15 License, or (at your option) any later version.
17 The Midnight Commander is distributed in the hope that it will be
18 useful, but WITHOUT ANY WARRANTY; without even the implied warranty
19 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
25 MA 02110-1301, USA.
28 #include <config.h>
29 #include <string.h>
32 #include "../src/global.h"
33 #include "../src/util.h"
34 #include "../src/skin/skin.h"
35 #include "../src/filehighlight/fhl.h"
36 #include "internal.h"
38 /*** global variables ****************************************************************************/
40 /*** file scope macro definitions ****************************************************************/
42 /*** file scope type declarations ****************************************************************/
44 /*** file scope variables ************************************************************************/
46 /*** file scope functions ************************************************************************/
47 /* --------------------------------------------------------------------------------------------- */
48 /*inline functions*/
49 inline static gboolean
50 mc_fhl_is_file (file_entry * fe)
52 return S_ISREG (fe->st.st_mode);
55 inline static gboolean
56 mc_fhl_is_file_exec (file_entry * fe)
58 return is_exe (fe->st.st_mode);
61 inline static gboolean
62 mc_fhl_is_dir (file_entry * fe)
64 return S_ISDIR (fe->st.st_mode);
67 inline static gboolean
68 mc_fhl_is_link (file_entry * fe)
70 return S_ISLNK (fe->st.st_mode);
73 inline static gboolean
74 mc_fhl_is_link_to_dir (file_entry * fe)
76 return mc_fhl_is_link (fe) && (fe->f.link_to_dir);
79 inline static gboolean
80 mc_fhl_is_stale_link (file_entry * fe)
82 return mc_fhl_is_link (fe) && (fe->f.stale_link);
85 inline static gboolean
86 mc_fhl_is_device_char (file_entry * fe)
88 return S_ISCHR (fe->st.st_mode);
91 inline static gboolean
92 mc_fhl_is_device_block (file_entry * fe)
94 return S_ISBLK (fe->st.st_mode);
97 inline static gboolean
98 mc_fhl_is_special_socket (file_entry * fe)
100 return S_ISSOCK (fe->st.st_mode);
103 inline static gboolean
104 mc_fhl_is_special_fifo (file_entry * fe)
106 return S_ISFIFO (fe->st.st_mode);
109 inline static gboolean
110 mc_fhl_is_special_door (file_entry * fe)
112 return S_ISDOOR (fe->st.st_mode);
116 inline static gboolean
117 mc_fhl_is_special (file_entry * fe)
119 return
120 (mc_fhl_is_special_socket (fe) || mc_fhl_is_special_fifo (fe) || mc_fhl_is_special_door (fe)
125 /* --------------------------------------------------------------------------------------------- */
127 static int
128 mc_fhl_get_color_filetype (mc_fhl_filter_t * mc_filter, mc_fhl_t * fhl, file_entry * fe)
130 gboolean my_color = FALSE;
131 (void) fhl;
133 switch (mc_filter->file_type) {
134 case MC_FLHGH_FTYPE_T_FILE:
135 if (mc_fhl_is_file (fe))
136 my_color = TRUE;
137 break;
138 case MC_FLHGH_FTYPE_T_FILE_EXE:
139 if ((mc_fhl_is_file (fe)) && (mc_fhl_is_file_exec (fe)))
140 my_color = TRUE;
141 break;
142 case MC_FLHGH_FTYPE_T_DIR:
143 if (mc_fhl_is_dir (fe) || mc_fhl_is_link_to_dir (fe))
144 my_color = TRUE;
145 break;
146 case MC_FLHGH_FTYPE_T_LINK_DIR:
147 if (mc_fhl_is_link_to_dir (fe))
148 my_color = TRUE;
149 break;
150 case MC_FLHGH_FTYPE_T_LINK:
151 if (mc_fhl_is_link (fe))
152 my_color = TRUE;
153 break;
154 case MC_FLHGH_FTYPE_T_HARDLINK:
155 /*TODO: hanlde it */
156 if (mc_fhl_is_link (fe))
157 my_color = TRUE;
158 break;
159 case MC_FLHGH_FTYPE_T_SYMLINK:
160 /*TODO: hanlde it */
161 if (mc_fhl_is_link (fe))
162 my_color = TRUE;
163 break;
164 case MC_FLHGH_FTYPE_T_STALE_LINK:
165 if (mc_fhl_is_stale_link (fe))
166 my_color = TRUE;
167 break;
168 case MC_FLHGH_FTYPE_T_DEVICE:
169 if ((mc_fhl_is_device_char (fe)) || (mc_fhl_is_device_block (fe)))
170 my_color = TRUE;
171 break;
172 case MC_FLHGH_FTYPE_T_DEVICE_BLOCK:
173 if (mc_fhl_is_device_block (fe))
174 my_color = TRUE;
175 break;
176 case MC_FLHGH_FTYPE_T_DEVICE_CHAR:
177 if (mc_fhl_is_device_char (fe))
178 my_color = TRUE;
179 break;
180 case MC_FLHGH_FTYPE_T_SPECIAL:
181 if (mc_fhl_is_special (fe))
182 my_color = TRUE;
183 break;
184 case MC_FLHGH_FTYPE_T_SPECIAL_SOCKET:
185 if (mc_fhl_is_special_socket (fe))
186 my_color = TRUE;
187 break;
188 case MC_FLHGH_FTYPE_T_SPECIAL_FIFO:
189 if (mc_fhl_is_special_fifo (fe))
190 my_color = TRUE;
191 break;
192 case MC_FLHGH_FTYPE_T_SPECIAL_DOOR:
193 if (mc_fhl_is_special_door (fe))
194 my_color = TRUE;
195 break;
198 return (my_color) ? mc_filter->color_pair_index : -1;
201 /* --------------------------------------------------------------------------------------------- */
203 static int
204 mc_fhl_get_color_regexp (mc_fhl_filter_t * mc_filter, mc_fhl_t * fhl, file_entry * fe)
206 (void) fhl;
207 if (mc_filter->search_condition == NULL)
208 return -1;
210 if (mc_search_run (mc_filter->search_condition, fe->fname, 0, strlen (fe->fname), NULL))
211 return mc_filter->color_pair_index;
213 return -1;
216 /* --------------------------------------------------------------------------------------------- */
218 /* --------------------------------------------------------------------------------------------- */
219 /*** public functions ****************************************************************************/
220 /* --------------------------------------------------------------------------------------------- */
224 mc_fhl_get_color (mc_fhl_t * fhl, file_entry * fe)
226 guint i;
227 mc_fhl_filter_t *mc_filter;
228 int ret;
230 if (fhl == NULL)
231 return NORMAL_COLOR;
233 for (i = 0; i < fhl->filters->len; i++) {
234 mc_filter = (mc_fhl_filter_t *) g_ptr_array_index (fhl->filters, i);
235 switch (mc_filter->type) {
236 case MC_FLHGH_T_FTYPE:
237 ret = mc_fhl_get_color_filetype (mc_filter, fhl, fe);
238 if (ret > 0)
239 return -ret;
240 break;
241 case MC_FLHGH_T_EXT:
242 case MC_FLHGH_T_FREGEXP:
243 ret = mc_fhl_get_color_regexp (mc_filter, fhl, fe);
244 if (ret > 0)
245 return -ret;
246 break;
249 return NORMAL_COLOR;
252 /* --------------------------------------------------------------------------------------------- */