1998-02-27 07:54:42 +03:00
|
|
|
/* Color setup
|
2007-09-26 14:22:25 +04:00
|
|
|
Copyright (C) 1994, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
|
2009-05-10 19:01:15 +04:00
|
|
|
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.
|
2009-05-10 19:01:15 +04:00
|
|
|
|
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
|
2005-05-27 07:35:10 +04:00
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
|
1998-02-27 07:54:42 +03:00
|
|
|
|
2009-02-05 21:28:18 +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>
|
2009-05-10 19:01:15 +04:00
|
|
|
#include <sys/types.h> /* size_t */
|
2005-02-08 12:04:03 +03:00
|
|
|
|
2009-05-08 14:01:05 +04:00
|
|
|
#include "../../src/global.h"
|
|
|
|
|
|
|
|
#include "../../src/tty/tty.h"
|
|
|
|
#include "../../src/tty/color.h"
|
2009-05-10 19:01:15 +04:00
|
|
|
#include "../../src/tty/color-internal.h"
|
1998-02-27 07:54:42 +03:00
|
|
|
|
2009-05-10 19:01:15 +04: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 */
|
2009-05-10 19:01:15 +04:00
|
|
|
gboolean use_colors = FALSE;
|
1998-02-27 07:54:42 +03:00
|
|
|
|
2009-05-10 19:01:15 +04:00
|
|
|
void
|
|
|
|
get_color (const char *cpp, CTYPE *colp)
|
1998-02-27 07:54:42 +03:00
|
|
|
{
|
2009-05-10 19:01:15 +04:00
|
|
|
const size_t table_len = color_table_len ();
|
2004-08-16 07:12:05 +04:00
|
|
|
size_t i;
|
2009-05-10 19:01:15 +04:00
|
|
|
|
|
|
|
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);
|
2009-05-10 19:01:15 +04:00
|
|
|
break;
|
1998-02-27 07:54:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-10 19:01:15 +04: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;
|
2009-05-10 19:01:15 +04:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2009-05-10 19:01:15 +04:00
|
|
|
if (*p == ',') {
|
1998-02-27 07:54:42 +03:00
|
|
|
state = 1;
|
|
|
|
*p = 0;
|
|
|
|
get_color (*cpp, &colorpairp->fg);
|
|
|
|
*p = ',';
|
|
|
|
*cpp = p + 1;
|
|
|
|
}
|
|
|
|
}
|
2009-05-10 19:01:15 +04:00
|
|
|
|
1998-02-27 07:54:42 +03:00
|
|
|
get_color (*cpp, state ? &colorpairp->bg : &colorpairp->fg);
|
|
|
|
}
|
|
|
|
|
2009-05-10 19:01:15 +04:00
|
|
|
static void
|
|
|
|
configure_colors_string (const char *the_color_string)
|
1998-02-27 07:54:42 +03:00
|
|
|
{
|
2009-05-10 19:01:15 +04:00
|
|
|
const size_t map_len = color_map_len ();
|
1998-04-15 10:05:34 +04:00
|
|
|
char *color_string, *p;
|
2004-08-16 07:12:05 +04:00
|
|
|
size_t i;
|
2009-05-10 19:01:15 +04:00
|
|
|
gboolean found;
|
1998-02-27 07:54:42 +03:00
|
|
|
|
|
|
|
if (!the_color_string)
|
|
|
|
return;
|
|
|
|
|
2009-02-06 01:40:32 +03:00
|
|
|
p = color_string = g_strdup (the_color_string);
|
2009-05-10 19:01:15 +04:00
|
|
|
while (color_string && *color_string) {
|
1998-02-27 07:54:42 +03:00
|
|
|
while (*color_string == ' ' || *color_string == '\t')
|
|
|
|
color_string++;
|
|
|
|
|
2009-05-10 19:01:15 +04:00
|
|
|
found = FALSE;
|
|
|
|
for (i = 0; i < map_len; i++){
|
|
|
|
size_t klen;
|
1998-12-29 19:52:49 +03:00
|
|
|
|
2009-05-10 19:01:15 +04:00
|
|
|
if (!color_map [i].name)
|
|
|
|
continue;
|
|
|
|
klen = strlen (color_map [i].name);
|
1998-02-27 07:54:42 +03:00
|
|
|
|
2009-05-10 19:01:15 +04: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]);
|
2009-05-10 19:01:15 +04:00
|
|
|
found = TRUE;
|
1998-02-27 07:54:42 +03:00
|
|
|
}
|
|
|
|
}
|
2009-05-10 19:01:15 +04:00
|
|
|
if (!found) {
|
|
|
|
while (*color_string && *color_string != ':')
|
|
|
|
color_string++;
|
|
|
|
if (*color_string)
|
|
|
|
color_string++;
|
1998-02-27 07:54:42 +03:00
|
|
|
}
|
|
|
|
}
|
2009-02-06 01:27:37 +03:00
|
|
|
g_free (p);
|
1998-02-27 07:54:42 +03:00
|
|
|
}
|
|
|
|
|
2009-05-10 19:01:15 +04:00
|
|
|
void
|
|
|
|
configure_colors (void)
|
1998-02-27 07:54:42 +03:00
|
|
|
{
|
|
|
|
extern char *command_line_colors;
|
2009-04-24 02:47:22 +04:00
|
|
|
|
2003-06-22 13:32:52 +04:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2002-07-15 01:41:12 +04:00
|
|
|
/* Functions necessary to implement syntax highlighting */
|
2009-05-10 19:01:15 +04:00
|
|
|
struct colors_avail c;
|
|
|
|
int max_index = 0;
|
2002-07-15 01:41:12 +04:00
|
|
|
|
2009-05-10 19:01:15 +04:00
|
|
|
int
|
2002-07-15 01:41:12 +04:00
|
|
|
alloc_color_pair (CTYPE foreground, CTYPE background)
|
|
|
|
{
|
|
|
|
mc_init_pair (++max_index, foreground, background);
|
|
|
|
return max_index;
|
|
|
|
}
|
|
|
|
|
2002-07-25 17:04:19 +04:00
|
|
|
void
|
2009-05-10 19:01:15 +04:00
|
|
|
tty_colors_done (void)
|
2002-07-25 17:04:19 +04:00
|
|
|
{
|
|
|
|
struct colors_avail *p, *next;
|
|
|
|
|
|
|
|
for (p = c.next; p; p = next) {
|
|
|
|
next = p->next;
|
2009-02-06 01:27:37 +03:00
|
|
|
g_free (p->fg);
|
|
|
|
g_free (p->bg);
|
|
|
|
g_free (p);
|
2002-07-25 17:04:19 +04:00
|
|
|
}
|
|
|
|
c.next = NULL;
|
|
|
|
}
|
|
|
|
|
2009-05-10 19:01:15 +04:00
|
|
|
gboolean
|
|
|
|
tty_use_colors (void)
|
|
|
|
{
|
|
|
|
return use_colors;
|
|
|
|
}
|