Merge pull request #18145 from bfredl/term_opt

feat(api): allow remote UI to set terminal options
This commit is contained in:
bfredl 2022-04-20 14:50:04 +02:00 committed by GitHub
commit 81f1e33d15
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 89 additions and 12 deletions

View File

@ -49,6 +49,9 @@ with these (optional) keys:
'wildmenu'. |ui-popupmenu|
`ext_tabline` Externalize the tabline. |ui-tabline|
`ext_termcolors` Use external default colors.
`term_name` Sets the name of the terminal 'term'.
`term_colors` Sets the number of supported colors 't_Co'.
`term_background` Sets the default value of 'background'.
Specifying an unknown option is an error; UIs can check the |api-metadata|
`ui_options` key for supported options.

View File

@ -14,6 +14,7 @@
#include "nvim/map.h"
#include "nvim/memory.h"
#include "nvim/msgpack_rpc/channel.h"
#include "nvim/option.h"
#include "nvim/popupmnu.h"
#include "nvim/screen.h"
#include "nvim/ui.h"
@ -255,6 +256,33 @@ static void ui_set_option(UI *ui, bool init, String name, Object value, Error *e
return;
}
if (strequal(name.data, "term_name")) {
if (value.type != kObjectTypeString) {
api_set_error(error, kErrorTypeValidation, "term_name must be a String");
return;
}
set_tty_option("term", xstrdup(value.data.string.data));
return;
}
if (strequal(name.data, "term_colors")) {
if (value.type != kObjectTypeInteger) {
api_set_error(error, kErrorTypeValidation, "term_colors must be a Integer");
return;
}
t_colors = (int)value.data.integer;
return;
}
if (strequal(name.data, "term_background")) {
if (value.type != kObjectTypeString) {
api_set_error(error, kErrorTypeValidation, "term_background must be a String");
return;
}
set_tty_background(value.data.string.data);
return;
}
// LEGACY: Deprecated option, use `ext_cmdline` instead.
bool is_popupmenu = strequal(name.data, "popupmenu_external");

View File

@ -4921,6 +4921,23 @@ bool set_tty_option(const char *name, char *value)
return false;
}
void set_tty_background(const char *value)
{
if (option_was_set("bg") || strequal((char *)p_bg, value)) {
// background is already set... ignore
return;
}
if (starting) {
// Wait until after startup, so OptionSet is triggered.
do_cmdline_cmd((value[0] == 'l')
? "autocmd VimEnter * ++once ++nested set bg=light"
: "autocmd VimEnter * ++once ++nested set bg=dark");
} else {
set_option_value("bg", 0L, value, 0);
reset_option_was_set("bg");
}
}
/// Find index for an option
///
/// @param[in] arg Option name.

View File

@ -442,18 +442,7 @@ static HandleState handle_bracketed_paste(TermInput *input)
static void set_bg_deferred(void **argv)
{
char *bgvalue = argv[0];
if (!option_was_set("bg") && !strequal((char *)p_bg, bgvalue)) {
// Value differs, apply it.
if (starting) {
// Wait until after startup, so OptionSet is triggered.
do_cmdline_cmd((bgvalue[0] == 'l')
? "autocmd VimEnter * ++once ++nested set bg=light"
: "autocmd VimEnter * ++once ++nested set bg=dark");
} else {
set_option_value("bg", 0L, bgvalue, 0);
reset_option_was_set("bg");
}
}
set_tty_background(bgvalue);
}
// During startup, tui.c requests the background color (see `ext.get_bg`).

View File

@ -4,6 +4,7 @@ local clear = helpers.clear
local command = helpers.command
local eq = helpers.eq
local shallowcopy = helpers.shallowcopy
local eval = helpers.eval
describe('UI receives option updates', function()
local screen
@ -168,3 +169,42 @@ describe('UI receives option updates', function()
it('from startup options with --headless', function() startup_test(true) end)
it('from startup options with --embed', function() startup_test(false) end)
end)
describe('UI can set terminal option', function()
local screen
before_each(function()
-- by default we implicity "--cmd 'set bg=light'" which ruins everything
clear{args_rm={'--cmd'}}
screen = Screen.new(20,5)
end)
it('term_background', function()
eq('dark', eval '&background')
screen:attach {term_background='light'}
eq('light', eval '&background')
end)
it("term_background but not if 'background' already set by user", function()
eq('dark', eval '&background')
command 'set background=dark'
screen:attach {term_background='light'}
eq('dark', eval '&background')
end)
it('term_name', function()
eq('nvim', eval '&term')
screen:attach {term_name='xterm'}
eq('xterm', eval '&term')
end)
it('term_colors', function()
eq('256', eval '&t_Co')
screen:attach {term_colors=8}
eq('8', eval '&t_Co')
end)
end)