2001-05-05 19:01:42 +04:00
|
|
|
/* $Id$ */
|
|
|
|
/**************************************************************************
|
|
|
|
* color.c *
|
|
|
|
* *
|
2005-03-10 23:55:11 +03:00
|
|
|
* Copyright (C) 1999-2005 Chris Allegretta *
|
2001-05-05 19:01:42 +04:00
|
|
|
* This program is free software; you can redistribute it and/or modify *
|
|
|
|
* it under the terms of the GNU General Public License as published by *
|
2001-10-24 15:33:54 +04:00
|
|
|
* the Free Software Foundation; either version 2, or (at your option) *
|
2001-05-05 19:01:42 +04:00
|
|
|
* any later version. *
|
|
|
|
* *
|
2005-05-15 23:57:17 +04:00
|
|
|
* This program is distributed in the hope that it will be useful, but *
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
|
|
|
|
* General Public License for more details. *
|
2001-05-05 19:01:42 +04:00
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
|
|
|
* along with this program; if not, write to the Free Software *
|
2005-05-15 23:57:17 +04:00
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
|
|
|
|
* 02110-1301, USA. *
|
2001-05-05 19:01:42 +04:00
|
|
|
* *
|
|
|
|
**************************************************************************/
|
|
|
|
|
2004-11-18 02:17:05 +03:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include <config.h>
|
|
|
|
#endif
|
2001-05-05 19:01:42 +04:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include "proto.h"
|
|
|
|
|
2002-07-19 05:08:59 +04:00
|
|
|
#ifdef ENABLE_COLOR
|
|
|
|
|
2002-09-27 18:21:59 +04:00
|
|
|
/* For each syntax list entry, we go through the list of colors and
|
|
|
|
* assign color pairs. */
|
|
|
|
void set_colorpairs(void)
|
2001-05-05 19:01:42 +04:00
|
|
|
{
|
2002-09-27 18:21:59 +04:00
|
|
|
const syntaxtype *this_syntax = syntaxes;
|
|
|
|
|
2003-11-06 01:04:08 +03:00
|
|
|
for (; this_syntax != NULL; this_syntax = this_syntax->next) {
|
2002-09-27 18:21:59 +04:00
|
|
|
colortype *this_color = this_syntax->color;
|
|
|
|
int color_pair = 1;
|
|
|
|
|
2003-11-06 01:04:08 +03:00
|
|
|
for (; this_color != NULL; this_color = this_color->next) {
|
2002-09-27 18:21:59 +04:00
|
|
|
const colortype *beforenow = this_syntax->color;
|
|
|
|
|
2005-03-10 23:55:11 +03:00
|
|
|
for (; beforenow != this_color &&
|
|
|
|
(beforenow->fg != this_color->fg ||
|
|
|
|
beforenow->bg != this_color->bg ||
|
|
|
|
beforenow->bright != this_color->bright);
|
|
|
|
beforenow = beforenow->next)
|
2002-09-27 18:21:59 +04:00
|
|
|
;
|
|
|
|
|
2005-03-10 23:55:11 +03:00
|
|
|
if (beforenow != this_color)
|
2002-09-27 18:21:59 +04:00
|
|
|
this_color->pairnum = beforenow->pairnum;
|
|
|
|
else {
|
|
|
|
this_color->pairnum = color_pair;
|
|
|
|
color_pair++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2001-11-29 05:42:27 +03:00
|
|
|
|
2005-07-15 04:36:49 +04:00
|
|
|
/* Initialize the color information. */
|
2005-07-14 00:18:46 +04:00
|
|
|
void color_init(void)
|
2002-09-27 18:21:59 +04:00
|
|
|
{
|
2005-07-14 00:18:46 +04:00
|
|
|
assert(openfile != NULL);
|
|
|
|
|
2005-07-27 07:12:22 +04:00
|
|
|
if (has_colors()) {
|
2005-07-14 00:18:46 +04:00
|
|
|
const colortype *tmpcolor;
|
2002-09-27 18:21:59 +04:00
|
|
|
#ifdef HAVE_USE_DEFAULT_COLORS
|
2005-03-10 23:55:11 +03:00
|
|
|
bool defok;
|
2002-09-27 18:21:59 +04:00
|
|
|
#endif
|
|
|
|
|
2001-05-05 19:01:42 +04:00
|
|
|
start_color();
|
2001-11-29 05:42:27 +03:00
|
|
|
|
2001-05-05 19:01:42 +04:00
|
|
|
#ifdef HAVE_USE_DEFAULT_COLORS
|
2005-07-14 00:18:46 +04:00
|
|
|
/* Add in colors, if available. */
|
2005-03-10 23:55:11 +03:00
|
|
|
defok = (use_default_colors() != ERR);
|
2001-05-05 19:01:42 +04:00
|
|
|
#endif
|
2001-11-29 05:42:27 +03:00
|
|
|
|
2005-07-14 00:18:46 +04:00
|
|
|
for (tmpcolor = openfile->colorstrings; tmpcolor != NULL;
|
2001-11-29 05:42:27 +03:00
|
|
|
tmpcolor = tmpcolor->next) {
|
2005-07-15 04:17:13 +04:00
|
|
|
short foreground = tmpcolor->fg, background = tmpcolor->bg;
|
|
|
|
if (foreground == -1) {
|
|
|
|
#ifdef HAVE_USE_DEFAULT_COLORS
|
|
|
|
if (!defok)
|
|
|
|
#endif
|
|
|
|
foreground = COLOR_WHITE;
|
|
|
|
}
|
2001-11-29 05:42:27 +03:00
|
|
|
|
2005-07-14 00:18:46 +04:00
|
|
|
if (background == -1) {
|
2002-09-27 18:21:59 +04:00
|
|
|
#ifdef HAVE_USE_DEFAULT_COLORS
|
2003-01-13 04:35:15 +03:00
|
|
|
if (!defok)
|
2002-09-27 18:21:59 +04:00
|
|
|
#endif
|
|
|
|
background = COLOR_BLACK;
|
2005-07-14 00:18:46 +04:00
|
|
|
}
|
2001-12-02 07:55:44 +03:00
|
|
|
|
2005-07-15 04:17:13 +04:00
|
|
|
init_pair(tmpcolor->pairnum, foreground, background);
|
2001-11-29 05:42:27 +03:00
|
|
|
|
2001-12-12 04:45:01 +03:00
|
|
|
#ifdef DEBUG
|
2005-07-14 00:18:46 +04:00
|
|
|
fprintf(stderr, "init_pair(): fg = %hd, bg = %hd\n",
|
2005-03-10 23:55:11 +03:00
|
|
|
tmpcolor->fg, tmpcolor->bg);
|
2001-12-12 04:45:01 +03:00
|
|
|
#endif
|
2001-11-29 05:42:27 +03:00
|
|
|
}
|
|
|
|
}
|
2001-05-05 19:01:42 +04:00
|
|
|
}
|
|
|
|
|
2003-01-13 04:35:15 +03:00
|
|
|
/* Update the color information based on the current filename. */
|
2005-07-14 00:18:46 +04:00
|
|
|
void color_update(void)
|
2002-05-04 07:47:33 +04:00
|
|
|
{
|
2002-09-27 18:21:59 +04:00
|
|
|
const syntaxtype *tmpsyntax;
|
2005-07-14 00:18:46 +04:00
|
|
|
colortype *tmpcolor;
|
|
|
|
|
|
|
|
assert(openfile != NULL);
|
2002-05-04 07:47:33 +04:00
|
|
|
|
2005-07-14 00:18:46 +04:00
|
|
|
openfile->colorstrings = NULL;
|
2005-03-10 23:55:11 +03:00
|
|
|
for (tmpsyntax = syntaxes; tmpsyntax != NULL;
|
|
|
|
tmpsyntax = tmpsyntax->next) {
|
2005-07-30 01:42:08 +04:00
|
|
|
exttype *e;
|
2002-09-27 18:21:59 +04:00
|
|
|
|
2002-05-04 07:47:33 +04:00
|
|
|
for (e = tmpsyntax->extensions; e != NULL; e = e->next) {
|
2005-07-30 02:08:56 +04:00
|
|
|
bool not_compiled = (e->ext == NULL);
|
2005-07-30 01:59:44 +04:00
|
|
|
|
2005-07-30 01:42:08 +04:00
|
|
|
/* e->ext_regex has already been checked for validity
|
|
|
|
* elsewhere. Compile its specified regex if we haven't
|
|
|
|
* already. */
|
2005-07-30 02:08:56 +04:00
|
|
|
if (not_compiled) {
|
2005-07-30 01:42:08 +04:00
|
|
|
e->ext = (regex_t *)nmalloc(sizeof(regex_t));
|
|
|
|
regcomp(e->ext, e->ext_regex, REG_EXTENDED);
|
|
|
|
}
|
|
|
|
|
2005-03-10 23:55:11 +03:00
|
|
|
/* Set colorstrings if we matched the extension regex. */
|
2005-07-30 01:42:08 +04:00
|
|
|
if (regexec(e->ext, openfile->filename, 0, NULL, 0) == 0)
|
2005-07-14 00:18:46 +04:00
|
|
|
openfile->colorstrings = tmpsyntax->color;
|
2002-05-13 00:54:16 +04:00
|
|
|
|
2005-07-14 00:18:46 +04:00
|
|
|
if (openfile->colorstrings != NULL)
|
2002-09-27 18:21:59 +04:00
|
|
|
break;
|
2005-07-30 01:59:44 +04:00
|
|
|
|
|
|
|
/* Decompile e->ext_regex's specified regex if we aren't
|
|
|
|
* going to use it. */
|
2005-07-30 02:08:56 +04:00
|
|
|
if (not_compiled) {
|
2005-07-30 01:59:44 +04:00
|
|
|
regfree(e->ext);
|
|
|
|
free(e->ext);
|
2005-07-30 02:08:56 +04:00
|
|
|
e->ext = NULL;
|
2005-07-30 01:59:44 +04:00
|
|
|
}
|
2002-05-04 07:47:33 +04:00
|
|
|
}
|
|
|
|
}
|
2002-05-04 08:23:30 +04:00
|
|
|
|
2005-03-10 23:55:11 +03:00
|
|
|
/* If we haven't found a match, use the override string. */
|
2005-07-14 00:18:46 +04:00
|
|
|
if (openfile->colorstrings == NULL && syntaxstr != NULL) {
|
2004-05-27 22:39:16 +04:00
|
|
|
for (tmpsyntax = syntaxes; tmpsyntax != NULL;
|
2005-03-10 23:55:11 +03:00
|
|
|
tmpsyntax = tmpsyntax->next) {
|
2005-07-15 02:28:22 +04:00
|
|
|
if (mbstrcasecmp(tmpsyntax->desc, syntaxstr) == 0)
|
2005-07-14 00:18:46 +04:00
|
|
|
openfile->colorstrings = tmpsyntax->color;
|
|
|
|
|
2005-07-15 02:28:22 +04:00
|
|
|
if (openfile->colorstrings != NULL)
|
|
|
|
break;
|
2005-07-14 00:18:46 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (tmpcolor = openfile->colorstrings; tmpcolor != NULL;
|
|
|
|
tmpcolor = tmpcolor->next) {
|
2005-07-30 01:42:08 +04:00
|
|
|
/* tmpcolor->start_regex and tmpcolor->end_regex have already
|
|
|
|
* been checked for validity elsewhere. Compile their specified
|
|
|
|
* regexes if we haven't already. */
|
|
|
|
if (tmpcolor->start == NULL) {
|
2005-07-14 00:18:46 +04:00
|
|
|
tmpcolor->start = (regex_t *)nmalloc(sizeof(regex_t));
|
2005-07-14 22:33:51 +04:00
|
|
|
regcomp(tmpcolor->start, tmpcolor->start_regex,
|
|
|
|
REG_EXTENDED | (tmpcolor->icase ? REG_ICASE : 0));
|
2005-07-14 00:18:46 +04:00
|
|
|
}
|
2005-07-14 22:33:51 +04:00
|
|
|
|
2005-07-30 01:42:08 +04:00
|
|
|
if (tmpcolor->end_regex != NULL && tmpcolor->end == NULL) {
|
2005-07-14 00:18:46 +04:00
|
|
|
tmpcolor->end = (regex_t *)nmalloc(sizeof(regex_t));
|
2005-07-14 22:33:51 +04:00
|
|
|
regcomp(tmpcolor->end, tmpcolor->end_regex,
|
|
|
|
REG_EXTENDED | (tmpcolor->icase ? REG_ICASE : 0));
|
2002-05-04 08:23:30 +04:00
|
|
|
}
|
|
|
|
}
|
2002-05-04 07:47:33 +04:00
|
|
|
}
|
|
|
|
|
2001-05-05 19:01:42 +04:00
|
|
|
#endif /* ENABLE_COLOR */
|