From 963c238ff8d3f734bd852669d379e951f4fc7649 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Andr=C3=A9=20Tanner?= Date: Tue, 31 Jan 2017 09:10:07 +0100 Subject: vis: add workaround for broken color handling in Terminal.app Terminal.app sets $TERM=xterm-256color and ships a corresponding terminfo description advocating that it is capable of color changes to the 256 color palette when in fact it can not. We introduce a new boolean option "change-256colors" which is true by default but can be used to disable color changes. It is automatically set if Terminal.app is detected using $TERM_PROGRAM. This should fix display artifacts as described in #456. --- lua/vis-std.lua | 3 +++ sam.c | 7 +++++++ ui-curses.c | 19 ++++++++++--------- vis-cmds.c | 3 +++ vis-core.h | 1 + vis.c | 1 + 6 files changed, 25 insertions(+), 9 deletions(-) diff --git a/lua/vis-std.lua b/lua/vis-std.lua index 4da7293..6c42a0c 100644 --- a/lua/vis-std.lua +++ b/lua/vis-std.lua @@ -19,6 +19,9 @@ vis.events.subscribe(vis.events.INIT, function() vis.lexers = require('lexer') end + if os.getenv("TERM_PROGRAM") == "Apple_Terminal" then + vis:command("set change-256colors false"); + end vis:command("set theme ".. (vis.ui.colors <= 16 and "default-16" or "default-256")) end) diff --git a/sam.c b/sam.c index 95b2690..b232726 100644 --- a/sam.c +++ b/sam.c @@ -302,6 +302,7 @@ enum { OPTION_COLOR_COLUMN, OPTION_HORIZON, OPTION_SAVE_METHOD, + OPTION_CHANGE_256COLORS, }; static const OptionDef options[] = { @@ -385,6 +386,12 @@ static const OptionDef options[] = { OPTION_TYPE_STRING, OPTION_FLAG_WINDOW, "Save method to use for current file 'auto', 'atomic' or 'inplace'", }, + [OPTION_CHANGE_256COLORS] = { + { "change-256colors" }, + OPTION_TYPE_BOOL, OPTION_FLAG_NONE, + "Change 256 color palette to support 24bit colors", + }, + }; bool sam_init(Vis *vis) { diff --git a/ui-curses.c b/ui-curses.c index 94734d5..81a075f 100644 --- a/ui-curses.c +++ b/ui-curses.c @@ -65,7 +65,7 @@ #define MAX_COLOR_CLOBBER 240 static short color_clobber_idx = 0; static uint32_t clobbering_colors[MAX_COLOR_CLOBBER]; -static bool change_colors; +static int change_colors = -1; typedef struct { attr_t attr; @@ -143,8 +143,10 @@ static void undo_palette(void) } /* Work out the nearest color from the 256 color set, or perhaps exactly. */ -static int color_find_rgb(unsigned char r, unsigned char g, unsigned char b) +static int color_find_rgb(UiCurses *ui, unsigned char r, unsigned char g, unsigned char b) { + if (change_colors == -1) + change_colors = ui->vis->change_colors && can_change_color() && COLORS >= 256; if (change_colors) { uint32_t hexrep = ((r << 16) | (g << 8) | b) + 1; for (short i = 0; i < MAX_COLOR_CLOBBER; ++i) { @@ -217,7 +219,7 @@ static int color_find_rgb(unsigned char r, unsigned char g, unsigned char b) } /* Convert color from string. */ -static int color_fromstring(const char *s) +static int color_fromstring(UiCurses *ui, const char *s) { if (!s) return -1; @@ -230,7 +232,7 @@ static int color_fromstring(const char *s) int n = sscanf(s + 1, "%2hhx%2hhx%2hhx", &r, &g, &b); if (n != 3) return -1; - return color_find_rgb(r, g, b); + return color_find_rgb(ui, r, g, b); } else if ('0' <= *s && *s <= '9') { int col = atoi(s); return (col <= 0 || col > 255) ? -1 : col; @@ -349,9 +351,9 @@ static bool ui_window_syntax_style(UiWin *w, int id, const char *style) { } else if (!strcasecmp(option, "notblink")) { cell_style.attr &= ~A_BLINK; } else if (!strcasecmp(option, "fore")) { - cell_style.fg = color_fromstring(p); + cell_style.fg = color_fromstring(win->ui, p); } else if (!strcasecmp(option, "back")) { - cell_style.bg = color_fromstring(p); + cell_style.bg = color_fromstring(win->ui, p); } option = next; } @@ -830,7 +832,7 @@ static TermKey *ui_termkey_get(Ui *ui) { } static void ui_suspend(Ui *ui) { - if (change_colors) + if (change_colors == 1) undo_palette(); endwin(); kill(0, SIGSTOP); @@ -913,7 +915,6 @@ static bool ui_init(Ui *ui, Vis *vis) { keypad(stdscr, TRUE); meta(stdscr, TRUE); curs_set(0); - change_colors = can_change_color() && COLORS >= 256; ui_resize(ui); return true; @@ -960,7 +961,7 @@ void ui_curses_free(Ui *ui) { return; while (uic->windows) ui_window_free((UiWin*)uic->windows); - if (change_colors) + if (change_colors == 1) undo_palette(); endwin(); if (uic->termkey) diff --git a/vis-cmds.c b/vis-cmds.c index 879eadd..8e512cc 100644 --- a/vis-cmds.c +++ b/vis-cmds.c @@ -291,6 +291,9 @@ static bool cmd_set(Vis *vis, Win *win, Command *cmd, const char *argv[], Cursor return false; } break; + case OPTION_CHANGE_256COLORS: + vis->change_colors = toggle ? !vis->change_colors : arg.b; + break; default: return false; } diff --git a/vis-core.h b/vis-core.h index cc8636d..66c71c4 100644 --- a/vis-core.h +++ b/vis-core.h @@ -168,6 +168,7 @@ struct Vis { int tabwidth; /* how many spaces should be used to display a tab */ bool expandtab; /* whether typed tabs should be converted to spaces */ bool autoindent; /* whether indentation should be copied from previous line on newline */ + bool change_colors; /* whether to adjust 256 color palette for true colors */ char *shell; /* shell used to launch external commands */ Map *cmds; /* ":"-commands, used for unique prefix queries */ Map *usercmds; /* user registered ":"-commands */ diff --git a/vis.c b/vis.c index 790069a..e0b2c1e 100644 --- a/vis.c +++ b/vis.c @@ -532,6 +532,7 @@ Vis *vis_new(Ui *ui, VisEvent *event) { vis->ui = ui; vis->tabwidth = 8; vis->expandtab = false; + vis->change_colors = true; vis->registers[VIS_REG_BLACKHOLE].type = REGISTER_BLACKHOLE; vis->registers[VIS_REG_CLIPBOARD].type = REGISTER_CLIPBOARD; array_init(&vis->motions); -- cgit v1.2.3