Add a ncurses_change_colors option that can disable color palette change

Closes #1057
This commit is contained in:
Maxime Coste 2016-12-30 07:01:13 +00:00
parent ea6994dd3b
commit 190a04d6c8
5 changed files with 51 additions and 26 deletions

View File

@ -946,6 +946,8 @@ Some options are built in Kakoune, and can be used to control it's behaviour:
- `ncurses_assistant`: specify the nice assistant you get in info boxes, can - `ncurses_assistant`: specify the nice assistant you get in info boxes, can
be 'clippy' (the default), 'cat' or 'none' be 'clippy' (the default), 'cat' or 'none'
- `ncurses_enable_mouse`: boolean option that enables mouse support - `ncurses_enable_mouse`: boolean option that enables mouse support
- `ncurses_change_colors`: boolean option that can disable color palette
changing if the terminfo enables it but the terminal does not support it.
- `ncurses_wheel_down_button` and `ncurses_wheel_up_button`: specify which - `ncurses_wheel_down_button` and `ncurses_wheel_up_button`: specify which
button send for wheel down/up events. button send for wheel down/up events.
- `ncurses_buffer_padding_str`: string that will be used to mark the - `ncurses_buffer_padding_str`: string that will be used to mark the

View File

@ -158,6 +158,10 @@ Builtin options
*ncurses_enable_mouse*::: *ncurses_enable_mouse*:::
boolean option that enables mouse support boolean option that enables mouse support
*ncurses_change_colors*:::
boolean option that can disable color palette changing if the
terminfo enables it but the terminal does not support it.
*ncurses_wheel_down_button*, *ncurses_wheel_up_button*::: *ncurses_wheel_down_button*, *ncurses_wheel_up_button*:::
specify which button send for wheel down/up events specify which button send for wheel down/up events

View File

@ -272,10 +272,9 @@ void register_options()
" ncurses_status_on_top bool\n" " ncurses_status_on_top bool\n"
" ncurses_set_title bool\n" " ncurses_set_title bool\n"
" ncurses_enable_mouse bool\n" " ncurses_enable_mouse bool\n"
" ncurses_change_colors bool\n"
" ncurses_wheel_up_button int\n" " ncurses_wheel_up_button int\n"
" ncurses_wheel_down_button int\n" " ncurses_wheel_down_button int\n",
" ncurses_buffer_padding_str str\n"
" ncurses_buffer_padding_type fill|single|off\n",
UserInterface::Options{}); UserInterface::Options{});
reg.declare_option("modelinefmt", "format string used to generate the modeline", reg.declare_option("modelinefmt", "format string used to generate the modeline",
"%val{bufname} %val{cursor_line}:%val{cursor_char_column} "_str); "%val{bufname} %val{cursor_line}:%val{cursor_char_column} "_str);

View File

@ -133,7 +133,7 @@ int NCursesUI::get_color(Color color)
auto it = m_colors.find(color); auto it = m_colors.find(color);
if (it != m_colors.end()) if (it != m_colors.end())
return it->second; return it->second;
else if (can_change_color() and COLORS > 16) else if (m_change_colors and can_change_color() and COLORS > 16)
{ {
kak_assert(color.color == Color::RGB); kak_assert(color.color == Color::RGB);
if (m_next_color > COLORS) if (m_next_color > COLORS)
@ -168,26 +168,22 @@ int NCursesUI::get_color(Color color)
int NCursesUI::get_color_pair(const Face& face) int NCursesUI::get_color_pair(const Face& face)
{ {
static int next_pair = 1;
ColorPair colors{face.fg, face.bg}; ColorPair colors{face.fg, face.bg};
auto it = m_colorpairs.find(colors); auto it = m_colorpairs.find(colors);
if (it != m_colorpairs.end()) if (it != m_colorpairs.end())
return it->second; return it->second;
else else
{ {
init_pair(next_pair, get_color(face.fg), get_color(face.bg)); init_pair(m_next_pair, get_color(face.fg), get_color(face.bg));
m_colorpairs[colors] = next_pair; m_colorpairs[colors] = m_next_pair;
return next_pair++; return m_next_pair++;
} }
} }
void NCursesUI::set_face(NCursesWin* window, Face face, const Face& default_face) void NCursesUI::set_face(NCursesWin* window, Face face, const Face& default_face)
{ {
static int current_pair = -1; if (m_active_pair != -1)
wattroff(window, COLOR_PAIR(m_active_pair));
if (current_pair != -1)
wattroff(window, COLOR_PAIR(current_pair));
if (face.fg == Color::Default) if (face.fg == Color::Default)
face.fg = default_face.fg; face.fg = default_face.fg;
@ -196,8 +192,8 @@ void NCursesUI::set_face(NCursesWin* window, Face face, const Face& default_face
if (face.fg != Color::Default or face.bg != Color::Default) if (face.fg != Color::Default or face.bg != Color::Default)
{ {
current_pair = get_color_pair(face); m_active_pair = get_color_pair(face);
wattron(window, COLOR_PAIR(current_pair)); wattron(window, COLOR_PAIR(m_active_pair));
} }
set_attribute(window, A_UNDERLINE, face.attributes & Attribute::Underline); set_attribute(window, A_UNDERLINE, face.attributes & Attribute::Underline);
@ -218,6 +214,19 @@ void on_term_resize(int)
EventManager::instance().force_signal(0); EventManager::instance().force_signal(0);
} }
static constexpr std::initializer_list<std::pair<const Kakoune::Color, int>>
default_colors = {
{ Color::Default, -1 },
{ Color::Black, COLOR_BLACK },
{ Color::Red, COLOR_RED },
{ Color::Green, COLOR_GREEN },
{ Color::Yellow, COLOR_YELLOW },
{ Color::Blue, COLOR_BLUE },
{ Color::Magenta, COLOR_MAGENTA },
{ Color::Cyan, COLOR_CYAN },
{ Color::White, COLOR_WHITE },
};
NCursesUI::NCursesUI() NCursesUI::NCursesUI()
: m_stdin_watcher{0, FdEvents::Read, : m_stdin_watcher{0, FdEvents::Read,
[this](FDWatcher&, FdEvents, EventMode mode) { [this](FDWatcher&, FdEvents, EventMode mode) {
@ -228,17 +237,7 @@ NCursesUI::NCursesUI()
m_on_key(*key); m_on_key(*key);
}}, }},
m_assistant(assistant_clippy), m_assistant(assistant_clippy),
m_colors{ m_colors{default_colors}
{ Color::Default, -1 },
{ Color::Black, COLOR_BLACK },
{ Color::Red, COLOR_RED },
{ Color::Green, COLOR_GREEN },
{ Color::Yellow, COLOR_YELLOW },
{ Color::Blue, COLOR_BLUE },
{ Color::Magenta, COLOR_MAGENTA },
{ Color::Cyan, COLOR_CYAN },
{ Color::White, COLOR_WHITE },
}
{ {
initscr(); initscr();
raw(); raw();
@ -1015,6 +1014,24 @@ void NCursesUI::set_ui_options(const Options& options)
(it->value == "yes" or it->value == "true"); (it->value == "yes" or it->value == "true");
} }
{
auto it = options.find("ncurses_change_colors");
auto value = it == options.end() or
(it->value == "yes" or it->value == "true");
if (can_change_color() and m_change_colors != value)
{
fputs("\033]104;\007", stdout); // try to reset palette
fflush(stdout);
m_colorpairs.clear();
m_colors = default_colors;
m_next_color = 16;
m_next_pair = 1;
m_active_pair = -1;
}
m_change_colors = value;
}
{ {
auto enable_mouse_it = options.find("ncurses_enable_mouse"); auto enable_mouse_it = options.find("ncurses_enable_mouse");
enable_mouse(enable_mouse_it == options.end() or enable_mouse(enable_mouse_it == options.end() or

View File

@ -80,6 +80,8 @@ private:
UnorderedMap<Color, int, MemoryDomain::Faces> m_colors; UnorderedMap<Color, int, MemoryDomain::Faces> m_colors;
UnorderedMap<ColorPair, int, MemoryDomain::Faces> m_colorpairs; UnorderedMap<ColorPair, int, MemoryDomain::Faces> m_colorpairs;
int m_next_color = 16; int m_next_color = 16;
int m_next_pair = 1;
int m_active_pair = -1;
struct Window : Rect struct Window : Rect
{ {
@ -130,6 +132,7 @@ private:
int m_wheel_down_button = 5; int m_wheel_down_button = 5;
bool m_set_title = true; bool m_set_title = true;
bool m_change_colors = true;
bool m_dirty = false; bool m_dirty = false;
}; };