color.c 5.38 KB
Newer Older
1
2
3
4
/* $Id$ */
/**************************************************************************
 *   color.c                                                              *
 *                                                                        *
5
 *   Copyright (C) 1999-2005 Chris Allegretta                             *
6
7
 *   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 *
8
 *   the Free Software Foundation; either version 2, or (at your option)  *
9
10
 *   any later version.                                                   *
 *                                                                        *
11
12
13
14
 *   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.                             *
15
16
17
 *                                                                        *
 *   You should have received a copy of the GNU General Public License    *
 *   along with this program; if not, write to the Free Software          *
18
19
 *   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA            *
 *   02110-1301, USA.                                                     *
20
21
22
 *                                                                        *
 **************************************************************************/

23
24
25
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
26
27
28
29

#include <stdio.h>
#include "proto.h"

Chris Allegretta's avatar
Chris Allegretta committed
30
31
#ifdef ENABLE_COLOR

32
33
34
/* For each syntax list entry, we go through the list of colors and
 * assign color pairs. */
void set_colorpairs(void)
35
{
36
37
    const syntaxtype *this_syntax = syntaxes;

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
38
    for (; this_syntax != NULL; this_syntax = this_syntax->next) {
39
40
41
	colortype *this_color = this_syntax->color;
	int color_pair = 1;

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
42
	for (; this_color != NULL; this_color = this_color->next) {
43
44
	    const colortype *beforenow = this_syntax->color;

45
46
47
48
49
	    for (; beforenow != this_color &&
		(beforenow->fg != this_color->fg ||
		beforenow->bg != this_color->bg ||
		beforenow->bright != this_color->bright);
		beforenow = beforenow->next)
50
51
		;

52
	    if (beforenow != this_color)
53
54
55
56
57
58
59
60
		this_color->pairnum = beforenow->pairnum;
	    else {
		this_color->pairnum = color_pair;
		color_pair++;
	    }
	}
    }
}
61

62
/* Initialize the color information. */
63
void color_init(void)
64
{
65
66
    assert(openfile != NULL);

67
    if (has_colors()) {
68
	const colortype *tmpcolor;
69
#ifdef HAVE_USE_DEFAULT_COLORS
70
	bool defok;
71
72
#endif

73
	start_color();
74

75
#ifdef HAVE_USE_DEFAULT_COLORS
76
	/* Add in colors, if available. */
77
	defok = (use_default_colors() != ERR);
78
#endif
79

80
	for (tmpcolor = openfile->colorstrings; tmpcolor != NULL;
81
		tmpcolor = tmpcolor->next) {
82
83
84
85
86
87
88
	    short foreground = tmpcolor->fg, background = tmpcolor->bg;
	    if (foreground == -1) {
#ifdef HAVE_USE_DEFAULT_COLORS
		if (!defok)
#endif
		    foreground = COLOR_WHITE;
	    }
89

90
	    if (background == -1) {
91
#ifdef HAVE_USE_DEFAULT_COLORS
Chris Allegretta's avatar
Chris Allegretta committed
92
		if (!defok)
93
94
#endif
		    background = COLOR_BLACK;
95
	    }
96

97
	    init_pair(tmpcolor->pairnum, foreground, background);
98

99
#ifdef DEBUG
100
	    fprintf(stderr, "init_pair(): fg = %hd, bg = %hd\n",
101
		tmpcolor->fg, tmpcolor->bg);
102
#endif
103
104
	}
    }
105
106
}

Chris Allegretta's avatar
Chris Allegretta committed
107
/* Update the color information based on the current filename. */
108
void color_update(void)
109
{
110
    const syntaxtype *tmpsyntax;
111
112
113
    colortype *tmpcolor;

    assert(openfile != NULL);
114

115
    openfile->colorstrings = NULL;
116
117
    for (tmpsyntax = syntaxes; tmpsyntax != NULL;
	tmpsyntax = tmpsyntax->next) {
118
	exttype *e;
119

120
	for (e = tmpsyntax->extensions; e != NULL; e = e->next) {
121
	    bool not_compiled = (e->ext == NULL);
122

123
124
125
	    /* e->ext_regex has already been checked for validity
	     * elsewhere.  Compile its specified regex if we haven't
	     * already. */
126
	    if (not_compiled) {
127
128
129
130
		e->ext = (regex_t *)nmalloc(sizeof(regex_t));
		regcomp(e->ext, e->ext_regex, REG_EXTENDED);
	    }

131
	    /* Set colorstrings if we matched the extension regex. */
132
	    if (regexec(e->ext, openfile->filename, 0, NULL, 0) == 0)
133
		openfile->colorstrings = tmpsyntax->color;
134

135
	    if (openfile->colorstrings != NULL)
136
		break;
137
138
139

	    /* Decompile e->ext_regex's specified regex if we aren't
	     * going to use it. */
140
	    if (not_compiled) {
141
142
		regfree(e->ext);
		free(e->ext);
143
		e->ext = NULL;
144
	    }
145
146
	}
    }
147

148
    /* If we haven't found a match, use the override string. */
149
    if (openfile->colorstrings == NULL && syntaxstr != NULL) {
150
	for (tmpsyntax = syntaxes; tmpsyntax != NULL;
151
		tmpsyntax = tmpsyntax->next) {
152
	    if (mbstrcasecmp(tmpsyntax->desc, syntaxstr) == 0)
153
154
		openfile->colorstrings = tmpsyntax->color;

155
156
	    if (openfile->colorstrings != NULL)
		break;
157
158
159
160
161
	}
    }

    for (tmpcolor = openfile->colorstrings; tmpcolor != NULL;
	tmpcolor = tmpcolor->next) {
162
163
164
165
	/* 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) {
166
	    tmpcolor->start = (regex_t *)nmalloc(sizeof(regex_t));
167
168
	    regcomp(tmpcolor->start, tmpcolor->start_regex,
		REG_EXTENDED | (tmpcolor->icase ? REG_ICASE : 0));
169
	}
170

171
	if (tmpcolor->end_regex != NULL && tmpcolor->end == NULL) {
172
	    tmpcolor->end = (regex_t *)nmalloc(sizeof(regex_t));
173
174
	    regcomp(tmpcolor->end, tmpcolor->end_regex,
		REG_EXTENDED | (tmpcolor->icase ? REG_ICASE : 0));
175
176
	}
    }
177
178
}

179
#endif /* ENABLE_COLOR */