2001-05-05 19:01:42 +04:00
|
|
|
/* $Id$ */
|
|
|
|
/**************************************************************************
|
|
|
|
* color.c *
|
|
|
|
* *
|
2002-01-04 20:57:40 +03:00
|
|
|
* Copyright (C) 1999-2002 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. *
|
|
|
|
* *
|
|
|
|
* 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. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
|
|
|
* along with this program; if not, write to the Free Software *
|
|
|
|
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
|
|
|
|
* *
|
|
|
|
**************************************************************************/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include "proto.h"
|
|
|
|
#include "nano.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;
|
|
|
|
|
|
|
|
for(; this_syntax != NULL; this_syntax = this_syntax->next) {
|
|
|
|
colortype *this_color = this_syntax->color;
|
|
|
|
int color_pair = 1;
|
|
|
|
|
|
|
|
for(; this_color != NULL; this_color = this_color->next) {
|
|
|
|
const colortype *beforenow = this_syntax->color;
|
|
|
|
|
|
|
|
for(; beforenow != NULL && beforenow != this_color &&
|
|
|
|
(beforenow->fg != this_color->fg ||
|
|
|
|
beforenow->bg != this_color->bg ||
|
|
|
|
beforenow->bright != this_color->bright);
|
|
|
|
beforenow = beforenow->next)
|
|
|
|
;
|
|
|
|
|
|
|
|
if (beforenow != NULL && beforenow != this_color)
|
|
|
|
this_color->pairnum = beforenow->pairnum;
|
|
|
|
else {
|
|
|
|
this_color->pairnum = color_pair;
|
|
|
|
color_pair++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2001-11-29 05:42:27 +03:00
|
|
|
|
2002-09-27 18:21:59 +04:00
|
|
|
void do_colorinit(void)
|
|
|
|
{
|
2001-05-05 19:01:42 +04:00
|
|
|
if (has_colors()) {
|
2002-09-27 18:21:59 +04:00
|
|
|
const colortype *tmpcolor = NULL;
|
|
|
|
#ifdef HAVE_USE_DEFAULT_COLORS
|
|
|
|
int defok;
|
|
|
|
#endif
|
|
|
|
|
2001-05-05 19:01:42 +04:00
|
|
|
start_color();
|
|
|
|
/* Add in colors, if available */
|
2001-11-29 05:42:27 +03:00
|
|
|
|
2001-05-05 19:01:42 +04:00
|
|
|
#ifdef HAVE_USE_DEFAULT_COLORS
|
2002-09-27 18:21:59 +04:00
|
|
|
defok = use_default_colors() != ERR;
|
2001-05-05 19:01:42 +04:00
|
|
|
#endif
|
2001-11-29 05:42:27 +03:00
|
|
|
|
|
|
|
for (tmpcolor = colorstrings; tmpcolor != NULL;
|
|
|
|
tmpcolor = tmpcolor->next) {
|
2002-09-27 18:21:59 +04:00
|
|
|
short background = tmpcolor->bg;
|
2001-11-29 05:42:27 +03:00
|
|
|
|
2002-09-27 18:21:59 +04:00
|
|
|
if (background == -1)
|
|
|
|
#ifdef HAVE_USE_DEFAULT_COLORS
|
|
|
|
if (!defok)
|
|
|
|
#endif
|
|
|
|
background = COLOR_BLACK;
|
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
|
2002-06-13 04:40:19 +04:00
|
|
|
fprintf(stderr, _("Running init_pair with fg = %d and bg = %d\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
|
|
|
}
|
|
|
|
|
2002-05-04 07:47:33 +04:00
|
|
|
/* Update the color information based on the current filename */
|
|
|
|
void update_color(void)
|
|
|
|
{
|
2002-09-27 18:21:59 +04:00
|
|
|
const syntaxtype *tmpsyntax;
|
2002-05-04 07:47:33 +04:00
|
|
|
|
|
|
|
colorstrings = NULL;
|
|
|
|
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) {
|
2002-09-27 18:21:59 +04:00
|
|
|
regex_t syntaxfile_regexp;
|
|
|
|
|
|
|
|
regcomp(&syntaxfile_regexp, e->val, REG_EXTENDED | REG_NOSUB);
|
2002-05-04 07:47:33 +04:00
|
|
|
|
|
|
|
/* Set colorstrings if we matched the extension regex */
|
2002-09-27 18:21:59 +04:00
|
|
|
if (!regexec(&syntaxfile_regexp, filename, 0, NULL, 0))
|
2002-08-21 20:10:37 +04:00
|
|
|
colorstrings = tmpsyntax->color;
|
2002-05-13 00:54:16 +04:00
|
|
|
|
|
|
|
regfree(&syntaxfile_regexp);
|
2002-09-27 18:21:59 +04:00
|
|
|
if (colorstrings != NULL)
|
|
|
|
break;
|
2002-05-04 07:47:33 +04:00
|
|
|
}
|
|
|
|
}
|
2002-05-04 08:23:30 +04:00
|
|
|
|
|
|
|
/* if we haven't found a match, use the override string */
|
|
|
|
if (colorstrings == NULL && syntaxstr != NULL) {
|
|
|
|
for (tmpsyntax = syntaxes; tmpsyntax != NULL;
|
|
|
|
tmpsyntax = tmpsyntax->next) {
|
|
|
|
if (!strcasecmp(tmpsyntax->desc, syntaxstr))
|
|
|
|
colorstrings = tmpsyntax->color;
|
|
|
|
}
|
|
|
|
}
|
2002-05-04 07:47:33 +04:00
|
|
|
do_colorinit();
|
|
|
|
edit_refresh();
|
|
|
|
}
|
|
|
|
|
2001-05-05 19:01:42 +04:00
|
|
|
#endif /* ENABLE_COLOR */
|