diff --git a/src/fe-common/core/formats.c b/src/fe-common/core/formats.c index aa3b7923..526823ce 100644 --- a/src/fe-common/core/formats.c +++ b/src/fe-common/core/formats.c @@ -178,9 +178,11 @@ static void unformat_24bit_color(const char **ptr, int off, int *fgcolor, int *b if (is_bg) { *bgcolor = color; + *flags &= ~GUI_PRINT_FLAG_MIRC_COLOR_BG; *flags |= GUI_PRINT_FLAG_COLOR_24_BG; } else { *fgcolor = color; + *flags &= ~GUI_PRINT_FLAG_MIRC_COLOR_FG; *flags |= GUI_PRINT_FLAG_COLOR_24_FG; } } @@ -1092,22 +1094,24 @@ void format_newline(TEXT_DEST_REC *dest) #define SS_FLAGS_PREV(stack) (stack)->flags_s[(stack)->cur - 1] #define SS_FLAGS_KEEP(stack) (stack)->flags_s[(stack)->keep] +inline static void copy_flags(int in_flags, unsigned int copy_flags, int *out_flags) +{ + *out_flags &= ~copy_flags; + *out_flags |= (in_flags & copy_flags); +} + inline static void ss_restore_fgcolor(THEME_REC *theme, STYLE_STACK_REC *stack, int *fgcolor, int *flags) { if (stack != NULL && stack->cur > 0) { *fgcolor = SS_FGCOLOR_PREV(stack); - if (SS_FLAGS_PREV(stack) & GUI_PRINT_FLAG_COLOR_24_FG) { - *flags |= GUI_PRINT_FLAG_COLOR_24_FG; - } else { - *flags &= ~GUI_PRINT_FLAG_COLOR_24_FG; - } + copy_flags(SS_FLAGS_PREV(stack), GUI_PRINT_FLAGS_FG_COLORS, flags); } else { if (theme != NULL) *fgcolor = theme->default_color; else *fgcolor = -1; - *flags &= ~GUI_PRINT_FLAG_COLOR_24_FG; + *flags &= ~GUI_PRINT_FLAGS_FG_COLORS; } } @@ -1117,14 +1121,10 @@ inline static void ss_restore_bgcolor(THEME_REC *theme, STYLE_STACK_REC *stack, (void) theme; if (stack != NULL && stack->cur > 0) { *bgcolor = SS_BGCOLOR_PREV(stack); - if (SS_FLAGS_PREV(stack) & GUI_PRINT_FLAG_COLOR_24_BG) { - *flags |= GUI_PRINT_FLAG_COLOR_24_BG; - } else { - *flags &= ~GUI_PRINT_FLAG_COLOR_24_BG; - } + copy_flags(SS_FLAGS_PREV(stack), GUI_PRINT_FLAGS_BG_COLORS, flags); } else { *bgcolor = -1; - *flags &= ~GUI_PRINT_FLAG_COLOR_24_BG; + *flags &= ~GUI_PRINT_FLAGS_BG_COLORS; } } @@ -1135,11 +1135,9 @@ inline static void ss_restore_flags(THEME_REC *theme, STYLE_STACK_REC *stack, in if (reset) oldflags &= ~reset; else - oldflags &= (GUI_PRINT_FLAG_COLOR_24_FG | GUI_PRINT_FLAG_COLOR_24_BG); + oldflags &= GUI_PRINT_FLAGS_COLORS; if (stack != NULL && stack->cur > 0) - *flags = (SS_FLAGS_PREV(stack) & - ~(GUI_PRINT_FLAG_COLOR_24_FG | GUI_PRINT_FLAG_COLOR_24_BG)) | - oldflags; + *flags = (SS_FLAGS_PREV(stack) & ~GUI_PRINT_FLAGS_COLORS) | oldflags; else *flags = oldflags; } @@ -1234,14 +1232,13 @@ static const char *get_ansi_color(THEME_REC *theme, const char *str, int *fg_ret switch (num) { case 0: /* reset colors and attributes back to default */ - ss_restore_fgcolor(theme, style_stack, &fg, &flags); - ss_restore_bgcolor(theme, style_stack, &bg, &flags); ss_restore_flags(theme, style_stack, &flags, GUI_PRINT_FLAG_INDENT | GUI_PRINT_FLAG_BOLD | GUI_PRINT_FLAG_ITALIC | GUI_PRINT_FLAG_UNDERLINE | GUI_PRINT_FLAG_BLINK | GUI_PRINT_FLAG_REVERSE | - GUI_PRINT_FLAG_COLOR_24_FG | - GUI_PRINT_FLAG_COLOR_24_BG); + GUI_PRINT_FLAGS_COLORS); + ss_restore_fgcolor(theme, style_stack, &fg, &flags); + ss_restore_bgcolor(theme, style_stack, &bg, &flags); break; case 1: /* hilight */ @@ -1329,9 +1326,11 @@ static const char *get_ansi_color(THEME_REC *theme, const char *str, int *fg_ret if (i == -1) break; if (num == 38) { + flags &= ~GUI_PRINT_FLAG_MIRC_COLOR_FG; flags |= GUI_PRINT_FLAG_COLOR_24_FG; fg = num2; } else if (num == 48) { + flags &= ~GUI_PRINT_FLAG_MIRC_COLOR_BG; flags |= GUI_PRINT_FLAG_COLOR_24_BG; bg = num2; } @@ -1351,10 +1350,10 @@ static const char *get_ansi_color(THEME_REC *theme, const char *str, int *fg_ret if (*str == '\0') return start; if (num == 38) { - flags &= ~GUI_PRINT_FLAG_COLOR_24_FG; + flags &= ~GUI_PRINT_FLAGS_FG_COLORS; fg = num2; } else if (num == 48) { - flags &= ~GUI_PRINT_FLAG_COLOR_24_BG; + flags &= ~GUI_PRINT_FLAGS_BG_COLORS; bg = num2; } @@ -1363,16 +1362,16 @@ static const char *get_ansi_color(THEME_REC *theme, const char *str, int *fg_ret break; default: if (num >= 30 && num <= 37) { - flags &= ~GUI_PRINT_FLAG_COLOR_24_FG; + flags &= ~GUI_PRINT_FLAGS_FG_COLORS; fg = ansitab[num-30]; } else if (num >= 40 && num <= 47) { - flags &= ~GUI_PRINT_FLAG_COLOR_24_BG; + flags &= ~GUI_PRINT_FLAGS_BG_COLORS; bg = ansitab[num-40]; } else if (num >= 90 && num <= 97) { - flags &= ~GUI_PRINT_FLAG_COLOR_24_FG; + flags &= ~GUI_PRINT_FLAGS_FG_COLORS; fg = 8 + ansitab[num-90]; } else if (num >= 100 && num <= 107) { - flags &= ~GUI_PRINT_FLAG_COLOR_24_BG; + flags &= ~GUI_PRINT_FLAGS_BG_COLORS; bg = 8 + ansitab[num-100]; } break; @@ -1393,17 +1392,17 @@ static const char *get_ansi_color(THEME_REC *theme, const char *str, int *fg_ret } /* parse MIRC color string */ -static void get_mirc_color(const char **str, int *fg_ret, int *bg_ret) +static void get_mirc_color(const char **str, int *fg_ret, int *bg_ret, int *flags) { int fg, bg; - fg = fg_ret == NULL ? -1 : *fg_ret; - bg = bg_ret == NULL ? -1 : *bg_ret; + fg = -1; + bg = -1; if (!i_isdigit(**str)) { /* turn off color */ - fg = -1; - bg = -1; + fg = 99; + bg = 99; } else { /* foreground color */ fg = **str-'0'; @@ -1425,8 +1424,20 @@ static void get_mirc_color(const char **str, int *fg_ret, int *bg_ret) } } - if (fg_ret) *fg_ret = fg; - if (bg_ret) *bg_ret = bg; + if (fg_ret && fg > -1) { + *fg_ret = fg; + if (flags) { + *flags &= ~GUI_PRINT_FLAG_COLOR_24_FG; + *flags |= GUI_PRINT_FLAG_MIRC_COLOR_FG; + } + } + if (bg_ret && bg > -1) { + *bg_ret = bg; + if (flags) { + *flags &= ~GUI_PRINT_FLAG_COLOR_24_BG; + *flags |= GUI_PRINT_FLAG_MIRC_COLOR_BG; + } + } } #define IS_COLOR_CODE(c) \ @@ -1452,7 +1463,7 @@ int strip_real_length(const char *str, int len, if (last_color_pos != NULL) *last_color_pos = (int) (str-start); str++; - get_mirc_color(&str, NULL, NULL); + get_mirc_color(&str, NULL, NULL, NULL); if (last_color_len != NULL) *last_color_len = (int) (str-mircstart); @@ -1508,7 +1519,7 @@ char *strip_codes(const char *input) p++; /* mirc color */ - get_mirc_color(&p, NULL, NULL); + get_mirc_color(&p, NULL, NULL, NULL); p--; continue; } @@ -1622,15 +1633,19 @@ void format_send_as_gui_flags(TEXT_DEST_REC *dest, const char *text, SIGNAL_FUNC break; case 3: /* MIRC color */ - get_mirc_color((const char **) &ptr, - hide_colors ? NULL : &fgcolor, - hide_colors ? NULL : &bgcolor); - if (!hide_colors) - flags |= GUI_PRINT_FLAG_MIRC_COLOR; + get_mirc_color((const char **) &ptr, hide_colors ? NULL : &fgcolor, + hide_colors ? NULL : &bgcolor, hide_colors ? NULL : &flags); + if ((flags & GUI_PRINT_FLAG_MIRC_COLOR_FG) && fgcolor == 99) { + flags &= ~GUI_PRINT_FLAG_MIRC_COLOR_FG; + ss_restore_fgcolor(theme, &style_stack, &fgcolor, &flags); + } + if ((flags & GUI_PRINT_FLAG_MIRC_COLOR_BG) && bgcolor == 99) { + flags &= ~GUI_PRINT_FLAG_MIRC_COLOR_BG; + ss_restore_bgcolor(theme, &style_stack, &bgcolor, &flags); + } break; case 4: /* user specific colors */ - flags &= ~GUI_PRINT_FLAG_MIRC_COLOR; switch (*ptr) { case FORMAT_STYLE_BLINK: flags ^= GUI_PRINT_FLAG_BLINK; @@ -1676,27 +1691,27 @@ void format_send_as_gui_flags(TEXT_DEST_REC *dest, const char *text, SIGNAL_FUNC break; case FORMAT_COLOR_EXT1: fgcolor = 0x10 + *++ptr - FORMAT_COLOR_NOCHANGE; - flags &= ~GUI_PRINT_FLAG_COLOR_24_FG; + flags &= ~GUI_PRINT_FLAGS_FG_COLORS; break; case FORMAT_COLOR_EXT1_BG: bgcolor = 0x10 + *++ptr - FORMAT_COLOR_NOCHANGE; - flags &= ~GUI_PRINT_FLAG_COLOR_24_BG; + flags &= ~GUI_PRINT_FLAGS_BG_COLORS; break; case FORMAT_COLOR_EXT2: fgcolor = 0x60 + *++ptr - FORMAT_COLOR_NOCHANGE; - flags &= ~GUI_PRINT_FLAG_COLOR_24_FG; + flags &= ~GUI_PRINT_FLAGS_FG_COLORS; break; case FORMAT_COLOR_EXT2_BG: bgcolor = 0x60 + *++ptr - FORMAT_COLOR_NOCHANGE; - flags &= ~GUI_PRINT_FLAG_COLOR_24_BG; + flags &= ~GUI_PRINT_FLAGS_BG_COLORS; break; case FORMAT_COLOR_EXT3: fgcolor = 0xb0 + *++ptr - FORMAT_COLOR_NOCHANGE; - flags &= ~GUI_PRINT_FLAG_COLOR_24_FG; + flags &= ~GUI_PRINT_FLAGS_FG_COLORS; break; case FORMAT_COLOR_EXT3_BG: bgcolor = 0xb0 + *++ptr - FORMAT_COLOR_NOCHANGE; - flags &= ~GUI_PRINT_FLAG_COLOR_24_BG; + flags &= ~GUI_PRINT_FLAGS_BG_COLORS; break; case FORMAT_COLOR_24: unformat_24bit_color((const char **) &ptr, 1, &fgcolor, &bgcolor, @@ -1704,16 +1719,26 @@ void format_send_as_gui_flags(TEXT_DEST_REC *dest, const char *text, SIGNAL_FUNC break; default: if (*ptr != FORMAT_COLOR_NOCHANGE) { - flags &= ~GUI_PRINT_FLAG_COLOR_24_FG; - fgcolor = *ptr==(char)0xff ? -1 : (unsigned char) *ptr-'0'; + if (*ptr == (char) 0xff) { + ss_restore_fgcolor(theme, &style_stack, &fgcolor, + &flags); + } else { + flags &= ~GUI_PRINT_FLAGS_FG_COLORS; + fgcolor = (unsigned char) *ptr - '0'; + } } if (ptr[1] == '\0') break; ptr++; if (*ptr != FORMAT_COLOR_NOCHANGE) { - flags &= ~GUI_PRINT_FLAG_COLOR_24_BG; - bgcolor = *ptr==(char)0xff ? -1 : *ptr-'0'; + if (*ptr == (char) 0xff) { + ss_restore_bgcolor(theme, &style_stack, &bgcolor, + &flags); + } else { + flags &= ~GUI_PRINT_FLAGS_BG_COLORS; + bgcolor = *ptr - '0'; + } } } if (*ptr == '\0') diff --git a/src/fe-common/core/formats.h b/src/fe-common/core/formats.h index 1aa99427..f0e23f95 100644 --- a/src/fe-common/core/formats.h +++ b/src/fe-common/core/formats.h @@ -5,11 +5,11 @@ #include #include +/* clang-format off */ #define GUI_PRINT_FLAG_BOLD 0x0001 #define GUI_PRINT_FLAG_REVERSE 0x0002 #define GUI_PRINT_FLAG_UNDERLINE 0x0004 #define GUI_PRINT_FLAG_BLINK 0x0008 -#define GUI_PRINT_FLAG_MIRC_COLOR 0x0010 #define GUI_PRINT_FLAG_INDENT 0x0020 #define GUI_PRINT_FLAG_ITALIC 0x0040 #define GUI_PRINT_FLAG_NEWLINE 0x0080 @@ -17,6 +17,17 @@ #define GUI_PRINT_FLAG_MONOSPACE 0x0200 #define GUI_PRINT_FLAG_COLOR_24_FG 0x0400 #define GUI_PRINT_FLAG_COLOR_24_BG 0x0800 +#define GUI_PRINT_FLAG_MIRC_COLOR_FG 0x1000 +#define GUI_PRINT_FLAG_MIRC_COLOR_BG 0x2000 +/* clang-format on */ + +#define GUI_PRINT_FLAGS_COLORS_24 (GUI_PRINT_FLAG_COLOR_24_FG | GUI_PRINT_FLAG_COLOR_24_BG) +#define GUI_PRINT_FLAGS_MIRC_COLORS (GUI_PRINT_FLAG_MIRC_COLOR_FG | GUI_PRINT_FLAG_MIRC_COLOR_BG) +#define GUI_PRINT_FLAGS_FG_COLORS (GUI_PRINT_FLAG_COLOR_24_FG | GUI_PRINT_FLAG_MIRC_COLOR_FG) +#define GUI_PRINT_FLAGS_BG_COLORS (GUI_PRINT_FLAG_COLOR_24_BG | GUI_PRINT_FLAG_MIRC_COLOR_BG) +#define GUI_PRINT_FLAGS_COLORS \ + (GUI_PRINT_FLAG_COLOR_24_FG | GUI_PRINT_FLAG_COLOR_24_BG | GUI_PRINT_FLAG_MIRC_COLOR_FG | \ + GUI_PRINT_FLAG_MIRC_COLOR_BG) #define MAX_FORMAT_PARAMS 10 #define DEFAULT_FORMAT_ARGLIST_SIZE 200 @@ -37,17 +48,17 @@ struct _FORMAT_REC { }; /* clang-format off */ -#define PRINT_FLAG_SET_LINE_START 0x0001 -#define PRINT_FLAG_SET_LINE_START_IRSSI 0x0002 -#define PRINT_FLAG_UNSET_LINE_START 0x0040 +#define PRINT_FLAG_SET_LINE_START 0x0001 +#define PRINT_FLAG_SET_LINE_START_IRSSI 0x0002 +#define PRINT_FLAG_UNSET_LINE_START 0x0040 -#define PRINT_FLAG_SET_TIMESTAMP 0x0004 -#define PRINT_FLAG_UNSET_TIMESTAMP 0x0008 +#define PRINT_FLAG_SET_TIMESTAMP 0x0004 +#define PRINT_FLAG_UNSET_TIMESTAMP 0x0008 -#define PRINT_FLAG_SET_SERVERTAG 0x0010 -#define PRINT_FLAG_UNSET_SERVERTAG 0x0020 +#define PRINT_FLAG_SET_SERVERTAG 0x0010 +#define PRINT_FLAG_UNSET_SERVERTAG 0x0020 -#define PRINT_FLAG_FORMAT 0x0080 +#define PRINT_FLAG_FORMAT 0x0080 /* clang-format on */ typedef struct _HILIGHT_REC HILIGHT_REC; diff --git a/src/fe-text/gui-printtext.c b/src/fe-text/gui-printtext.c index 7482d470..c472aa97 100644 --- a/src/fe-text/gui-printtext.c +++ b/src/fe-text/gui-printtext.c @@ -32,6 +32,7 @@ #include /* Terminal indexed colour map */ +/* clang-format off */ int mirc_colors[] = { 15, 0, 1, 2, 12, 4, 5, 6, 14, 10, 3, 11, 9, 13, 8, 7, /* 16-27 */ 52, 94, 100, 58, 22, 29, 23, 24, 17, 54, 53, 89, /* 28-39 */ 88, 130, 142, 64, 28, 35, 30, 25, 18, 91, 90, 125, @@ -40,9 +41,11 @@ int mirc_colors[] = { 15, 0, 1, 2, 12, 4, 5, 6, 14, 10, 3, 11, 9, 13, 8, 7, /* 64-75 */ 203, 215, 227, 191, 83, 122, 87, 111, 63, 177, 207, 205, /* 76-87 */ 217, 223, 229, 193, 157, 158, 159, 153, 147, 183, 219, 212, /* 88-98 */ 16, 233, 235, 237, 239, 241, 244, 247, 250, 254, 231, -1 }; +/* clang-format on */ /* RGB colour map */ -int mirc_colors24[] = { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, +/* clang-format off */ +int mirc_colors_24bit[] = { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 16-27 */ 0x470000, 0x472100, 0x474700, 0x324700, 0x004700, 0x00472c, 0x004747, 0x002747, 0x000047, 0x2e0047, 0x470047, 0x47002a, /* 28-39 */ 0x740000, 0x743a00, 0x747400, 0x517400, 0x007400, 0x007449, 0x007474, 0x004074, 0x000074, 0x4b0074, 0x740074, 0x740045, /* 40-51 */ 0xb50000, 0xb56300, 0xb5b500, 0x7db500, 0x00b500, 0x00b571, 0x00b5b5, 0x0063b5, 0x0000b5, 0x7500b5, 0xb500b5, 0xb5006b, @@ -50,8 +53,9 @@ int mirc_colors24[] = { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 64-75 */ 0xff5959, 0xffb459, 0xffff71, 0xcfff60, 0x6fff6f, 0x65ffc9, 0x6dffff, 0x59b4ff, 0x5959ff, 0xc459ff, 0xff66ff, 0xff59bc, /* 76-87 */ 0xff9c9c, 0xffd39c, 0xffff9c, 0xe2ff9c, 0x9cff9c, 0x9cffdb, 0x9cffff, 0x9cd3ff, 0x9c9cff, 0xdc9cff, 0xff9cff, 0xff94d3, /* 88-98 */ 0x000000, 0x131313, 0x282828, 0x363636, 0x4d4d4d, 0x656565, 0x818181, 0x9f9f9f, 0xbcbcbc, 0xe2e2e2, 0xffffff, -1 }; +/* clang-format on */ -static int scrollback_lines, scrollback_time, scrollback_burst_remove; +static int scrollback_lines, scrollback_time, scrollback_burst_remove, colors_ansi_24bit; /* * If positive, remove lines older than scrollback_max_age seconds. * Deletion is triggered by the "gui print text finished" signal (i.e. a message @@ -242,33 +246,29 @@ static void remove_old_lines(TEXT_BUFFER_VIEW_REC *view) void gui_printtext_get_colors(int *flags, int *fg, int *bg, int *attr) { *attr = 0; - if (*flags & GUI_PRINT_FLAG_MIRC_COLOR) { - /* mirc colors - extended colours proposal */ - gboolean use_24_map = FALSE; - use_24_map = settings_get_bool("colors_ansi_24bit"); - if (*bg >= 0) { - if (use_24_map && mirc_colors24[*bg % 100] != -1) { - *bg = mirc_colors24[*bg % 100]; - *flags |= GUI_PRINT_FLAG_COLOR_24_BG; - } else { - *bg = mirc_colors[*bg % 100]; - *flags &= ~GUI_PRINT_FLAG_COLOR_24_BG; - /* ignore mirc color 99 = -1 (reset) */ - if (*bg != -1 && settings_get_bool("mirc_blink_fix")) { - if (*bg < 16) /* ansi bit flip :-( */ - *bg = (*bg&8) | (*bg&4)>>2 | (*bg&2) | (*bg&1)<<2; - *bg = term_color256map[*bg&0xff] & 7; - } + /* mirc colors - extended colours proposal */ + if ((*flags & GUI_PRINT_FLAG_MIRC_COLOR_BG) && (*bg >= 0)) { + if (colors_ansi_24bit && mirc_colors_24bit[*bg % 100] != -1) { + *bg = mirc_colors_24bit[*bg % 100]; + *flags |= GUI_PRINT_FLAG_COLOR_24_BG; + } else { + *bg = mirc_colors[*bg % 100]; + *flags &= ~GUI_PRINT_FLAG_COLOR_24_BG; + if (*bg != -1 && settings_get_bool("mirc_blink_fix")) { + if (*bg < 16) /* ansi bit flip :-( */ + *bg = + (*bg & 8) | (*bg & 4) >> 2 | (*bg & 2) | (*bg & 1) << 2; + *bg = term_color256map[*bg & 0xff] & 7; } } - if (*fg >= 0) { - if (use_24_map && mirc_colors24[*fg % 100] != -1) { - *fg = mirc_colors24[*fg % 100]; - *flags |= GUI_PRINT_FLAG_COLOR_24_FG; - } else { - *fg = mirc_colors[*fg % 100]; - *flags &= ~GUI_PRINT_FLAG_COLOR_24_FG; - } + } + if ((*flags & GUI_PRINT_FLAG_MIRC_COLOR_FG) && (*fg >= 0)) { + if (colors_ansi_24bit && mirc_colors_24bit[*fg % 100] != -1) { + *fg = mirc_colors_24bit[*fg % 100]; + *flags |= GUI_PRINT_FLAG_COLOR_24_FG; + } else { + *fg = mirc_colors[*fg % 100]; + *flags &= ~GUI_PRINT_FLAG_COLOR_24_FG; } } @@ -396,6 +396,7 @@ static void read_settings(void) scrollback_time = settings_get_time("scrollback_time")/1000; scrollback_max_age = settings_get_time("scrollback_max_age")/1000; scrollback_burst_remove = settings_get_int("scrollback_burst_remove"); + colors_ansi_24bit = settings_get_bool("colors_ansi_24bit"); } void gui_printtext_init(void)