diff options
author | Tom Ryder <tom@sanctum.geek.nz> | 2017-11-06 12:55:17 +1300 |
---|---|---|
committer | Tom Ryder <tom@sanctum.geek.nz> | 2017-11-06 12:55:17 +1300 |
commit | d878f7359209a6f5bdb30d77086219fcb4b0f388 (patch) | |
tree | b8db4f6519a8bdfce3cc72a03f4559d9bec637f8 /vim/plugin/toggle_option_flag.vim | |
parent | Merge branch 'hotfix/v0.9.1' (diff) | |
parent | Bump version number to 0.10.0 (diff) | |
download | dotfiles-d878f7359209a6f5bdb30d77086219fcb4b0f388.tar.gz dotfiles-d878f7359209a6f5bdb30d77086219fcb4b0f388.zip |
Merge branch 'release/v0.10.0'v0.10.0
* release/v0.10.0:
Bump version number to 0.10.0
Correct a comment
Use stridx() instead of very-nomagic regex match
Use strlen() instead of len()
Escape option value assign correctly
Extend toggle_option_flag.vim for string flags
Rename l:op to l:operation for clarity
Separate command building from execution in toggle
Use idiomatic VimL regex for param validation
Restore some judicious \m\C hedging in Vim
Remove redundant has('eval') VimL test
Caution about :execute not eval() in VimL comments
Move pipes from end to start of continued lines
Remove \m\C prefix from inapplicable VimL regexes
Don't use VimL ==# for number comparisons
Don't overwrite plugin-specified user commands
Make background detection return not set value
Complete ToggleOptionFlag commands with opt names
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) |