* Makefile.am: Remove definition of CONFDIR, it's not used.
[midnight-commander.git] / src / color.c
blob5d06c4ba168f8dee1fd9cd9cfcab00b8b4841f94
1 /* Color setup
2 Copyright (C) 1994 Miguel de Icaza.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 #include <config.h>
19 #include <stdio.h>
20 #include <string.h>
21 #include "global.h"
22 #include "tty.h"
23 #include "setup.h" /* For the externs */
24 #include "color.h"
26 /* "$Id$" */
28 /* To avoid excessive calls to ncurses' has_colors () */
29 int hascolors = 0;
31 /* Set to force black and white display at program startup */
32 int disable_colors = 0;
34 /* Set if we are actually using colors */
35 int use_colors = 0;
37 int dialog_colors [4];
39 #define ELEMENTS(arr) ( sizeof(arr) / sizeof((arr)[0]) )
41 #ifdef HAVE_SLANG
42 # define color_map_fg(n) color_map[n].fg
43 # define color_map_bg(n) color_map[n].bg
44 #else
45 # define color_map_fg(n) (color_map[n].fg & COLOR_WHITE)
46 # define color_map_bg(n) (color_map[n].bg & COLOR_WHITE)
47 #endif
49 struct colorpair {
50 char *name; /* Name of the entry */
51 CTYPE fg; /* foreground color */
52 CTYPE bg; /* background color */
55 struct colorpair color_map [] = {
56 { "normal=", 0, 0 }, /* normal */ /* 1 */
57 { "selected=", 0, 0 }, /* selected */
58 { "marked=", 0, 0 }, /* marked */
59 { "markselect=", 0, 0 }, /* marked/selected */
60 { "errors=", 0, 0 }, /* errors */
61 { "menu=", 0, 0 }, /* menu entry */
62 { "reverse=", 0, 0 }, /* reverse */
64 /* Dialog colors */
65 { "dnormal=", 0, 0 }, /* Dialog normal */ /* 8 */
66 { "dfocus=", 0, 0 }, /* Dialog focused */
67 { "dhotnormal=", 0, 0 }, /* Dialog normal/hot */
68 { "dhotfocus=", 0, 0 }, /* Dialog focused/hot */
70 { "viewunderline=", 0, 0 }, /* _\b? sequence in view, underline in editor */
71 { "menusel=", 0, 0 }, /* Menu selected color */ /* 13 */
72 { "menuhot=", 0, 0 }, /* Color for menu hotkeys */
73 { "menuhotsel=", 0, 0 }, /* Menu hotkeys/selected entry */
75 { "helpnormal=", 0, 0 }, /* Help normal */ /* 16 */
76 { "helpitalic=", 0, 0 }, /* Italic in help */
77 { "helpbold=", 0, 0 }, /* Bold in help */
78 { "helplink=", 0, 0 }, /* Not selected hyperlink */
79 { "helpslink=", 0, 0 }, /* Selected hyperlink */
81 { "gauge=", 0, 0 }, /* Color of the progress bar (percentage) *//* 21 */
82 { "input=", 0, 0 },
84 /* Per file types colors */
85 { "directory=", 0, 0 }, /* 23 */
86 { "executable=", 0, 0 },
87 { "link=", 0, 0 }, /* symbolic link (neither stalled nor link to directory) */
88 { "stalledlink=",0, 0 }, /* stalled symbolic link */
89 { "device=", 0, 0 },
90 { "special=", 0, 0 }, /* sockets, fifo */
91 { "core=", 0, 0 }, /* core files */ /* 29 */
93 { 0, 0, 0 }, /* not usable (DEFAULT_COLOR_INDEX) *//* 30 */
94 { 0, 0, 0 }, /* unused */
95 { 0, 0, 0 }, /* not usable (A_REVERSE) */
96 { 0, 0, 0 }, /* not usable (A_REVERSE_BOLD) */
98 /* editor colors start at 34 */
99 { "editnormal=", 0, 0 }, /* normal */ /* 34 */
100 { "editbold=", 0, 0 }, /* search->found */
101 { "editmarked=", 0, 0 }, /* marked/selected */
104 struct color_table_s {
105 char *name;
106 int value;
110 static struct color_table_s const color_table [] = {
111 { "black", COLOR_BLACK },
112 { "gray", COLOR_BLACK | A_BOLD },
113 { "red", COLOR_RED },
114 { "brightred", COLOR_RED | A_BOLD },
115 { "green", COLOR_GREEN },
116 { "brightgreen", COLOR_GREEN | A_BOLD },
117 { "brown", COLOR_YELLOW },
118 { "yellow", COLOR_YELLOW | A_BOLD },
119 { "blue", COLOR_BLUE },
120 { "brightblue", COLOR_BLUE | A_BOLD },
121 { "magenta", COLOR_MAGENTA },
122 { "brightmagenta", COLOR_MAGENTA | A_BOLD },
123 { "cyan", COLOR_CYAN },
124 { "brightcyan", COLOR_CYAN | A_BOLD },
125 { "lightgray", COLOR_WHITE },
126 { "white", COLOR_WHITE | A_BOLD },
127 { "default", 0 } /* hack for transparent background */
130 #ifdef HAVE_SLANG
131 # define color_value(i) color_table [i].name
132 # define color_name(i) color_table [i].name
133 #else
134 # define color_value(i) color_table [i].value
135 # define color_name(i) color_table [i].name
136 #endif
138 static void get_color (char *cpp, CTYPE *colp)
140 int i;
142 for (i = 0; i < ELEMENTS(color_table); i++){
143 if (strcmp (cpp, color_name (i)) == 0){
144 *colp = color_value (i);
145 return;
150 static void get_two_colors (char **cpp, struct colorpair *colorpairp)
152 char *p = *cpp;
153 int state;
155 state = 0;
157 for (; *p; p++){
158 if (*p == ':'){
159 *p = 0;
160 get_color (*cpp, state ? &colorpairp->bg : &colorpairp->fg);
161 *p = ':';
162 *cpp = p + 1;
163 return;
166 if (*p == ','){
167 state = 1;
168 *p = 0;
169 get_color (*cpp, &colorpairp->fg);
170 *p = ',';
171 *cpp = p + 1;
174 get_color (*cpp, state ? &colorpairp->bg : &colorpairp->fg);
177 static void configure_colors_string (char *the_color_string)
179 char *color_string, *p;
180 int i, found;
182 if (!the_color_string)
183 return;
185 p = color_string = g_strdup (the_color_string);
186 while (color_string && *color_string){
187 while (*color_string == ' ' || *color_string == '\t')
188 color_string++;
190 found = 0;
191 for (i = 0; i < ELEMENTS(color_map); i++){
192 int klen;
194 if (!color_map [i].name)
195 continue;
196 klen = strlen (color_map [i].name);
198 if (strncmp (color_string, color_map [i].name, klen) == 0){
199 color_string += klen;
200 get_two_colors (&color_string, &color_map [i]);
201 found = 1;
204 if (!found){
205 while (*color_string && *color_string != ':')
206 color_string++;
207 if (*color_string)
208 color_string++;
211 g_free (p);
214 static void configure_colors (void)
216 extern char *command_line_colors;
217 extern char *default_edition_colors;
219 configure_colors_string (default_edition_colors);
220 configure_colors_string (setup_color_string);
221 configure_colors_string (term_color_string);
222 configure_colors_string (getenv ("MC_COLOR_TABLE"));
223 configure_colors_string (command_line_colors);
226 #ifndef HAVE_SLANG
227 #define MAX_PAIRS 64
228 int attr_pairs [MAX_PAIRS];
229 #endif
231 static void
232 load_dialog_colors (void)
234 dialog_colors [0] = COLOR_NORMAL;
235 dialog_colors [1] = COLOR_FOCUS;
236 dialog_colors [2] = COLOR_HOT_NORMAL;
237 dialog_colors [3] = COLOR_HOT_FOCUS;
240 void init_colors (void)
242 int i;
244 hascolors = has_colors ();
246 if (!disable_colors && hascolors){
247 use_colors = 1;
250 if (use_colors){
251 start_color ();
252 configure_colors ();
254 #ifndef HAVE_SLANG
255 if (ELEMENTS (color_map) > MAX_PAIRS){
256 /* This message should only be seen by the developers */
257 fprintf (stderr,
258 "Too many defined colors, resize MAX_PAIRS on color.c");
259 exit (1);
261 #endif /* !HAVE_SLANG */
263 if (use_colors) {
264 #ifdef HAVE_SLANG
266 * We are relying on undocumented feature of
267 * S-Lang to make COLOR_PAIR(DEFAULT_COLOR_INDEX)
268 * the default fg/bg of the terminal.
269 * Hopefully, future versions of S-Lang will
270 * document this feature.
272 SLtt_set_color (DEFAULT_COLOR_INDEX, NULL, "default", "default");
273 #else
274 /* Always white on black */
275 mc_init_pair(DEFAULT_COLOR_INDEX, COLOR_WHITE, COLOR_BLACK);
276 #endif /* !HAVE_SLANG */
279 for (i = 0; i < ELEMENTS (color_map); i++){
280 if (!color_map [i].name)
281 continue;
283 mc_init_pair (i+1, color_map_fg(i), color_map_bg(i));
285 #ifndef HAVE_SLANG
287 * ncurses doesn't remember bold attribute in the color pairs,
288 * so we should keep track of it in a separate array.
290 attr_pairs [i+1] = color_map [i].fg & A_BOLD;
291 #endif /* !HAVE_SLANG */
294 load_dialog_colors ();
297 /* Functions necessary to implement syntax highlighting */
299 static int max_index = 0;
301 static int
302 alloc_color_pair (CTYPE foreground, CTYPE background)
304 mc_init_pair (++max_index, foreground, background);
305 return max_index;
308 static struct colors_avail {
309 struct colors_avail *next;
310 char *fg, *bg;
311 int index;
312 } c = { 0, 0, 0, 0 };
314 #ifdef HAVE_SLANG
315 void
316 mc_init_pair (int index, CTYPE foreground, CTYPE background)
318 /* hack for transparent background for Eterm, rxvt or else */
319 if (background && !strcmp (background, "default"))
320 background = NULL;
321 /* if foreground is default, I guess we should use normal fore-color. */
323 SLtt_set_color (index, "", foreground, background);
324 if (index > max_index)
325 max_index = index;
329 try_alloc_color_pair (char *fg, char *bg)
331 struct colors_avail *p = &c;
333 c.index = EDITOR_NORMAL_COLOR_INDEX;
334 for (;;) {
335 if (((fg && p->fg) ? !strcmp (fg, p->fg) : fg == p->fg) != 0
336 && ((bg && p->bg) ? !strcmp (bg, p->bg) : bg == p->bg) != 0)
337 return p->index;
338 if (!p->next)
339 break;
340 p = p->next;
342 p->next = g_new (struct colors_avail, 1);
343 p = p->next;
344 p->next = 0;
345 p->fg = fg ? g_strdup (fg) : 0;
346 p->bg = bg ? g_strdup (bg) : 0;
347 if (!fg)
348 /* Index in color_map array = COLOR_INDEX - 1 */
349 fg = color_map[EDITOR_NORMAL_COLOR_INDEX - 1].fg;
350 if (!bg)
351 bg = color_map[EDITOR_NORMAL_COLOR_INDEX - 1].bg;
352 p->index = alloc_color_pair (fg, bg);
353 return p->index;
356 #else /* !HAVE_SLANG */
357 void
358 mc_init_pair (int index, CTYPE foreground, CTYPE background)
360 init_pair (index, foreground, background);
361 if (index > max_index)
362 max_index = index;
366 try_alloc_color_pair (char *fg, char *bg)
368 int fg_index, bg_index;
369 int bold_attr;
370 struct colors_avail *p = &c;
372 c.index = EDITOR_NORMAL_COLOR_INDEX;
373 for (;;) {
374 if (((fg && p->fg) ? !strcmp (fg, p->fg) : fg == p->fg) != 0
375 && ((bg && p->bg) ? !strcmp (bg, p->bg) : bg == p->bg) != 0)
376 return p->index;
377 if (!p->next)
378 break;
379 p = p->next;
381 p->next = g_new (struct colors_avail, 1);
382 p = p->next;
383 p->next = 0;
384 p->fg = fg ? g_strdup (fg) : 0;
385 p->bg = bg ? g_strdup (bg) : 0;
386 if (!fg)
387 /* Index in color_map array = COLOR_INDEX - 1 */
388 fg_index = color_map[EDITOR_NORMAL_COLOR_INDEX - 1].fg;
389 else
390 get_color (fg, &fg_index);
392 if (!bg)
393 bg_index = color_map[EDITOR_NORMAL_COLOR_INDEX - 1].bg;
394 else
395 get_color (bg, &bg_index);
397 bold_attr = fg_index & A_BOLD;
398 fg_index = fg_index & COLOR_WHITE;
399 bg_index = bg_index & COLOR_WHITE;
401 p->index = alloc_color_pair (fg_index, bg_index);
402 attr_pairs [p->index] = bold_attr;
403 return p->index;
405 #endif /* !HAVE_SLANG */
407 void
408 dealloc_color_pairs (void)
410 struct colors_avail *p, *next;
412 for (p = c.next; p; p = next) {
413 next = p->next;
414 if (p->fg)
415 g_free (p->fg);
416 if (p->bg)
417 g_free (p->bg);
418 g_free (p);
420 c.next = NULL;