From 119545190c1ec35e1c7b482f593b98d0fc485b6f Mon Sep 17 00:00:00 2001 From: watiko Date: Fri, 27 Nov 2015 23:56:52 +0900 Subject: [PATCH 01/15] vim-patch:7.4.786 Problem: It is not possible for a plugin to adjust to a changed setting. Solution: Add the OptionSet autocommand event. (Christian Brabandt) https://github.com/vim/vim/commit/537443018d41918639695a442c91b34ccec69fc3 --- runtime/doc/autocmd.txt | 19 ++++ runtime/doc/eval.txt | 9 ++ src/nvim/auevents.lua | 1 + src/nvim/eval.c | 14 ++- src/nvim/eval.h | 3 + src/nvim/fileio.c | 3 +- src/nvim/option.c | 68 ++++++++++++- src/nvim/testdir/Makefile | 1 + src/nvim/testdir/test_autocmd_option.in | 68 +++++++++++++ src/nvim/testdir/test_autocmd_option.ok | 129 ++++++++++++++++++++++++ src/nvim/version.c | 2 +- 11 files changed, 310 insertions(+), 7 deletions(-) create mode 100644 src/nvim/testdir/test_autocmd_option.in create mode 100644 src/nvim/testdir/test_autocmd_option.ok diff --git a/runtime/doc/autocmd.txt b/runtime/doc/autocmd.txt index a0ed91c95d..38d53249d1 100644 --- a/runtime/doc/autocmd.txt +++ b/runtime/doc/autocmd.txt @@ -258,6 +258,7 @@ Name triggered by ~ |Syntax| when the 'syntax' option has been set |EncodingChanged| after the 'encoding' option has been changed |TermChanged| after the value of 'term' has changed +|OptionSet| after setting any option Startup and exit |VimEnter| after doing all the startup stuff @@ -745,6 +746,24 @@ MenuPopup Just before showing the popup menu (under the o Operator-pending i Insert c Command line + *OptionSet* +OptionSet After setting an option. The pattern is + matched against the long option name. + The |v:option_old| variable indicates the + old option value, |v:option_new| variable + indicates the newly set value, the + |v:option_type| variable indicates whether + it's global or local scoped and || + indicates what option has been set. + + Note: It's a bad idea, to reset an option + during this autocommand, since this will + probably break plugins. You can always use + |noa| to prevent triggering this autocommand. + Could be used, to check for existence of the + 'backupdir' and 'undodir' options and create + directories, if they don't exist yet. + *QuickFixCmdPre* QuickFixCmdPre Before a quickfix command is run (|:make|, |:lmake|, |:grep|, |:lgrep|, |:grepadd|, diff --git a/runtime/doc/eval.txt b/runtime/doc/eval.txt index 7b12d2082f..4ff0636b61 100644 --- a/runtime/doc/eval.txt +++ b/runtime/doc/eval.txt @@ -1535,6 +1535,15 @@ v:oldfiles List of file names that is loaded from the |shada| file on than String this will cause trouble. {only when compiled with the |+shada| feature} + *v:option_new* +v:option_new New value of the option. Valid while executing an |OptionSet| + autocommand. + *v:option_old* +v:option_old Old value of the option. Valid while executing an |OptionSet| + autocommand. + *v:option_type* +v:option_type Scope of the set command. Valid while executing an + |OptionSet| autocommand. Can be either "global" or "local" *v:operator* *operator-variable* v:operator The last operator given in Normal mode. This is a single character except for commands starting with or , diff --git a/src/nvim/auevents.lua b/src/nvim/auevents.lua index 7624dd2303..aa4a8d8332 100644 --- a/src/nvim/auevents.lua +++ b/src/nvim/auevents.lua @@ -57,6 +57,7 @@ return { 'InsertLeave', -- when leaving Insert mode 'JobActivity', -- when job sent some data 'MenuPopup', -- just before popup menu is displayed + 'OptionSet', -- after setting any option 'QuickFixCmdPost', -- after :make, :grep etc. 'QuickFixCmdPre', -- before :make, :grep etc. 'QuitPre', -- before :quit diff --git a/src/nvim/eval.c b/src/nvim/eval.c index cec7c91c03..cb9f40f851 100644 --- a/src/nvim/eval.c +++ b/src/nvim/eval.c @@ -382,6 +382,9 @@ static struct vimvar { {VV_NAME("progpath", VAR_STRING), VV_RO}, {VV_NAME("command_output", VAR_STRING), 0}, {VV_NAME("completed_item", VAR_DICT), VV_RO}, + {VV_NAME("option_new", VAR_STRING), VV_RO}, + {VV_NAME("option_old", VAR_STRING), VV_RO}, + {VV_NAME("option_type", VAR_STRING), VV_RO}, {VV_NAME("msgpack_types", VAR_DICT), VV_RO}, }; @@ -21247,9 +21250,14 @@ void ex_oldfiles(exarg_T *eap) } } - - - +/* reset v:option_new, v:option_old and v:option_type */ +void +reset_v_option_vars(void) +{ + set_vim_var_string(VV_OPTION_NEW, NULL, -1); + set_vim_var_string(VV_OPTION_OLD, NULL, -1); + set_vim_var_string(VV_OPTION_TYPE, NULL, -1); +} /* * Adjust a filename, according to a string of modifiers. diff --git a/src/nvim/eval.h b/src/nvim/eval.h index 8ccf71068c..19a1bbb083 100644 --- a/src/nvim/eval.h +++ b/src/nvim/eval.h @@ -108,6 +108,9 @@ enum { VV_PROGPATH, VV_COMMAND_OUTPUT, VV_COMPLETED_ITEM, + VV_OPTION_NEW, + VV_OPTION_OLD, + VV_OPTION_TYPE, VV_MSGPACK_TYPES, VV_LEN, /* number of v: vars */ }; diff --git a/src/nvim/fileio.c b/src/nvim/fileio.c index 4aa4d4c399..f57b48cd15 100644 --- a/src/nvim/fileio.c +++ b/src/nvim/fileio.c @@ -6426,7 +6426,7 @@ apply_autocmds_group ( * invalid. */ if (fname_io == NULL) { - if (event == EVENT_COLORSCHEME) + if (event == EVENT_COLORSCHEME || event == EVENT_OPTIONSET) autocmd_fname = NULL; else if (fname != NULL && *fname != NUL) autocmd_fname = fname; @@ -6476,6 +6476,7 @@ apply_autocmds_group ( if (event == EVENT_COLORSCHEME || event == EVENT_FILETYPE || event == EVENT_FUNCUNDEFINED + || event == EVENT_OPTIONSET || event == EVENT_QUICKFIXCMDPOST || event == EVENT_QUICKFIXCMDPRE || event == EVENT_REMOTEREPLY diff --git a/src/nvim/option.c b/src/nvim/option.c index 8b4aab88a3..20f005eba9 100644 --- a/src/nvim/option.c +++ b/src/nvim/option.c @@ -1511,9 +1511,10 @@ do_set ( } else if (opt_idx >= 0) { /* string */ char_u *save_arg = NULL; char_u *s = NULL; - char_u *oldval; /* previous value if *varp */ + char_u *oldval = NULL; /* previous value if *varp */ char_u *newval; - char_u *origval; + char_u *origval = NULL; + char_u *saved_origval = NULL; unsigned newlen; int comma; int bs; @@ -1780,6 +1781,11 @@ do_set ( /* Set the new value. */ *(char_u **)(varp) = newval; + if (!starting && origval != NULL) + /* origval may be freed by + * did_set_string_option(), make a copy. */ + saved_origval = vim_strsave(origval); + /* Handle side effects, and set the global value for * ":set" on local options. */ errmsg = did_set_string_option(opt_idx, (char_u **)varp, @@ -1788,6 +1794,20 @@ do_set ( /* If error detected, print the error message. */ if (errmsg != NULL) goto skip; + if (saved_origval != NULL) { + char_u buf_type[7]; + + sprintf((char *)buf_type, "%s", + (opt_flags & OPT_LOCAL) ? "local" : "global"); + set_vim_var_string(VV_OPTION_NEW, newval, -1); + set_vim_var_string(VV_OPTION_OLD, saved_origval, -1); + set_vim_var_string(VV_OPTION_TYPE, buf_type, -1); + apply_autocmds(EVENT_OPTIONSET, + (char_u *)options[opt_idx].fullname, + NULL, FALSE, NULL); + reset_v_option_vars(); + xfree(saved_origval); + } } else { // key code option(FIXME(tarruda): Show a warning or something // similar) @@ -2337,6 +2357,7 @@ set_string_option ( char_u *s; char_u **varp; char_u *oldval; + char_u *saved_oldval = NULL; char_u *r = NULL; if (options[opt_idx].var == NULL) /* don't set hidden option */ @@ -2350,10 +2371,27 @@ set_string_option ( : opt_flags); oldval = *varp; *varp = s; + + if (!starting) + saved_oldval = vim_strsave(oldval); + if ((r = did_set_string_option(opt_idx, varp, TRUE, oldval, NULL, opt_flags)) == NULL) did_set_option(opt_idx, opt_flags, TRUE); + /* call autocommand after handling side effects */ + if (saved_oldval != NULL) { + char_u buf_type[7]; + sprintf((char *)buf_type, "%s", + (opt_flags & OPT_LOCAL) ? "local" : "global"); + set_vim_var_string(VV_OPTION_NEW, s, -1); + set_vim_var_string(VV_OPTION_OLD, oldval, -1); + set_vim_var_string(VV_OPTION_TYPE, buf_type, -1); + apply_autocmds(EVENT_OPTIONSET, (char_u *)options[opt_idx].fullname, NULL, FALSE, NULL); + reset_v_option_vars(); + xfree(saved_oldval); + } + return r; } @@ -3822,8 +3860,22 @@ set_bool_option ( * End of handling side effects for bool options. */ + /* after handling side effects, call autocommand */ + options[opt_idx].flags |= P_WAS_SET; + if (!starting) { + char_u buf_old[2], buf_new[2], buf_type[7]; + snprintf((char *)buf_old, 2, "%d", old_value ? TRUE: FALSE); + snprintf((char *)buf_new, 2, "%d", value ? TRUE: FALSE); + sprintf((char *)buf_type, "%s", (opt_flags & OPT_LOCAL) ? "local" : "global"); + set_vim_var_string(VV_OPTION_NEW, buf_new, -1); + set_vim_var_string(VV_OPTION_OLD, buf_old, -1); + set_vim_var_string(VV_OPTION_TYPE, buf_type, -1); + apply_autocmds(EVENT_OPTIONSET, (char_u *) options[opt_idx].fullname, NULL, FALSE, NULL); + reset_v_option_vars(); + } + comp_col(); /* in case 'ruler' or 'showcmd' changed */ if (curwin->w_curswant != MAXCOL && (options[opt_idx].flags & (P_CURSWANT | P_RALL)) != 0) @@ -4195,6 +4247,18 @@ set_num_option ( options[opt_idx].flags |= P_WAS_SET; + if (!starting && errmsg == NULL) { + char_u buf_old[11], buf_new[11], buf_type[7]; + snprintf((char *)buf_old, 10, "%ld", old_value); + snprintf((char *)buf_new, 10, "%ld", value); + snprintf((char *)buf_type, 7, "%s", (opt_flags & OPT_LOCAL) ? "local" : "global"); + set_vim_var_string(VV_OPTION_NEW, buf_new, -1); + set_vim_var_string(VV_OPTION_OLD, buf_old, -1); + set_vim_var_string(VV_OPTION_TYPE, buf_type, -1); + apply_autocmds(EVENT_OPTIONSET, (char_u *) options[opt_idx].fullname, NULL, FALSE, NULL); + reset_v_option_vars(); + } + comp_col(); /* in case 'columns' or 'ls' changed */ if (curwin->w_curswant != MAXCOL && (options[opt_idx].flags & (P_CURSWANT | P_RALL)) != 0) diff --git a/src/nvim/testdir/Makefile b/src/nvim/testdir/Makefile index 8c3e99c624..75232ad66a 100644 --- a/src/nvim/testdir/Makefile +++ b/src/nvim/testdir/Makefile @@ -25,6 +25,7 @@ SCRIPTS := test_eval.out \ test83.out \ test88.out \ test_listlbr.out \ + test_autocmd_option.out \ test_breakindent.out \ test_charsearch.out \ test_close_count.out \ diff --git a/src/nvim/testdir/test_autocmd_option.in b/src/nvim/testdir/test_autocmd_option.in new file mode 100644 index 0000000000..070312087d --- /dev/null +++ b/src/nvim/testdir/test_autocmd_option.in @@ -0,0 +1,68 @@ +Test for option autocommand + +STARTTEST +:so small.vim +:if !has("eval") || !has("autocmd") | e! test.ok | w! test.out | qa! | endif +:fu! AutoCommand(match) +: let c=g:testcase +: let item=remove(g:options, 0) +: let c.=printf("Expected: Name: <%s>, Oldval: <%s>, NewVal: <%s>, Scope: <%s>\n", item[0], item[1], item[2], item[3]) +: let c.=printf("Autocmd Option: <%s>,", a:match) +: let c.=printf(" OldVal: <%s>,", v:option_old) +: let c.=printf(" NewVal: <%s>,", v:option_new) +: let c.=printf(" Scope: <%s>\n", v:option_type) +: call setreg('r', printf("%s\n%s", getreg('r'), c)) +:endfu +:au OptionSet * :call AutoCommand(expand("")) +:let g:testcase="1: Setting number option\n" +:let g:options=[['number', 0, 1, 'global']] +:set nu +:let g:testcase="2: Setting local number option\n" +:let g:options=[['number', 1, 0, 'local']] +:setlocal nonu +:let g:testcase="3: Setting global number option\n" +:let g:options=[['number', 1, 0, 'global']] +:setglobal nonu +:let g:testcase="4: Setting local autoindent option\n" +:let g:options=[['autoindent', 1, 0, 'local']] +:setlocal noai +:let g:testcase="5: Setting global autoindent option\n" +:let g:options=[['autoindent', 1, 0, 'global']] +:setglobal noai +:let g:testcase="6: Setting global autoindent option\n" +:let g:options=[['autoindent', 0, 1, 'global']] +:set ai! +: Should not print anything, use :noa +:noa :set nonu +:let g:testcase="7: Setting several global list and number option\n" +:let g:options=[['list', 0, 1, 'global'], ['number', 0, 1, 'global']] +:set list nu +:noa set nolist nonu +:let g:testcase="8: Setting global acd\n" +:let g:options=[['autochdir', 0, 1, 'global']] +:setlocal acd +:let g:testcase="9: Setting global autoread\n" +:let g:options=[['autoread', 1, 0, 'global']] +:set noar +:let g:testcase="10: Setting local autoread\n" +:let g:options=[['autoread', 1, 0, 'local']] +:setlocal noar +:let g:testcase="11: Setting global autoread\n" +:let g:options=[['autoread', 0, 1, 'global']] +:setglobal invar +:let g:testcase="12: Setting option backspace through :let\n" +:let g:options=[['backspace', 'indent,eol,start', '', 'global']] +:let &bs="" +:let g:testcase="13: Setting option backspace through setbufvar()\n" +:let g:options=[['backup', '', '1', 'local']] +: "try twice, first time, shouldn't trigger because option name is invalid, second time, it should trigger +:call setbufvar(1, '&l:bk', 1) +: "should trigger, use correct option name +:call setbufvar(1, '&backup', 1) +:" Write register now, because next test shouldn't output anything. +:$put r +:let @r='' +:%w! test.out +:qa! +ENDTEST +dummy text diff --git a/src/nvim/testdir/test_autocmd_option.ok b/src/nvim/testdir/test_autocmd_option.ok new file mode 100644 index 0000000000..230f53a6f5 --- /dev/null +++ b/src/nvim/testdir/test_autocmd_option.ok @@ -0,0 +1,129 @@ +Test for option autocommand + +STARTTEST +:so small.vim +:if !has("eval") || !has("autocmd") | e! test.ok | w! test.out | qa! | endif +:fu! AutoCommand(match) +: let c=g:testcase +: let item=remove(g:options, 0) +: let c.=printf("Expected: Name: <%s>, Oldval: <%s>, NewVal: <%s>, Scope: <%s>\n", item[0], item[1], item[2], item[3]) +: let c.=printf("Autocmd Option: <%s>,", a:match) +: let c.=printf(" OldVal: <%s>,", v:option_old) +: let c.=printf(" NewVal: <%s>,", v:option_new) +: let c.=printf(" Scope: <%s>\n", v:option_type) +: call setreg('r', printf("%s\n%s", getreg('r'), c)) +:endfu +:au OptionSet * :call AutoCommand(expand("")) +:let g:testcase="1: Setting number option\n" +:let g:options=[['number', 0, 1, 'global']] +:set nu +:let g:testcase="2: Setting local number option\n" +:let g:options=[['number', 1, 0, 'local']] +:setlocal nonu +:let g:testcase="3: Setting global number option\n" +:let g:options=[['number', 1, 0, 'global']] +:setglobal nonu +:let g:testcase="4: Setting local autoindent option\n" +:let g:options=[['autoindent', 0, 1, 'local']] +:setlocal ai +:let g:testcase="5: Setting global autoindent option\n" +:let g:options=[['autoindent', 0, 1, 'global']] +:setglobal ai +:let g:testcase="6: Setting global autoindent option\n" +:let g:options=[['autoindent', 1, 0, 'global']] +:set ai! +: Should not print anything, use :noa +:noa :set nonu +:let g:testcase="7: Setting several global list and number option\n" +:let g:options=[['list', 0, 1, 'global'], ['number', 0, 1, 'global']] +:set list nu +:noa set nolist nonu +:let g:testcase="8: Setting global acd\n" +:let g:options=[['autochdir', 0, 1, 'global']] +:setlocal acd +:let g:testcase="9: Setting global autoread\n" +:let g:options=[['autoread', 0, 1, 'global']] +:set ar +:let g:testcase="10: Setting local autoread\n" +:let g:options=[['autoread', 0, 1, 'local']] +:setlocal ar +:let g:testcase="11: Setting global autoread\n" +:let g:options=[['autoread', 1, 0, 'global']] +:setglobal invar +:let g:testcase="12: Setting option backspace through :let\n" +:let g:options=[['backspace', '', 'eol,indent,start', 'global']] +:let &bs="eol,indent,start" +:let g:testcase="13: Setting option backspace through setbufvar()\n" +:let g:options=[['backup', '', '1', 'local']] +: "try twice, first time, shouldn't trigger because option name is invalid, second time, it should trigger +:call setbufvar(1, '&l:bk', 1) +: "should trigger, use correct option name +:call setbufvar(1, '&backup', 1) +:" Write register now, because next test shouldn't output anything. +:$put r +:let @r='' +:let g:testcase="\n14: Setting key option, shouldn't trigger\n" +:let g:options=[['key', 'invalid', 'invalid1', 'invalid']] +:setlocal key=blah +:setlocal key= +:$put =g:testcase +:%w! test.out +:qa! +ENDTEST +dummy text + +1: Setting number option +Expected: Name: , Oldval: <0>, NewVal: <1>, Scope: +Autocmd Option: , OldVal: <0>, NewVal: <1>, Scope: + +2: Setting local number option +Expected: Name: , Oldval: <1>, NewVal: <0>, Scope: +Autocmd Option: , OldVal: <1>, NewVal: <0>, Scope: + +3: Setting global number option +Expected: Name: , Oldval: <1>, NewVal: <0>, Scope: +Autocmd Option: , OldVal: <1>, NewVal: <0>, Scope: + +4: Setting local autoindent option +Expected: Name: , Oldval: <1>, NewVal: <0>, Scope: +Autocmd Option: , OldVal: <1>, NewVal: <0>, Scope: + +5: Setting global autoindent option +Expected: Name: , Oldval: <1>, NewVal: <0>, Scope: +Autocmd Option: , OldVal: <1>, NewVal: <0>, Scope: + +6: Setting global autoindent option +Expected: Name: , Oldval: <0>, NewVal: <1>, Scope: +Autocmd Option: , OldVal: <0>, NewVal: <1>, Scope: + +7: Setting several global list and number option +Expected: Name: , Oldval: <0>, NewVal: <1>, Scope: +Autocmd Option: , OldVal: <0>, NewVal: <1>, Scope: + +7: Setting several global list and number option +Expected: Name: , Oldval: <0>, NewVal: <1>, Scope: +Autocmd Option: , OldVal: <0>, NewVal: <1>, Scope: + +8: Setting global acd +Expected: Name: , Oldval: <0>, NewVal: <1>, Scope: +Autocmd Option: , OldVal: <0>, NewVal: <1>, Scope: + +9: Setting global autoread +Expected: Name: , Oldval: <1>, NewVal: <0>, Scope: +Autocmd Option: , OldVal: <1>, NewVal: <0>, Scope: + +10: Setting local autoread +Expected: Name: , Oldval: <1>, NewVal: <0>, Scope: +Autocmd Option: , OldVal: <1>, NewVal: <0>, Scope: + +11: Setting global autoread +Expected: Name: , Oldval: <0>, NewVal: <1>, Scope: +Autocmd Option: , OldVal: <0>, NewVal: <1>, Scope: + +12: Setting option backspace through :let +Expected: Name: , Oldval: , NewVal: <>, Scope: +Autocmd Option: , OldVal: , NewVal: <>, Scope: + +13: Setting option backspace through setbufvar() +Expected: Name: , Oldval: <>, NewVal: <1>, Scope: +Autocmd Option: , OldVal: <0>, NewVal: <1>, Scope: diff --git a/src/nvim/version.c b/src/nvim/version.c index d5bbd734f4..83897fba86 100644 --- a/src/nvim/version.c +++ b/src/nvim/version.c @@ -210,7 +210,7 @@ static int included_patches[] = { // 789, // 788 NA // 787, - // 786, + 786, // 785, 784, // 783 NA From 789041c2829d8abb96a0f5f7a844632052a1a448 Mon Sep 17 00:00:00 2001 From: watiko Date: Sat, 28 Nov 2015 01:24:01 +0900 Subject: [PATCH 02/15] vim-patch:7.4.787 Problem: snprintf() isn't available everywhere. Solution: Use vim_snprintf(). (Ken Takata) https://github.com/vim/vim/commit/fb9bc4829a1442fc8e93f078c9f923c9d382dbd2 --- src/nvim/option.c | 12 ++++++------ src/nvim/version.c | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/nvim/option.c b/src/nvim/option.c index 20f005eba9..87b213818c 100644 --- a/src/nvim/option.c +++ b/src/nvim/option.c @@ -3866,9 +3866,9 @@ set_bool_option ( if (!starting) { char_u buf_old[2], buf_new[2], buf_type[7]; - snprintf((char *)buf_old, 2, "%d", old_value ? TRUE: FALSE); - snprintf((char *)buf_new, 2, "%d", value ? TRUE: FALSE); - sprintf((char *)buf_type, "%s", (opt_flags & OPT_LOCAL) ? "local" : "global"); + vim_snprintf((char *)buf_old, 2, "%d", old_value ? TRUE: FALSE); + vim_snprintf((char *)buf_new, 2, "%d", value ? TRUE: FALSE); + vim_snprintf((char *)buf_type, 7, "%s", (opt_flags & OPT_LOCAL) ? "local" : "global"); set_vim_var_string(VV_OPTION_NEW, buf_new, -1); set_vim_var_string(VV_OPTION_OLD, buf_old, -1); set_vim_var_string(VV_OPTION_TYPE, buf_type, -1); @@ -4249,9 +4249,9 @@ set_num_option ( if (!starting && errmsg == NULL) { char_u buf_old[11], buf_new[11], buf_type[7]; - snprintf((char *)buf_old, 10, "%ld", old_value); - snprintf((char *)buf_new, 10, "%ld", value); - snprintf((char *)buf_type, 7, "%s", (opt_flags & OPT_LOCAL) ? "local" : "global"); + vim_snprintf((char *)buf_old, 10, "%ld", old_value); + vim_snprintf((char *)buf_new, 10, "%ld", value); + vim_snprintf((char *)buf_type, 7, "%s", (opt_flags & OPT_LOCAL) ? "local" : "global"); set_vim_var_string(VV_OPTION_NEW, buf_new, -1); set_vim_var_string(VV_OPTION_OLD, buf_old, -1); set_vim_var_string(VV_OPTION_TYPE, buf_type, -1); diff --git a/src/nvim/version.c b/src/nvim/version.c index 83897fba86..4736d3f186 100644 --- a/src/nvim/version.c +++ b/src/nvim/version.c @@ -209,7 +209,7 @@ static int included_patches[] = { // 790, // 789, // 788 NA - // 787, + 787, 786, // 785, 784, From 09f6066bb48150566ba7f1f44ae4148deb0e02c0 Mon Sep 17 00:00:00 2001 From: watiko Date: Sat, 28 Nov 2015 01:31:00 +0900 Subject: [PATCH 03/15] vim-patch:7.4.789 Problem: Using freed memory and crash. (Dominique Pellej) Solution: Correct use of pointers. (Hirohito Higashi) https://github.com/vim/vim/commit/9cac424d05c0e79cd621f1b9f2f01a9f459fcbe6 --- src/nvim/option.c | 7 ++++--- src/nvim/version.c | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/nvim/option.c b/src/nvim/option.c index 87b213818c..84b953e592 100644 --- a/src/nvim/option.c +++ b/src/nvim/option.c @@ -1799,7 +1799,8 @@ do_set ( sprintf((char *)buf_type, "%s", (opt_flags & OPT_LOCAL) ? "local" : "global"); - set_vim_var_string(VV_OPTION_NEW, newval, -1); + set_vim_var_string(VV_OPTION_NEW, + *(char_u **)varp, -1); set_vim_var_string(VV_OPTION_OLD, saved_origval, -1); set_vim_var_string(VV_OPTION_TYPE, buf_type, -1); apply_autocmds(EVENT_OPTIONSET, @@ -2384,8 +2385,8 @@ set_string_option ( char_u buf_type[7]; sprintf((char *)buf_type, "%s", (opt_flags & OPT_LOCAL) ? "local" : "global"); - set_vim_var_string(VV_OPTION_NEW, s, -1); - set_vim_var_string(VV_OPTION_OLD, oldval, -1); + set_vim_var_string(VV_OPTION_NEW, *varp, -1); + set_vim_var_string(VV_OPTION_OLD, saved_oldval, -1); set_vim_var_string(VV_OPTION_TYPE, buf_type, -1); apply_autocmds(EVENT_OPTIONSET, (char_u *)options[opt_idx].fullname, NULL, FALSE, NULL); reset_v_option_vars(); diff --git a/src/nvim/version.c b/src/nvim/version.c index 4736d3f186..6bf443dbbf 100644 --- a/src/nvim/version.c +++ b/src/nvim/version.c @@ -207,7 +207,7 @@ static int included_patches[] = { // 792, 791, // 790, - // 789, + 789, // 788 NA 787, 786, From fe9d91ceb618528e0632874f9cdfa27ccf13a7b0 Mon Sep 17 00:00:00 2001 From: watiko Date: Fri, 27 Nov 2015 18:26:45 +0900 Subject: [PATCH 04/15] vim-patch:7.4.790 Problem: Test fails when the autochdir feature is not available. Test output contains the test script. Solution: Check for the autochdir feature. (Kazunobu Kuriyama) Only write the relevant test output. https://github.com/vim/vim/commit/d113a80c77101e9f5723abb075882012753cf611 --- src/nvim/testdir/test_autocmd_option.in | 4 +- src/nvim/testdir/test_autocmd_option.ok | 72 ------------------------- src/nvim/version.c | 2 +- 3 files changed, 3 insertions(+), 75 deletions(-) diff --git a/src/nvim/testdir/test_autocmd_option.in b/src/nvim/testdir/test_autocmd_option.in index 070312087d..782e6c6d5f 100644 --- a/src/nvim/testdir/test_autocmd_option.in +++ b/src/nvim/testdir/test_autocmd_option.in @@ -2,7 +2,7 @@ Test for option autocommand STARTTEST :so small.vim -:if !has("eval") || !has("autocmd") | e! test.ok | w! test.out | qa! | endif +:if !has("eval") || !has("autocmd") || !exists("+autochdir") | e! test.ok | w! test.out | qa! | endif :fu! AutoCommand(match) : let c=g:testcase : let item=remove(g:options, 0) @@ -62,7 +62,7 @@ STARTTEST :" Write register now, because next test shouldn't output anything. :$put r :let @r='' -:%w! test.out +:/^dummy text/,$w! test.out :qa! ENDTEST dummy text diff --git a/src/nvim/testdir/test_autocmd_option.ok b/src/nvim/testdir/test_autocmd_option.ok index 230f53a6f5..0b6906815f 100644 --- a/src/nvim/testdir/test_autocmd_option.ok +++ b/src/nvim/testdir/test_autocmd_option.ok @@ -1,75 +1,3 @@ -Test for option autocommand - -STARTTEST -:so small.vim -:if !has("eval") || !has("autocmd") | e! test.ok | w! test.out | qa! | endif -:fu! AutoCommand(match) -: let c=g:testcase -: let item=remove(g:options, 0) -: let c.=printf("Expected: Name: <%s>, Oldval: <%s>, NewVal: <%s>, Scope: <%s>\n", item[0], item[1], item[2], item[3]) -: let c.=printf("Autocmd Option: <%s>,", a:match) -: let c.=printf(" OldVal: <%s>,", v:option_old) -: let c.=printf(" NewVal: <%s>,", v:option_new) -: let c.=printf(" Scope: <%s>\n", v:option_type) -: call setreg('r', printf("%s\n%s", getreg('r'), c)) -:endfu -:au OptionSet * :call AutoCommand(expand("")) -:let g:testcase="1: Setting number option\n" -:let g:options=[['number', 0, 1, 'global']] -:set nu -:let g:testcase="2: Setting local number option\n" -:let g:options=[['number', 1, 0, 'local']] -:setlocal nonu -:let g:testcase="3: Setting global number option\n" -:let g:options=[['number', 1, 0, 'global']] -:setglobal nonu -:let g:testcase="4: Setting local autoindent option\n" -:let g:options=[['autoindent', 0, 1, 'local']] -:setlocal ai -:let g:testcase="5: Setting global autoindent option\n" -:let g:options=[['autoindent', 0, 1, 'global']] -:setglobal ai -:let g:testcase="6: Setting global autoindent option\n" -:let g:options=[['autoindent', 1, 0, 'global']] -:set ai! -: Should not print anything, use :noa -:noa :set nonu -:let g:testcase="7: Setting several global list and number option\n" -:let g:options=[['list', 0, 1, 'global'], ['number', 0, 1, 'global']] -:set list nu -:noa set nolist nonu -:let g:testcase="8: Setting global acd\n" -:let g:options=[['autochdir', 0, 1, 'global']] -:setlocal acd -:let g:testcase="9: Setting global autoread\n" -:let g:options=[['autoread', 0, 1, 'global']] -:set ar -:let g:testcase="10: Setting local autoread\n" -:let g:options=[['autoread', 0, 1, 'local']] -:setlocal ar -:let g:testcase="11: Setting global autoread\n" -:let g:options=[['autoread', 1, 0, 'global']] -:setglobal invar -:let g:testcase="12: Setting option backspace through :let\n" -:let g:options=[['backspace', '', 'eol,indent,start', 'global']] -:let &bs="eol,indent,start" -:let g:testcase="13: Setting option backspace through setbufvar()\n" -:let g:options=[['backup', '', '1', 'local']] -: "try twice, first time, shouldn't trigger because option name is invalid, second time, it should trigger -:call setbufvar(1, '&l:bk', 1) -: "should trigger, use correct option name -:call setbufvar(1, '&backup', 1) -:" Write register now, because next test shouldn't output anything. -:$put r -:let @r='' -:let g:testcase="\n14: Setting key option, shouldn't trigger\n" -:let g:options=[['key', 'invalid', 'invalid1', 'invalid']] -:setlocal key=blah -:setlocal key= -:$put =g:testcase -:%w! test.out -:qa! -ENDTEST dummy text 1: Setting number option diff --git a/src/nvim/version.c b/src/nvim/version.c index 6bf443dbbf..b45d98aa37 100644 --- a/src/nvim/version.c +++ b/src/nvim/version.c @@ -206,7 +206,7 @@ static int included_patches[] = { 793, // 792, 791, - // 790, + 790, 789, // 788 NA 787, From 21956adb6edc82a18cb8e9ea96bf177f54239f2d Mon Sep 17 00:00:00 2001 From: watiko Date: Sat, 28 Nov 2015 00:41:50 +0900 Subject: [PATCH 05/15] tests: Migrate legacy test autocmd_option. --- src/nvim/testdir/Makefile | 1 - src/nvim/testdir/test_autocmd_option.in | 68 --------- src/nvim/testdir/test_autocmd_option.ok | 57 ------- .../functional/legacy/autocmd_option_spec.lua | 144 ++++++++++++++++++ 4 files changed, 144 insertions(+), 126 deletions(-) delete mode 100644 src/nvim/testdir/test_autocmd_option.in delete mode 100644 src/nvim/testdir/test_autocmd_option.ok create mode 100644 test/functional/legacy/autocmd_option_spec.lua diff --git a/src/nvim/testdir/Makefile b/src/nvim/testdir/Makefile index 75232ad66a..8c3e99c624 100644 --- a/src/nvim/testdir/Makefile +++ b/src/nvim/testdir/Makefile @@ -25,7 +25,6 @@ SCRIPTS := test_eval.out \ test83.out \ test88.out \ test_listlbr.out \ - test_autocmd_option.out \ test_breakindent.out \ test_charsearch.out \ test_close_count.out \ diff --git a/src/nvim/testdir/test_autocmd_option.in b/src/nvim/testdir/test_autocmd_option.in deleted file mode 100644 index 782e6c6d5f..0000000000 --- a/src/nvim/testdir/test_autocmd_option.in +++ /dev/null @@ -1,68 +0,0 @@ -Test for option autocommand - -STARTTEST -:so small.vim -:if !has("eval") || !has("autocmd") || !exists("+autochdir") | e! test.ok | w! test.out | qa! | endif -:fu! AutoCommand(match) -: let c=g:testcase -: let item=remove(g:options, 0) -: let c.=printf("Expected: Name: <%s>, Oldval: <%s>, NewVal: <%s>, Scope: <%s>\n", item[0], item[1], item[2], item[3]) -: let c.=printf("Autocmd Option: <%s>,", a:match) -: let c.=printf(" OldVal: <%s>,", v:option_old) -: let c.=printf(" NewVal: <%s>,", v:option_new) -: let c.=printf(" Scope: <%s>\n", v:option_type) -: call setreg('r', printf("%s\n%s", getreg('r'), c)) -:endfu -:au OptionSet * :call AutoCommand(expand("")) -:let g:testcase="1: Setting number option\n" -:let g:options=[['number', 0, 1, 'global']] -:set nu -:let g:testcase="2: Setting local number option\n" -:let g:options=[['number', 1, 0, 'local']] -:setlocal nonu -:let g:testcase="3: Setting global number option\n" -:let g:options=[['number', 1, 0, 'global']] -:setglobal nonu -:let g:testcase="4: Setting local autoindent option\n" -:let g:options=[['autoindent', 1, 0, 'local']] -:setlocal noai -:let g:testcase="5: Setting global autoindent option\n" -:let g:options=[['autoindent', 1, 0, 'global']] -:setglobal noai -:let g:testcase="6: Setting global autoindent option\n" -:let g:options=[['autoindent', 0, 1, 'global']] -:set ai! -: Should not print anything, use :noa -:noa :set nonu -:let g:testcase="7: Setting several global list and number option\n" -:let g:options=[['list', 0, 1, 'global'], ['number', 0, 1, 'global']] -:set list nu -:noa set nolist nonu -:let g:testcase="8: Setting global acd\n" -:let g:options=[['autochdir', 0, 1, 'global']] -:setlocal acd -:let g:testcase="9: Setting global autoread\n" -:let g:options=[['autoread', 1, 0, 'global']] -:set noar -:let g:testcase="10: Setting local autoread\n" -:let g:options=[['autoread', 1, 0, 'local']] -:setlocal noar -:let g:testcase="11: Setting global autoread\n" -:let g:options=[['autoread', 0, 1, 'global']] -:setglobal invar -:let g:testcase="12: Setting option backspace through :let\n" -:let g:options=[['backspace', 'indent,eol,start', '', 'global']] -:let &bs="" -:let g:testcase="13: Setting option backspace through setbufvar()\n" -:let g:options=[['backup', '', '1', 'local']] -: "try twice, first time, shouldn't trigger because option name is invalid, second time, it should trigger -:call setbufvar(1, '&l:bk', 1) -: "should trigger, use correct option name -:call setbufvar(1, '&backup', 1) -:" Write register now, because next test shouldn't output anything. -:$put r -:let @r='' -:/^dummy text/,$w! test.out -:qa! -ENDTEST -dummy text diff --git a/src/nvim/testdir/test_autocmd_option.ok b/src/nvim/testdir/test_autocmd_option.ok deleted file mode 100644 index 0b6906815f..0000000000 --- a/src/nvim/testdir/test_autocmd_option.ok +++ /dev/null @@ -1,57 +0,0 @@ -dummy text - -1: Setting number option -Expected: Name: , Oldval: <0>, NewVal: <1>, Scope: -Autocmd Option: , OldVal: <0>, NewVal: <1>, Scope: - -2: Setting local number option -Expected: Name: , Oldval: <1>, NewVal: <0>, Scope: -Autocmd Option: , OldVal: <1>, NewVal: <0>, Scope: - -3: Setting global number option -Expected: Name: , Oldval: <1>, NewVal: <0>, Scope: -Autocmd Option: , OldVal: <1>, NewVal: <0>, Scope: - -4: Setting local autoindent option -Expected: Name: , Oldval: <1>, NewVal: <0>, Scope: -Autocmd Option: , OldVal: <1>, NewVal: <0>, Scope: - -5: Setting global autoindent option -Expected: Name: , Oldval: <1>, NewVal: <0>, Scope: -Autocmd Option: , OldVal: <1>, NewVal: <0>, Scope: - -6: Setting global autoindent option -Expected: Name: , Oldval: <0>, NewVal: <1>, Scope: -Autocmd Option: , OldVal: <0>, NewVal: <1>, Scope: - -7: Setting several global list and number option -Expected: Name: , Oldval: <0>, NewVal: <1>, Scope: -Autocmd Option: , OldVal: <0>, NewVal: <1>, Scope: - -7: Setting several global list and number option -Expected: Name: , Oldval: <0>, NewVal: <1>, Scope: -Autocmd Option: , OldVal: <0>, NewVal: <1>, Scope: - -8: Setting global acd -Expected: Name: , Oldval: <0>, NewVal: <1>, Scope: -Autocmd Option: , OldVal: <0>, NewVal: <1>, Scope: - -9: Setting global autoread -Expected: Name: , Oldval: <1>, NewVal: <0>, Scope: -Autocmd Option: , OldVal: <1>, NewVal: <0>, Scope: - -10: Setting local autoread -Expected: Name: , Oldval: <1>, NewVal: <0>, Scope: -Autocmd Option: , OldVal: <1>, NewVal: <0>, Scope: - -11: Setting global autoread -Expected: Name: , Oldval: <0>, NewVal: <1>, Scope: -Autocmd Option: , OldVal: <0>, NewVal: <1>, Scope: - -12: Setting option backspace through :let -Expected: Name: , Oldval: , NewVal: <>, Scope: -Autocmd Option: , OldVal: , NewVal: <>, Scope: - -13: Setting option backspace through setbufvar() -Expected: Name: , Oldval: <>, NewVal: <1>, Scope: -Autocmd Option: , OldVal: <0>, NewVal: <1>, Scope: diff --git a/test/functional/legacy/autocmd_option_spec.lua b/test/functional/legacy/autocmd_option_spec.lua new file mode 100644 index 0000000000..146c82d29b --- /dev/null +++ b/test/functional/legacy/autocmd_option_spec.lua @@ -0,0 +1,144 @@ +-- Test for option autocommand + +local helpers = require('test.functional.helpers') +local feed, source = helpers.feed, helpers.source +local clear, execute, expect = helpers.clear, helpers.execute, helpers.expect + +describe('autocmd_option', function() + setup(clear) + + it('is working', function() + execute('so small.vim') + + source([[ + fu! AutoCommand(match) + let c=g:testcase + let item=remove(g:options, 0) + let c.=printf("Expected: Name: <%s>, Oldval: <%s>, NewVal: <%s>, Scope: <%s>\n", + \item[0], item[1], item[2], item[3]) + let c.=printf("Autocmd Option: <%s>,", a:match) + let c.=printf(" OldVal: <%s>,", v:option_old) + let c.=printf(" NewVal: <%s>,", v:option_new) + let c.=printf(" Scope: <%s>\n", v:option_type) + call setreg('r', printf("%s\n%s", getreg('r'), c)) + endfu + ]]) + + execute('au OptionSet * :call AutoCommand(expand(""))') + + source([=[ + let g:testcase="1: Setting number option\n" + let g:options=[['number', 0, 1, 'global']] + set nu + let g:testcase="2: Setting local number option\n" + let g:options=[['number', 1, 0, 'local']] + setlocal nonu + let g:testcase="3: Setting global number option\n" + let g:options=[['number', 1, 0, 'global']] + setglobal nonu + let g:testcase="4: Setting local autoindent option\n" + let g:options=[['autoindent', 0, 1, 'local']] + setlocal ai + let g:testcase="5: Setting global autoindent option\n" + let g:options=[['autoindent', 0, 1, 'global']] + setglobal ai + let g:testcase="6: Setting global autoindent option\n" + let g:options=[['autoindent', 1, 0, 'global']] + set ai! + ]=]) + + -- Should not print anything, use :noa. + source([=[ + noa :set nonu + let g:testcase="7: Setting several global list and number option\n" + let g:options=[['list', 0, 1, 'global'], ['number', 0, 1, 'global']] + set list nu + noa set nolist nonu + let g:testcase="8: Setting global acd\n" + let g:options=[['autochdir', 0, 1, 'global']] + setlocal acd + let g:testcase="9: Setting global autoread\n" + let g:options=[['autoread', 1, 0, 'global']] + set noar + let g:testcase="10: Setting local autoread\n" + let g:options=[['autoread', 0, 1, 'local']] + setlocal ar + let g:testcase="11: Setting global autoread\n" + let g:options=[['autoread', 0, 1, 'global']] + setglobal invar + let g:testcase="12: Setting option backspace through :let\n" + let g:options=[['backspace', 'indent,eol,start', '', 'global']] + let &bs="" + let g:testcase="13: Setting option backspace through setbufvar()\n" + let g:options=[['backup', '', '1', 'local']] + ]=]) + + -- Try twice, first time, shouldn't trigger because option name is invalid, second time, it should trigger. + execute('call setbufvar(1, "&l:bk", 1)') + -- Should trigger, use correct option name. + execute('call setbufvar(1, "&backup", 1)') + -- Write register now + execute('$put! r') + + -- Remove the first and last blank lines. + feed('ggddGdd') + + -- Assert buffer contents. + expect([=[ + 1: Setting number option + Expected: Name: , Oldval: <0>, NewVal: <1>, Scope: + Autocmd Option: , OldVal: <0>, NewVal: <1>, Scope: + + 2: Setting local number option + Expected: Name: , Oldval: <1>, NewVal: <0>, Scope: + Autocmd Option: , OldVal: <1>, NewVal: <0>, Scope: + + 3: Setting global number option + Expected: Name: , Oldval: <1>, NewVal: <0>, Scope: + Autocmd Option: , OldVal: <1>, NewVal: <0>, Scope: + + 4: Setting local autoindent option + Expected: Name: , Oldval: <0>, NewVal: <1>, Scope: + Autocmd Option: , OldVal: <0>, NewVal: <1>, Scope: + + 5: Setting global autoindent option + Expected: Name: , Oldval: <0>, NewVal: <1>, Scope: + Autocmd Option: , OldVal: <0>, NewVal: <1>, Scope: + + 6: Setting global autoindent option + Expected: Name: , Oldval: <1>, NewVal: <0>, Scope: + Autocmd Option: , OldVal: <1>, NewVal: <0>, Scope: + + 7: Setting several global list and number option + Expected: Name: , Oldval: <0>, NewVal: <1>, Scope: + Autocmd Option: , OldVal: <0>, NewVal: <1>, Scope: + + 7: Setting several global list and number option + Expected: Name: , Oldval: <0>, NewVal: <1>, Scope: + Autocmd Option: , OldVal: <0>, NewVal: <1>, Scope: + + 8: Setting global acd + Expected: Name: , Oldval: <0>, NewVal: <1>, Scope: + Autocmd Option: , OldVal: <0>, NewVal: <1>, Scope: + + 9: Setting global autoread + Expected: Name: , Oldval: <1>, NewVal: <0>, Scope: + Autocmd Option: , OldVal: <1>, NewVal: <0>, Scope: + + 10: Setting local autoread + Expected: Name: , Oldval: <0>, NewVal: <1>, Scope: + Autocmd Option: , OldVal: <0>, NewVal: <1>, Scope: + + 11: Setting global autoread + Expected: Name: , Oldval: <0>, NewVal: <1>, Scope: + Autocmd Option: , OldVal: <0>, NewVal: <1>, Scope: + + 12: Setting option backspace through :let + Expected: Name: , Oldval: , NewVal: <>, Scope: + Autocmd Option: , OldVal: , NewVal: <>, Scope: + + 13: Setting option backspace through setbufvar() + Expected: Name: , Oldval: <>, NewVal: <1>, Scope: + Autocmd Option: , OldVal: <0>, NewVal: <1>, Scope: ]=]) + end) +end) From d9c029382442ab9d6e50ca83aece0c2a2f9b817b Mon Sep 17 00:00:00 2001 From: watiko Date: Sat, 28 Nov 2015 17:06:23 +0900 Subject: [PATCH 06/15] tests: Improve legacy test autocmd_option. --- .../functional/legacy/autocmd_option_spec.lua | 246 ++++++++---------- 1 file changed, 115 insertions(+), 131 deletions(-) diff --git a/test/functional/legacy/autocmd_option_spec.lua b/test/functional/legacy/autocmd_option_spec.lua index 146c82d29b..dffddd2cfa 100644 --- a/test/functional/legacy/autocmd_option_spec.lua +++ b/test/functional/legacy/autocmd_option_spec.lua @@ -1,144 +1,128 @@ --- Test for option autocommand - local helpers = require('test.functional.helpers') -local feed, source = helpers.feed, helpers.source -local clear, execute, expect = helpers.clear, helpers.execute, helpers.expect +local clear, nvim, eq = helpers.clear, helpers.nvim, helpers.eq +local source, execute = helpers.source, helpers.execute -describe('autocmd_option', function() +describe('au OptionSet', function() setup(clear) - it('is working', function() - execute('so small.vim') + describe('with * as ', function() + describe('matches when being set any option', function() - source([[ - fu! AutoCommand(match) - let c=g:testcase - let item=remove(g:options, 0) - let c.=printf("Expected: Name: <%s>, Oldval: <%s>, NewVal: <%s>, Scope: <%s>\n", - \item[0], item[1], item[2], item[3]) - let c.=printf("Autocmd Option: <%s>,", a:match) - let c.=printf(" OldVal: <%s>,", v:option_old) - let c.=printf(" NewVal: <%s>,", v:option_new) - let c.=printf(" Scope: <%s>\n", v:option_type) - call setreg('r', printf("%s\n%s", getreg('r'), c)) - endfu - ]]) + local function expected_str(option, oldval, newval, scope) + return '' + .. string.format('Autocmd Option: <%s>,', option) + .. string.format(' OldVal: <%s>,', oldval) + .. string.format(' NewVal: <%s>,', newval) + .. string.format(' Scope: <%s>', scope) + end - execute('au OptionSet * :call AutoCommand(expand(""))') + local function get_result() + return nvim('get_var', 'ret') + end - source([=[ - let g:testcase="1: Setting number option\n" - let g:options=[['number', 0, 1, 'global']] - set nu - let g:testcase="2: Setting local number option\n" - let g:options=[['number', 1, 0, 'local']] - setlocal nonu - let g:testcase="3: Setting global number option\n" - let g:options=[['number', 1, 0, 'global']] - setglobal nonu - let g:testcase="4: Setting local autoindent option\n" - let g:options=[['autoindent', 0, 1, 'local']] - setlocal ai - let g:testcase="5: Setting global autoindent option\n" - let g:options=[['autoindent', 0, 1, 'global']] - setglobal ai - let g:testcase="6: Setting global autoindent option\n" - let g:options=[['autoindent', 1, 0, 'global']] - set ai! - ]=]) + local function expected_combination(option, oldval, newval, scope) + eq(expected_str(option, oldval, newval, scope), get_result()) + end - -- Should not print anything, use :noa. - source([=[ - noa :set nonu - let g:testcase="7: Setting several global list and number option\n" - let g:options=[['list', 0, 1, 'global'], ['number', 0, 1, 'global']] - set list nu - noa set nolist nonu - let g:testcase="8: Setting global acd\n" - let g:options=[['autochdir', 0, 1, 'global']] - setlocal acd - let g:testcase="9: Setting global autoread\n" - let g:options=[['autoread', 1, 0, 'global']] - set noar - let g:testcase="10: Setting local autoread\n" - let g:options=[['autoread', 0, 1, 'local']] - setlocal ar - let g:testcase="11: Setting global autoread\n" - let g:options=[['autoread', 0, 1, 'global']] - setglobal invar - let g:testcase="12: Setting option backspace through :let\n" - let g:options=[['backspace', 'indent,eol,start', '', 'global']] - let &bs="" - let g:testcase="13: Setting option backspace through setbufvar()\n" - let g:options=[['backup', '', '1', 'local']] - ]=]) + local function expected_empty() + eq('', get_result()) + end - -- Try twice, first time, shouldn't trigger because option name is invalid, second time, it should trigger. - execute('call setbufvar(1, "&l:bk", 1)') - -- Should trigger, use correct option name. - execute('call setbufvar(1, "&backup", 1)') - -- Write register now - execute('$put! r') + setup(function() - -- Remove the first and last blank lines. - feed('ggddGdd') + source([[ + fu! AutoCommand(match) + let g:ret.=printf('Autocmd Option: <%s>,', a:match) + let g:ret.=printf(' OldVal: <%s>,', v:option_old) + let g:ret.=printf(' NewVal: <%s>,', v:option_new) + let g:ret.=printf(' Scope: <%s>', v:option_type) + endfu + + au OptionSet * :call AutoCommand(expand("")) + ]]) + end) - -- Assert buffer contents. - expect([=[ - 1: Setting number option - Expected: Name: , Oldval: <0>, NewVal: <1>, Scope: - Autocmd Option: , OldVal: <0>, NewVal: <1>, Scope: - - 2: Setting local number option - Expected: Name: , Oldval: <1>, NewVal: <0>, Scope: - Autocmd Option: , OldVal: <1>, NewVal: <0>, Scope: - - 3: Setting global number option - Expected: Name: , Oldval: <1>, NewVal: <0>, Scope: - Autocmd Option: , OldVal: <1>, NewVal: <0>, Scope: - - 4: Setting local autoindent option - Expected: Name: , Oldval: <0>, NewVal: <1>, Scope: - Autocmd Option: , OldVal: <0>, NewVal: <1>, Scope: - - 5: Setting global autoindent option - Expected: Name: , Oldval: <0>, NewVal: <1>, Scope: - Autocmd Option: , OldVal: <0>, NewVal: <1>, Scope: - - 6: Setting global autoindent option - Expected: Name: , Oldval: <1>, NewVal: <0>, Scope: - Autocmd Option: , OldVal: <1>, NewVal: <0>, Scope: - - 7: Setting several global list and number option - Expected: Name: , Oldval: <0>, NewVal: <1>, Scope: - Autocmd Option: , OldVal: <0>, NewVal: <1>, Scope: - - 7: Setting several global list and number option - Expected: Name: , Oldval: <0>, NewVal: <1>, Scope: - Autocmd Option: , OldVal: <0>, NewVal: <1>, Scope: - - 8: Setting global acd - Expected: Name: , Oldval: <0>, NewVal: <1>, Scope: - Autocmd Option: , OldVal: <0>, NewVal: <1>, Scope: - - 9: Setting global autoread - Expected: Name: , Oldval: <1>, NewVal: <0>, Scope: - Autocmd Option: , OldVal: <1>, NewVal: <0>, Scope: - - 10: Setting local autoread - Expected: Name: , Oldval: <0>, NewVal: <1>, Scope: - Autocmd Option: , OldVal: <0>, NewVal: <1>, Scope: - - 11: Setting global autoread - Expected: Name: , Oldval: <0>, NewVal: <1>, Scope: - Autocmd Option: , OldVal: <0>, NewVal: <1>, Scope: - - 12: Setting option backspace through :let - Expected: Name: , Oldval: , NewVal: <>, Scope: - Autocmd Option: , OldVal: , NewVal: <>, Scope: - - 13: Setting option backspace through setbufvar() - Expected: Name: , Oldval: <>, NewVal: <1>, Scope: - Autocmd Option: , OldVal: <0>, NewVal: <1>, Scope: ]=]) + before_each(function() + execute([[let g:ret = '']]) + end) + + it('should set number option', function() + execute('set nu') + expected_combination('number', 0, 1, 'global') + end) + + it('should set local nonumber option',function() + execute('setlocal nonu') + expected_combination('number', 1, 0, 'local') + end) + + it('should set global nonumber option',function() + execute('setglobal nonu') + expected_combination('number', 1, 0, 'global') + end) + + it('should set local autoindent option',function() + execute('setlocal ai') + expected_combination('autoindent', 0, 1, 'local') + end) + + it('should set global autoindent option',function() + execute('setglobal ai') + expected_combination('autoindent', 0, 1, 'global') + end) + + it('should invert global autoindent option',function() + execute('set ai!') + expected_combination('autoindent', 1, 0, 'global') + end) + + it('should set several global list and number option',function() + execute('set list nu') + eq(expected_str('list', 0, 1, 'global') .. expected_str('number', 0, 1, 'global'), + get_result()) + end) + + it('should not print anything, use :noa.', function() + execute('noa set nolist nonu') + expected_empty() + end) + + it('should set global acd', function() + execute('setlocal acd') + expected_combination('autochdir', 0, 1, 'local') + end) + + it('should set global noautoread', function() + execute('set noar') + expected_combination('autoread', 1, 0, 'global') + end) + + it('should set local autoread', function() + execute('setlocal ar') + expected_combination('autoread', 0, 1, 'local') + end) + + it('should invert global autoread', function() + execute('setglobal invar') + expected_combination('autoread', 0, 1, 'global') + end) + + it('should set option backspace through :let', function() + execute('let &bs=""') + expected_combination('backspace', 'indent,eol,start', '', 'global') + end) + + describe('setting option through setbufvar()', function() + it('shouldn\'t trigger because option name is invalid', function() + execute('call setbufvar(1, "&l:bk", 1)') + expected_empty() + end) + + it('should trigger, use correct option name.', function() + execute('call setbufvar(1, "&backup", 1)') + expected_combination('backup', 0, 1, 'local') + end) + end) + end) end) end) From cf673f60c64e82528207e636b7a04e7ed3610f77 Mon Sep 17 00:00:00 2001 From: watiko Date: Sat, 28 Nov 2015 18:19:40 +0900 Subject: [PATCH 07/15] Improve coding style --- src/nvim/eval.c | 5 ++--- src/nvim/option.c | 50 ++++++++++++++++++++++++++++------------------- 2 files changed, 32 insertions(+), 23 deletions(-) diff --git a/src/nvim/eval.c b/src/nvim/eval.c index cb9f40f851..3e502a94ba 100644 --- a/src/nvim/eval.c +++ b/src/nvim/eval.c @@ -21250,9 +21250,8 @@ void ex_oldfiles(exarg_T *eap) } } -/* reset v:option_new, v:option_old and v:option_type */ -void -reset_v_option_vars(void) +// reset v:option_new, v:option_old and v:option_type +void reset_v_option_vars(void) { set_vim_var_string(VV_OPTION_NEW, NULL, -1); set_vim_var_string(VV_OPTION_OLD, NULL, -1); diff --git a/src/nvim/option.c b/src/nvim/option.c index 84b953e592..a3da057374 100644 --- a/src/nvim/option.c +++ b/src/nvim/option.c @@ -1511,7 +1511,7 @@ do_set ( } else if (opt_idx >= 0) { /* string */ char_u *save_arg = NULL; char_u *s = NULL; - char_u *oldval = NULL; /* previous value if *varp */ + char_u *oldval = NULL; // previous value if *varp char_u *newval; char_u *origval = NULL; char_u *saved_origval = NULL; @@ -1781,10 +1781,11 @@ do_set ( /* Set the new value. */ *(char_u **)(varp) = newval; - if (!starting && origval != NULL) - /* origval may be freed by - * did_set_string_option(), make a copy. */ + if (!starting && origval != NULL) { + // origval may be freed by + // did_set_string_option(), make a copy. saved_origval = vim_strsave(origval); + } /* Handle side effects, and set the global value for * ":set" on local options. */ @@ -1798,14 +1799,14 @@ do_set ( char_u buf_type[7]; sprintf((char *)buf_type, "%s", - (opt_flags & OPT_LOCAL) ? "local" : "global"); + (opt_flags & OPT_LOCAL) ? "local" : "global"); set_vim_var_string(VV_OPTION_NEW, - *(char_u **)varp, -1); + *(char_u **)varp, -1); set_vim_var_string(VV_OPTION_OLD, saved_origval, -1); set_vim_var_string(VV_OPTION_TYPE, buf_type, -1); apply_autocmds(EVENT_OPTIONSET, - (char_u *)options[opt_idx].fullname, - NULL, FALSE, NULL); + (char_u *)options[opt_idx].fullname, + NULL, FALSE, NULL); reset_v_option_vars(); xfree(saved_origval); } @@ -2373,22 +2374,25 @@ set_string_option ( oldval = *varp; *varp = s; - if (!starting) - saved_oldval = vim_strsave(oldval); + if (!starting) { + saved_oldval = vim_strsave(oldval); + } if ((r = did_set_string_option(opt_idx, varp, TRUE, oldval, NULL, opt_flags)) == NULL) did_set_option(opt_idx, opt_flags, TRUE); - /* call autocommand after handling side effects */ + // call autocommand after handling side effects if (saved_oldval != NULL) { char_u buf_type[7]; sprintf((char *)buf_type, "%s", - (opt_flags & OPT_LOCAL) ? "local" : "global"); + (opt_flags & OPT_LOCAL) ? "local" : "global"); set_vim_var_string(VV_OPTION_NEW, *varp, -1); set_vim_var_string(VV_OPTION_OLD, saved_oldval, -1); set_vim_var_string(VV_OPTION_TYPE, buf_type, -1); - apply_autocmds(EVENT_OPTIONSET, (char_u *)options[opt_idx].fullname, NULL, FALSE, NULL); + apply_autocmds(EVENT_OPTIONSET, + (char_u *)options[opt_idx].fullname, + NULL, false, NULL); reset_v_option_vars(); xfree(saved_oldval); } @@ -3861,19 +3865,22 @@ set_bool_option ( * End of handling side effects for bool options. */ - /* after handling side effects, call autocommand */ + // after handling side effects, call autocommand options[opt_idx].flags |= P_WAS_SET; if (!starting) { char_u buf_old[2], buf_new[2], buf_type[7]; - vim_snprintf((char *)buf_old, 2, "%d", old_value ? TRUE: FALSE); - vim_snprintf((char *)buf_new, 2, "%d", value ? TRUE: FALSE); - vim_snprintf((char *)buf_type, 7, "%s", (opt_flags & OPT_LOCAL) ? "local" : "global"); + vim_snprintf((char *)buf_old, 2, "%d", old_value ? true: false); + vim_snprintf((char *)buf_new, 2, "%d", value ? true: false); + vim_snprintf((char *)buf_type, 7, "%s", + (opt_flags & OPT_LOCAL) ? "local" : "global"); set_vim_var_string(VV_OPTION_NEW, buf_new, -1); set_vim_var_string(VV_OPTION_OLD, buf_old, -1); set_vim_var_string(VV_OPTION_TYPE, buf_type, -1); - apply_autocmds(EVENT_OPTIONSET, (char_u *) options[opt_idx].fullname, NULL, FALSE, NULL); + apply_autocmds(EVENT_OPTIONSET, + (char_u *) options[opt_idx].fullname, + NULL, false, NULL); reset_v_option_vars(); } @@ -4252,11 +4259,14 @@ set_num_option ( char_u buf_old[11], buf_new[11], buf_type[7]; vim_snprintf((char *)buf_old, 10, "%ld", old_value); vim_snprintf((char *)buf_new, 10, "%ld", value); - vim_snprintf((char *)buf_type, 7, "%s", (opt_flags & OPT_LOCAL) ? "local" : "global"); + vim_snprintf((char *)buf_type, 7, "%s", + (opt_flags & OPT_LOCAL) ? "local" : "global"); set_vim_var_string(VV_OPTION_NEW, buf_new, -1); set_vim_var_string(VV_OPTION_OLD, buf_old, -1); set_vim_var_string(VV_OPTION_TYPE, buf_type, -1); - apply_autocmds(EVENT_OPTIONSET, (char_u *) options[opt_idx].fullname, NULL, FALSE, NULL); + apply_autocmds(EVENT_OPTIONSET, + (char_u *) options[opt_idx].fullname, + NULL, false, NULL); reset_v_option_vars(); } From 972b43459b22d5500cbb8f0004f45d8bdfacecf8 Mon Sep 17 00:00:00 2001 From: watiko Date: Sat, 28 Nov 2015 19:48:17 +0900 Subject: [PATCH 08/15] Improve coding style --- src/nvim/option.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/nvim/option.c b/src/nvim/option.c index a3da057374..6ec0d4a095 100644 --- a/src/nvim/option.c +++ b/src/nvim/option.c @@ -1806,7 +1806,7 @@ do_set ( set_vim_var_string(VV_OPTION_TYPE, buf_type, -1); apply_autocmds(EVENT_OPTIONSET, (char_u *)options[opt_idx].fullname, - NULL, FALSE, NULL); + NULL, false, NULL); reset_v_option_vars(); xfree(saved_origval); } From 597547e7977c6c9404ec4d730bfc02584c9f1564 Mon Sep 17 00:00:00 2001 From: watiko Date: Sat, 28 Nov 2015 20:14:49 +0900 Subject: [PATCH 09/15] Use vim_snprintf instead of sprintf --- src/nvim/option.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/nvim/option.c b/src/nvim/option.c index 6ec0d4a095..3e74d4d23c 100644 --- a/src/nvim/option.c +++ b/src/nvim/option.c @@ -1798,8 +1798,8 @@ do_set ( if (saved_origval != NULL) { char_u buf_type[7]; - sprintf((char *)buf_type, "%s", - (opt_flags & OPT_LOCAL) ? "local" : "global"); + vim_snprintf((char *)buf_type, 7, "%s", + (opt_flags & OPT_LOCAL) ? "local" : "global"); set_vim_var_string(VV_OPTION_NEW, *(char_u **)varp, -1); set_vim_var_string(VV_OPTION_OLD, saved_origval, -1); @@ -2385,8 +2385,8 @@ set_string_option ( // call autocommand after handling side effects if (saved_oldval != NULL) { char_u buf_type[7]; - sprintf((char *)buf_type, "%s", - (opt_flags & OPT_LOCAL) ? "local" : "global"); + vim_snprintf((char *)buf_type, 7, "%s", + (opt_flags & OPT_LOCAL) ? "local" : "global"); set_vim_var_string(VV_OPTION_NEW, *varp, -1); set_vim_var_string(VV_OPTION_OLD, saved_oldval, -1); set_vim_var_string(VV_OPTION_TYPE, buf_type, -1); From 8c00c34b91b44115275dc8d1c10bab95ae62a7a9 Mon Sep 17 00:00:00 2001 From: watiko Date: Sat, 28 Nov 2015 20:09:56 +0900 Subject: [PATCH 10/15] tests: Keep each autocmd_option's test in isolation. --- .../functional/legacy/autocmd_option_spec.lua | 28 +++++++------------ 1 file changed, 10 insertions(+), 18 deletions(-) diff --git a/test/functional/legacy/autocmd_option_spec.lua b/test/functional/legacy/autocmd_option_spec.lua index dffddd2cfa..257a4ac5d4 100644 --- a/test/functional/legacy/autocmd_option_spec.lua +++ b/test/functional/legacy/autocmd_option_spec.lua @@ -3,11 +3,8 @@ local clear, nvim, eq = helpers.clear, helpers.nvim, helpers.eq local source, execute = helpers.source, helpers.execute describe('au OptionSet', function() - setup(clear) - describe('with * as ', function() describe('matches when being set any option', function() - local function expected_str(option, oldval, newval, scope) return '' .. string.format('Autocmd Option: <%s>,', option) @@ -22,13 +19,15 @@ describe('au OptionSet', function() local function expected_combination(option, oldval, newval, scope) eq(expected_str(option, oldval, newval, scope), get_result()) + execute([[let g:ret = '']]) end local function expected_empty() eq('', get_result()) end - setup(function() + before_each(function() + clear() source([[ fu! AutoCommand(match) @@ -40,40 +39,35 @@ describe('au OptionSet', function() au OptionSet * :call AutoCommand(expand("")) ]]) - end) - before_each(function() execute([[let g:ret = '']]) end) it('should set number option', function() execute('set nu') expected_combination('number', 0, 1, 'global') - end) - it('should set local nonumber option',function() execute('setlocal nonu') expected_combination('number', 1, 0, 'local') - end) - it('should set global nonumber option',function() execute('setglobal nonu') expected_combination('number', 1, 0, 'global') end) - it('should set local autoindent option',function() + it('should set autoindent option',function() execute('setlocal ai') expected_combination('autoindent', 0, 1, 'local') - end) - it('should set global autoindent option',function() execute('setglobal ai') expected_combination('autoindent', 0, 1, 'global') + + execute('set noai') + expected_combination('autoindent', 1, 0, 'global') end) it('should invert global autoindent option',function() execute('set ai!') - expected_combination('autoindent', 1, 0, 'global') + expected_combination('autoindent', 0, 1, 'global') end) it('should set several global list and number option',function() @@ -92,19 +86,17 @@ describe('au OptionSet', function() expected_combination('autochdir', 0, 1, 'local') end) - it('should set global noautoread', function() + it('should set autoread', function() execute('set noar') expected_combination('autoread', 1, 0, 'global') - end) - it('should set local autoread', function() execute('setlocal ar') expected_combination('autoread', 0, 1, 'local') end) it('should invert global autoread', function() execute('setglobal invar') - expected_combination('autoread', 0, 1, 'global') + expected_combination('autoread', 1, 0, 'global') end) it('should set option backspace through :let', function() From 3a60f927b818bd870d184407859fb85c140a3aa0 Mon Sep 17 00:00:00 2001 From: watiko Date: Tue, 1 Dec 2015 23:35:37 +0900 Subject: [PATCH 11/15] Improve coding style --- src/nvim/option.c | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/src/nvim/option.c b/src/nvim/option.c index 3e74d4d23c..878bc33c6d 100644 --- a/src/nvim/option.c +++ b/src/nvim/option.c @@ -1796,9 +1796,8 @@ do_set ( if (errmsg != NULL) goto skip; if (saved_origval != NULL) { - char_u buf_type[7]; - - vim_snprintf((char *)buf_type, 7, "%s", + char buf_type[7]; + vim_snprintf(buf_type, ARRAY_SIZE(buf_type), "%s", (opt_flags & OPT_LOCAL) ? "local" : "global"); set_vim_var_string(VV_OPTION_NEW, *(char_u **)varp, -1); @@ -2384,8 +2383,8 @@ set_string_option ( // call autocommand after handling side effects if (saved_oldval != NULL) { - char_u buf_type[7]; - vim_snprintf((char *)buf_type, 7, "%s", + char buf_type[7]; + vim_snprintf(buf_type, ARRAY_SIZE(buf_type), "%s", (opt_flags & OPT_LOCAL) ? "local" : "global"); set_vim_var_string(VV_OPTION_NEW, *varp, -1); set_vim_var_string(VV_OPTION_OLD, saved_oldval, -1); @@ -3870,10 +3869,14 @@ set_bool_option ( options[opt_idx].flags |= P_WAS_SET; if (!starting) { - char_u buf_old[2], buf_new[2], buf_type[7]; - vim_snprintf((char *)buf_old, 2, "%d", old_value ? true: false); - vim_snprintf((char *)buf_new, 2, "%d", value ? true: false); - vim_snprintf((char *)buf_type, 7, "%s", + char buf_old[2]; + char buf_new[2]; + char buf_type[7]; + vim_snprintf(buf_old, ARRAY_SIZE(buf_old), "%d", + old_value ? true: false); + vim_snprintf(buf_new, ARRAY_SIZE(buf_new), "%d", + value ? true: false); + vim_snprintf(buf_type, ARRAY_SIZE(buf_type), "%s", (opt_flags & OPT_LOCAL) ? "local" : "global"); set_vim_var_string(VV_OPTION_NEW, buf_new, -1); set_vim_var_string(VV_OPTION_OLD, buf_old, -1); @@ -4256,10 +4259,12 @@ set_num_option ( options[opt_idx].flags |= P_WAS_SET; if (!starting && errmsg == NULL) { - char_u buf_old[11], buf_new[11], buf_type[7]; - vim_snprintf((char *)buf_old, 10, "%ld", old_value); - vim_snprintf((char *)buf_new, 10, "%ld", value); - vim_snprintf((char *)buf_type, 7, "%s", + char buf_old[NUMBUFLEN]; + char buf_new[NUMBUFLEN]; + char buf_type[7]; + vim_snprintf(buf_old, ARRAY_SIZE(buf_old), "%ld", old_value); + vim_snprintf(buf_new, ARRAY_SIZE(buf_new), "%ld", value); + vim_snprintf(buf_type, ARRAY_SIZE(buf_type), "%s", (opt_flags & OPT_LOCAL) ? "local" : "global"); set_vim_var_string(VV_OPTION_NEW, buf_new, -1); set_vim_var_string(VV_OPTION_OLD, buf_old, -1); From a3a8df835985f77e7bc8d0786c0e3851cfe3dc44 Mon Sep 17 00:00:00 2001 From: watiko Date: Tue, 1 Dec 2015 23:41:03 +0900 Subject: [PATCH 12/15] tests: Improve legacy autocmd_option. * Compare tables instead of strings * Add neovim specific test --- .../functional/legacy/autocmd_option_spec.lua | 325 +++++++++++++----- 1 file changed, 241 insertions(+), 84 deletions(-) diff --git a/test/functional/legacy/autocmd_option_spec.lua b/test/functional/legacy/autocmd_option_spec.lua index 257a4ac5d4..855e9c6271 100644 --- a/test/functional/legacy/autocmd_option_spec.lua +++ b/test/functional/legacy/autocmd_option_spec.lua @@ -1,119 +1,276 @@ local helpers = require('test.functional.helpers') -local clear, nvim, eq = helpers.clear, helpers.nvim, helpers.eq +local nvim = helpers.meths +local clear, eq, neq = helpers.clear, helpers.eq, helpers.neq +local curbuf, buf = helpers.curbuf, helpers.bufmeths local source, execute = helpers.source, helpers.execute +local function declare_hook_function() + source([[ + fu! AutoCommand(match, bufnr, winnr) + let l:acc = { + \ 'option' : a:match, + \ 'oldval' : v:option_old, + \ 'newval' : v:option_new, + \ 'scope' : v:option_type, + \ 'attr' : { + \ 'bufnr' : a:bufnr, + \ 'winnr' : a:winnr, + \ } + \ } + call add(g:ret, l:acc) + endfu + ]]) +end + +local function set_hook(pattern) + execute( + 'au OptionSet ' + .. pattern .. + ' :call AutoCommand(expand(""), bufnr("%"), winnr())' + ) +end + +local function init_var() + execute('let g:ret = []') +end + +local function get_result() + local ret = nvim.get_var('ret') + init_var() + return ret +end + +local function expected_table(option, oldval, newval, scope, attr) + return { + option = option, + oldval = tostring(oldval), + newval = tostring(newval), + scope = scope, + attr = attr, + } +end + +local function expected_combination(...) + local args = {...} + local ret = get_result() + + if not (#args == #ret) then + local expecteds = {} + for _, v in pairs(args) do + table.insert(expecteds, expected_table(unpack(v))) + end + eq(expecteds, ret) + return + end + + for i, v in ipairs(args) do + local attr = v[5] + if not attr then + -- remove attr entries + ret[i].attr = nil + else + -- remove attr entries which are not required + for k in pairs(ret[i].attr) do + if not attr[k] then + ret[i].attr[k] = nil + end + end + end + eq(expected_table(unpack(v)), ret[i]) + end +end + +local function expected_empty() + eq({}, get_result()) +end + +local function make_buffer() + local old_buf = curbuf() + execute('new') + local new_buf = curbuf() + execute('wincmd p') -- move previous window + + neq(old_buf, new_buf) + eq(old_buf, curbuf()) + + return new_buf +end + describe('au OptionSet', function() - describe('with * as ', function() - describe('matches when being set any option', function() - local function expected_str(option, oldval, newval, scope) - return '' - .. string.format('Autocmd Option: <%s>,', option) - .. string.format(' OldVal: <%s>,', oldval) - .. string.format(' NewVal: <%s>,', newval) - .. string.format(' Scope: <%s>', scope) - end + describe('with any opton (*)', function() - local function get_result() - return nvim('get_var', 'ret') - end + before_each(function() + clear() + declare_hook_function() + init_var() + set_hook('*') + end) - local function expected_combination(option, oldval, newval, scope) - eq(expected_str(option, oldval, newval, scope), get_result()) - execute([[let g:ret = '']]) - end + it('should be called in setting number option', function() + execute('set nu') + expected_combination({'number', 0, 1, 'global'}) - local function expected_empty() - eq('', get_result()) - end + execute('setlocal nonu') + expected_combination({'number', 1, 0, 'local'}) - before_each(function() - clear() + execute('setglobal nonu') + expected_combination({'number', 1, 0, 'global'}) + end) - source([[ - fu! AutoCommand(match) - let g:ret.=printf('Autocmd Option: <%s>,', a:match) - let g:ret.=printf(' OldVal: <%s>,', v:option_old) - let g:ret.=printf(' NewVal: <%s>,', v:option_new) - let g:ret.=printf(' Scope: <%s>', v:option_type) - endfu - - au OptionSet * :call AutoCommand(expand("")) - ]]) + it('should be called in setting autoindent option',function() + execute('setlocal ai') + expected_combination({'autoindent', 0, 1, 'local'}) - execute([[let g:ret = '']]) - end) + execute('setglobal ai') + expected_combination({'autoindent', 0, 1, 'global'}) - it('should set number option', function() - execute('set nu') - expected_combination('number', 0, 1, 'global') + execute('set noai') + expected_combination({'autoindent', 1, 0, 'global'}) + end) - execute('setlocal nonu') - expected_combination('number', 1, 0, 'local') + it('should be called in inverting global autoindent option',function() + execute('set ai!') + expected_combination({'autoindent', 0, 1, 'global'}) + end) - execute('setglobal nonu') - expected_combination('number', 1, 0, 'global') - end) + it('should be called in being unset local autoindent option',function() + execute('setlocal ai') + expected_combination({'autoindent', 0, 1, 'local'}) - it('should set autoindent option',function() - execute('setlocal ai') - expected_combination('autoindent', 0, 1, 'local') + execute('setlocal ai<') + expected_combination({'autoindent', 1, 0, 'local'}) + end) - execute('setglobal ai') - expected_combination('autoindent', 0, 1, 'global') + it('should be called in setting global list and number option at the same time',function() + execute('set list nu') + expected_combination( + {'list', 0, 1, 'global'}, + {'number', 0, 1, 'global'} + ) + end) - execute('set noai') - expected_combination('autoindent', 1, 0, 'global') - end) + it('should not print anything, use :noa', function() + execute('noa set nolist nonu') + expected_empty() + end) - it('should invert global autoindent option',function() - execute('set ai!') - expected_combination('autoindent', 0, 1, 'global') - end) + it('should be called in setting local acd', function() + execute('setlocal acd') + expected_combination({'autochdir', 0, 1, 'local'}) + end) - it('should set several global list and number option',function() - execute('set list nu') - eq(expected_str('list', 0, 1, 'global') .. expected_str('number', 0, 1, 'global'), - get_result()) - end) + it('should be called in setting autoread', function() + execute('set noar') + expected_combination({'autoread', 1, 0, 'global'}) - it('should not print anything, use :noa.', function() - execute('noa set nolist nonu') + execute('setlocal ar') + expected_combination({'autoread', 0, 1, 'local'}) + end) + + it('should be called in inverting global autoread', function() + execute('setglobal invar') + expected_combination({'autoread', 1, 0, 'global'}) + end) + + it('should be called in setting backspace option through :let', function() + execute('let &bs=""') + expected_combination({'backspace', 'indent,eol,start', '', 'global'}) + end) + + describe('being set by setbufvar()', function() + it('should not trigger because option name is invalid', function() + execute('call setbufvar(1, "&l:bk", 1)') expected_empty() end) - it('should set global acd', function() - execute('setlocal acd') - expected_combination('autochdir', 0, 1, 'local') + it('should trigger using correct option name', function() + execute('call setbufvar(1, "&backup", 1)') + expected_combination({'backup', 0, 1, 'local'}) end) - it('should set autoread', function() - execute('set noar') - expected_combination('autoread', 1, 0, 'global') + it('should trigger if the current buffer is different from the targetted buffer', function() + local new_buffer = make_buffer() + local new_bufnr = buf.get_number(new_buffer) - execute('setlocal ar') - expected_combination('autoread', 0, 1, 'local') + execute('call setbufvar(' .. new_bufnr .. ', "&buftype", "nofile")') + expected_combination({'buftype', '', 'nofile', 'local', {bufnr = new_bufnr}}) + end) + end) + end) + + describe('with specific option', function() + + before_each(function() + clear() + declare_hook_function() + init_var() + end) + + it('should be called iff setting readonly', function() + set_hook('readonly') + + execute('set nu') + expected_empty() + + execute('setlocal ro') + expected_combination({'readonly', 0, 1, 'local'}) + + execute('setglobal ro') + expected_combination({'readonly', 0, 1, 'global'}) + + execute('set noro') + expected_combination({'readonly', 1, 0, 'global'}) + end) + + describe('being set by setbufvar()', function() + it('should not trigger because option name does not match with backup', function() + set_hook('backup') + + execute('call setbufvar(1, "&l:bk", 1)') + expected_empty() end) - it('should invert global autoread', function() - execute('setglobal invar') - expected_combination('autoread', 1, 0, 'global') + it('should trigger, use correct option name backup', function() + set_hook('backup') + + execute('call setbufvar(1, "&backup", 1)') + expected_combination({'backup', 0, 1, 'local'}) end) - it('should set option backspace through :let', function() - execute('let &bs=""') - expected_combination('backspace', 'indent,eol,start', '', 'global') + it('should trigger if the current buffer is different from the targetted buffer', function() + set_hook('buftype') + + local new_buffer = make_buffer() + local new_bufnr = buf.get_number(new_buffer) + + execute('call setbufvar(' .. new_bufnr .. ', "&buftype", "nofile")') + expected_combination({'buftype', '', 'nofile', 'local', {bufnr = new_bufnr}}) + end) + end) + + describe('being set by neovim api', function() + it('should trigger if a boolean option be set globally', function() + set_hook('autochdir') + + nvim.set_option('autochdir', true) + eq(true, nvim.get_option('autochdir')) + expected_combination({'autochdir', '0', '1', 'global'}) end) - describe('setting option through setbufvar()', function() - it('shouldn\'t trigger because option name is invalid', function() - execute('call setbufvar(1, "&l:bk", 1)') - expected_empty() - end) + it('should trigger if a number option be set globally', function() + set_hook('cmdheight') - it('should trigger, use correct option name.', function() - execute('call setbufvar(1, "&backup", 1)') - expected_combination('backup', 0, 1, 'local') - end) + nvim.set_option('cmdheight', 5) + eq(5, nvim.get_option('cmdheight')) + expected_combination({'cmdheight', 1, 5, 'global'}) + end) + + it('should trigger if a string option be set globally', function() + set_hook('ambiwidth') + + nvim.set_option('ambiwidth', 'double') + eq('double', nvim.get_option('ambiwidth')) + expected_combination({'ambiwidth', 'single', 'double', 'global'}) end) end) end) From 8c684b2fdb58487a40968a2fee996bb70e911d56 Mon Sep 17 00:00:00 2001 From: watiko Date: Sat, 12 Dec 2015 04:17:39 +0900 Subject: [PATCH 13/15] Revert char to char_u --- src/nvim/option.c | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/src/nvim/option.c b/src/nvim/option.c index 878bc33c6d..1db8831941 100644 --- a/src/nvim/option.c +++ b/src/nvim/option.c @@ -1796,8 +1796,8 @@ do_set ( if (errmsg != NULL) goto skip; if (saved_origval != NULL) { - char buf_type[7]; - vim_snprintf(buf_type, ARRAY_SIZE(buf_type), "%s", + char_u buf_type[7]; + vim_snprintf((char *)buf_type, ARRAY_SIZE(buf_type), "%s", (opt_flags & OPT_LOCAL) ? "local" : "global"); set_vim_var_string(VV_OPTION_NEW, *(char_u **)varp, -1); @@ -2383,8 +2383,8 @@ set_string_option ( // call autocommand after handling side effects if (saved_oldval != NULL) { - char buf_type[7]; - vim_snprintf(buf_type, ARRAY_SIZE(buf_type), "%s", + char_u buf_type[7]; + vim_snprintf((char *)buf_type, ARRAY_SIZE(buf_type), "%s", (opt_flags & OPT_LOCAL) ? "local" : "global"); set_vim_var_string(VV_OPTION_NEW, *varp, -1); set_vim_var_string(VV_OPTION_OLD, saved_oldval, -1); @@ -3869,14 +3869,14 @@ set_bool_option ( options[opt_idx].flags |= P_WAS_SET; if (!starting) { - char buf_old[2]; - char buf_new[2]; - char buf_type[7]; - vim_snprintf(buf_old, ARRAY_SIZE(buf_old), "%d", + char_u buf_old[2]; + char_u buf_new[2]; + char_u buf_type[7]; + vim_snprintf((char *)buf_old, ARRAY_SIZE(buf_old), "%d", old_value ? true: false); - vim_snprintf(buf_new, ARRAY_SIZE(buf_new), "%d", + vim_snprintf((char *)buf_new, ARRAY_SIZE(buf_new), "%d", value ? true: false); - vim_snprintf(buf_type, ARRAY_SIZE(buf_type), "%s", + vim_snprintf((char *)buf_type, ARRAY_SIZE(buf_type), "%s", (opt_flags & OPT_LOCAL) ? "local" : "global"); set_vim_var_string(VV_OPTION_NEW, buf_new, -1); set_vim_var_string(VV_OPTION_OLD, buf_old, -1); @@ -4259,12 +4259,12 @@ set_num_option ( options[opt_idx].flags |= P_WAS_SET; if (!starting && errmsg == NULL) { - char buf_old[NUMBUFLEN]; - char buf_new[NUMBUFLEN]; - char buf_type[7]; - vim_snprintf(buf_old, ARRAY_SIZE(buf_old), "%ld", old_value); - vim_snprintf(buf_new, ARRAY_SIZE(buf_new), "%ld", value); - vim_snprintf(buf_type, ARRAY_SIZE(buf_type), "%s", + char_u buf_old[NUMBUFLEN]; + char_u buf_new[NUMBUFLEN]; + char_u buf_type[7]; + vim_snprintf((char *)buf_old, ARRAY_SIZE(buf_old), "%ld", old_value); + vim_snprintf((char *)buf_new, ARRAY_SIZE(buf_new), "%ld", value); + vim_snprintf((char *)buf_type, ARRAY_SIZE(buf_type), "%s", (opt_flags & OPT_LOCAL) ? "local" : "global"); set_vim_var_string(VV_OPTION_NEW, buf_new, -1); set_vim_var_string(VV_OPTION_OLD, buf_old, -1); From 74341ca5cacfcd254aee49fa2ed130ed7ffb5071 Mon Sep 17 00:00:00 2001 From: watiko Date: Sat, 12 Dec 2015 05:28:27 +0900 Subject: [PATCH 14/15] Fix the memory leaking --- src/nvim/option.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/nvim/option.c b/src/nvim/option.c index 1db8831941..ff140bd221 100644 --- a/src/nvim/option.c +++ b/src/nvim/option.c @@ -1792,9 +1792,12 @@ do_set ( errmsg = did_set_string_option(opt_idx, (char_u **)varp, new_value_alloced, oldval, errbuf, opt_flags); - /* If error detected, print the error message. */ - if (errmsg != NULL) + // If error detected, print the error message. + if (errmsg != NULL) { + xfree(saved_origval); goto skip; + } + if (saved_origval != NULL) { char_u buf_type[7]; vim_snprintf((char *)buf_type, ARRAY_SIZE(buf_type), "%s", From 1b56425662068a40cf3a19cd47ac79d7511840b5 Mon Sep 17 00:00:00 2001 From: watiko Date: Sat, 12 Dec 2015 05:58:21 +0900 Subject: [PATCH 15/15] Make clint status valid --- src/nvim/option.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/nvim/option.c b/src/nvim/option.c index ff140bd221..60c415f297 100644 --- a/src/nvim/option.c +++ b/src/nvim/option.c @@ -2380,7 +2380,7 @@ set_string_option ( saved_oldval = vim_strsave(oldval); } - if ((r = did_set_string_option(opt_idx, varp, TRUE, oldval, NULL, + if ((r = did_set_string_option(opt_idx, varp, (int)true, oldval, NULL, opt_flags)) == NULL) did_set_option(opt_idx, opt_flags, TRUE);