2001-05-05 19:01:42 +04:00
|
|
|
/* $Id$ */
|
|
|
|
/**************************************************************************
|
|
|
|
* color.c *
|
|
|
|
* *
|
2007-10-11 09:01:32 +04:00
|
|
|
* Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007 *
|
|
|
|
* Free Software Foundation, Inc. *
|
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 *
|
2007-08-11 09:17:36 +04:00
|
|
|
* the Free Software Foundation; either version 3, 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
|
|
|
* *
|
|
|
|
**************************************************************************/
|
|
|
|
|
2005-12-08 05:47:10 +03:00
|
|
|
#include "proto.h"
|
2001-05-05 19:01:42 +04:00
|
|
|
|
|
|
|
#include <stdio.h>
|
2005-08-01 08:34:27 +04:00
|
|
|
#include <string.h>
|
2001-05-05 19:01:42 +04:00
|
|
|
|
2002-07-19 05:08:59 +04:00
|
|
|
#ifdef ENABLE_COLOR
|
|
|
|
|
2005-11-16 11:14:16 +03:00
|
|
|
/* For each syntax list entry, go through the list of colors and assign
|
2007-01-01 08:15:32 +03:00
|
|
|
* the color pairs. */
|
2002-09-27 18:21:59 +04:00
|
|
|
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
|
2006-01-02 20:09:35 +03:00
|
|
|
/* Use the default 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-08-01 09:54:11 +04:00
|
|
|
fprintf(stderr, "init_pair(): fg = %hd, bg = %hd\n", 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
|
|
|
{
|
2009-01-25 10:25:17 +03:00
|
|
|
syntaxtype *tmpsyntax;
|
|
|
|
syntaxtype *defsyntax = NULL;
|
2005-08-01 08:23:29 +04:00
|
|
|
colortype *tmpcolor, *defcolor = NULL;
|
2005-07-14 00:18:46 +04:00
|
|
|
|
|
|
|
assert(openfile != NULL);
|
2002-05-04 07:47:33 +04:00
|
|
|
|
2009-01-25 10:25:17 +03:00
|
|
|
openfile->syntax = NULL;
|
2005-07-14 00:18:46 +04:00
|
|
|
openfile->colorstrings = NULL;
|
2005-07-30 01:42:08 +04:00
|
|
|
|
2005-07-31 01:24:56 +04:00
|
|
|
/* If we specified a syntax override string, use it. */
|
|
|
|
if (syntaxstr != NULL) {
|
2005-08-01 08:59:34 +04:00
|
|
|
/* If the syntax override is "none", it's the same as not having
|
|
|
|
* a syntax at all, so get out. */
|
|
|
|
if (strcmp(syntaxstr, "none") == 0)
|
|
|
|
return;
|
|
|
|
|
2005-07-31 01:24:56 +04:00
|
|
|
for (tmpsyntax = syntaxes; tmpsyntax != NULL;
|
|
|
|
tmpsyntax = tmpsyntax->next) {
|
2009-01-25 10:25:17 +03:00
|
|
|
if (strcmp(tmpsyntax->desc, syntaxstr) == 0) {
|
|
|
|
openfile->syntax = tmpsyntax;
|
2005-07-14 00:18:46 +04:00
|
|
|
openfile->colorstrings = tmpsyntax->color;
|
2009-01-25 10:25:17 +03:00
|
|
|
}
|
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-07-31 01:24:56 +04:00
|
|
|
/* If we didn't specify a syntax override string, or if we did and
|
|
|
|
* there was no syntax by that name, get the syntax based on the
|
2008-09-22 03:02:30 +04:00
|
|
|
* file extension, and then look in the header. */
|
2005-07-31 01:24:56 +04:00
|
|
|
if (openfile->colorstrings == 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-31 01:24:56 +04:00
|
|
|
exttype *e;
|
|
|
|
|
2005-08-01 08:23:29 +04:00
|
|
|
/* If this is the default syntax, it has no associated
|
2006-07-30 01:40:32 +04:00
|
|
|
* extensions, which we've checked for elsewhere. Skip over
|
|
|
|
* it here, but keep track of its color regexes. */
|
2005-08-01 08:34:27 +04:00
|
|
|
if (strcmp(tmpsyntax->desc, "default") == 0) {
|
2009-01-25 10:25:17 +03:00
|
|
|
defsyntax = tmpsyntax;
|
2006-05-28 22:51:48 +04:00
|
|
|
defcolor = tmpsyntax->color;
|
2005-08-01 08:23:29 +04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2005-07-31 01:24:56 +04:00
|
|
|
for (e = tmpsyntax->extensions; e != NULL; e = e->next) {
|
|
|
|
bool not_compiled = (e->ext == NULL);
|
|
|
|
|
|
|
|
/* e->ext_regex has already been checked for validity
|
|
|
|
* elsewhere. Compile its specified regex if we haven't
|
|
|
|
* already. */
|
|
|
|
if (not_compiled) {
|
|
|
|
e->ext = (regex_t *)nmalloc(sizeof(regex_t));
|
2008-08-30 09:16:20 +04:00
|
|
|
regcomp(e->ext, fixbounds(e->ext_regex), REG_EXTENDED);
|
2005-07-31 01:24:56 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Set colorstrings if we matched the extension
|
|
|
|
* regex. */
|
|
|
|
if (regexec(e->ext, openfile->filename, 0, NULL,
|
2009-01-25 10:25:17 +03:00
|
|
|
0) == 0) {
|
|
|
|
openfile->syntax = tmpsyntax;
|
2005-07-31 01:24:56 +04:00
|
|
|
openfile->colorstrings = tmpsyntax->color;
|
2009-01-25 10:25:17 +03:00
|
|
|
}
|
2005-07-31 01:24:56 +04:00
|
|
|
|
|
|
|
if (openfile->colorstrings != NULL)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Decompile e->ext_regex's specified regex if we aren't
|
|
|
|
* going to use it. */
|
|
|
|
if (not_compiled) {
|
|
|
|
regfree(e->ext);
|
|
|
|
free(e->ext);
|
|
|
|
e->ext = NULL;
|
|
|
|
}
|
|
|
|
}
|
2005-07-14 00:18:46 +04:00
|
|
|
}
|
2008-09-22 03:02:30 +04:00
|
|
|
|
|
|
|
/* If we haven't matched anything yet, try the headers */
|
|
|
|
if (openfile->colorstrings == NULL) {
|
|
|
|
#ifdef DEBUG
|
|
|
|
fprintf(stderr, "No match for file extensions, looking at headers...\n");
|
|
|
|
#endif
|
|
|
|
for (tmpsyntax = syntaxes; tmpsyntax != NULL;
|
|
|
|
tmpsyntax = tmpsyntax->next) {
|
|
|
|
exttype *e;
|
|
|
|
|
|
|
|
for (e = tmpsyntax->headers; e != NULL; e = e->next) {
|
|
|
|
bool not_compiled = (e->ext == NULL);
|
|
|
|
|
|
|
|
/* e->ext_regex has already been checked for validity
|
|
|
|
* elsewhere. Compile its specified regex if we haven't
|
|
|
|
* already. */
|
|
|
|
if (not_compiled) {
|
|
|
|
e->ext = (regex_t *)nmalloc(sizeof(regex_t));
|
|
|
|
regcomp(e->ext, fixbounds(e->ext_regex), REG_EXTENDED);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set colorstrings if we matched the extension
|
|
|
|
* regex. */
|
|
|
|
#ifdef DEBUG
|
|
|
|
fprintf(stderr, "Comparing header regex \"%s\" to fileage \"%s\"...\n", e->ext_regex, openfile->fileage->data);
|
|
|
|
#endif
|
2009-01-25 10:25:17 +03:00
|
|
|
if (regexec(e->ext, openfile->fileage->data, 0, NULL, 0) == 0) {
|
|
|
|
openfile->syntax = tmpsyntax;
|
2008-09-22 03:02:30 +04:00
|
|
|
openfile->colorstrings = tmpsyntax->color;
|
2009-01-25 10:25:17 +03:00
|
|
|
}
|
2008-09-22 03:02:30 +04:00
|
|
|
|
|
|
|
if (openfile->colorstrings != NULL)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Decompile e->ext_regex's specified regex if we aren't
|
|
|
|
* going to use it. */
|
|
|
|
if (not_compiled) {
|
|
|
|
regfree(e->ext);
|
|
|
|
free(e->ext);
|
|
|
|
e->ext = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2005-07-14 00:18:46 +04:00
|
|
|
}
|
|
|
|
|
2008-09-22 03:02:30 +04:00
|
|
|
|
2005-08-01 08:23:29 +04:00
|
|
|
/* If we didn't get a syntax based on the file extension, and we
|
|
|
|
* have a default syntax, use it. */
|
2009-01-25 10:25:17 +03:00
|
|
|
if (openfile->colorstrings == NULL && defcolor != NULL) {
|
|
|
|
openfile->syntax = defsyntax;
|
2005-08-01 08:23:29 +04:00
|
|
|
openfile->colorstrings = defcolor;
|
2009-01-25 10:25:17 +03:00
|
|
|
}
|
2005-08-01 08:23:29 +04:00
|
|
|
|
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));
|
2008-08-30 09:16:20 +04:00
|
|
|
regcomp(tmpcolor->start, fixbounds(tmpcolor->start_regex),
|
2005-07-14 22:33:51 +04:00
|
|
|
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));
|
2008-08-30 09:16:20 +04:00
|
|
|
regcomp(tmpcolor->end, fixbounds(tmpcolor->end_regex),
|
2005-07-14 22:33:51 +04:00
|
|
|
REG_EXTENDED | (tmpcolor->icase ? REG_ICASE : 0));
|
2002-05-04 08:23:30 +04:00
|
|
|
}
|
|
|
|
}
|
2002-05-04 07:47:33 +04:00
|
|
|
}
|
|
|
|
|
2009-01-25 10:25:17 +03:00
|
|
|
/* Reset multi line strings around a filestruct ptr, trying to be smart about stopping */
|
|
|
|
void reset_multis(filestruct *fileptr)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
filestruct *oof;
|
|
|
|
|
2009-01-30 11:34:27 +03:00
|
|
|
if (!openfile->syntax)
|
|
|
|
return;
|
|
|
|
|
2009-01-25 10:25:17 +03:00
|
|
|
for (i = 0; i < openfile->syntax->nmultis; i++) {
|
|
|
|
for (oof = fileptr->next; oof != NULL; oof = oof->next) {
|
2009-02-03 08:05:58 +03:00
|
|
|
if (oof->multidata == NULL)
|
2009-01-25 10:25:17 +03:00
|
|
|
continue;
|
2009-02-03 08:05:58 +03:00
|
|
|
if (oof->multidata[i] != 0)
|
|
|
|
oof->multidata[i] = -1;
|
2009-01-25 10:25:17 +03:00
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
for (oof = fileptr->prev; oof != NULL; oof = oof->prev) {
|
2009-02-03 08:05:58 +03:00
|
|
|
if (oof->multidata == NULL)
|
2009-01-25 10:25:17 +03:00
|
|
|
continue;
|
2009-02-03 08:05:58 +03:00
|
|
|
if (oof->multidata[i] == 0)
|
|
|
|
oof->multidata[i] = -1;
|
2009-01-25 10:25:17 +03:00
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
2009-02-03 08:05:58 +03:00
|
|
|
fileptr->multidata[i] = -1;
|
2009-01-25 10:25:17 +03:00
|
|
|
}
|
|
|
|
}
|
2001-05-05 19:01:42 +04:00
|
|
|
#endif /* ENABLE_COLOR */
|