get-color.c 7.3 KB

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