#include "proto.h"
#include <stdio.h>
#include <string.h>
#ifdef ENABLE_COLOR
void set_colorpairs(void)
{
const syntaxtype *this_syntax = syntaxes;
for (; this_syntax != NULL; this_syntax = this_syntax->next) {
colortype *this_color = this_syntax->color;
int color_pair = 1;
for (; this_color != NULL; this_color = this_color->next) {
const colortype *beforenow = this_syntax->color;
for (; beforenow != this_color &&
(beforenow->fg != this_color->fg ||
beforenow->bg != this_color->bg ||
beforenow->bright != this_color->bright);
beforenow = beforenow->next)
;
if (beforenow != this_color)
this_color->pairnum = beforenow->pairnum;
else {
this_color->pairnum = color_pair;
color_pair++;
}
}
}
}
void color_init(void)
{
assert(openfile != NULL);
if (has_colors()) {
const colortype *tmpcolor;
#ifdef HAVE_USE_DEFAULT_COLORS
bool defok;
#endif
start_color();
#ifdef HAVE_USE_DEFAULT_COLORS
defok = (use_default_colors() != ERR);
#endif
for (tmpcolor = openfile->colorstrings; tmpcolor != NULL;
tmpcolor = tmpcolor->next) {
short foreground = tmpcolor->fg, background = tmpcolor->bg;
if (foreground == -1) {
#ifdef HAVE_USE_DEFAULT_COLORS
if (!defok)
#endif
foreground = COLOR_WHITE;
}
if (background == -1) {
#ifdef HAVE_USE_DEFAULT_COLORS
if (!defok)
#endif
background = COLOR_BLACK;
}
init_pair(tmpcolor->pairnum, foreground, background);
#ifdef DEBUG
fprintf(stderr, "init_pair(): fg = %hd, bg = %hd\n", tmpcolor->fg, tmpcolor->bg);
#endif
}
}
}
void color_update(void)
{
const syntaxtype *tmpsyntax;
colortype *tmpcolor, *defcolor = NULL;
assert(openfile != NULL);
openfile->colorstrings = NULL;
if (syntaxstr != NULL) {
if (strcmp(syntaxstr, "none") == 0)
return;
for (tmpsyntax = syntaxes; tmpsyntax != NULL;
tmpsyntax = tmpsyntax->next) {
if (strcmp(tmpsyntax->desc, syntaxstr) == 0)
openfile->colorstrings = tmpsyntax->color;
if (openfile->colorstrings != NULL)
break;
}
}
if (openfile->colorstrings == NULL) {
for (tmpsyntax = syntaxes; tmpsyntax != NULL;
tmpsyntax = tmpsyntax->next) {
exttype *e;
if (strcmp(tmpsyntax->desc, "default") == 0) {
defcolor = tmpsyntax->color;
continue;
}
for (e = tmpsyntax->extensions; e != NULL; e = e->next) {
bool not_compiled = (e->ext == NULL);
if (not_compiled) {
e->ext = (regex_t *)nmalloc(sizeof(regex_t));
regcomp(e->ext, e->ext_regex, REG_EXTENDED);
}
if (regexec(e->ext, openfile->filename, 0, NULL,
0) == 0)
openfile->colorstrings = tmpsyntax->color;
if (openfile->colorstrings != NULL)
break;
if (not_compiled) {
regfree(e->ext);
free(e->ext);
e->ext = NULL;
}
}
}
}
if (openfile->colorstrings == NULL && defcolor != NULL)
openfile->colorstrings = defcolor;
for (tmpcolor = openfile->colorstrings; tmpcolor != NULL;
tmpcolor = tmpcolor->next) {
if (tmpcolor->start == NULL) {
tmpcolor->start = (regex_t *)nmalloc(sizeof(regex_t));
regcomp(tmpcolor->start, tmpcolor->start_regex,
REG_EXTENDED | (tmpcolor->icase ? REG_ICASE : 0));
}
if (tmpcolor->end_regex != NULL && tmpcolor->end == NULL) {
tmpcolor->end = (regex_t *)nmalloc(sizeof(regex_t));
regcomp(tmpcolor->end, tmpcolor->end_regex,
REG_EXTENDED | (tmpcolor->icase ? REG_ICASE : 0));
}
}
}
#endif