2001-05-05 15:01:42 +00:00
|
|
|
/* $Id$ */
|
|
|
|
/**************************************************************************
|
|
|
|
* color.c *
|
|
|
|
* *
|
2007-10-11 05:01:32 +00:00
|
|
|
* Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007 *
|
|
|
|
* Free Software Foundation, Inc. *
|
2001-05-05 15:01:42 +00: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 05:17:36 +00:00
|
|
|
* the Free Software Foundation; either version 3, or (at your option) *
|
2001-05-05 15:01:42 +00:00
|
|
|
* any later version. *
|
|
|
|
* *
|
2005-05-15 19:57:17 +00: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 15:01:42 +00: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 19:57:17 +00:00
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
|
|
|
|
* 02110-1301, USA. *
|
2001-05-05 15:01:42 +00:00
|
|
|
* *
|
|
|
|
**************************************************************************/
|
|
|
|
|
2005-12-08 02:47:10 +00:00
|
|
|
#include "proto.h"
|
2001-05-05 15:01:42 +00:00
|
|
|
|
|
|
|
#include <stdio.h>
|
2005-08-01 04:34:27 +00:00
|
|
|
#include <string.h>
|
2001-05-05 15:01:42 +00:00
|
|
|
|
2002-07-19 01:08:59 +00:00
|
|
|
#ifdef ENABLE_COLOR
|
|
|
|
|
2005-11-16 08:14:16 +00:00
|
|
|
/* For each syntax list entry, go through the list of colors and assign
|
2007-01-01 05:15:32 +00:00
|
|
|
* the color pairs. */
|
2002-09-27 14:21:59 +00:00
|
|
|
void set_colorpairs(void)
|
2001-05-05 15:01:42 +00:00
|
|
|
{
|
2002-09-27 14:21:59 +00:00
|
|
|
const syntaxtype *this_syntax = syntaxes;
|
|
|
|
|
2003-11-05 22:04:08 +00:00
|
|
|
for (; this_syntax != NULL; this_syntax = this_syntax->next) {
|
2002-09-27 14:21:59 +00:00
|
|
|
colortype *this_color = this_syntax->color;
|
|
|
|
int color_pair = 1;
|
|
|
|
|
2003-11-05 22:04:08 +00:00
|
|
|
for (; this_color != NULL; this_color = this_color->next) {
|
2002-09-27 14:21:59 +00:00
|
|
|
const colortype *beforenow = this_syntax->color;
|
|
|
|
|
2005-03-10 20:55:11 +00: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 14:21:59 +00:00
|
|
|
;
|
|
|
|
|
2005-03-10 20:55:11 +00:00
|
|
|
if (beforenow != this_color)
|
2002-09-27 14:21:59 +00:00
|
|
|
this_color->pairnum = beforenow->pairnum;
|
|
|
|
else {
|
|
|
|
this_color->pairnum = color_pair;
|
|
|
|
color_pair++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2001-11-29 02:42:27 +00:00
|
|
|
|
2005-07-15 00:36:49 +00:00
|
|
|
/* Initialize the color information. */
|
2005-07-13 20:18:46 +00:00
|
|
|
void color_init(void)
|
2002-09-27 14:21:59 +00:00
|
|
|
{
|
2005-07-13 20:18:46 +00:00
|
|
|
assert(openfile != NULL);
|
|
|
|
|
2005-07-27 03:12:22 +00:00
|
|
|
if (has_colors()) {
|
2005-07-13 20:18:46 +00:00
|
|
|
const colortype *tmpcolor;
|
2002-09-27 14:21:59 +00:00
|
|
|
#ifdef HAVE_USE_DEFAULT_COLORS
|
2005-03-10 20:55:11 +00:00
|
|
|
bool defok;
|
2002-09-27 14:21:59 +00:00
|
|
|
#endif
|
|
|
|
|
2001-05-05 15:01:42 +00:00
|
|
|
start_color();
|
2001-11-29 02:42:27 +00:00
|
|
|
|
2001-05-05 15:01:42 +00:00
|
|
|
#ifdef HAVE_USE_DEFAULT_COLORS
|
2006-01-02 17:09:35 +00:00
|
|
|
/* Use the default colors, if available. */
|
2005-03-10 20:55:11 +00:00
|
|
|
defok = (use_default_colors() != ERR);
|
2001-05-05 15:01:42 +00:00
|
|
|
#endif
|
2001-11-29 02:42:27 +00:00
|
|
|
|
2005-07-13 20:18:46 +00:00
|
|
|
for (tmpcolor = openfile->colorstrings; tmpcolor != NULL;
|
2001-11-29 02:42:27 +00:00
|
|
|
tmpcolor = tmpcolor->next) {
|
2005-07-15 00:17:13 +00: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 02:42:27 +00:00
|
|
|
|
2005-07-13 20:18:46 +00:00
|
|
|
if (background == -1) {
|
2002-09-27 14:21:59 +00:00
|
|
|
#ifdef HAVE_USE_DEFAULT_COLORS
|
2003-01-13 01:35:15 +00:00
|
|
|
if (!defok)
|
2002-09-27 14:21:59 +00:00
|
|
|
#endif
|
|
|
|
background = COLOR_BLACK;
|
2005-07-13 20:18:46 +00:00
|
|
|
}
|
2001-12-02 04:55:44 +00:00
|
|
|
|
2005-07-15 00:17:13 +00:00
|
|
|
init_pair(tmpcolor->pairnum, foreground, background);
|
2001-11-29 02:42:27 +00:00
|
|
|
|
2001-12-12 01:45:01 +00:00
|
|
|
#ifdef DEBUG
|
2005-08-01 05:54:11 +00:00
|
|
|
fprintf(stderr, "init_pair(): fg = %hd, bg = %hd\n", tmpcolor->fg, tmpcolor->bg);
|
2001-12-12 01:45:01 +00:00
|
|
|
#endif
|
2001-11-29 02:42:27 +00:00
|
|
|
}
|
|
|
|
}
|
2001-05-05 15:01:42 +00:00
|
|
|
}
|
|
|
|
|
2003-01-13 01:35:15 +00:00
|
|
|
/* Update the color information based on the current filename. */
|
2005-07-13 20:18:46 +00:00
|
|
|
void color_update(void)
|
2002-05-04 03:47:33 +00:00
|
|
|
{
|
2009-01-25 07:25:17 +00:00
|
|
|
syntaxtype *tmpsyntax;
|
|
|
|
syntaxtype *defsyntax = NULL;
|
2005-08-01 04:23:29 +00:00
|
|
|
colortype *tmpcolor, *defcolor = NULL;
|
2005-07-13 20:18:46 +00:00
|
|
|
|
|
|
|
assert(openfile != NULL);
|
2002-05-04 03:47:33 +00:00
|
|
|
|
2009-01-25 07:25:17 +00:00
|
|
|
openfile->syntax = NULL;
|
2005-07-13 20:18:46 +00:00
|
|
|
openfile->colorstrings = NULL;
|
2005-07-29 21:42:08 +00:00
|
|
|
|
2005-07-30 21:24:56 +00:00
|
|
|
/* If we specified a syntax override string, use it. */
|
|
|
|
if (syntaxstr != NULL) {
|
2005-08-01 04:59:34 +00: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-30 21:24:56 +00:00
|
|
|
for (tmpsyntax = syntaxes; tmpsyntax != NULL;
|
|
|
|
tmpsyntax = tmpsyntax->next) {
|
2009-01-25 07:25:17 +00:00
|
|
|
if (strcmp(tmpsyntax->desc, syntaxstr) == 0) {
|
|
|
|
openfile->syntax = tmpsyntax;
|
2005-07-13 20:18:46 +00:00
|
|
|
openfile->colorstrings = tmpsyntax->color;
|
2009-01-25 07:25:17 +00:00
|
|
|
}
|
2002-05-12 20:54:16 +00:00
|
|
|
|
2005-07-13 20:18:46 +00:00
|
|
|
if (openfile->colorstrings != NULL)
|
2002-09-27 14:21:59 +00:00
|
|
|
break;
|
2002-05-04 03:47:33 +00:00
|
|
|
}
|
|
|
|
}
|
2002-05-04 04:23:30 +00:00
|
|
|
|
2005-07-30 21:24:56 +00: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-21 23:02:30 +00:00
|
|
|
* file extension, and then look in the header. */
|
2005-07-30 21:24:56 +00:00
|
|
|
if (openfile->colorstrings == NULL) {
|
2004-05-27 18:39:16 +00:00
|
|
|
for (tmpsyntax = syntaxes; tmpsyntax != NULL;
|
2005-03-10 20:55:11 +00:00
|
|
|
tmpsyntax = tmpsyntax->next) {
|
2005-07-30 21:24:56 +00:00
|
|
|
exttype *e;
|
|
|
|
|
2005-08-01 04:23:29 +00:00
|
|
|
/* If this is the default syntax, it has no associated
|
2006-07-29 21:40:32 +00:00
|
|
|
* extensions, which we've checked for elsewhere. Skip over
|
|
|
|
* it here, but keep track of its color regexes. */
|
2005-08-01 04:34:27 +00:00
|
|
|
if (strcmp(tmpsyntax->desc, "default") == 0) {
|
2009-01-25 07:25:17 +00:00
|
|
|
defsyntax = tmpsyntax;
|
2006-05-28 18:51:48 +00:00
|
|
|
defcolor = tmpsyntax->color;
|
2005-08-01 04:23:29 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2005-07-30 21:24:56 +00: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 05:16:20 +00:00
|
|
|
regcomp(e->ext, fixbounds(e->ext_regex), REG_EXTENDED);
|
2005-07-30 21:24:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Set colorstrings if we matched the extension
|
|
|
|
* regex. */
|
|
|
|
if (regexec(e->ext, openfile->filename, 0, NULL,
|
2009-01-25 07:25:17 +00:00
|
|
|
0) == 0) {
|
|
|
|
openfile->syntax = tmpsyntax;
|
2005-07-30 21:24:56 +00:00
|
|
|
openfile->colorstrings = tmpsyntax->color;
|
2009-01-25 07:25:17 +00:00
|
|
|
}
|
2005-07-30 21:24:56 +00: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-13 20:18:46 +00:00
|
|
|
}
|
2008-09-21 23:02:30 +00: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 07:25:17 +00:00
|
|
|
if (regexec(e->ext, openfile->fileage->data, 0, NULL, 0) == 0) {
|
|
|
|
openfile->syntax = tmpsyntax;
|
2008-09-21 23:02:30 +00:00
|
|
|
openfile->colorstrings = tmpsyntax->color;
|
2009-01-25 07:25:17 +00:00
|
|
|
}
|
2008-09-21 23:02:30 +00: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-13 20:18:46 +00:00
|
|
|
}
|
|
|
|
|
2008-09-21 23:02:30 +00:00
|
|
|
|
2005-08-01 04:23:29 +00:00
|
|
|
/* If we didn't get a syntax based on the file extension, and we
|
|
|
|
* have a default syntax, use it. */
|
2009-01-25 07:25:17 +00:00
|
|
|
if (openfile->colorstrings == NULL && defcolor != NULL) {
|
|
|
|
openfile->syntax = defsyntax;
|
2005-08-01 04:23:29 +00:00
|
|
|
openfile->colorstrings = defcolor;
|
2009-01-25 07:25:17 +00:00
|
|
|
}
|
2005-08-01 04:23:29 +00:00
|
|
|
|
2005-07-13 20:18:46 +00:00
|
|
|
for (tmpcolor = openfile->colorstrings; tmpcolor != NULL;
|
|
|
|
tmpcolor = tmpcolor->next) {
|
2005-07-29 21:42:08 +00: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-13 20:18:46 +00:00
|
|
|
tmpcolor->start = (regex_t *)nmalloc(sizeof(regex_t));
|
2008-08-30 05:16:20 +00:00
|
|
|
regcomp(tmpcolor->start, fixbounds(tmpcolor->start_regex),
|
2005-07-14 18:33:51 +00:00
|
|
|
REG_EXTENDED | (tmpcolor->icase ? REG_ICASE : 0));
|
2005-07-13 20:18:46 +00:00
|
|
|
}
|
2005-07-14 18:33:51 +00:00
|
|
|
|
2005-07-29 21:42:08 +00:00
|
|
|
if (tmpcolor->end_regex != NULL && tmpcolor->end == NULL) {
|
2005-07-13 20:18:46 +00:00
|
|
|
tmpcolor->end = (regex_t *)nmalloc(sizeof(regex_t));
|
2008-08-30 05:16:20 +00:00
|
|
|
regcomp(tmpcolor->end, fixbounds(tmpcolor->end_regex),
|
2005-07-14 18:33:51 +00:00
|
|
|
REG_EXTENDED | (tmpcolor->icase ? REG_ICASE : 0));
|
2002-05-04 04:23:30 +00:00
|
|
|
}
|
|
|
|
}
|
2002-05-04 03:47:33 +00:00
|
|
|
}
|
|
|
|
|
2009-01-25 07:25:17 +00: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 08:34:27 +00:00
|
|
|
if (!openfile->syntax)
|
|
|
|
return;
|
|
|
|
|
2009-01-25 07:25:17 +00:00
|
|
|
for (i = 0; i < openfile->syntax->nmultis; i++) {
|
|
|
|
for (oof = fileptr->next; oof != NULL; oof = oof->next) {
|
|
|
|
if (oof->multiswatching == NULL)
|
|
|
|
continue;
|
|
|
|
if (oof->multiswatching[i] == FALSE)
|
|
|
|
oof->multiswatching[i] = TRUE;
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
for (oof = fileptr->prev; oof != NULL; oof = oof->prev) {
|
|
|
|
if (oof->multiswatching == NULL)
|
|
|
|
continue;
|
|
|
|
if (oof->multiswatching[i] == FALSE)
|
|
|
|
oof->multiswatching[i] = TRUE;
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
fileptr->multiswatching[i] = TRUE;
|
|
|
|
}
|
|
|
|
}
|
2001-05-05 15:01:42 +00:00
|
|
|
#endif /* ENABLE_COLOR */
|