mc/src/tty/color.c

165 lines
3.6 KiB
C
Raw Normal View History

1998-02-27 07:54:42 +03:00
/* Color setup
Copyright (C) 1994, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
2007, 2008, 2009 Free Software Foundation, Inc.
1998-02-27 07:54:42 +03: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
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
1998-02-27 07:54:42 +03: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.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
1998-02-27 07:54:42 +03:00
/** \file color.c
* \brief Source: color setup
*/
1998-02-27 07:54:42 +03:00
#include <config.h>
2005-02-08 12:04:03 +03:00
1998-02-27 07:54:42 +03:00
#include <stdio.h>
2005-02-08 12:04:03 +03:00
#include <stdlib.h>
1998-02-27 07:54:42 +03:00
#include <string.h>
#include <sys/types.h> /* size_t */
2005-02-08 12:04:03 +03:00
#include "../../src/global.h"
#include "../../src/tty/tty.h"
#include "../../src/tty/color.h"
#include "../../src/tty/color-internal.h"
1998-02-27 07:54:42 +03:00
#include "../../src/setup.h" /* setup_color_string, term_color_string */
1998-02-27 07:54:42 +03:00
/* Set if we are actually using colors */
gboolean use_colors = FALSE;
1998-02-27 07:54:42 +03:00
void
get_color (const char *cpp, CTYPE *colp)
1998-02-27 07:54:42 +03:00
{
const size_t table_len = color_table_len ();
size_t i;
for (i = 0; i < table_len; i++)
if (strcmp (cpp, color_name (i)) == 0) {
1998-02-27 07:54:42 +03:00
*colp = color_value (i);
break;
1998-02-27 07:54:42 +03:00
}
}
static void
get_two_colors (char **cpp, struct colorpair *colorpairp)
1998-02-27 07:54:42 +03:00
{
char *p = *cpp;
int state;
state = 0;
for (; *p; p++) {
if (*p == ':') {
1998-02-27 07:54:42 +03:00
*p = 0;
get_color (*cpp, state ? &colorpairp->bg : &colorpairp->fg);
*p = ':';
*cpp = p + 1;
return;
}
if (*p == ',') {
1998-02-27 07:54:42 +03:00
state = 1;
*p = 0;
get_color (*cpp, &colorpairp->fg);
*p = ',';
*cpp = p + 1;
}
}
1998-02-27 07:54:42 +03:00
get_color (*cpp, state ? &colorpairp->bg : &colorpairp->fg);
}
static void
configure_colors_string (const char *the_color_string)
1998-02-27 07:54:42 +03:00
{
const size_t map_len = color_map_len ();
char *color_string, *p;
size_t i;
gboolean found;
1998-02-27 07:54:42 +03:00
if (!the_color_string)
return;
p = color_string = g_strdup (the_color_string);
while (color_string && *color_string) {
1998-02-27 07:54:42 +03:00
while (*color_string == ' ' || *color_string == '\t')
color_string++;
found = FALSE;
for (i = 0; i < map_len; i++){
size_t klen;
if (!color_map [i].name)
continue;
klen = strlen (color_map [i].name);
1998-02-27 07:54:42 +03:00
if (strncmp (color_string, color_map [i].name, klen) == 0) {
1998-02-27 07:54:42 +03:00
color_string += klen;
get_two_colors (&color_string, &color_map [i]);
found = TRUE;
1998-02-27 07:54:42 +03:00
}
}
if (!found) {
while (*color_string && *color_string != ':')
color_string++;
if (*color_string)
color_string++;
1998-02-27 07:54:42 +03:00
}
}
g_free (p);
1998-02-27 07:54:42 +03:00
}
void
configure_colors (void)
1998-02-27 07:54:42 +03:00
{
extern char *command_line_colors;
configure_colors_string (default_colors);
1998-02-27 07:54:42 +03:00
configure_colors_string (setup_color_string);
configure_colors_string (term_color_string);
configure_colors_string (getenv ("MC_COLOR_TABLE"));
configure_colors_string (command_line_colors);
}
/* Functions necessary to implement syntax highlighting */
struct colors_avail c;
int max_index = 0;
int
alloc_color_pair (CTYPE foreground, CTYPE background)
{
mc_init_pair (++max_index, foreground, background);
return max_index;
}
void
tty_colors_done (void)
{
struct colors_avail *p, *next;
for (p = c.next; p; p = next) {
next = p->next;
g_free (p->fg);
g_free (p->bg);
g_free (p);
}
c.next = NULL;
}
gboolean
tty_use_colors (void)
{
return use_colors;
}