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. */
26 #include "setup.h" /* For the externs */
29 /* Set to force black and white display at program startup */
30 int disable_colors
= 0;
32 /* Set if we are actually using colors */
35 /* Color styles for normal and error dialogs */
36 int dialog_colors
[4];
39 #define ELEMENTS(arr) ( sizeof(arr) / sizeof((arr)[0]) )
42 # define color_map_fg(n) color_map[n].fg
43 # define color_map_bg(n) color_map[n].bg
45 # define color_map_fg(n) (color_map[n].fg & COLOR_WHITE)
46 # define color_map_bg(n) (color_map[n].bg & COLOR_WHITE)
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 */
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 */
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 */
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
{
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:"
141 "reverse=black,lightgray:"
142 "dnormal=black,lightgray:"
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:"
157 "directory=white,blue:"
158 "executable=brightgreen,blue:"
159 "link=lightgray,blue:"
160 "stalelink=brightred,blue:"
161 "device=brightmagenta,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";
171 # define color_value(i) color_table [i].name
172 # define color_name(i) color_table [i].name
174 # define color_value(i) color_table [i].value
175 # define color_name(i) color_table [i].name
178 static void get_color (const char *cpp
, CTYPE
*colp
)
182 for (i
= 0; i
< ELEMENTS(color_table
); i
++){
183 if (strcmp (cpp
, color_name (i
)) == 0){
184 *colp
= color_value (i
);
190 static void get_two_colors (char **cpp
, struct colorpair
*colorpairp
)
200 get_color (*cpp
, state
? &colorpairp
->bg
: &colorpairp
->fg
);
209 get_color (*cpp
, &colorpairp
->fg
);
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
;
223 if (!the_color_string
)
226 p
= color_string
= g_strdup (the_color_string
);
227 while (color_string
&& *color_string
){
228 while (*color_string
== ' ' || *color_string
== '\t')
232 for (i
= 0; i
< ELEMENTS(color_map
); i
++){
235 if (!color_map
[i
].name
)
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
]);
246 while (*color_string
&& *color_string
!= ':')
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
);
268 int attr_pairs
[MAX_PAIRS
];
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)
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
){
304 if (ELEMENTS (color_map
) > MAX_PAIRS
){
305 /* This message should only be seen by the developers */
307 "Too many defined colors, resize MAX_PAIRS on color.c");
310 #endif /* !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");
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
)
332 mc_init_pair (i
+1, color_map_fg(i
), color_map_bg(i
));
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;
351 alloc_color_pair (CTYPE foreground
, CTYPE background
)
353 mc_init_pair (++max_index
, foreground
, background
);
357 static struct colors_avail
{
358 struct colors_avail
*next
;
361 } c
= { 0, 0, 0, 0 };
365 mc_init_pair (int index
, CTYPE foreground
, CTYPE background
)
368 background
= "default";
371 foreground
= "default";
373 SLtt_set_color (index
, "", (char *) foreground
, (char *) background
);
374 if (index
> max_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
;
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)
392 p
->next
= g_new (struct colors_avail
, 1);
395 p
->fg
= fg
? g_strdup (fg
) : 0;
396 p
->bg
= bg
? g_strdup (bg
) : 0;
398 /* Index in color_map array = COLOR_INDEX - 1 */
399 fg
= color_map
[EDITOR_NORMAL_COLOR_INDEX
- 1].fg
;
401 bg
= color_map
[EDITOR_NORMAL_COLOR_INDEX
- 1].bg
;
402 p
->index
= alloc_color_pair (fg
, bg
);
406 #else /* !HAVE_SLANG */
408 mc_init_pair (int index
, CTYPE foreground
, CTYPE background
)
410 init_pair (index
, foreground
, background
);
411 if (index
> max_index
)
416 try_alloc_color_pair (const char *fg
, const char *bg
)
418 int fg_index
, bg_index
;
420 struct colors_avail
*p
= &c
;
422 c
.index
= EDITOR_NORMAL_COLOR_INDEX
;
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)
431 p
->next
= g_new (struct colors_avail
, 1);
434 p
->fg
= fg
? g_strdup (fg
) : 0;
435 p
->bg
= bg
? g_strdup (bg
) : 0;
437 /* Index in color_map array = COLOR_INDEX - 1 */
438 fg_index
= color_map
[EDITOR_NORMAL_COLOR_INDEX
- 1].fg
;
440 get_color (fg
, &fg_index
);
443 bg_index
= color_map
[EDITOR_NORMAL_COLOR_INDEX
- 1].bg
;
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
;
455 #endif /* !HAVE_SLANG */
460 struct colors_avail
*p
, *next
;
462 for (p
= c
.next
; p
; p
= next
) {