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 <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
2005-07-14 00:18:46 +04:00
|
|
|
#include <assert.h>
|
2001-05-05 19:01:42 +04:00
|
|
|
#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-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);
|
|
|
|
|
2001-05-05 19:01:42 +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) {
|
2002-09-27 18:21:59 +04:00
|
|
|
short background = tmpcolor->bg;
|
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
|
|
|
|
2002-09-27 18:21:59 +04:00
|
|
|
init_pair(tmpcolor->pairnum, tmpcolor->fg, 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) {
|
2002-09-27 18:21:59 +04:00
|
|
|
const exttype *e;
|
|
|
|
|
2002-05-04 07:47:33 +04:00
|
|
|
for (e = tmpsyntax->extensions; e != NULL; e = e->next) {
|
2005-03-10 23:55:11 +03:00
|
|
|
/* Set colorstrings if we matched the extension regex. */
|
2005-07-09 00:09:16 +04:00
|
|
|
if (regexec(&e->val, 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;
|
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-14 00:18:46 +04:00
|
|
|
if (mbstrcasecmp(tmpsyntax->desc, syntaxstr) == 0) {
|
|
|
|
openfile->colorstrings = tmpsyntax->color;
|
|
|
|
|
|
|
|
if (openfile->colorstrings != NULL)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-07-14 22:33:51 +04:00
|
|
|
/* tmpcolor->start_regex and tmpcolor->end_regex have already been
|
|
|
|
* checked for validity elsewhere. Compile their associated regexes
|
|
|
|
* if we haven't already. */
|
2005-07-14 00:18:46 +04:00
|
|
|
for (tmpcolor = openfile->colorstrings; tmpcolor != NULL;
|
|
|
|
tmpcolor = tmpcolor->next) {
|
2005-07-14 22:33:51 +04:00
|
|
|
if (tmpcolor->start_regex != 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
|
|
|
|
|
|
|
if (tmpcolor->end_regex != 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
|
|
|
}
|
|
|
|
}
|
2005-07-14 00:18:46 +04:00
|
|
|
|
|
|
|
color_init();
|
2002-05-04 07:47:33 +04:00
|
|
|
}
|
|
|
|
|
2001-05-05 19:01:42 +04:00
|
|
|
#endif /* ENABLE_COLOR */
|