Added -g, --oldmouse option to support of NORMAL/BUTTON_EVENT mouse type.
[midnight-commander.git] / lib / filehighlight / get-color.c
blob898cc8f9371bef2bde8536c3b5380758fa89beab
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>
31 #include "lib/global.h"
32 #include "lib/skin.h"
33 #include "lib/util.h" /* is_exe() */
34 #include "lib/filehighlight.h"
35 #include "internal.h"
37 /*** global variables ****************************************************************************/
39 /*** file scope macro definitions ****************************************************************/
41 /*** file scope type declarations ****************************************************************/
43 /*** file scope variables ************************************************************************/
45 /*** file scope functions ************************************************************************/
47 /* --------------------------------------------------------------------------------------------- */
49 /*inline functions */
50 inline static gboolean
51 mc_fhl_is_file (file_entry * fe)
53 #if S_ISREG == 0
54 (void) fe;
55 #endif
56 return S_ISREG (fe->st.st_mode);
59 inline static gboolean
60 mc_fhl_is_file_exec (file_entry * fe)
62 return is_exe (fe->st.st_mode);
65 inline static gboolean
66 mc_fhl_is_dir (file_entry * fe)
68 #if S_ISDIR == 0
69 (void) fe;
70 #endif
71 return S_ISDIR (fe->st.st_mode);
74 inline static gboolean
75 mc_fhl_is_link (file_entry * fe)
77 #if S_ISLNK == 0
78 (void) fe;
79 #endif
80 return S_ISLNK (fe->st.st_mode);
83 inline static gboolean
84 mc_fhl_is_hlink (file_entry * fe)
86 return (fe->st.st_nlink > 1);
89 inline static gboolean
90 mc_fhl_is_link_to_dir (file_entry * fe)
92 return mc_fhl_is_link (fe) && (fe->f.link_to_dir);
95 inline static gboolean
96 mc_fhl_is_stale_link (file_entry * fe)
98 return mc_fhl_is_link (fe) ? fe->f.stale_link : !mc_fhl_is_file (fe);
101 inline static gboolean
102 mc_fhl_is_device_char (file_entry * fe)
104 #if S_ISCHR == 0
105 (void) fe;
106 #endif
107 return S_ISCHR (fe->st.st_mode);
110 inline static gboolean
111 mc_fhl_is_device_block (file_entry * fe)
113 #if S_ISBLK == 0
114 (void) fe;
115 #endif
116 return S_ISBLK (fe->st.st_mode);
119 inline static gboolean
120 mc_fhl_is_special_socket (file_entry * fe)
122 #if S_ISSOCK == 0
123 (void) fe;
124 #endif
125 return S_ISSOCK (fe->st.st_mode);
128 inline static gboolean
129 mc_fhl_is_special_fifo (file_entry * fe)
131 #if S_ISFIFO == 0
132 (void) fe;
133 #endif
134 return S_ISFIFO (fe->st.st_mode);
137 inline static gboolean
138 mc_fhl_is_special_door (file_entry * fe)
140 #if S_ISDOOR == 0
141 (void) fe;
142 #endif
144 return S_ISDOOR (fe->st.st_mode);
148 inline static gboolean
149 mc_fhl_is_special (file_entry * fe)
151 return
152 (mc_fhl_is_special_socket (fe) || mc_fhl_is_special_fifo (fe)
153 || mc_fhl_is_special_door (fe));
157 /* --------------------------------------------------------------------------------------------- */
159 static int
160 mc_fhl_get_color_filetype (mc_fhl_filter_t * mc_filter, mc_fhl_t * fhl, file_entry * fe)
162 gboolean my_color = FALSE;
163 (void) fhl;
165 switch (mc_filter->file_type)
167 case MC_FLHGH_FTYPE_T_FILE:
168 if (mc_fhl_is_file (fe))
169 my_color = TRUE;
170 break;
171 case MC_FLHGH_FTYPE_T_FILE_EXE:
172 if ((mc_fhl_is_file (fe)) && (mc_fhl_is_file_exec (fe)))
173 my_color = TRUE;
174 break;
175 case MC_FLHGH_FTYPE_T_DIR:
176 if (mc_fhl_is_dir (fe) || mc_fhl_is_link_to_dir (fe))
177 my_color = TRUE;
178 break;
179 case MC_FLHGH_FTYPE_T_LINK_DIR:
180 if (mc_fhl_is_link_to_dir (fe))
181 my_color = TRUE;
182 break;
183 case MC_FLHGH_FTYPE_T_LINK:
184 if ((mc_fhl_is_link (fe)) || (mc_fhl_is_hlink (fe)))
185 my_color = TRUE;
186 break;
187 case MC_FLHGH_FTYPE_T_HARDLINK:
188 if (mc_fhl_is_hlink (fe))
189 my_color = TRUE;
190 break;
191 case MC_FLHGH_FTYPE_T_SYMLINK:
192 if (mc_fhl_is_link (fe))
193 my_color = TRUE;
194 break;
195 case MC_FLHGH_FTYPE_T_STALE_LINK:
196 if (mc_fhl_is_stale_link (fe))
197 my_color = TRUE;
198 break;
199 case MC_FLHGH_FTYPE_T_DEVICE:
200 if ((mc_fhl_is_device_char (fe)) || (mc_fhl_is_device_block (fe)))
201 my_color = TRUE;
202 break;
203 case MC_FLHGH_FTYPE_T_DEVICE_BLOCK:
204 if (mc_fhl_is_device_block (fe))
205 my_color = TRUE;
206 break;
207 case MC_FLHGH_FTYPE_T_DEVICE_CHAR:
208 if (mc_fhl_is_device_char (fe))
209 my_color = TRUE;
210 break;
211 case MC_FLHGH_FTYPE_T_SPECIAL:
212 if (mc_fhl_is_special (fe))
213 my_color = TRUE;
214 break;
215 case MC_FLHGH_FTYPE_T_SPECIAL_SOCKET:
216 if (mc_fhl_is_special_socket (fe))
217 my_color = TRUE;
218 break;
219 case MC_FLHGH_FTYPE_T_SPECIAL_FIFO:
220 if (mc_fhl_is_special_fifo (fe))
221 my_color = TRUE;
222 break;
223 case MC_FLHGH_FTYPE_T_SPECIAL_DOOR:
224 if (mc_fhl_is_special_door (fe))
225 my_color = TRUE;
226 break;
229 return (my_color) ? mc_filter->color_pair_index : -1;
232 /* --------------------------------------------------------------------------------------------- */
234 static int
235 mc_fhl_get_color_regexp (mc_fhl_filter_t * mc_filter, mc_fhl_t * fhl, file_entry * fe)
237 (void) fhl;
238 if (mc_filter->search_condition == NULL)
239 return -1;
241 if (mc_search_run (mc_filter->search_condition, fe->fname, 0, strlen (fe->fname), NULL))
242 return mc_filter->color_pair_index;
244 return -1;
247 /* --------------------------------------------------------------------------------------------- */
249 /* --------------------------------------------------------------------------------------------- */
250 /*** public functions ****************************************************************************/
251 /* --------------------------------------------------------------------------------------------- */
254 mc_fhl_get_color (mc_fhl_t * fhl, file_entry * fe)
256 guint i;
257 mc_fhl_filter_t *mc_filter;
258 int ret;
260 if (fhl == NULL)
261 return NORMAL_COLOR;
263 for (i = 0; i < fhl->filters->len; i++)
265 mc_filter = (mc_fhl_filter_t *) g_ptr_array_index (fhl->filters, i);
266 switch (mc_filter->type)
268 case MC_FLHGH_T_FTYPE:
269 ret = mc_fhl_get_color_filetype (mc_filter, fhl, fe);
270 if (ret > 0)
271 return -ret;
272 break;
273 case MC_FLHGH_T_EXT:
274 case MC_FLHGH_T_FREGEXP:
275 ret = mc_fhl_get_color_regexp (mc_filter, fhl, fe);
276 if (ret > 0)
277 return -ret;
278 break;
281 return NORMAL_COLOR;
284 /* --------------------------------------------------------------------------------------------- */