1
0
mirror of https://github.com/irssi/irssi.git synced 2024-11-03 04:27:19 -05:00

Compress colors.

git-svn-id: http://svn.irssi.org/repos/irssi/trunk@793 dbcabf3a-b0e7-0310-adc4-f8d773084564
This commit is contained in:
Timo Sirainen 2000-10-28 00:48:36 +00:00 committed by cras
parent 1541257ac7
commit 1e6f81e38f

View File

@ -98,10 +98,12 @@ void theme_destroy(THEME_REC *rec)
g_free(rec); g_free(rec);
} }
#define EXPAND_FLAG_ROOT 0x01
#define EXPAND_FLAG_LASTCOLOR_ARG 0x02
static char *theme_format_expand_data(THEME_REC *theme, static char *theme_format_expand_data(THEME_REC *theme,
const char **format, const char **format,
char default_color, char default_color,
char *before_arg_color, int root); char *save_color, int flags);
static int theme_replace_find(THEME_REC *theme, char chr) static int theme_replace_find(THEME_REC *theme, char chr)
{ {
@ -118,7 +120,8 @@ static int theme_replace_find(THEME_REC *theme, char chr)
} }
static char *theme_replace_expand(THEME_REC *theme, int index, static char *theme_replace_expand(THEME_REC *theme, int index,
char default_color, char chr) char default_color, char *last_color,
char chr)
{ {
GSList *rec; GSList *rec;
char *ret, *abstract, data[2]; char *ret, *abstract, data[2];
@ -130,7 +133,7 @@ static char *theme_replace_expand(THEME_REC *theme, int index,
abstract = rec->data; abstract = rec->data;
abstract = theme_format_expand_data(theme, (const char **) &abstract, abstract = theme_format_expand_data(theme, (const char **) &abstract,
default_color, NULL, FALSE); default_color, last_color, 0);
ret = parse_special_string(abstract, NULL, NULL, data, NULL); ret = parse_special_string(abstract, NULL, NULL, data, NULL);
g_free(abstract); g_free(abstract);
return ret; return ret;
@ -139,30 +142,13 @@ static char *theme_replace_expand(THEME_REC *theme, int index,
static const char *colorformats = "n01234567krgybmpcwKRGYBMPCW"; static const char *colorformats = "n01234567krgybmpcwKRGYBMPCW";
#define IS_COLOR_FORMAT(c) \ #define IS_COLOR_FORMAT(c) \
(strchr(colorformats, c) != NULL) ((c) != '\0' && strchr(colorformats, c) != NULL)
/* append next "item", either a character, $variable or %format */ /* append "variable" part in $variable, ie. not the contents of the variable */
static void theme_format_append_next(THEME_REC *theme, GString *str, static void theme_format_append_variable(GString *str, const char **format)
const char **format,
char default_color, char *last_color)
{ {
int index;
char *value, chr;
chr = **format;
if ((chr == '$' || chr == '%') &&
(*format)[1] == '\0') {
/* last char, always append */
g_string_append_c(str, chr);
(*format)++;
return;
}
if (chr == '$') {
/* $variable .. we'll always need to skip this, since it
may contain characters that are in replace chars. */
const char *orig; const char *orig;
char *args[1] = { NULL }; char *value, *args[1] = { NULL };
int free_ret; int free_ret;
orig = *format; orig = *format;
@ -177,6 +163,29 @@ static void theme_format_append_next(THEME_REC *theme, GString *str,
value = g_strndup(orig, (int) (*format-orig)); value = g_strndup(orig, (int) (*format-orig));
g_string_append(str, value); g_string_append(str, value);
g_free(value); g_free(value);
}
/* append next "item", either a character, $variable or %format */
static void theme_format_append_next(THEME_REC *theme, GString *str,
const char **format,
char default_color, char *last_color)
{
int index;
char chr;
chr = **format;
if ((chr == '$' || chr == '%') &&
(*format)[1] == '\0') {
/* last char, always append */
g_string_append_c(str, chr);
(*format)++;
return;
}
if (chr == '$') {
/* $variable .. we'll always need to skip this, since it
may contain characters that are in replace chars. */
theme_format_append_variable(str, format);
return; return;
} }
@ -188,10 +197,13 @@ static void theme_format_append_next(THEME_REC *theme, GString *str,
if (**format == 'n') if (**format == 'n')
chr = default_color; chr = default_color;
if (chr != *last_color) {
if (IS_COLOR_FORMAT(chr)) if (IS_COLOR_FORMAT(chr))
*last_color = chr; *last_color = chr;
g_string_append_c(str, '%'); g_string_append_c(str, '%');
g_string_append_c(str, chr); g_string_append_c(str, chr);
}
(*format)++; (*format)++;
return; return;
} }
@ -204,7 +216,10 @@ static void theme_format_append_next(THEME_REC *theme, GString *str,
if (index == -1) if (index == -1)
g_string_append_c(str, chr); g_string_append_c(str, chr);
else { else {
value = theme_replace_expand(theme, index, default_color, chr); char *value;
value = theme_replace_expand(theme, index, default_color,
last_color, chr);
g_string_append(str, value); g_string_append(str, value);
g_free(value); g_free(value);
} }
@ -249,13 +264,14 @@ static char *theme_format_expand_abstract(THEME_REC *theme,
/* abstract may itself contain abstracts or replaces :) */ /* abstract may itself contain abstracts or replaces :) */
p = data = abstract; p = data = abstract;
abstract = theme_format_expand_data(theme, &p, default_color, abstract = theme_format_expand_data(theme, &p, default_color,
&default_color, FALSE); &default_color,
EXPAND_FLAG_LASTCOLOR_ARG);
g_free(data); g_free(data);
/* now we'll need to get the data part. it may contain /* now we'll need to get the data part. it may contain
more abstracts, they are automatically expanded. */ more abstracts, they are automatically expanded. */
data = theme_format_expand_data(theme, formatp, default_color, data = theme_format_expand_data(theme, formatp, default_color,
NULL, FALSE); NULL, 0);
ret = parse_special_string(abstract, NULL, NULL, data, NULL); ret = parse_special_string(abstract, NULL, NULL, data, NULL);
g_free(abstract); g_free(abstract);
@ -263,12 +279,12 @@ static char *theme_format_expand_abstract(THEME_REC *theme,
return ret; return ret;
} }
/* expand the data part in {abstract data}. If root is TRUE, we're actually /* expand the data part in {abstract data} */
expanding the original format string so we ignore all extra } chars. */
static char *theme_format_expand_data(THEME_REC *theme, static char *theme_format_expand_data(THEME_REC *theme,
const char **format, const char **format,
char default_color, char default_color,
char *before_arg_color, int root) char *save_last_color,
int flags)
{ {
GString *str; GString *str;
char *ret, *abstract; char *ret, *abstract;
@ -277,18 +293,20 @@ static char *theme_format_expand_data(THEME_REC *theme,
str = g_string_new(NULL); str = g_string_new(NULL);
while (**format != '\0') { while (**format != '\0') {
if (!root && **format == '}') { if ((flags & EXPAND_FLAG_ROOT) == 0 && **format == '}') {
/* ignore } if we're expanding original string */
(*format)++; (*format)++;
break; break;
} }
if (**format != '{') { if (**format != '{') {
if (before_arg_color != NULL && if (save_last_color != NULL &&
(flags & EXPAND_FLAG_LASTCOLOR_ARG) &&
**format == '$' && (*format)[1] == '0') { **format == '$' && (*format)[1] == '0') {
/* save the color before $0 .. /* save the color before $0 ..
this is for the %n replacing */ this is for the %n replacing */
*before_arg_color = last_color; *save_last_color = last_color;
before_arg_color = NULL; save_last_color = NULL;
} }
theme_format_append_next(theme, str, format, theme_format_append_next(theme, str, format,
@ -309,6 +327,53 @@ static char *theme_format_expand_data(THEME_REC *theme,
} }
} }
if (save_last_color != NULL &&
(flags & EXPAND_FLAG_LASTCOLOR_ARG) == 0) {
/* save the last color */
*save_last_color = last_color;
}
ret = str->str;
g_string_free(str, FALSE);
return ret;
}
static char *theme_format_compress_colors(THEME_REC *theme, const char *format)
{
GString *str;
char *ret, last_color = 'n';
str = g_string_new(NULL);
while (*format != '\0') {
if (*format == '$') {
/* $variable, skrip it entirely */
theme_format_append_variable(str, &format);
} else if (*format != '%') {
/* a normal character */
g_string_append_c(str, *format);
format++;
} else {
/* %format */
format++;
if (*format == last_color) {
/* active color set again */
} else if (IS_COLOR_FORMAT(*format) &&
format[1] == '%' &&
IS_COLOR_FORMAT(format[2])) {
/* two colors in a row */
} else {
/* some format, add it */
g_string_append_c(str, '%');
g_string_append_c(str, *format);
if (IS_COLOR_FORMAT(*format))
last_color = *format;
}
format++;
}
}
ret = str->str; ret = str->str;
g_string_free(str, FALSE); g_string_free(str, FALSE);
return ret; return ret;
@ -316,10 +381,16 @@ static char *theme_format_expand_data(THEME_REC *theme,
static char *theme_format_expand(THEME_REC *theme, const char *format) static char *theme_format_expand(THEME_REC *theme, const char *format)
{ {
char *data, *ret;
g_return_val_if_fail(theme != NULL, NULL); g_return_val_if_fail(theme != NULL, NULL);
g_return_val_if_fail(format != NULL, NULL); g_return_val_if_fail(format != NULL, NULL);
return theme_format_expand_data(theme, &format, 'n', NULL, TRUE); data = theme_format_expand_data(theme, &format, 'n', NULL,
EXPAND_FLAG_ROOT);
ret = theme_format_compress_colors(theme, data);
g_free(data);
return ret;
} }
static MODULE_THEME_REC *theme_module_create(THEME_REC *theme, const char *module) static MODULE_THEME_REC *theme_module_create(THEME_REC *theme, const char *module)