diff options
Diffstat (limited to 'vim/plugin/toggle_option_flag.vim')
-rw-r--r-- | vim/plugin/toggle_option_flag.vim | 54 |
1 files changed, 34 insertions, 20 deletions
diff --git a/vim/plugin/toggle_option_flag.vim b/vim/plugin/toggle_option_flag.vim index ad89d080..1d4b11ce 100644 --- a/vim/plugin/toggle_option_flag.vim +++ b/vim/plugin/toggle_option_flag.vim @@ -1,8 +1,6 @@ " -" toggle_option_flag.vim: Provide commands to toggle flags in single-char -" grouped options like 'formatoptions', 'shortmess', 'complete' etc. -" -" This will fail hilariously if you try to set e.g. 'switchbuf' with it! +" toggle_option_flag.vim: Provide commands to toggle flags in grouped options +" like 'formatoptions', 'shortmess', 'complete', 'switchbuf', etc. " " Author: Tom Ryder <tom@sanctum.geek.nz> " License: Same as Vim itself @@ -17,38 +15,54 @@ let g:loaded_toggle_option_flag = 1 " Internal function to do the toggling function! s:Toggle(option, flag, local) - " Check for weird options, we don't want to eval() anything funny - if a:option =~# '[^a-z]' + " Check for weird options, we don't want to :execute anything funny + if a:option =~# '\m\L' echoerr 'Illegal option name' return endif - " Weird flags, too; should be a single inoffensive char - if a:flag !~# '^[a-z0-9.]$' - echoerr 'Illegal flag' - return - endif - " Choose which set command to use let l:set = a:local \ ? 'setlocal' \ : 'set' - " eval() to assign -= or += to l:op for the option toggle + " Horrible :execute to get the option's current setting into a variable " (I couldn't get {curly braces} indirection to work) - let l:op = '' - execute 'let l:op = &' . a:option . ' =~# a:flag ? "-=" : "+="' + let l:current = '' + execute 'let l:current = &' . a:option + + " If the flag we're toggling is longer than one character, this must by + " necessity be a delimited option. I think all of those in VimL are + " comma-separated. Extend the flag and current setting so that they'll still + " match at the start and end. Otherwise, use them as-is. + if strlen(a:flag) > 1 + let l:search_flag = ',' . a:flag . ',' + let l:search_current = ',' . l:current . ',' + else + let l:search_flag = a:flag + let l:search_current = l:current + endif + + " Assign -= or += as the operation to run based on whether the flag already + " appears in the option value or not + let l:operation = stridx(l:search_current, l:search_flag) > -1 + \ ? '-=' + \ : '+=' + + " Build the command strings to set and then show the value + let l:cmd_set = l:set . ' ' . a:option . l:operation . escape(a:flag, '\ ') + let l:cmd_show = l:set . ' ' . a:option . '?' - " eval() to perform the option toggle and then print the value - execute l:set . ' ' . a:option . l:op . a:flag - execute l:set . ' ' . a:option . '?' + " Run the set and show command strings + execute l:cmd_set + execute l:cmd_show endfunction " User commands wrapping around calls to the above function -command! -nargs=+ +command -nargs=+ -complete=option \ ToggleOptionFlag \ call <SID>Toggle(<f-args>, 0) -command! -nargs=+ +command -nargs=+ -complete=option \ ToggleOptionFlagLocal \ call <SID>Toggle(<f-args>, 1) |