diff --git a/src/nvim/option.c b/src/nvim/option.c index 4fa99424e8..11f3df7cfa 100644 --- a/src/nvim/option.c +++ b/src/nvim/option.c @@ -1773,14 +1773,13 @@ do_set ( // Set the new value. *(char_u **)(varp) = newval; - if (!starting && origval != NULL && newval != NULL) { - // origval may be freed by - // did_set_string_option(), make a copy. - saved_origval = xstrdup((char *)origval); - // newval (and varp) may become invalid if the - // buffer is closed by autocommands. - saved_newval = xstrdup((char *)newval); - } + // origval may be freed by + // did_set_string_option(), make a copy. + saved_origval = (origval != NULL) ? xstrdup((char *)origval) : 0; + + // newval (and varp) may become invalid if the + // buffer is closed by autocommands. + saved_newval = (newval != NULL) ? xstrdup((char *)newval) : 0; // Handle side effects, and set the global value for // ":set" on local options. Note: when setting 'syntax' @@ -1790,8 +1789,14 @@ do_set ( new_value_alloced, oldval, errbuf, opt_flags); if (errmsg == NULL) { - trigger_optionsset_string(opt_idx, opt_flags, saved_origval, - saved_newval); + if (!starting) { + trigger_optionsset_string(opt_idx, opt_flags, saved_origval, + saved_newval); + } + if (options[opt_idx].flags & P_UI_OPTION) { + ui_call_option_set(cstr_as_string(options[opt_idx].fullname), + STRING_OBJ(cstr_as_string(saved_newval))); + } } xfree(saved_origval); xfree(saved_newval); @@ -2382,8 +2387,8 @@ static char *set_string_option(const int opt_idx, const char *const value, char *const oldval = *varp; *varp = s; - char *const saved_oldval = (starting ? NULL : xstrdup(oldval)); - char *const saved_newval = (starting ? NULL : xstrdup(s)); + char *const saved_oldval = xstrdup(oldval); + char *const saved_newval = xstrdup(s); char *const r = (char *)did_set_string_option( opt_idx, (char_u **)varp, (int)true, (char_u *)oldval, NULL, opt_flags); @@ -2393,8 +2398,13 @@ static char *set_string_option(const int opt_idx, const char *const value, // call autocommand after handling side effects if (r == NULL) { - trigger_optionsset_string(opt_idx, opt_flags, - saved_oldval, saved_newval); + if (!starting) { + trigger_optionsset_string(opt_idx, opt_flags, saved_oldval, saved_newval); + } + if (options[opt_idx].flags & P_UI_OPTION) { + ui_call_option_set(cstr_as_string(options[opt_idx].fullname), + STRING_OBJ(cstr_as_string(saved_newval))); + } } xfree(saved_oldval); xfree(saved_newval); @@ -4060,10 +4070,11 @@ static char *set_bool_option(const int opt_idx, char_u *const varp, (char_u *) options[opt_idx].fullname, NULL, false, NULL); reset_v_option_vars(); - if (options[opt_idx].flags & P_UI_OPTION) { - ui_call_option_set(cstr_as_string(options[opt_idx].fullname), - BOOLEAN_OBJ(value)); - } + } + + if (options[opt_idx].flags & P_UI_OPTION) { + ui_call_option_set(cstr_as_string(options[opt_idx].fullname), + BOOLEAN_OBJ(value)); } comp_col(); /* in case 'ruler' or 'showcmd' changed */ @@ -4433,10 +4444,11 @@ static char *set_num_option(int opt_idx, char_u *varp, long value, (char_u *) options[opt_idx].fullname, NULL, false, NULL); reset_v_option_vars(); - if (options[opt_idx].flags & P_UI_OPTION) { - ui_call_option_set(cstr_as_string(options[opt_idx].fullname), - INTEGER_OBJ(value)); - } + } + + if (errmsg == NULL && options[opt_idx].flags & P_UI_OPTION) { + ui_call_option_set(cstr_as_string(options[opt_idx].fullname), + INTEGER_OBJ(value)); } comp_col(); /* in case 'columns' or 'ls' changed */ @@ -4465,10 +4477,6 @@ static void trigger_optionsset_string(int opt_idx, int opt_flags, apply_autocmds(EVENT_OPTIONSET, (char_u *)options[opt_idx].fullname, NULL, false, NULL); reset_v_option_vars(); - if (options[opt_idx].flags & P_UI_OPTION) { - ui_call_option_set(cstr_as_string(options[opt_idx].fullname), - STRING_OBJ(cstr_as_string(newval))); - } } } diff --git a/test/functional/ui/options_spec.lua b/test/functional/ui/options_spec.lua index 99aae16183..32e8faf7d3 100644 --- a/test/functional/ui/options_spec.lua +++ b/test/functional/ui/options_spec.lua @@ -1,85 +1,85 @@ +local global_helpers = require('test.helpers') local helpers = require('test.functional.helpers')(after_each) local Screen = require('test.functional.ui.screen') local clear = helpers.clear local command = helpers.command local eq = helpers.eq +local shallowcopy = global_helpers.shallowcopy describe('ui receives option updates', function() local screen - before_each(function() - clear() + local function reset(opts, ...) + local defaults = { + ambiwidth='single', + arabicshape=true, + emoji=true, + guifont='', + guifontset='', + guifontwide='', + linespace=0, + showtabline=1, + termguicolors=false, + ext_cmdline=false, + ext_popupmenu=false, + ext_tabline=false, + ext_wildmenu=false, + ext_linegrid=false, + ext_hlstate=false, + } + + clear(...) screen = Screen.new(20,5) - end) + screen:attach(opts) + -- NB: UI test suite can be run in both "linegrid" and legacy grid mode. + -- In both cases check that the received value is the one requested. + defaults.ext_linegrid = screen._options.ext_linegrid or false + return defaults + end after_each(function() screen:detach() end) - local defaults = { - ambiwidth='single', - arabicshape=true, - emoji=true, - guifont='', - guifontset='', - guifontwide='', - linespace=0, - showtabline=1, - termguicolors=false, - ext_cmdline=false, - ext_popupmenu=false, - ext_tabline=false, - ext_wildmenu=false, - ext_linegrid=false, - ext_hlstate=false, - } - it("for defaults", function() - screen:attach() - -- NB: UI test suite can be run in both "linegrid" and legacy grid mode. - -- In both cases check that the received value is the one requested. - defaults.ext_linegrid = screen._options.ext_linegrid or false + local expected = reset() screen:expect(function() - eq(defaults, screen.options) + eq(expected, screen.options) end) end) it("when setting options", function() - screen:attach() - defaults.ext_linegrid = screen._options.ext_linegrid or false - local changed = {} - for k,v in pairs(defaults) do - changed[k] = v - end + local expected = reset() + local defaults = shallowcopy(expected) command("set termguicolors") - changed.termguicolors = true + expected.termguicolors = true screen:expect(function() - eq(changed, screen.options) + eq(expected, screen.options) end) command("set guifont=Comic\\ Sans") - changed.guifont = "Comic Sans" + expected.guifont = "Comic Sans" screen:expect(function() - eq(changed, screen.options) + eq(expected, screen.options) end) command("set showtabline=0") - changed.showtabline = 0 + expected.showtabline = 0 screen:expect(function() - eq(changed, screen.options) + eq(expected, screen.options) end) command("set linespace=13") - changed.linespace = 13 + expected.linespace = 13 screen:expect(function() - eq(changed, screen.options) + eq(expected, screen.options) end) command("set linespace=-11") - changed.linespace = -11 + expected.linespace = -11 screen:expect(function() - eq(changed, screen.options) + eq(expected, screen.options) end) command("set all&") @@ -89,29 +89,35 @@ describe('ui receives option updates', function() end) it('with UI extensions', function() - local changed = {} - for k,v in pairs(defaults) do - changed[k] = v - end + local expected = reset({ext_cmdline=true, ext_wildmenu=true}) - screen:attach({ext_cmdline=true, ext_wildmenu=true}) - defaults.ext_linegrid = screen._options.ext_linegrid or false - changed.ext_cmdline = true - changed.ext_wildmenu = true + expected.ext_cmdline = true + expected.ext_wildmenu = true screen:expect(function() - eq(changed, screen.options) + eq(expected, screen.options) end) screen:set_option('ext_popupmenu', true) - changed.ext_popupmenu = true + expected.ext_popupmenu = true screen:expect(function() - eq(changed, screen.options) + eq(expected, screen.options) end) screen:set_option('ext_wildmenu', false) - changed.ext_wildmenu = false + expected.ext_wildmenu = false screen:expect(function() - eq(changed, screen.options) + eq(expected, screen.options) end) end) + + local function startup_test(headless) + local expected = reset(nil,{headless=headless,args={'--cmd', 'set guifont=Comic\\ Sans\\ 12'}}) + expected.guifont = "Comic Sans 12" + screen:expect(function() + eq(expected, screen.options) + end) + end + + it('from startup options with --headless', function() startup_test(true) end) + it('from startup options with --embed', function() startup_test(false) end) end)