aboutsummaryrefslogtreecommitdiff
path: root/vim/plugin/toggle_option_flag.vim
diff options
context:
space:
mode:
authorTom Ryder <tom@sanctum.geek.nz>2017-11-06 11:51:07 +1300
committerTom Ryder <tom@sanctum.geek.nz>2017-11-06 11:53:06 +1300
commit57b34a5645b5d0325426dbb60b8d3f80ff6bfe7a (patch)
tree93e4e5968da966e034b0720bf95bf97771589440 /vim/plugin/toggle_option_flag.vim
parentRename l:op to l:operation for clarity (diff)
downloaddotfiles-57b34a5645b5d0325426dbb60b8d3f80ff6bfe7a.tar.gz
dotfiles-57b34a5645b5d0325426dbb60b8d3f80ff6bfe7a.zip
Extend toggle_option_flag.vim for string flags
This commit extends toggle_option_flag.vim to allow the exported commands to toggle values of more than one character, for comma-separated options like 'switchbuf', e.g.: :ToggleOptionFlag switchbuf useopen
Diffstat (limited to 'vim/plugin/toggle_option_flag.vim')
-rw-r--r--vim/plugin/toggle_option_flag.vim35
1 files changed, 22 insertions, 13 deletions
diff --git a/vim/plugin/toggle_option_flag.vim b/vim/plugin/toggle_option_flag.vim
index dd7c95f7..68db2703 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
@@ -23,21 +21,32 @@ function! s:Toggle(option, flag, local)
return
endif
- " Weird flags, too; should be a single inoffensive char
- if a:flag !~# '\m^[\a.]$'
- echoerr 'Illegal flag'
- return
- endif
-
" Choose which set command to use
let l:set = a:local
\ ? 'setlocal'
\ : 'set'
- " :execute to assign -= or += to l:op for the option toggle
+ " Make a flag pattern to allow us to search for the literal string with no
+ " regular expression devilry at all
+ let l:flag_pattern = escape(a:flag, '\')
+
+ " Horrible :execute to get the option's current current into a variable
" (I couldn't get {curly braces} indirection to work)
- let l:operation = ''
- execute 'let l:operation = &' . 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 pattern and current setting so that they'll
+ " still match at the start and end.
+ if len(a:flag) > 1
+ let l:flag_pattern = ',' . l:flag_pattern . ','
+ let l: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 = l:current =~# '\V\C' . l:flag_pattern ? '-=' : '+='
" Build the command strings to set and then show the value
let l:cmd_set = l:set . ' ' . a:option . l:operation . a:flag