From eecdc7ea6b7bd1f7c7d4b94f4e05775a95306c3e Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Mon, 6 Nov 2017 12:49:52 +1300 Subject: Use stridx() instead of very-nomagic regex match I couldn't find this function at first, but it's what I need: a way to check whether a string appears in another one. --- vim/plugin/toggle_option_flag.vim | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) (limited to 'vim/plugin') diff --git a/vim/plugin/toggle_option_flag.vim b/vim/plugin/toggle_option_flag.vim index da9a6110..ea7064d4 100644 --- a/vim/plugin/toggle_option_flag.vim +++ b/vim/plugin/toggle_option_flag.vim @@ -26,10 +26,6 @@ function! s:Toggle(option, flag, local) \ ? 'setlocal' \ : 'set' - " 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:current = '' @@ -37,16 +33,21 @@ function! s:Toggle(option, flag, local) " 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. + " 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:flag_pattern = ',' . l:flag_pattern . ',' - let l:current = ',' . l:current . ',' + 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 = l:current =~# '\V\C' . l:flag_pattern ? '-=' : '+=' + 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, '\ ') -- cgit v1.2.3