From e47622f26b40d88bb8582c391df30474a64a082c Mon Sep 17 00:00:00 2001 From: Jakob Schnitzer Date: Sun, 26 Mar 2017 13:20:44 +0200 Subject: options: setlocal should only set local value For 'iminsert' and 'imsearch' the global value was always changed. --- src/nvim/option.c | 2 -- .../functional/options/setlocal_setglobal_spec.lua | 22 ++++++++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) create mode 100644 test/functional/options/setlocal_setglobal_spec.lua diff --git a/src/nvim/option.c b/src/nvim/option.c index 0bf81b4d3a..f622efeb0b 100644 --- a/src/nvim/option.c +++ b/src/nvim/option.c @@ -4120,7 +4120,6 @@ static char *set_num_option(int opt_idx, char_u *varp, long value, errmsg = e_invarg; curbuf->b_p_iminsert = B_IMODE_NONE; } - p_iminsert = curbuf->b_p_iminsert; showmode(); /* Show/unshow value of 'keymap' in status lines. */ status_redraw_curbuf(); @@ -4134,7 +4133,6 @@ static char *set_num_option(int opt_idx, char_u *varp, long value, errmsg = e_invarg; curbuf->b_p_imsearch = B_IMODE_NONE; } - p_imsearch = curbuf->b_p_imsearch; } /* if 'titlelen' has changed, redraw the title */ else if (pp == &p_titlelen) { diff --git a/test/functional/options/setlocal_setglobal_spec.lua b/test/functional/options/setlocal_setglobal_spec.lua new file mode 100644 index 0000000000..6902437403 --- /dev/null +++ b/test/functional/options/setlocal_setglobal_spec.lua @@ -0,0 +1,22 @@ +-- Tests for :setlocal and :setglobal + +local helpers = require('test.functional.helpers')(after_each) +local clear, execute, eval, eq, nvim = + helpers.clear, helpers.execute, helpers.eval, helpers.eq, helpers.nvim + +local function get_num_option_global(opt) + return nvim('command_output', 'setglobal ' .. opt .. '?'):match('%d+') +end + +describe(':setlocal', function() + before_each(clear) + + it('setlocal sets only local value', function() + eq('0', get_num_option_global('iminsert')) + execute('setlocal iminsert=1') + eq('0', get_num_option_global('iminsert')) + eq('0', get_num_option_global('imsearch')) + execute('setlocal imsearch=1') + eq('0', get_num_option_global('imsearch')) + end) +end) -- cgit From 628d0335b8e402008b3c03db9f5d2a109d5e8ef2 Mon Sep 17 00:00:00 2001 From: Jakob Schnitzer Date: Mon, 27 Mar 2017 09:51:14 +0200 Subject: options: add some tests --- test/functional/options/num_options_spec.lua | 42 ++++++++++++++++++++++ .../functional/options/setlocal_setglobal_spec.lua | 22 ------------ 2 files changed, 42 insertions(+), 22 deletions(-) create mode 100644 test/functional/options/num_options_spec.lua delete mode 100644 test/functional/options/setlocal_setglobal_spec.lua diff --git a/test/functional/options/num_options_spec.lua b/test/functional/options/num_options_spec.lua new file mode 100644 index 0000000000..c37bbeffc3 --- /dev/null +++ b/test/functional/options/num_options_spec.lua @@ -0,0 +1,42 @@ +-- Tests for :setlocal and :setglobal + +local helpers = require('test.functional.helpers')(after_each) +local clear, execute, eval, eq, nvim = + helpers.clear, helpers.execute, helpers.eval, helpers.eq, helpers.nvim + +local function get_num_option_global(opt) + return nvim('command_output', 'setglobal ' .. opt .. '?'):match('%d+') +end + +local function should_fail(opt, value, errmsg) + execute('let v:errmsg = ""') + execute('setglobal ' .. opt .. '=' .. value) + eq(errmsg, eval("v:errmsg"):match("E%d*:")) + execute('let v:errmsg = ""') + execute('setlocal ' .. opt .. '=' .. value) + eq(errmsg, eval("v:errmsg"):match("E%d*:")) + execute('let v:errmsg = ""') +end + +describe(':setlocal', function() + before_each(clear) + + it('setlocal sets only local value', function() + eq('0', get_num_option_global('iminsert')) + execute('setlocal iminsert=1') + eq('0', get_num_option_global('iminsert')) + eq('0', get_num_option_global('imsearch')) + execute('setlocal imsearch=1') + eq('0', get_num_option_global('imsearch')) + end) +end) + +describe(':set validation', function() + before_each(clear) + + it('setlocal and setglobal validate values', function() + should_fail('shiftwidth', -10, 'E487') + should_fail('tabstop', -10, 'E487') + should_fail('winheight', -10, 'E487') + end) +end) diff --git a/test/functional/options/setlocal_setglobal_spec.lua b/test/functional/options/setlocal_setglobal_spec.lua deleted file mode 100644 index 6902437403..0000000000 --- a/test/functional/options/setlocal_setglobal_spec.lua +++ /dev/null @@ -1,22 +0,0 @@ --- Tests for :setlocal and :setglobal - -local helpers = require('test.functional.helpers')(after_each) -local clear, execute, eval, eq, nvim = - helpers.clear, helpers.execute, helpers.eval, helpers.eq, helpers.nvim - -local function get_num_option_global(opt) - return nvim('command_output', 'setglobal ' .. opt .. '?'):match('%d+') -end - -describe(':setlocal', function() - before_each(clear) - - it('setlocal sets only local value', function() - eq('0', get_num_option_global('iminsert')) - execute('setlocal iminsert=1') - eq('0', get_num_option_global('iminsert')) - eq('0', get_num_option_global('imsearch')) - execute('setlocal imsearch=1') - eq('0', get_num_option_global('imsearch')) - end) -end) -- cgit From 79d3e9494299ae91f297c5a06dc3fd4f2f71f13e Mon Sep 17 00:00:00 2001 From: Jakob Schnitzer Date: Mon, 27 Mar 2017 18:52:16 +0200 Subject: options: move code around in set_num_option handle side-effects after validation --- src/nvim/option.c | 175 +++++++++++++++++++++++++++++++----------------------- 1 file changed, 101 insertions(+), 74 deletions(-) diff --git a/src/nvim/option.c b/src/nvim/option.c index f622efeb0b..248b5205ec 100644 --- a/src/nvim/option.c +++ b/src/nvim/option.c @@ -4004,9 +4004,7 @@ static char *set_num_option(int opt_idx, char_u *varp, long value, curbuf->b_p_sw = curbuf->b_p_ts; } - /* - * Number options that need some action when changed - */ + // Number options that need some validation when changed. if (pp == &p_wh || pp == &p_hh) { if (p_wh < 1) { errmsg = e_positive; @@ -4020,14 +4018,6 @@ static char *set_num_option(int opt_idx, char_u *varp, long value, errmsg = e_positive; p_hh = 0; } - - /* Change window height NOW */ - if (lastwin != firstwin) { - if (pp == &p_wh && curwin->w_height < p_wh) - win_setheight((int)p_wh); - if (pp == &p_hh && curbuf->b_help && curwin->w_height < p_hh) - win_setheight((int)p_hh); - } } /* 'winminheight' */ else if (pp == &p_wmh) { @@ -4039,7 +4029,6 @@ static char *set_num_option(int opt_idx, char_u *varp, long value, errmsg = e_winheight; p_wmh = p_wh; } - win_setminheight(); } else if (pp == &p_wiw) { if (p_wiw < 1) { errmsg = e_positive; @@ -4049,10 +4038,6 @@ static char *set_num_option(int opt_idx, char_u *varp, long value, errmsg = e_winwidth; p_wiw = p_wmw; } - - /* Change window width NOW */ - if (lastwin != firstwin && curwin->w_width < p_wiw) - win_setwidth((int)p_wiw); } /* 'winminwidth' */ else if (pp == &p_wmw) { @@ -4064,29 +4049,11 @@ static char *set_num_option(int opt_idx, char_u *varp, long value, errmsg = e_winwidth; p_wmw = p_wiw; } - win_setminheight(); - } else if (pp == &p_ls) { - /* (re)set last window status line */ - last_status(false); - } - /* (re)set tab page line */ - else if (pp == &p_stal) { - shell_new_rows(); /* recompute window positions and heights */ } /* 'foldlevel' */ else if (pp == &curwin->w_p_fdl) { if (curwin->w_p_fdl < 0) curwin->w_p_fdl = 0; - newFoldLevel(); - } - /* 'foldminlines' */ - else if (pp == &curwin->w_p_fml) { - foldUpdateAll(curwin); - } - /* 'foldnestmax' */ - else if (pp == &curwin->w_p_fdn) { - if (foldmethodIsSyntax(curwin) || foldmethodIsIndent(curwin)) - foldUpdateAll(curwin); } /* 'foldcolumn' */ else if (pp == &curwin->w_p_fdc) { @@ -4097,16 +4064,6 @@ static char *set_num_option(int opt_idx, char_u *varp, long value, errmsg = e_invarg; curwin->w_p_fdc = 12; } - // 'shiftwidth' or 'tabstop' - } else if (pp == &curbuf->b_p_sw || pp == (long *)&curbuf->b_p_ts) { - if (foldmethodIsIndent(curwin)) { - foldUpdateAll(curwin); - } - // When 'shiftwidth' changes, or it's zero and 'tabstop' changes: - // parse 'cinoptions'. - if (pp == &curbuf->b_p_sw || curbuf->b_p_sw == 0) { - parse_cino(curbuf); - } } /* 'maxcombine' */ else if (pp == &p_mco) { @@ -4114,15 +4071,11 @@ static char *set_num_option(int opt_idx, char_u *varp, long value, p_mco = MAX_MCO; else if (p_mco < 0) p_mco = 0; - screenclear(); /* will re-allocate the screen */ } else if (pp == &curbuf->b_p_iminsert) { if (curbuf->b_p_iminsert < 0 || curbuf->b_p_iminsert > B_IMODE_LAST) { errmsg = e_invarg; curbuf->b_p_iminsert = B_IMODE_NONE; } - showmode(); - /* Show/unshow value of 'keymap' in status lines. */ - status_redraw_curbuf(); } else if (pp == &p_window) { if (p_window < 1) p_window = 1; @@ -4140,8 +4093,6 @@ static char *set_num_option(int opt_idx, char_u *varp, long value, errmsg = e_positive; p_titlelen = 85; } - if (starting != NO_SCREEN && old_value != p_titlelen) - need_maketitle = TRUE; } /* if p_ch changed value, change the command line height */ else if (pp == &p_ch) { @@ -4151,12 +4102,6 @@ static char *set_num_option(int opt_idx, char_u *varp, long value, } if (p_ch > Rows - min_rows() + 1) p_ch = Rows - min_rows() + 1; - - /* Only compute the new window layout when startup has been - * completed. Otherwise the frame sizes may be wrong. */ - if (p_ch != old_value && full_screen - ) - command_height(); } /* when 'updatecount' changes from zero to non-zero, open swap files */ else if (pp == &p_uc) { @@ -4164,8 +4109,6 @@ static char *set_num_option(int opt_idx, char_u *varp, long value, errmsg = e_positive; p_uc = 100; } - if (p_uc && !old_value) - ml_open_files(); } else if (pp == &curwin->w_p_cole) { if (curwin->w_p_cole < 0) { errmsg = e_positive; @@ -4175,18 +4118,6 @@ static char *set_num_option(int opt_idx, char_u *varp, long value, curwin->w_p_cole = 3; } } - /* sync undo before 'undolevels' changes */ - else if (pp == &p_ul) { - /* use the old value, otherwise u_sync() may not work properly */ - p_ul = old_value; - u_sync(TRUE); - p_ul = value; - } else if (pp == &curbuf->b_p_ul) { - /* use the old value, otherwise u_sync() may not work properly */ - curbuf->b_p_ul = old_value; - u_sync(TRUE); - curbuf->b_p_ul = value; - } /* 'numberwidth' must be positive */ else if (pp == &curwin->w_p_nuw) { if (curwin->w_p_nuw < 1) { @@ -4203,10 +4134,6 @@ static char *set_num_option(int opt_idx, char_u *varp, long value, errmsg = e_positive; curbuf->b_p_tw = 0; } - - FOR_ALL_TAB_WINDOWS(tp, wp) { - check_colorcolumn(wp); - } } else if (pp == &curbuf->b_p_scbk || pp == &p_scbk) { // 'scrollback' if (*pp < -1 || *pp > SB_MAX @@ -4219,6 +4146,106 @@ static char *set_num_option(int opt_idx, char_u *varp, long value, } } + // Number options that need some action when changed + if (pp == &p_wh || pp == &p_hh) { + /* Change window height NOW */ + if (lastwin != firstwin) { + if (pp == &p_wh && curwin->w_height < p_wh) + win_setheight((int)p_wh); + if (pp == &p_hh && curbuf->b_help && curwin->w_height < p_hh) + win_setheight((int)p_hh); + } + } + else if (pp == &p_wmh) { + win_setminheight(); + } else if (pp == &p_wiw) { + /* Change window width NOW */ + if (lastwin != firstwin && curwin->w_width < p_wiw) + win_setwidth((int)p_wiw); + } + else if (pp == &p_wmw) { + win_setminheight(); + } else if (pp == &p_ls) { + /* (re)set last window status line */ + last_status(false); + } + /* (re)set tab page line */ + else if (pp == &p_stal) { + shell_new_rows(); /* recompute window positions and heights */ + } + else if (pp == &curwin->w_p_fdl) { + newFoldLevel(); + } + /* 'foldminlines' */ + else if (pp == &curwin->w_p_fml) { + foldUpdateAll(curwin); + } + /* 'foldnestmax' */ + else if (pp == &curwin->w_p_fdn) { + if (foldmethodIsSyntax(curwin) || foldmethodIsIndent(curwin)) + foldUpdateAll(curwin); + // 'shiftwidth' or 'tabstop' + } else if (pp == &curbuf->b_p_sw || pp == (long *)&curbuf->b_p_ts) { + if (foldmethodIsIndent(curwin)) { + foldUpdateAll(curwin); + } + // When 'shiftwidth' changes, or it's zero and 'tabstop' changes: + // parse 'cinoptions'. + if (pp == &curbuf->b_p_sw || curbuf->b_p_sw == 0) { + parse_cino(curbuf); + } + } + /* 'maxcombine' */ + else if (pp == &p_mco) { + screenclear(); /* will re-allocate the screen */ + } else if (pp == &curbuf->b_p_iminsert) { + showmode(); + /* Show/unshow value of 'keymap' in status lines. */ + status_redraw_curbuf(); + } + /* if 'titlelen' has changed, redraw the title */ + else if (pp == &p_titlelen) { + if (starting != NO_SCREEN && old_value != p_titlelen) + need_maketitle = TRUE; + } + /* if p_ch changed value, change the command line height */ + else if (pp == &p_ch) { + + /* Only compute the new window layout when startup has been + * completed. Otherwise the frame sizes may be wrong. */ + if (p_ch != old_value && full_screen + ) + command_height(); + } + /* when 'updatecount' changes from zero to non-zero, open swap files */ + else if (pp == &p_uc) { + if (p_uc && !old_value) + ml_open_files(); + } + /* sync undo before 'undolevels' changes */ + else if (pp == &p_ul) { + /* use the old value, otherwise u_sync() may not work properly */ + p_ul = old_value; + u_sync(TRUE); + p_ul = value; + } else if (pp == &curbuf->b_p_ul) { + /* use the old value, otherwise u_sync() may not work properly */ + curbuf->b_p_ul = old_value; + u_sync(TRUE); + curbuf->b_p_ul = value; + } else if (pp == &curbuf->b_p_tw) { + FOR_ALL_TAB_WINDOWS(tp, wp) { + check_colorcolumn(wp); + } + } + else if (pp == &curbuf->b_p_scbk || pp == &p_scbk) { + if (curbuf->terminal) { + // Force the scrollback to take effect. + terminal_resize(curbuf->terminal, UINT16_MAX, UINT16_MAX); + } + } + + /* * Check the bounds for numeric options here */ -- cgit From f4920fb485da92d546c1e01f5f10766d00c3791d Mon Sep 17 00:00:00 2001 From: Jakob Schnitzer Date: Mon, 27 Mar 2017 19:03:46 +0200 Subject: options: if invalid value is given, reset to old value --- src/nvim/option.c | 112 ++++++++++++++++++++---------------------------------- 1 file changed, 42 insertions(+), 70 deletions(-) diff --git a/src/nvim/option.c b/src/nvim/option.c index 248b5205ec..d069cceb39 100644 --- a/src/nvim/option.c +++ b/src/nvim/option.c @@ -4005,144 +4005,114 @@ static char *set_num_option(int opt_idx, char_u *varp, long value, } // Number options that need some validation when changed. - if (pp == &p_wh || pp == &p_hh) { + if (pp == &p_wh) { if (p_wh < 1) { errmsg = e_positive; - p_wh = 1; } if (p_wmh > p_wh) { errmsg = e_winheight; - p_wh = p_wmh; } + } else if (pp == &p_hh) { if (p_hh < 0) { errmsg = e_positive; - p_hh = 0; } - } - /* 'winminheight' */ - else if (pp == &p_wmh) { + } else if (pp == &p_wmh) { if (p_wmh < 0) { errmsg = e_positive; - p_wmh = 0; } if (p_wmh > p_wh) { errmsg = e_winheight; - p_wmh = p_wh; } } else if (pp == &p_wiw) { if (p_wiw < 1) { errmsg = e_positive; - p_wiw = 1; } if (p_wmw > p_wiw) { errmsg = e_winwidth; - p_wiw = p_wmw; } - } - /* 'winminwidth' */ - else if (pp == &p_wmw) { + } else if (pp == &p_wmw) { if (p_wmw < 0) { errmsg = e_positive; - p_wmw = 0; } if (p_wmw > p_wiw) { errmsg = e_winwidth; - p_wmw = p_wiw; } - } - /* 'foldlevel' */ - else if (pp == &curwin->w_p_fdl) { - if (curwin->w_p_fdl < 0) - curwin->w_p_fdl = 0; - } - /* 'foldcolumn' */ - else if (pp == &curwin->w_p_fdc) { + } else if (pp == &curwin->w_p_fdl) { + if (curwin->w_p_fdl < 0) { + errmsg = e_positive; + } + } else if (pp == &curwin->w_p_fdc) { if (curwin->w_p_fdc < 0) { errmsg = e_positive; - curwin->w_p_fdc = 0; } else if (curwin->w_p_fdc > 12) { errmsg = e_invarg; - curwin->w_p_fdc = 12; } - } - /* 'maxcombine' */ - else if (pp == &p_mco) { - if (p_mco > MAX_MCO) - p_mco = MAX_MCO; - else if (p_mco < 0) - p_mco = 0; + } else if (pp == &p_mco) { + if (p_mco > MAX_MCO) { + errmsg = e_invarg; + } else if (p_mco < 0) { + errmsg = e_positive; + } } else if (pp == &curbuf->b_p_iminsert) { if (curbuf->b_p_iminsert < 0 || curbuf->b_p_iminsert > B_IMODE_LAST) { errmsg = e_invarg; - curbuf->b_p_iminsert = B_IMODE_NONE; } - } else if (pp == &p_window) { - if (p_window < 1) - p_window = 1; - else if (p_window >= Rows) - p_window = Rows - 1; } else if (pp == &curbuf->b_p_imsearch) { if (curbuf->b_p_imsearch < -1 || curbuf->b_p_imsearch > B_IMODE_LAST) { errmsg = e_invarg; - curbuf->b_p_imsearch = B_IMODE_NONE; } - } - /* if 'titlelen' has changed, redraw the title */ - else if (pp == &p_titlelen) { + } else if (pp == &p_titlelen) { if (p_titlelen < 0) { errmsg = e_positive; - p_titlelen = 85; - } - } - /* if p_ch changed value, change the command line height */ - else if (pp == &p_ch) { - if (p_ch < 1) { - errmsg = e_positive; - p_ch = 1; } - if (p_ch > Rows - min_rows() + 1) - p_ch = Rows - min_rows() + 1; - } - /* when 'updatecount' changes from zero to non-zero, open swap files */ - else if (pp == &p_uc) { + } else if (pp == &p_uc) { if (p_uc < 0) { errmsg = e_positive; - p_uc = 100; } } else if (pp == &curwin->w_p_cole) { if (curwin->w_p_cole < 0) { errmsg = e_positive; - curwin->w_p_cole = 0; } else if (curwin->w_p_cole > 3) { errmsg = e_invarg; - curwin->w_p_cole = 3; } - } - /* 'numberwidth' must be positive */ - else if (pp == &curwin->w_p_nuw) { + } else if (pp == &curwin->w_p_nuw) { if (curwin->w_p_nuw < 1) { errmsg = e_positive; - curwin->w_p_nuw = 1; } if (curwin->w_p_nuw > 10) { errmsg = e_invarg; - curwin->w_p_nuw = 10; } curwin->w_nrwidth_line_count = 0; } else if (pp == &curbuf->b_p_tw) { if (curbuf->b_p_tw < 0) { errmsg = e_positive; - curbuf->b_p_tw = 0; } } else if (pp == &curbuf->b_p_scbk || pp == &p_scbk) { - // 'scrollback' if (*pp < -1 || *pp > SB_MAX || (opt_flags == OPT_LOCAL && !curbuf->terminal)) { errmsg = e_invarg; - *pp = old_value; - } else if (curbuf->terminal) { - // Force the scrollback to take effect. - terminal_resize(curbuf->terminal, UINT16_MAX, UINT16_MAX); + } + } else if (pp == &p_ch) { + if (p_ch < 1) { + errmsg = e_positive; + } + } + + if (errmsg != NULL) { + *pp = old_value; + return errmsg; + } + + // For these options we want to fix some invalid values. + if (pp == &p_window) { + if (p_window < 1) { + p_window = 1; + } else if (p_window >= Rows) { + p_window = Rows - 1; + } + } else if (pp == &p_ch) { + if (p_ch > Rows - min_rows() + 1) { + p_ch = Rows - min_rows() + 1; } } @@ -4243,6 +4213,8 @@ static char *set_num_option(int opt_idx, char_u *varp, long value, // Force the scrollback to take effect. terminal_resize(curbuf->terminal, UINT16_MAX, UINT16_MAX); } + } else if (pp == &curwin->w_p_nuw) { + curwin->w_nrwidth_line_count = 0; } -- cgit From 1a56a032fe6060c3b4c8532c209ccfaa90fdf74e Mon Sep 17 00:00:00 2001 From: Jakob Schnitzer Date: Mon, 27 Mar 2017 19:17:58 +0200 Subject: options: clean up num_options side-effects --- src/nvim/option.c | 118 +++++++++++++++++++++++------------------------------- 1 file changed, 50 insertions(+), 68 deletions(-) diff --git a/src/nvim/option.c b/src/nvim/option.c index d069cceb39..f6639d4f6a 100644 --- a/src/nvim/option.c +++ b/src/nvim/option.c @@ -3999,11 +3999,6 @@ static char *set_num_option(int opt_idx, char_u *varp, long value, /* Remember where the option was set. */ set_option_scriptID_idx(opt_idx, opt_flags, current_SID); - if (curbuf->b_p_sw < 0) { - errmsg = e_positive; - curbuf->b_p_sw = curbuf->b_p_ts; - } - // Number options that need some validation when changed. if (pp == &p_wh) { if (p_wh < 1) { @@ -4091,13 +4086,18 @@ static char *set_num_option(int opt_idx, char_u *varp, long value, if (*pp < -1 || *pp > SB_MAX || (opt_flags == OPT_LOCAL && !curbuf->terminal)) { errmsg = e_invarg; - } + } + } else if (pp == &curbuf->b_p_sw) { + if (curbuf->b_p_sw < 0) { + errmsg = e_positive; + } } else if (pp == &p_ch) { if (p_ch < 1) { errmsg = e_positive; } } + // If validation failed, reset to old value and return. if (errmsg != NULL) { *pp = old_value; return errmsg; @@ -4117,45 +4117,38 @@ static char *set_num_option(int opt_idx, char_u *varp, long value, } // Number options that need some action when changed - if (pp == &p_wh || pp == &p_hh) { - /* Change window height NOW */ - if (lastwin != firstwin) { - if (pp == &p_wh && curwin->w_height < p_wh) - win_setheight((int)p_wh); - if (pp == &p_hh && curbuf->b_help && curwin->w_height < p_hh) - win_setheight((int)p_hh); + if (pp == &p_wh) { + if (lastwin != firstwin && curwin->w_height < p_wh) { + win_setheight((int)p_wh); } - } - else if (pp == &p_wmh) { + } else if (pp == &p_hh) { + if (lastwin != firstwin && curbuf->b_help && curwin->w_height < p_hh) { + win_setheight((int)p_hh); + } + } else if (pp == &p_wmh) { win_setminheight(); } else if (pp == &p_wiw) { - /* Change window width NOW */ - if (lastwin != firstwin && curwin->w_width < p_wiw) + if (lastwin != firstwin && curwin->w_width < p_wiw) { win_setwidth((int)p_wiw); - } - else if (pp == &p_wmw) { + } + } else if (pp == &p_wmw) { win_setminheight(); } else if (pp == &p_ls) { - /* (re)set last window status line */ + // (re)set last window status line. last_status(false); - } - /* (re)set tab page line */ - else if (pp == &p_stal) { - shell_new_rows(); /* recompute window positions and heights */ - } - else if (pp == &curwin->w_p_fdl) { + } else if (pp == &p_stal) { + // (re)set tab page line + shell_new_rows(); // recompute window positions and heights + } else if (pp == &curwin->w_p_fdl) { newFoldLevel(); - } - /* 'foldminlines' */ - else if (pp == &curwin->w_p_fml) { + } else if (pp == &curwin->w_p_fml) { foldUpdateAll(curwin); - } - /* 'foldnestmax' */ - else if (pp == &curwin->w_p_fdn) { - if (foldmethodIsSyntax(curwin) || foldmethodIsIndent(curwin)) + } else if (pp == &curwin->w_p_fdn) { + if (foldmethodIsSyntax(curwin) || foldmethodIsIndent(curwin)) { foldUpdateAll(curwin); - // 'shiftwidth' or 'tabstop' + } } else if (pp == &curbuf->b_p_sw || pp == (long *)&curbuf->b_p_ts) { + // 'shiftwidth' or 'tabstop' if (foldmethodIsIndent(curwin)) { foldUpdateAll(curwin); } @@ -4164,51 +4157,40 @@ static char *set_num_option(int opt_idx, char_u *varp, long value, if (pp == &curbuf->b_p_sw || curbuf->b_p_sw == 0) { parse_cino(curbuf); } - } - /* 'maxcombine' */ - else if (pp == &p_mco) { - screenclear(); /* will re-allocate the screen */ + } else if (pp == &p_mco) { + screenclear(); // will re-allocate the screen } else if (pp == &curbuf->b_p_iminsert) { showmode(); - /* Show/unshow value of 'keymap' in status lines. */ + // Show/unshow value of 'keymap' in status lines. status_redraw_curbuf(); - } - /* if 'titlelen' has changed, redraw the title */ - else if (pp == &p_titlelen) { - if (starting != NO_SCREEN && old_value != p_titlelen) + } else if (pp == &p_titlelen) { + // if 'titlelen' has changed, redraw the title + if (starting != NO_SCREEN && old_value != p_titlelen) { need_maketitle = TRUE; - } - /* if p_ch changed value, change the command line height */ - else if (pp == &p_ch) { - - /* Only compute the new window layout when startup has been - * completed. Otherwise the frame sizes may be wrong. */ - if (p_ch != old_value && full_screen - ) + } + } else if (pp == &p_ch) { + // if p_ch changed value, change the command line height + // Only compute the new window layout when startup has been + // completed. Otherwise the frame sizes may be wrong. + if (p_ch != old_value && full_screen) { command_height(); - } - /* when 'updatecount' changes from zero to non-zero, open swap files */ - else if (pp == &p_uc) { - if (p_uc && !old_value) + } + } else if (pp == &p_uc) { + // when 'updatecount' changes from zero to non-zero, open swap files + if (p_uc && !old_value) { ml_open_files(); - } - /* sync undo before 'undolevels' changes */ - else if (pp == &p_ul) { - /* use the old value, otherwise u_sync() may not work properly */ - p_ul = old_value; - u_sync(TRUE); - p_ul = value; - } else if (pp == &curbuf->b_p_ul) { - /* use the old value, otherwise u_sync() may not work properly */ - curbuf->b_p_ul = old_value; + } + } else if (pp == &p_ul || pp == &curbuf->b_p_ul) { + // sync undo before 'undolevels' changes + // use the old value, otherwise u_sync() may not work properly + *pp = old_value; u_sync(TRUE); - curbuf->b_p_ul = value; + *pp = value; } else if (pp == &curbuf->b_p_tw) { FOR_ALL_TAB_WINDOWS(tp, wp) { check_colorcolumn(wp); } - } - else if (pp == &curbuf->b_p_scbk || pp == &p_scbk) { + } else if (pp == &curbuf->b_p_scbk || pp == &p_scbk) { if (curbuf->terminal) { // Force the scrollback to take effect. terminal_resize(curbuf->terminal, UINT16_MAX, UINT16_MAX); -- cgit From 0273f96ef69d2a70a9c2e77b7f5da3a811bca460 Mon Sep 17 00:00:00 2001 From: Jakob Schnitzer Date: Mon, 27 Mar 2017 19:26:41 +0200 Subject: options: move more validation together --- src/nvim/option.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/nvim/option.c b/src/nvim/option.c index f6639d4f6a..65457dec9f 100644 --- a/src/nvim/option.c +++ b/src/nvim/option.c @@ -4095,6 +4095,14 @@ static char *set_num_option(int opt_idx, char_u *varp, long value, if (p_ch < 1) { errmsg = e_positive; } + } else if (pp == &curbuf->b_p_ts) { + if (curbuf->b_p_ts <= 0) { + errmsg = e_positive; + } + } else if (pp == &p_tm) { + if (p_tm < 0) { + errmsg = e_positive; + } } // If validation failed, reset to old value and return. @@ -4246,14 +4254,6 @@ static char *set_num_option(int opt_idx, char_u *varp, long value, } } - if (curbuf->b_p_ts <= 0) { - errmsg = e_positive; - curbuf->b_p_ts = 8; - } - if (p_tm < 0) { - errmsg = e_positive; - p_tm = 0; - } if ((curwin->w_p_scr <= 0 || (curwin->w_p_scr > curwin->w_height && curwin->w_height > 0)) -- cgit From 2b0abdbd9adcdaee5fb4f8d056c6385749c45579 Mon Sep 17 00:00:00 2001 From: Jakob Schnitzer Date: Mon, 27 Mar 2017 19:29:51 +0200 Subject: options: more of the same --- src/nvim/option.c | 69 +++++++++++++++++++++++++++---------------------------- 1 file changed, 34 insertions(+), 35 deletions(-) diff --git a/src/nvim/option.c b/src/nvim/option.c index 65457dec9f..24c9b9b72e 100644 --- a/src/nvim/option.c +++ b/src/nvim/option.c @@ -4103,6 +4103,40 @@ static char *set_num_option(int opt_idx, char_u *varp, long value, if (p_tm < 0) { errmsg = e_positive; } + } else if (pp == &p_hi) { + if (p_hi < 0) { + errmsg = e_positive; + } else if (p_hi > 10000) { + errmsg = e_invarg; + } + } else if (pp == &p_re) { + if (p_re < 0 || p_re > 2) { + errmsg = e_invarg; + } + } else if (pp == &p_report) { + if (p_report < 0) { + errmsg = e_positive; + } + } else if (pp == &p_so) { + if (p_so < 0 && full_screen) { + errmsg = e_scroll; + } + } else if (pp == &p_siso) { + if (p_siso < 0 && full_screen) { + errmsg = e_positive; + } + } else if (pp == &p_cwh) { + if (p_cwh < 1) { + errmsg = e_positive; + } + } else if (pp == &p_ut) { + if (p_ut < 0) { + errmsg = e_positive; + } + } else if (pp == &p_ss) { + if (p_ss < 0) { + errmsg = e_positive; + } } // If validation failed, reset to old value and return. @@ -4270,21 +4304,6 @@ static char *set_num_option(int opt_idx, char_u *varp, long value, else /* curwin->w_p_scr > curwin->w_height */ curwin->w_p_scr = curwin->w_height; } - if (p_hi < 0) { - errmsg = e_positive; - p_hi = 0; - } else if (p_hi > 10000) { - errmsg = e_invarg; - p_hi = 10000; - } - if (p_re < 0 || p_re > 2) { - errmsg = e_invarg; - p_re = 0; - } - if (p_report < 0) { - errmsg = e_positive; - p_report = 1; - } if ((p_sj < -100 || p_sj >= Rows) && full_screen) { if (Rows != old_Rows) /* Rows changed, just adjust p_sj */ p_sj = Rows / 2; @@ -4293,26 +4312,6 @@ static char *set_num_option(int opt_idx, char_u *varp, long value, p_sj = 1; } } - if (p_so < 0 && full_screen) { - errmsg = e_scroll; - p_so = 0; - } - if (p_siso < 0 && full_screen) { - errmsg = e_positive; - p_siso = 0; - } - if (p_cwh < 1) { - errmsg = e_positive; - p_cwh = 1; - } - if (p_ut < 0) { - errmsg = e_positive; - p_ut = 2000; - } - if (p_ss < 0) { - errmsg = e_positive; - p_ss = 0; - } /* May set global value for local option. */ if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0) -- cgit From 2290a7a1b1f8ea3c04365667b751fdfff62b43f5 Mon Sep 17 00:00:00 2001 From: Jakob Schnitzer Date: Mon, 27 Mar 2017 19:37:39 +0200 Subject: options: group num_option validation by type --- src/nvim/option.c | 96 +++++++++++++++++++++++++++---------------------------- 1 file changed, 47 insertions(+), 49 deletions(-) diff --git a/src/nvim/option.c b/src/nvim/option.c index 24c9b9b72e..976aadbc9c 100644 --- a/src/nvim/option.c +++ b/src/nvim/option.c @@ -4032,30 +4032,12 @@ static char *set_num_option(int opt_idx, char_u *varp, long value, if (p_wmw > p_wiw) { errmsg = e_winwidth; } - } else if (pp == &curwin->w_p_fdl) { - if (curwin->w_p_fdl < 0) { - errmsg = e_positive; - } - } else if (pp == &curwin->w_p_fdc) { - if (curwin->w_p_fdc < 0) { - errmsg = e_positive; - } else if (curwin->w_p_fdc > 12) { - errmsg = e_invarg; - } } else if (pp == &p_mco) { if (p_mco > MAX_MCO) { errmsg = e_invarg; } else if (p_mco < 0) { errmsg = e_positive; } - } else if (pp == &curbuf->b_p_iminsert) { - if (curbuf->b_p_iminsert < 0 || curbuf->b_p_iminsert > B_IMODE_LAST) { - errmsg = e_invarg; - } - } else if (pp == &curbuf->b_p_imsearch) { - if (curbuf->b_p_imsearch < -1 || curbuf->b_p_imsearch > B_IMODE_LAST) { - errmsg = e_invarg; - } } else if (pp == &p_titlelen) { if (p_titlelen < 0) { errmsg = e_positive; @@ -4064,41 +4046,10 @@ static char *set_num_option(int opt_idx, char_u *varp, long value, if (p_uc < 0) { errmsg = e_positive; } - } else if (pp == &curwin->w_p_cole) { - if (curwin->w_p_cole < 0) { - errmsg = e_positive; - } else if (curwin->w_p_cole > 3) { - errmsg = e_invarg; - } - } else if (pp == &curwin->w_p_nuw) { - if (curwin->w_p_nuw < 1) { - errmsg = e_positive; - } - if (curwin->w_p_nuw > 10) { - errmsg = e_invarg; - } - curwin->w_nrwidth_line_count = 0; - } else if (pp == &curbuf->b_p_tw) { - if (curbuf->b_p_tw < 0) { - errmsg = e_positive; - } - } else if (pp == &curbuf->b_p_scbk || pp == &p_scbk) { - if (*pp < -1 || *pp > SB_MAX - || (opt_flags == OPT_LOCAL && !curbuf->terminal)) { - errmsg = e_invarg; - } - } else if (pp == &curbuf->b_p_sw) { - if (curbuf->b_p_sw < 0) { - errmsg = e_positive; - } } else if (pp == &p_ch) { if (p_ch < 1) { errmsg = e_positive; } - } else if (pp == &curbuf->b_p_ts) { - if (curbuf->b_p_ts <= 0) { - errmsg = e_positive; - } } else if (pp == &p_tm) { if (p_tm < 0) { errmsg = e_positive; @@ -4137,6 +4088,53 @@ static char *set_num_option(int opt_idx, char_u *varp, long value, if (p_ss < 0) { errmsg = e_positive; } + } else if (pp == &curwin->w_p_fdl) { + if (curwin->w_p_fdl < 0) { + errmsg = e_positive; + } + } else if (pp == &curwin->w_p_fdc) { + if (curwin->w_p_fdc < 0) { + errmsg = e_positive; + } else if (curwin->w_p_fdc > 12) { + errmsg = e_invarg; + } + } else if (pp == &curwin->w_p_cole) { + if (curwin->w_p_cole < 0) { + errmsg = e_positive; + } else if (curwin->w_p_cole > 3) { + errmsg = e_invarg; + } + } else if (pp == &curwin->w_p_nuw) { + if (curwin->w_p_nuw < 1) { + errmsg = e_positive; + } else if (curwin->w_p_nuw > 10) { + errmsg = e_invarg; + } + } else if (pp == &curbuf->b_p_iminsert) { + if (curbuf->b_p_iminsert < 0 || curbuf->b_p_iminsert > B_IMODE_LAST) { + errmsg = e_invarg; + } + } else if (pp == &curbuf->b_p_imsearch) { + if (curbuf->b_p_imsearch < -1 || curbuf->b_p_imsearch > B_IMODE_LAST) { + errmsg = e_invarg; + } + } else if (pp == &curbuf->b_p_tw) { + if (curbuf->b_p_tw < 0) { + errmsg = e_positive; + } + } else if (pp == &curbuf->b_p_scbk || pp == &p_scbk) { + if (*pp < -1 || *pp > SB_MAX + || (opt_flags == OPT_LOCAL && !curbuf->terminal)) { + errmsg = e_invarg; + } + } else if (pp == &curbuf->b_p_sw) { + if (curbuf->b_p_sw < 0) { + errmsg = e_positive; + } + } else if (pp == &curbuf->b_p_ts) { + if (curbuf->b_p_ts <= 0) { + errmsg = e_positive; + } } // If validation failed, reset to old value and return. -- cgit From 44f039a1c8bf1a3111825f2d3385730e31536d9a Mon Sep 17 00:00:00 2001 From: Jakob Schnitzer Date: Mon, 27 Mar 2017 19:50:04 +0200 Subject: options: fix setglobal for buf-local number options --- src/nvim/option.c | 33 +++++++++++++--------------- test/functional/options/num_options_spec.lua | 6 ++--- 2 files changed, 18 insertions(+), 21 deletions(-) diff --git a/src/nvim/option.c b/src/nvim/option.c index 976aadbc9c..637591d7ca 100644 --- a/src/nvim/option.c +++ b/src/nvim/option.c @@ -4014,22 +4014,19 @@ static char *set_num_option(int opt_idx, char_u *varp, long value, } else if (pp == &p_wmh) { if (p_wmh < 0) { errmsg = e_positive; - } - if (p_wmh > p_wh) { + } else if (p_wmh > p_wh) { errmsg = e_winheight; } } else if (pp == &p_wiw) { if (p_wiw < 1) { errmsg = e_positive; - } - if (p_wmw > p_wiw) { + } else if (p_wmw > p_wiw) { errmsg = e_winwidth; } } else if (pp == &p_wmw) { if (p_wmw < 0) { errmsg = e_positive; - } - if (p_wmw > p_wiw) { + } else if (p_wmw > p_wiw) { errmsg = e_winwidth; } } else if (pp == &p_mco) { @@ -4110,16 +4107,16 @@ static char *set_num_option(int opt_idx, char_u *varp, long value, } else if (curwin->w_p_nuw > 10) { errmsg = e_invarg; } - } else if (pp == &curbuf->b_p_iminsert) { - if (curbuf->b_p_iminsert < 0 || curbuf->b_p_iminsert > B_IMODE_LAST) { + } else if (pp == &curbuf->b_p_iminsert || pp == &p_iminsert) { + if (*pp < 0 || *pp > B_IMODE_LAST) { errmsg = e_invarg; } - } else if (pp == &curbuf->b_p_imsearch) { - if (curbuf->b_p_imsearch < -1 || curbuf->b_p_imsearch > B_IMODE_LAST) { + } else if (pp == &curbuf->b_p_imsearch || pp == &p_imsearch) { + if (*pp < -1 || *pp > B_IMODE_LAST) { errmsg = e_invarg; } - } else if (pp == &curbuf->b_p_tw) { - if (curbuf->b_p_tw < 0) { + } else if (pp == &curbuf->b_p_tw || pp == &p_tw) { + if (*pp < 0) { errmsg = e_positive; } } else if (pp == &curbuf->b_p_scbk || pp == &p_scbk) { @@ -4127,12 +4124,12 @@ static char *set_num_option(int opt_idx, char_u *varp, long value, || (opt_flags == OPT_LOCAL && !curbuf->terminal)) { errmsg = e_invarg; } - } else if (pp == &curbuf->b_p_sw) { - if (curbuf->b_p_sw < 0) { + } else if (pp == &curbuf->b_p_sw || pp == &p_sw) { + if (*pp < 0) { errmsg = e_positive; } - } else if (pp == &curbuf->b_p_ts) { - if (curbuf->b_p_ts <= 0) { + } else if (pp == &curbuf->b_p_ts || pp == &p_ts) { + if (*pp <= 0) { errmsg = e_positive; } } @@ -4206,7 +4203,7 @@ static char *set_num_option(int opt_idx, char_u *varp, long value, } else if (pp == &p_titlelen) { // if 'titlelen' has changed, redraw the title if (starting != NO_SCREEN && old_value != p_titlelen) { - need_maketitle = TRUE; + need_maketitle = true; } } else if (pp == &p_ch) { // if p_ch changed value, change the command line height @@ -4224,7 +4221,7 @@ static char *set_num_option(int opt_idx, char_u *varp, long value, // sync undo before 'undolevels' changes // use the old value, otherwise u_sync() may not work properly *pp = old_value; - u_sync(TRUE); + u_sync(true); *pp = value; } else if (pp == &curbuf->b_p_tw) { FOR_ALL_TAB_WINDOWS(tp, wp) { diff --git a/test/functional/options/num_options_spec.lua b/test/functional/options/num_options_spec.lua index c37bbeffc3..9b66d4f862 100644 --- a/test/functional/options/num_options_spec.lua +++ b/test/functional/options/num_options_spec.lua @@ -11,10 +11,10 @@ end local function should_fail(opt, value, errmsg) execute('let v:errmsg = ""') execute('setglobal ' .. opt .. '=' .. value) - eq(errmsg, eval("v:errmsg"):match("E%d*:")) + eq(errmsg, eval("v:errmsg"):match("E%d*")) execute('let v:errmsg = ""') execute('setlocal ' .. opt .. '=' .. value) - eq(errmsg, eval("v:errmsg"):match("E%d*:")) + eq(errmsg, eval("v:errmsg"):match("E%d*")) execute('let v:errmsg = ""') end @@ -37,6 +37,6 @@ describe(':set validation', function() it('setlocal and setglobal validate values', function() should_fail('shiftwidth', -10, 'E487') should_fail('tabstop', -10, 'E487') - should_fail('winheight', -10, 'E487') + should_fail('winheight', -10, 'E591') end) end) -- cgit From db095f65636664afb4b09a3920571bf0565c7763 Mon Sep 17 00:00:00 2001 From: Jakob Schnitzer Date: Tue, 28 Mar 2017 16:17:53 +0200 Subject: options: more tests; check first set later; stricter validation --- src/nvim/option.c | 141 ++++++++++++++------------- test/functional/options/num_options_spec.lua | 21 +++- 2 files changed, 91 insertions(+), 71 deletions(-) diff --git a/src/nvim/option.c b/src/nvim/option.c index 637591d7ca..d5bc3c1765 100644 --- a/src/nvim/option.c +++ b/src/nvim/option.c @@ -7,7 +7,8 @@ // - For a window option, add some code to copy_winopt(). // - For a buffer option, add some code to buf_copy_options(). // - For a buffer string option, add code to check_buf_options(). -// - If it's a numeric option, add any necessary bounds checks to do_set(). +// - If it's a numeric option, add any necessary bounds checks to +// set_num_option(). // - If it's a list of flags, add some code in do_set(), search for WW_ALL. // - When adding an option with expansion (P_EXPAND), but with a different // default for Vi and Vim (no P_VI_DEF), add some code at VIMEXP. @@ -1445,8 +1446,7 @@ do_set ( goto skip; } } else if (*arg == '-' || ascii_isdigit(*arg)) { - // Allow negative (for 'undolevels'), octal and - // hex numbers. + // Allow negative, octal and hex numbers. vim_str2nr(arg, NULL, &i, STR2NR_ALL, &value, NULL, 0); if (arg[i] != NUL && !ascii_iswhite(arg[i])) { errmsg = e_invarg; @@ -3995,151 +3995,158 @@ static char *set_num_option(int opt_idx, char_u *varp, long value, return (char *)e_secure; } - *pp = value; - /* Remember where the option was set. */ - set_option_scriptID_idx(opt_idx, opt_flags, current_SID); + // Many number options assume their value is in the signed int range. + if (value < INT_MIN || value > INT_MAX) { + return e_invarg; + } - // Number options that need some validation when changed. + // Options that need some validation. if (pp == &p_wh) { - if (p_wh < 1) { + if (value < 1) { errmsg = e_positive; - } - if (p_wmh > p_wh) { + } else if (p_wmh > value) { errmsg = e_winheight; } } else if (pp == &p_hh) { - if (p_hh < 0) { + if (value < 0) { errmsg = e_positive; } } else if (pp == &p_wmh) { - if (p_wmh < 0) { + if (value < 0) { errmsg = e_positive; - } else if (p_wmh > p_wh) { + } else if (value > p_wh) { errmsg = e_winheight; } } else if (pp == &p_wiw) { - if (p_wiw < 1) { + if (value < 1) { errmsg = e_positive; - } else if (p_wmw > p_wiw) { + } else if (p_wmw > value) { errmsg = e_winwidth; } } else if (pp == &p_wmw) { - if (p_wmw < 0) { + if (value < 0) { errmsg = e_positive; - } else if (p_wmw > p_wiw) { + } else if (value > p_wiw) { errmsg = e_winwidth; } } else if (pp == &p_mco) { - if (p_mco > MAX_MCO) { + if (value > MAX_MCO) { errmsg = e_invarg; - } else if (p_mco < 0) { + } else if (value < 0) { errmsg = e_positive; } } else if (pp == &p_titlelen) { - if (p_titlelen < 0) { + if (value < 0) { errmsg = e_positive; } } else if (pp == &p_uc) { - if (p_uc < 0) { + if (value < 0) { errmsg = e_positive; } } else if (pp == &p_ch) { - if (p_ch < 1) { + if (value < 1) { errmsg = e_positive; } } else if (pp == &p_tm) { - if (p_tm < 0) { + if (value < 0) { errmsg = e_positive; } } else if (pp == &p_hi) { - if (p_hi < 0) { + if (value < 0) { errmsg = e_positive; - } else if (p_hi > 10000) { + } else if (value > 10000) { errmsg = e_invarg; } } else if (pp == &p_re) { - if (p_re < 0 || p_re > 2) { + if (value < 0 || value > 2) { errmsg = e_invarg; } } else if (pp == &p_report) { - if (p_report < 0) { + if (value < 0) { errmsg = e_positive; } } else if (pp == &p_so) { - if (p_so < 0 && full_screen) { + if (value < 0 && full_screen) { errmsg = e_scroll; } } else if (pp == &p_siso) { - if (p_siso < 0 && full_screen) { + if (value < 0 && full_screen) { errmsg = e_positive; } } else if (pp == &p_cwh) { - if (p_cwh < 1) { + if (value < 1) { errmsg = e_positive; } } else if (pp == &p_ut) { - if (p_ut < 0) { + if (value < 0) { errmsg = e_positive; } } else if (pp == &p_ss) { - if (p_ss < 0) { + if (value < 0) { errmsg = e_positive; } - } else if (pp == &curwin->w_p_fdl) { - if (curwin->w_p_fdl < 0) { + } else if (pp == &curwin->w_p_fdl + || pp == (long *)GLOBAL_WO(&curwin->w_p_fdl)) { + if (value < 0) { errmsg = e_positive; } - } else if (pp == &curwin->w_p_fdc) { - if (curwin->w_p_fdc < 0) { + } else if (pp == &curwin->w_p_fdc + || pp == (long *)GLOBAL_WO(&curwin->w_p_fdc)) { + if (value < 0) { errmsg = e_positive; - } else if (curwin->w_p_fdc > 12) { + } else if (value > 12) { errmsg = e_invarg; } - } else if (pp == &curwin->w_p_cole) { - if (curwin->w_p_cole < 0) { + } else if (pp == &curwin->w_p_cole + || pp == (long *)GLOBAL_WO(&curwin->w_p_cole)) { + if (value < 0) { errmsg = e_positive; - } else if (curwin->w_p_cole > 3) { + } else if (value > 3) { errmsg = e_invarg; } - } else if (pp == &curwin->w_p_nuw) { - if (curwin->w_p_nuw < 1) { + } else if (pp == &curwin->w_p_nuw + || pp == (long *)GLOBAL_WO(&curwin->w_p_nuw)) { + if (value < 1) { errmsg = e_positive; - } else if (curwin->w_p_nuw > 10) { + } else if (value > 10) { errmsg = e_invarg; } } else if (pp == &curbuf->b_p_iminsert || pp == &p_iminsert) { - if (*pp < 0 || *pp > B_IMODE_LAST) { + if (value < 0 || value > B_IMODE_LAST) { errmsg = e_invarg; } } else if (pp == &curbuf->b_p_imsearch || pp == &p_imsearch) { - if (*pp < -1 || *pp > B_IMODE_LAST) { + if (value < -1 || value > B_IMODE_LAST) { errmsg = e_invarg; } - } else if (pp == &curbuf->b_p_tw || pp == &p_tw) { - if (*pp < 0) { - errmsg = e_positive; - } } else if (pp == &curbuf->b_p_scbk || pp == &p_scbk) { - if (*pp < -1 || *pp > SB_MAX + if (value < -1 || value > SB_MAX || (opt_flags == OPT_LOCAL && !curbuf->terminal)) { errmsg = e_invarg; } } else if (pp == &curbuf->b_p_sw || pp == &p_sw) { - if (*pp < 0) { + if (value < 0) { errmsg = e_positive; } } else if (pp == &curbuf->b_p_ts || pp == &p_ts) { - if (*pp <= 0) { + if (value <= 0) { + errmsg = e_positive; + } + } else if (pp == &curbuf->b_p_tw || pp == &p_tw) { + if (value < 0) { errmsg = e_positive; } } - // If validation failed, reset to old value and return. + // Don't change the value and return early if validation failed. if (errmsg != NULL) { - *pp = old_value; return errmsg; } + *pp = value; + // Remember where the option was set. + set_option_scriptID_idx(opt_idx, opt_flags, current_SID); + // For these options we want to fix some invalid values. if (pp == &p_window) { if (p_window < 1) { @@ -4168,11 +4175,8 @@ static char *set_num_option(int opt_idx, char_u *varp, long value, if (lastwin != firstwin && curwin->w_width < p_wiw) { win_setwidth((int)p_wiw); } - } else if (pp == &p_wmw) { - win_setminheight(); } else if (pp == &p_ls) { - // (re)set last window status line. - last_status(false); + last_status(false); // (re)set last window status line. } else if (pp == &p_stal) { // (re)set tab page line shell_new_rows(); // recompute window positions and heights @@ -4237,9 +4241,7 @@ static char *set_num_option(int opt_idx, char_u *varp, long value, } - /* - * Check the bounds for numeric options here - */ + // Check the (new) bounds for Rows and Columns here. if (Rows < min_rows() && full_screen) { if (errbuf != NULL) { vim_snprintf((char *)errbuf, errbuflen, @@ -4259,19 +4261,17 @@ static char *set_num_option(int opt_idx, char_u *varp, long value, limit_screen_size(); - /* - * If the screen (shell) height has been changed, assume it is the - * physical screenheight. - */ + // If the screen (shell) height has been changed, assume it is the + // physical screenheight. if (old_Rows != Rows || old_Columns != Columns) { - /* Changing the screen size is not allowed while updating the screen. */ + // Changing the screen size is not allowed while updating the screen. if (updating_screen) { *pp = old_value; } else if (full_screen) { screen_resize((int)Columns, (int)Rows); } else { - /* Postpone the resizing; check the size and cmdline position for - * messages. */ + // Postpone the resizing; check the size and cmdline position for + // messages. check_shellsize(); if (cmdline_row > Rows - p_ch && Rows > p_ch) { assert(p_ch >= 0 && Rows - p_ch <= INT_MAX); @@ -4308,9 +4308,10 @@ static char *set_num_option(int opt_idx, char_u *varp, long value, } } - /* May set global value for local option. */ - if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0) + // May set global value for local option. + if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0) { *(long *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) = *pp; + } if (pp == &curbuf->b_p_scbk && !curbuf->terminal) { // Normal buffer: reset local 'scrollback' after updating the global value. diff --git a/test/functional/options/num_options_spec.lua b/test/functional/options/num_options_spec.lua index 9b66d4f862..d0b63d3f91 100644 --- a/test/functional/options/num_options_spec.lua +++ b/test/functional/options/num_options_spec.lua @@ -37,6 +37,25 @@ describe(':set validation', function() it('setlocal and setglobal validate values', function() should_fail('shiftwidth', -10, 'E487') should_fail('tabstop', -10, 'E487') - should_fail('winheight', -10, 'E591') + should_fail('winheight', -10, 'E487') + should_fail('helpheight', -10, 'E487') + should_fail('maxcombine', 10, 'E474') + should_fail('history', 1000000, 'E474') + should_fail('regexpengine', 3, 'E474') + + should_fail('foldlevel', -5, 'E487') + should_fail('foldcolumn', 100, 'E474') + should_fail('conceallevel', 4, 'E474') + should_fail('numberwidth', 20, 'E474') + end) + + it('set wmh/wh wmw/wiw checks', function() + execute('set winheight=2') + execute('set winminheight=3') + eq('E591', eval("v:errmsg"):match("E%d*")) + + execute('set winwidth=2') + execute('set winminwidth=3') + eq('E592', eval("v:errmsg"):match("E%d*")) end) end) -- cgit From 8a55f9b1c8bab2cf22e266d7e1eecde85119d75d Mon Sep 17 00:00:00 2001 From: Jakob Schnitzer Date: Fri, 31 Mar 2017 18:30:06 +0200 Subject: update for changes in master; fix 'window'; tests --- src/nvim/option.c | 10 ++--- test/functional/options/num_options_spec.lua | 57 +++++++++++++++++++++------- 2 files changed, 48 insertions(+), 19 deletions(-) diff --git a/src/nvim/option.c b/src/nvim/option.c index d5bc3c1765..eddfdd6218 100644 --- a/src/nvim/option.c +++ b/src/nvim/option.c @@ -3997,7 +3997,7 @@ static char *set_num_option(int opt_idx, char_u *varp, long value, // Many number options assume their value is in the signed int range. if (value < INT_MIN || value > INT_MAX) { - return e_invarg; + return (char *)e_invarg; } // Options that need some validation. @@ -4129,7 +4129,7 @@ static char *set_num_option(int opt_idx, char_u *varp, long value, errmsg = e_positive; } } else if (pp == &curbuf->b_p_ts || pp == &p_ts) { - if (value <= 0) { + if (value < 1) { errmsg = e_positive; } } else if (pp == &curbuf->b_p_tw || pp == &p_tw) { @@ -4140,7 +4140,7 @@ static char *set_num_option(int opt_idx, char_u *varp, long value, // Don't change the value and return early if validation failed. if (errmsg != NULL) { - return errmsg; + return (char *)errmsg; } *pp = value; @@ -4150,7 +4150,7 @@ static char *set_num_option(int opt_idx, char_u *varp, long value, // For these options we want to fix some invalid values. if (pp == &p_window) { if (p_window < 1) { - p_window = 1; + p_window = Rows - 1; } else if (p_window >= Rows) { p_window = Rows - 1; } @@ -4188,7 +4188,7 @@ static char *set_num_option(int opt_idx, char_u *varp, long value, if (foldmethodIsSyntax(curwin) || foldmethodIsIndent(curwin)) { foldUpdateAll(curwin); } - } else if (pp == &curbuf->b_p_sw || pp == (long *)&curbuf->b_p_ts) { + } else if (pp == &curbuf->b_p_sw || pp == &curbuf->b_p_ts) { // 'shiftwidth' or 'tabstop' if (foldmethodIsIndent(curwin)) { foldUpdateAll(curwin); diff --git a/test/functional/options/num_options_spec.lua b/test/functional/options/num_options_spec.lua index d0b63d3f91..620e758141 100644 --- a/test/functional/options/num_options_spec.lua +++ b/test/functional/options/num_options_spec.lua @@ -1,12 +1,8 @@ -- Tests for :setlocal and :setglobal local helpers = require('test.functional.helpers')(after_each) -local clear, execute, eval, eq, nvim = - helpers.clear, helpers.execute, helpers.eval, helpers.eq, helpers.nvim - -local function get_num_option_global(opt) - return nvim('command_output', 'setglobal ' .. opt .. '?'):match('%d+') -end +local clear, execute, eval, eq, meths = + helpers.clear, helpers.execute, helpers.eval, helpers.eq, helpers.meths local function should_fail(opt, value, errmsg) execute('let v:errmsg = ""') @@ -18,16 +14,23 @@ local function should_fail(opt, value, errmsg) execute('let v:errmsg = ""') end +local function should_succeed(opt, value) + execute('setglobal ' .. opt .. '=' .. value) + eq('', eval("v:errmsg")) + execute('setlocal ' .. opt .. '=' .. value) + eq('', eval("v:errmsg")) +end + describe(':setlocal', function() before_each(clear) it('setlocal sets only local value', function() - eq('0', get_num_option_global('iminsert')) + eq(0, meths.get_option('iminsert')) execute('setlocal iminsert=1') - eq('0', get_num_option_global('iminsert')) - eq('0', get_num_option_global('imsearch')) + eq(0, meths.get_option('iminsert')) + eq(0, meths.get_option('imsearch')) execute('setlocal imsearch=1') - eq('0', get_num_option_global('imsearch')) + eq(0, meths.get_option('imsearch')) end) end) @@ -36,17 +39,43 @@ describe(':set validation', function() it('setlocal and setglobal validate values', function() should_fail('shiftwidth', -10, 'E487') + should_succeed('shiftwidth', 0) should_fail('tabstop', -10, 'E487') should_fail('winheight', -10, 'E487') - should_fail('helpheight', -10, 'E487') - should_fail('maxcombine', 10, 'E474') + should_fail('winheight', 0, 'E487') + should_fail('winminheight', -1, 'E487') + should_succeed('winminheight', 0) + should_fail('winwidth', 0, 'E487') + should_fail('helpheight', -1, 'E487') + should_fail('maxcombine', 7, 'E474') + should_fail('iminsert', 3, 'E474') + should_fail('imsearch', 3, 'E474') + should_fail('titlelen', -1, 'E487') + should_fail('cmdheight', 0, 'E487') + should_fail('updatecount', -1, 'E487') + should_fail('textwidth', -1, 'E487') + should_fail('tabstop', 0, 'E487') + should_fail('timeoutlen', -1, 'E487') should_fail('history', 1000000, 'E474') + should_fail('regexpengine', -1, 'E474') should_fail('regexpengine', 3, 'E474') + should_succeed('regexpengine', 2) + should_fail('report', -1, 'E487') + should_succeed('report', 0) + should_fail('scrolloff', -1, 'E49') + should_fail('sidescrolloff', -1, 'E487') + should_fail('sidescroll', -1, 'E487') + should_fail('cmdwinheight', 0, 'E487') + should_fail('updatetime', -1, 'E487') should_fail('foldlevel', -5, 'E487') - should_fail('foldcolumn', 100, 'E474') + should_fail('foldcolumn', 13, 'E474') should_fail('conceallevel', 4, 'E474') - should_fail('numberwidth', 20, 'E474') + should_fail('numberwidth', 11, 'E474') + should_fail('numberwidth', 0, 'E487') + + -- If smaller than one this one is set to 'lines'-1 + should_succeed('window', -10) end) it('set wmh/wh wmw/wiw checks', function() -- cgit From 4049492b6d7b8805686b14dbacb3b729abd03308 Mon Sep 17 00:00:00 2001 From: Jakob Schnitzer Date: Sat, 1 Apr 2017 12:26:58 +0200 Subject: also test set_option --- test/functional/options/num_options_spec.lua | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/test/functional/options/num_options_spec.lua b/test/functional/options/num_options_spec.lua index 620e758141..42af4cb9b8 100644 --- a/test/functional/options/num_options_spec.lua +++ b/test/functional/options/num_options_spec.lua @@ -5,19 +5,23 @@ local clear, execute, eval, eq, meths = helpers.clear, helpers.execute, helpers.eval, helpers.eq, helpers.meths local function should_fail(opt, value, errmsg) - execute('let v:errmsg = ""') execute('setglobal ' .. opt .. '=' .. value) eq(errmsg, eval("v:errmsg"):match("E%d*")) execute('let v:errmsg = ""') execute('setlocal ' .. opt .. '=' .. value) eq(errmsg, eval("v:errmsg"):match("E%d*")) execute('let v:errmsg = ""') + local status, err = pcall(meths.set_option, opt, value) + eq(status, false) + eq(errmsg, err:match("E%d*")) + eq('', eval("v:errmsg")) end local function should_succeed(opt, value) execute('setglobal ' .. opt .. '=' .. value) - eq('', eval("v:errmsg")) execute('setlocal ' .. opt .. '=' .. value) + meths.set_option(opt, value) + eq(value, meths.get_option(opt)) eq('', eval("v:errmsg")) end @@ -74,8 +78,11 @@ describe(':set validation', function() should_fail('numberwidth', 11, 'E474') should_fail('numberwidth', 0, 'E487') - -- If smaller than one this one is set to 'lines'-1 - should_succeed('window', -10) + -- If smaller than 1 this one is set to 'lines'-1 + execute('setglobal window=-10') + meths.set_option('window', -10) + eq(23, meths.get_option('window')) + eq('', eval("v:errmsg")) end) it('set wmh/wh wmw/wiw checks', function() -- cgit From 4403864da3c48412595d439f36458d1e6ccfc49f Mon Sep 17 00:00:00 2001 From: Jakob Schnitzer Date: Wed, 28 Jun 2017 20:21:44 +0200 Subject: update tests --- test/functional/options/num_options_spec.lua | 30 ++++++++++++++-------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/test/functional/options/num_options_spec.lua b/test/functional/options/num_options_spec.lua index 42af4cb9b8..ed17ffdd3c 100644 --- a/test/functional/options/num_options_spec.lua +++ b/test/functional/options/num_options_spec.lua @@ -1,16 +1,16 @@ -- Tests for :setlocal and :setglobal local helpers = require('test.functional.helpers')(after_each) -local clear, execute, eval, eq, meths = - helpers.clear, helpers.execute, helpers.eval, helpers.eq, helpers.meths +local clear, feed_command, eval, eq, meths = + helpers.clear, helpers.feed_command, helpers.eval, helpers.eq, helpers.meths local function should_fail(opt, value, errmsg) - execute('setglobal ' .. opt .. '=' .. value) + feed_command('setglobal ' .. opt .. '=' .. value) eq(errmsg, eval("v:errmsg"):match("E%d*")) - execute('let v:errmsg = ""') - execute('setlocal ' .. opt .. '=' .. value) + feed_command('let v:errmsg = ""') + feed_command('setlocal ' .. opt .. '=' .. value) eq(errmsg, eval("v:errmsg"):match("E%d*")) - execute('let v:errmsg = ""') + feed_command('let v:errmsg = ""') local status, err = pcall(meths.set_option, opt, value) eq(status, false) eq(errmsg, err:match("E%d*")) @@ -18,8 +18,8 @@ local function should_fail(opt, value, errmsg) end local function should_succeed(opt, value) - execute('setglobal ' .. opt .. '=' .. value) - execute('setlocal ' .. opt .. '=' .. value) + feed_command('setglobal ' .. opt .. '=' .. value) + feed_command('setlocal ' .. opt .. '=' .. value) meths.set_option(opt, value) eq(value, meths.get_option(opt)) eq('', eval("v:errmsg")) @@ -30,10 +30,10 @@ describe(':setlocal', function() it('setlocal sets only local value', function() eq(0, meths.get_option('iminsert')) - execute('setlocal iminsert=1') + feed_command('setlocal iminsert=1') eq(0, meths.get_option('iminsert')) eq(0, meths.get_option('imsearch')) - execute('setlocal imsearch=1') + feed_command('setlocal imsearch=1') eq(0, meths.get_option('imsearch')) end) end) @@ -79,19 +79,19 @@ describe(':set validation', function() should_fail('numberwidth', 0, 'E487') -- If smaller than 1 this one is set to 'lines'-1 - execute('setglobal window=-10') + feed_command('setglobal window=-10') meths.set_option('window', -10) eq(23, meths.get_option('window')) eq('', eval("v:errmsg")) end) it('set wmh/wh wmw/wiw checks', function() - execute('set winheight=2') - execute('set winminheight=3') + feed_command('set winheight=2') + feed_command('set winminheight=3') eq('E591', eval("v:errmsg"):match("E%d*")) - execute('set winwidth=2') - execute('set winminwidth=3') + feed_command('set winwidth=2') + feed_command('set winminwidth=3') eq('E592', eval("v:errmsg"):match("E%d*")) end) end) -- cgit