Updated italian translation
[midnight-commander.git] / src / color.c
blobfe55a3814eb86025cc3790bbe4495e1934af08b6
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
18 #include <config.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
24 #include "global.h"
25 #include "tty.h"
26 #include "setup.h" /* For the externs */
27 #include "color.h"
29 /* Set to force black and white display at program startup */
30 int disable_colors = 0;
32 /* Set if we are actually using colors */
33 int use_colors = 0;
35 /* Color styles for normal and error dialogs */
36 int dialog_colors [4];
37 int alarm_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 const char *name; /* Name of the entry */
51 CTYPE fg; /* foreground color */
52 CTYPE bg; /* background color */
55 static 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 stale nor link to directory) */
88 { "stalelink=", 0, 0 }, /* stale 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 */
103 /* error dialog colors start at 37 */
104 { "errdhotnormal=", 0, 0 }, /* Error dialog normal/hot */ /* 37 */
105 { "errdhotfocus=", 0, 0 }, /* Error dialog focused/hot */
108 struct color_table_s {
109 const char *name;
110 int value;
114 static struct color_table_s const color_table [] = {
115 { "black", COLOR_BLACK },
116 { "gray", COLOR_BLACK | A_BOLD },
117 { "red", COLOR_RED },
118 { "brightred", COLOR_RED | A_BOLD },
119 { "green", COLOR_GREEN },
120 { "brightgreen", COLOR_GREEN | A_BOLD },
121 { "brown", COLOR_YELLOW },
122 { "yellow", COLOR_YELLOW | A_BOLD },
123 { "blue", COLOR_BLUE },
124 { "brightblue", COLOR_BLUE | A_BOLD },
125 { "magenta", COLOR_MAGENTA },
126 { "brightmagenta", COLOR_MAGENTA | A_BOLD },
127 { "cyan", COLOR_CYAN },
128 { "brightcyan", COLOR_CYAN | A_BOLD },
129 { "lightgray", COLOR_WHITE },
130 { "white", COLOR_WHITE | A_BOLD },
131 { "default", 0 } /* default color of the terminal */
134 static const char *default_colors =
135 "normal=lightgray,blue:"
136 "selected=black,cyan:"
137 "marked=yellow,blue:"
138 "markselect=yellow,cyan:"
139 "errors=white,red:"
140 "menu=white,cyan:"
141 "reverse=black,lightgray:"
142 "dnormal=black,lightgray:"
143 "dfocus=black,cyan:"
144 "dhotnormal=blue,lightgray:"
145 "dhotfocus=blue,cyan:"
146 "viewunderline=brightred,blue:"
147 "menuhot=yellow,cyan:"
148 "menusel=white,black:"
149 "menuhotsel=yellow,black:"
150 "helpnormal=black,lightgray:"
151 "helpitalic=red,lightgray:"
152 "helpbold=blue,lightgray:"
153 "helplink=black,cyan:"
154 "helpslink=yellow,blue:"
155 "gauge=white,black:"
156 "input=black,cyan:"
157 "directory=white,blue:"
158 "executable=brightgreen,blue:"
159 "link=lightgray,blue:"
160 "stalelink=brightred,blue:"
161 "device=brightmagenta,blue:"
162 "core=red,blue:"
163 "special=black,blue:"
164 "editnormal=lightgray,blue:"
165 "editbold=yellow,blue:"
166 "editmarked=black,cyan:"
167 "errdhotnormal=yellow,red:"
168 "errdhotfocus=yellow,lightgray";
170 #ifdef HAVE_SLANG
171 # define color_value(i) color_table [i].name
172 # define color_name(i) color_table [i].name
173 #else
174 # define color_value(i) color_table [i].value
175 # define color_name(i) color_table [i].name
176 #endif
178 static void get_color (const char *cpp, CTYPE *colp)
180 size_t i;
182 for (i = 0; i < ELEMENTS(color_table); i++){
183 if (strcmp (cpp, color_name (i)) == 0){
184 *colp = color_value (i);
185 return;
190 static void get_two_colors (char **cpp, struct colorpair *colorpairp)
192 char *p = *cpp;
193 int state;
195 state = 0;
197 for (; *p; p++){
198 if (*p == ':'){
199 *p = 0;
200 get_color (*cpp, state ? &colorpairp->bg : &colorpairp->fg);
201 *p = ':';
202 *cpp = p + 1;
203 return;
206 if (*p == ','){
207 state = 1;
208 *p = 0;
209 get_color (*cpp, &colorpairp->fg);
210 *p = ',';
211 *cpp = p + 1;
214 get_color (*cpp, state ? &colorpairp->bg : &colorpairp->fg);
217 static void configure_colors_string (const char *the_color_string)
219 char *color_string, *p;
220 size_t i;
221 int found;
223 if (!the_color_string)
224 return;
226 p = color_string = g_strdup (the_color_string);
227 while (color_string && *color_string){
228 while (*color_string == ' ' || *color_string == '\t')
229 color_string++;
231 found = 0;
232 for (i = 0; i < ELEMENTS(color_map); i++){
233 int klen;
235 if (!color_map [i].name)
236 continue;
237 klen = strlen (color_map [i].name);
239 if (strncmp (color_string, color_map [i].name, klen) == 0){
240 color_string += klen;
241 get_two_colors (&color_string, &color_map [i]);
242 found = 1;
245 if (!found){
246 while (*color_string && *color_string != ':')
247 color_string++;
248 if (*color_string)
249 color_string++;
252 g_free (p);
255 static void configure_colors (void)
257 extern char *command_line_colors;
259 configure_colors_string (default_colors);
260 configure_colors_string (setup_color_string);
261 configure_colors_string (term_color_string);
262 configure_colors_string (getenv ("MC_COLOR_TABLE"));
263 configure_colors_string (command_line_colors);
266 #ifndef HAVE_SLANG
267 #define MAX_PAIRS 64
268 int attr_pairs [MAX_PAIRS];
269 #endif
271 static void
272 load_dialog_colors (void)
274 dialog_colors [0] = COLOR_NORMAL;
275 dialog_colors [1] = COLOR_FOCUS;
276 dialog_colors [2] = COLOR_HOT_NORMAL;
277 dialog_colors [3] = COLOR_HOT_FOCUS;
279 alarm_colors [0] = ERROR_COLOR;
280 alarm_colors [1] = REVERSE_COLOR;
281 alarm_colors [2] = ERROR_HOT_NORMAL;
282 alarm_colors [3] = ERROR_HOT_FOCUS;
285 void init_colors (void)
287 size_t i;
289 int hascolors;
291 /* FIXME: if S-Lang is used, this function must be called regardless
292 of whether we are interested in its result */
293 hascolors = has_colors ();
295 if (!disable_colors && hascolors){
296 use_colors = 1;
299 if (use_colors){
300 start_color ();
301 configure_colors ();
303 #ifndef HAVE_SLANG
304 if (ELEMENTS (color_map) > MAX_PAIRS){
305 /* This message should only be seen by the developers */
306 fprintf (stderr,
307 "Too many defined colors, resize MAX_PAIRS on color.c");
308 exit (1);
310 #endif /* !HAVE_SLANG */
312 if (use_colors) {
313 #ifdef HAVE_SLANG
315 * We are relying on undocumented feature of
316 * S-Lang to make COLOR_PAIR(DEFAULT_COLOR_INDEX)
317 * the default fg/bg of the terminal.
318 * Hopefully, future versions of S-Lang will
319 * document this feature.
321 SLtt_set_color (DEFAULT_COLOR_INDEX, NULL, "default", "default");
322 #else
323 /* Always white on black */
324 mc_init_pair(DEFAULT_COLOR_INDEX, COLOR_WHITE, COLOR_BLACK);
325 #endif /* !HAVE_SLANG */
328 for (i = 0; i < ELEMENTS (color_map); i++){
329 if (!color_map [i].name)
330 continue;
332 mc_init_pair (i+1, color_map_fg(i), color_map_bg(i));
334 #ifndef HAVE_SLANG
336 * ncurses doesn't remember bold attribute in the color pairs,
337 * so we should keep track of it in a separate array.
339 attr_pairs [i+1] = color_map [i].fg & A_BOLD;
340 #endif /* !HAVE_SLANG */
343 load_dialog_colors ();
346 /* Functions necessary to implement syntax highlighting */
348 static int max_index = 0;
350 static int
351 alloc_color_pair (CTYPE foreground, CTYPE background)
353 mc_init_pair (++max_index, foreground, background);
354 return max_index;
357 static struct colors_avail {
358 struct colors_avail *next;
359 char *fg, *bg;
360 int index;
361 } c = { 0, 0, 0, 0 };
363 #ifdef HAVE_SLANG
364 void
365 mc_init_pair (int index, CTYPE foreground, CTYPE background)
367 if (!background)
368 background = "default";
370 if (!foreground)
371 foreground = "default";
373 SLtt_set_color (index, "", (char *) foreground, (char *) background);
374 if (index > max_index)
375 max_index = index;
379 try_alloc_color_pair (const char *fg, const char *bg)
381 struct colors_avail *p = &c;
383 c.index = EDITOR_NORMAL_COLOR_INDEX;
384 for (;;) {
385 if (((fg && p->fg) ? !strcmp (fg, p->fg) : fg == p->fg) != 0
386 && ((bg && p->bg) ? !strcmp (bg, p->bg) : bg == p->bg) != 0)
387 return p->index;
388 if (!p->next)
389 break;
390 p = p->next;
392 p->next = g_new (struct colors_avail, 1);
393 p = p->next;
394 p->next = 0;
395 p->fg = fg ? g_strdup (fg) : 0;
396 p->bg = bg ? g_strdup (bg) : 0;
397 if (!fg)
398 /* Index in color_map array = COLOR_INDEX - 1 */
399 fg = color_map[EDITOR_NORMAL_COLOR_INDEX - 1].fg;
400 if (!bg)
401 bg = color_map[EDITOR_NORMAL_COLOR_INDEX - 1].bg;
402 p->index = alloc_color_pair (fg, bg);
403 return p->index;
406 #else /* !HAVE_SLANG */
407 void
408 mc_init_pair (int index, CTYPE foreground, CTYPE background)
410 init_pair (index, foreground, background);
411 if (index > max_index)
412 max_index = index;
416 try_alloc_color_pair (const char *fg, const char *bg)
418 int fg_index, bg_index;
419 int bold_attr;
420 struct colors_avail *p = &c;
422 c.index = EDITOR_NORMAL_COLOR_INDEX;
423 for (;;) {
424 if (((fg && p->fg) ? !strcmp (fg, p->fg) : fg == p->fg) != 0
425 && ((bg && p->bg) ? !strcmp (bg, p->bg) : bg == p->bg) != 0)
426 return p->index;
427 if (!p->next)
428 break;
429 p = p->next;
431 p->next = g_new (struct colors_avail, 1);
432 p = p->next;
433 p->next = 0;
434 p->fg = fg ? g_strdup (fg) : 0;
435 p->bg = bg ? g_strdup (bg) : 0;
436 if (!fg)
437 /* Index in color_map array = COLOR_INDEX - 1 */
438 fg_index = color_map[EDITOR_NORMAL_COLOR_INDEX - 1].fg;
439 else
440 get_color (fg, &fg_index);
442 if (!bg)
443 bg_index = color_map[EDITOR_NORMAL_COLOR_INDEX - 1].bg;
444 else
445 get_color (bg, &bg_index);
447 bold_attr = fg_index & A_BOLD;
448 fg_index = fg_index & COLOR_WHITE;
449 bg_index = bg_index & COLOR_WHITE;
451 p->index = alloc_color_pair (fg_index, bg_index);
452 attr_pairs [p->index] = bold_attr;
453 return p->index;
455 #endif /* !HAVE_SLANG */
457 void
458 done_colors (void)
460 struct colors_avail *p, *next;
462 for (p = c.next; p; p = next) {
463 next = p->next;
464 g_free (p->fg);
465 g_free (p->bg);
466 g_free (p);
468 c.next = NULL;