* extfs.c (extfs_generate_entry): Initialize inode->last_in_subdir.
[midnight-commander.git] / src / color.c
blob921b8ae780c78ff44aad2e1dc7034581f5e19172
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 /* Set to force black and white display at program startup */
27 int disable_colors = 0;
29 /* Set if we are actually using colors */
30 int use_colors = 0;
32 /* Color styles for normal and error dialogs */
33 int dialog_colors [4];
34 int alarm_colors [4];
36 #define ELEMENTS(arr) ( sizeof(arr) / sizeof((arr)[0]) )
38 #ifdef HAVE_SLANG
39 # define color_map_fg(n) color_map[n].fg
40 # define color_map_bg(n) color_map[n].bg
41 #else
42 # define color_map_fg(n) (color_map[n].fg & COLOR_WHITE)
43 # define color_map_bg(n) (color_map[n].bg & COLOR_WHITE)
44 #endif
46 struct colorpair {
47 const char *name; /* Name of the entry */
48 CTYPE fg; /* foreground color */
49 CTYPE bg; /* background color */
52 static struct colorpair color_map [] = {
53 { "normal=", 0, 0 }, /* normal */ /* 1 */
54 { "selected=", 0, 0 }, /* selected */
55 { "marked=", 0, 0 }, /* marked */
56 { "markselect=", 0, 0 }, /* marked/selected */
57 { "errors=", 0, 0 }, /* errors */
58 { "menu=", 0, 0 }, /* menu entry */
59 { "reverse=", 0, 0 }, /* reverse */
61 /* Dialog colors */
62 { "dnormal=", 0, 0 }, /* Dialog normal */ /* 8 */
63 { "dfocus=", 0, 0 }, /* Dialog focused */
64 { "dhotnormal=", 0, 0 }, /* Dialog normal/hot */
65 { "dhotfocus=", 0, 0 }, /* Dialog focused/hot */
67 { "viewunderline=", 0, 0 }, /* _\b? sequence in view, underline in editor */
68 { "menusel=", 0, 0 }, /* Menu selected color */ /* 13 */
69 { "menuhot=", 0, 0 }, /* Color for menu hotkeys */
70 { "menuhotsel=", 0, 0 }, /* Menu hotkeys/selected entry */
72 { "helpnormal=", 0, 0 }, /* Help normal */ /* 16 */
73 { "helpitalic=", 0, 0 }, /* Italic in help */
74 { "helpbold=", 0, 0 }, /* Bold in help */
75 { "helplink=", 0, 0 }, /* Not selected hyperlink */
76 { "helpslink=", 0, 0 }, /* Selected hyperlink */
78 { "gauge=", 0, 0 }, /* Color of the progress bar (percentage) *//* 21 */
79 { "input=", 0, 0 },
81 /* Per file types colors */
82 { "directory=", 0, 0 }, /* 23 */
83 { "executable=", 0, 0 },
84 { "link=", 0, 0 }, /* symbolic link (neither stale nor link to directory) */
85 { "stalelink=", 0, 0 }, /* stale symbolic link */
86 { "device=", 0, 0 },
87 { "special=", 0, 0 }, /* sockets, fifo */
88 { "core=", 0, 0 }, /* core files */ /* 29 */
90 { 0, 0, 0 }, /* not usable (DEFAULT_COLOR_INDEX) *//* 30 */
91 { 0, 0, 0 }, /* unused */
92 { 0, 0, 0 }, /* not usable (A_REVERSE) */
93 { 0, 0, 0 }, /* not usable (A_REVERSE_BOLD) */
95 /* editor colors start at 34 */
96 { "editnormal=", 0, 0 }, /* normal */ /* 34 */
97 { "editbold=", 0, 0 }, /* search->found */
98 { "editmarked=", 0, 0 }, /* marked/selected */
100 /* error dialog colors start at 37 */
101 { "errdhotnormal=", 0, 0 }, /* Error dialog normal/hot */ /* 37 */
102 { "errdhotfocus=", 0, 0 }, /* Error dialog focused/hot */
105 struct color_table_s {
106 const char *name;
107 int value;
111 static struct color_table_s const color_table [] = {
112 { "black", COLOR_BLACK },
113 { "gray", COLOR_BLACK | A_BOLD },
114 { "red", COLOR_RED },
115 { "brightred", COLOR_RED | A_BOLD },
116 { "green", COLOR_GREEN },
117 { "brightgreen", COLOR_GREEN | A_BOLD },
118 { "brown", COLOR_YELLOW },
119 { "yellow", COLOR_YELLOW | A_BOLD },
120 { "blue", COLOR_BLUE },
121 { "brightblue", COLOR_BLUE | A_BOLD },
122 { "magenta", COLOR_MAGENTA },
123 { "brightmagenta", COLOR_MAGENTA | A_BOLD },
124 { "cyan", COLOR_CYAN },
125 { "brightcyan", COLOR_CYAN | A_BOLD },
126 { "lightgray", COLOR_WHITE },
127 { "white", COLOR_WHITE | A_BOLD },
128 { "default", 0 } /* default color of the terminal */
131 static const char *default_colors =
132 "normal=lightgray,blue:"
133 "selected=black,cyan:"
134 "marked=yellow,blue:"
135 "markselect=yellow,cyan:"
136 "errors=white,red:"
137 "menu=white,cyan:"
138 "reverse=black,lightgray:"
139 "dnormal=black,lightgray:"
140 "dfocus=black,cyan:"
141 "dhotnormal=blue,lightgray:"
142 "dhotfocus=blue,cyan:"
143 "viewunderline=brightred,blue:"
144 "menuhot=yellow,cyan:"
145 "menusel=white,black:"
146 "menuhotsel=yellow,black:"
147 "helpnormal=black,lightgray:"
148 "helpitalic=red,lightgray:"
149 "helpbold=blue,lightgray:"
150 "helplink=black,cyan:"
151 "helpslink=yellow,blue:"
152 "gauge=white,black:"
153 "input=black,cyan:"
154 "directory=white,blue:"
155 "executable=brightgreen,blue:"
156 "link=lightgray,blue:"
157 "stalelink=brightred,blue:"
158 "device=brightmagenta,blue:"
159 "core=red,blue:"
160 "special=black,blue:"
161 "editnormal=lightgray,blue:"
162 "editbold=yellow,blue:"
163 "editmarked=black,cyan:"
164 "errdhotnormal=yellow,red:"
165 "errdhotfocus=yellow,lightgray";
167 #ifdef HAVE_SLANG
168 # define color_value(i) color_table [i].name
169 # define color_name(i) color_table [i].name
170 #else
171 # define color_value(i) color_table [i].value
172 # define color_name(i) color_table [i].name
173 #endif
175 static void get_color (const char *cpp, CTYPE *colp)
177 size_t i;
179 for (i = 0; i < ELEMENTS(color_table); i++){
180 if (strcmp (cpp, color_name (i)) == 0){
181 *colp = color_value (i);
182 return;
187 static void get_two_colors (char **cpp, struct colorpair *colorpairp)
189 char *p = *cpp;
190 int state;
192 state = 0;
194 for (; *p; p++){
195 if (*p == ':'){
196 *p = 0;
197 get_color (*cpp, state ? &colorpairp->bg : &colorpairp->fg);
198 *p = ':';
199 *cpp = p + 1;
200 return;
203 if (*p == ','){
204 state = 1;
205 *p = 0;
206 get_color (*cpp, &colorpairp->fg);
207 *p = ',';
208 *cpp = p + 1;
211 get_color (*cpp, state ? &colorpairp->bg : &colorpairp->fg);
214 static void configure_colors_string (const char *the_color_string)
216 char *color_string, *p;
217 size_t i;
218 int found;
220 if (!the_color_string)
221 return;
223 p = color_string = g_strdup (the_color_string);
224 while (color_string && *color_string){
225 while (*color_string == ' ' || *color_string == '\t')
226 color_string++;
228 found = 0;
229 for (i = 0; i < ELEMENTS(color_map); i++){
230 int klen;
232 if (!color_map [i].name)
233 continue;
234 klen = strlen (color_map [i].name);
236 if (strncmp (color_string, color_map [i].name, klen) == 0){
237 color_string += klen;
238 get_two_colors (&color_string, &color_map [i]);
239 found = 1;
242 if (!found){
243 while (*color_string && *color_string != ':')
244 color_string++;
245 if (*color_string)
246 color_string++;
249 g_free (p);
252 static void configure_colors (void)
254 extern char *command_line_colors;
256 configure_colors_string (default_colors);
257 configure_colors_string (setup_color_string);
258 configure_colors_string (term_color_string);
259 configure_colors_string (getenv ("MC_COLOR_TABLE"));
260 configure_colors_string (command_line_colors);
263 #ifndef HAVE_SLANG
264 #define MAX_PAIRS 64
265 int attr_pairs [MAX_PAIRS];
266 #endif
268 static void
269 load_dialog_colors (void)
271 dialog_colors [0] = COLOR_NORMAL;
272 dialog_colors [1] = COLOR_FOCUS;
273 dialog_colors [2] = COLOR_HOT_NORMAL;
274 dialog_colors [3] = COLOR_HOT_FOCUS;
276 alarm_colors [0] = ERROR_COLOR;
277 alarm_colors [1] = REVERSE_COLOR;
278 alarm_colors [2] = ERROR_HOT_NORMAL;
279 alarm_colors [3] = ERROR_HOT_FOCUS;
282 void init_colors (void)
284 size_t i;
286 int hascolors;
288 /* FIXME: if S-Lang is used, this function must be called regardless
289 of whether we are interested in its result */
290 hascolors = has_colors ();
292 if (!disable_colors && hascolors){
293 use_colors = 1;
296 if (use_colors){
297 start_color ();
298 configure_colors ();
300 #ifndef HAVE_SLANG
301 if (ELEMENTS (color_map) > MAX_PAIRS){
302 /* This message should only be seen by the developers */
303 fprintf (stderr,
304 "Too many defined colors, resize MAX_PAIRS on color.c");
305 exit (1);
307 #endif /* !HAVE_SLANG */
309 if (use_colors) {
310 #ifdef HAVE_SLANG
312 * We are relying on undocumented feature of
313 * S-Lang to make COLOR_PAIR(DEFAULT_COLOR_INDEX)
314 * the default fg/bg of the terminal.
315 * Hopefully, future versions of S-Lang will
316 * document this feature.
318 SLtt_set_color (DEFAULT_COLOR_INDEX, NULL, "default", "default");
319 #else
320 /* Always white on black */
321 mc_init_pair(DEFAULT_COLOR_INDEX, COLOR_WHITE, COLOR_BLACK);
322 #endif /* !HAVE_SLANG */
325 for (i = 0; i < ELEMENTS (color_map); i++){
326 if (!color_map [i].name)
327 continue;
329 mc_init_pair (i+1, color_map_fg(i), color_map_bg(i));
331 #ifndef HAVE_SLANG
333 * ncurses doesn't remember bold attribute in the color pairs,
334 * so we should keep track of it in a separate array.
336 attr_pairs [i+1] = color_map [i].fg & A_BOLD;
337 #endif /* !HAVE_SLANG */
340 load_dialog_colors ();
343 /* Functions necessary to implement syntax highlighting */
345 static int max_index = 0;
347 static int
348 alloc_color_pair (CTYPE foreground, CTYPE background)
350 mc_init_pair (++max_index, foreground, background);
351 return max_index;
354 static struct colors_avail {
355 struct colors_avail *next;
356 char *fg, *bg;
357 int index;
358 } c = { 0, 0, 0, 0 };
360 #ifdef HAVE_SLANG
361 void
362 mc_init_pair (int index, CTYPE foreground, CTYPE background)
364 if (!background)
365 background = "default";
367 if (!foreground)
368 foreground = "default";
370 SLtt_set_color (index, "", (char *) foreground, (char *) background);
371 if (index > max_index)
372 max_index = index;
376 try_alloc_color_pair (const char *fg, const char *bg)
378 struct colors_avail *p = &c;
380 c.index = EDITOR_NORMAL_COLOR_INDEX;
381 for (;;) {
382 if (((fg && p->fg) ? !strcmp (fg, p->fg) : fg == p->fg) != 0
383 && ((bg && p->bg) ? !strcmp (bg, p->bg) : bg == p->bg) != 0)
384 return p->index;
385 if (!p->next)
386 break;
387 p = p->next;
389 p->next = g_new (struct colors_avail, 1);
390 p = p->next;
391 p->next = 0;
392 p->fg = fg ? g_strdup (fg) : 0;
393 p->bg = bg ? g_strdup (bg) : 0;
394 if (!fg)
395 /* Index in color_map array = COLOR_INDEX - 1 */
396 fg = color_map[EDITOR_NORMAL_COLOR_INDEX - 1].fg;
397 if (!bg)
398 bg = color_map[EDITOR_NORMAL_COLOR_INDEX - 1].bg;
399 p->index = alloc_color_pair (fg, bg);
400 return p->index;
403 #else /* !HAVE_SLANG */
404 void
405 mc_init_pair (int index, CTYPE foreground, CTYPE background)
407 init_pair (index, foreground, background);
408 if (index > max_index)
409 max_index = index;
413 try_alloc_color_pair (const char *fg, const char *bg)
415 int fg_index, bg_index;
416 int bold_attr;
417 struct colors_avail *p = &c;
419 c.index = EDITOR_NORMAL_COLOR_INDEX;
420 for (;;) {
421 if (((fg && p->fg) ? !strcmp (fg, p->fg) : fg == p->fg) != 0
422 && ((bg && p->bg) ? !strcmp (bg, p->bg) : bg == p->bg) != 0)
423 return p->index;
424 if (!p->next)
425 break;
426 p = p->next;
428 p->next = g_new (struct colors_avail, 1);
429 p = p->next;
430 p->next = 0;
431 p->fg = fg ? g_strdup (fg) : 0;
432 p->bg = bg ? g_strdup (bg) : 0;
433 if (!fg)
434 /* Index in color_map array = COLOR_INDEX - 1 */
435 fg_index = color_map[EDITOR_NORMAL_COLOR_INDEX - 1].fg;
436 else
437 get_color (fg, &fg_index);
439 if (!bg)
440 bg_index = color_map[EDITOR_NORMAL_COLOR_INDEX - 1].bg;
441 else
442 get_color (bg, &bg_index);
444 bold_attr = fg_index & A_BOLD;
445 fg_index = fg_index & COLOR_WHITE;
446 bg_index = bg_index & COLOR_WHITE;
448 p->index = alloc_color_pair (fg_index, bg_index);
449 attr_pairs [p->index] = bold_attr;
450 return p->index;
452 #endif /* !HAVE_SLANG */
454 void
455 done_colors (void)
457 struct colors_avail *p, *next;
459 for (p = c.next; p; p = next) {
460 next = p->next;
461 g_free (p->fg);
462 g_free (p->bg);
463 g_free (p);
465 c.next = NULL;