get-color.c 8.9 KB

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