diff options
-rw-r--r-- | runtime/doc/autocmd.txt | 19 | ||||
-rw-r--r-- | runtime/doc/eval.txt | 9 | ||||
-rw-r--r-- | src/nvim/auevents.lua | 1 | ||||
-rw-r--r-- | src/nvim/eval.c | 14 | ||||
-rw-r--r-- | src/nvim/eval.h | 3 | ||||
-rw-r--r-- | src/nvim/fileio.c | 3 | ||||
-rw-r--r-- | src/nvim/option.c | 68 | ||||
-rw-r--r-- | src/nvim/testdir/Makefile | 1 | ||||
-rw-r--r-- | src/nvim/testdir/test_autocmd_option.in | 68 | ||||
-rw-r--r-- | src/nvim/testdir/test_autocmd_option.ok | 129 | ||||
-rw-r--r-- | src/nvim/version.c | 2 |
11 files changed, 310 insertions, 7 deletions
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 |<amatch>| + 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 <g> or <z>, 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("<amatch>")) +: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("<amatch>")) +: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: <number>, Oldval: <0>, NewVal: <1>, Scope: <global> +Autocmd Option: <number>, OldVal: <0>, NewVal: <1>, Scope: <global> + +2: Setting local number option +Expected: Name: <number>, Oldval: <1>, NewVal: <0>, Scope: <local> +Autocmd Option: <number>, OldVal: <1>, NewVal: <0>, Scope: <local> + +3: Setting global number option +Expected: Name: <number>, Oldval: <1>, NewVal: <0>, Scope: <global> +Autocmd Option: <number>, OldVal: <1>, NewVal: <0>, Scope: <global> + +4: Setting local autoindent option +Expected: Name: <autoindent>, Oldval: <1>, NewVal: <0>, Scope: <local> +Autocmd Option: <autoindent>, OldVal: <1>, NewVal: <0>, Scope: <local> + +5: Setting global autoindent option +Expected: Name: <autoindent>, Oldval: <1>, NewVal: <0>, Scope: <global> +Autocmd Option: <autoindent>, OldVal: <1>, NewVal: <0>, Scope: <global> + +6: Setting global autoindent option +Expected: Name: <autoindent>, Oldval: <0>, NewVal: <1>, Scope: <global> +Autocmd Option: <autoindent>, OldVal: <0>, NewVal: <1>, Scope: <global> + +7: Setting several global list and number option +Expected: Name: <list>, Oldval: <0>, NewVal: <1>, Scope: <global> +Autocmd Option: <list>, OldVal: <0>, NewVal: <1>, Scope: <global> + +7: Setting several global list and number option +Expected: Name: <number>, Oldval: <0>, NewVal: <1>, Scope: <global> +Autocmd Option: <number>, OldVal: <0>, NewVal: <1>, Scope: <global> + +8: Setting global acd +Expected: Name: <autochdir>, Oldval: <0>, NewVal: <1>, Scope: <global> +Autocmd Option: <autochdir>, OldVal: <0>, NewVal: <1>, Scope: <local> + +9: Setting global autoread +Expected: Name: <autoread>, Oldval: <1>, NewVal: <0>, Scope: <global> +Autocmd Option: <autoread>, OldVal: <1>, NewVal: <0>, Scope: <global> + +10: Setting local autoread +Expected: Name: <autoread>, Oldval: <1>, NewVal: <0>, Scope: <local> +Autocmd Option: <autoread>, OldVal: <1>, NewVal: <0>, Scope: <local> + +11: Setting global autoread +Expected: Name: <autoread>, Oldval: <0>, NewVal: <1>, Scope: <global> +Autocmd Option: <autoread>, OldVal: <0>, NewVal: <1>, Scope: <global> + +12: Setting option backspace through :let +Expected: Name: <backspace>, Oldval: <indent,eol,start>, NewVal: <>, Scope: <global> +Autocmd Option: <backspace>, OldVal: <indent,eol,start>, NewVal: <>, Scope: <global> + +13: Setting option backspace through setbufvar() +Expected: Name: <backup>, Oldval: <>, NewVal: <1>, Scope: <local> +Autocmd Option: <backup>, OldVal: <0>, NewVal: <1>, Scope: <local> 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 |