diff options
author | Tom Ryder <tom@sanctum.geek.nz> | 2017-11-06 19:35:42 +1300 |
---|---|---|
committer | Tom Ryder <tom@sanctum.geek.nz> | 2017-11-06 19:35:42 +1300 |
commit | 2cdfb6b52afb3011ebfcf83db7214bccfcf8777a (patch) | |
tree | 768278f5de19997904c72259629b438e6b9d9217 | |
parent | Merge branch 'release/v0.10.0' into develop (diff) | |
parent | Simplify 'formatoptions' config (diff) | |
download | dotfiles-2cdfb6b52afb3011ebfcf83db7214bccfcf8777a.tar.gz dotfiles-2cdfb6b52afb3011ebfcf83db7214bccfcf8777a.zip |
Merge branch 'feature/vim-togglef...' into develop
* feature/vim-toggleflag-helpers:
Simplify 'formatoptions' config
Refactor toggle_option_flag.vim
-rw-r--r-- | vim/config/format.vim | 73 | ||||
-rw-r--r-- | vim/plugin/toggle_option_flag.vim | 81 |
2 files changed, 76 insertions, 78 deletions
diff --git a/vim/config/format.vim b/vim/config/format.vim index 1f24ee56..e266f42a 100644 --- a/vim/config/format.vim +++ b/vim/config/format.vim @@ -1,65 +1,28 @@ -" All of this variable logic requires 'eval', and I can't just short-circuit -" it due to a quirk in the way vim-tiny evaluates these expressions -if has('eval') +" Try to set the 'j' flag for 'formatoptions', to automatically delete comment +" leaders when joining lines +silent! set formatoptions+=j - " Figure out if we have the 'j' flag for 'formatoptions', to automatically - " delete comment leaders when joining lines; keep it in a script variable - let s:formatoptions_has_j = v:version > 703 - \ || v:version == 703 && has('patch541') +" Use toggle_option_flag.vim plugin to bind quick toggle actions for some +" 'formatoptions' flags +if has('user_commands') - " If we do have 'j', default to setting it - if s:formatoptions_has_j - set formatoptions+=j - endif - - " - " Use toggle_option_flag.vim plugin to bind quick toggle actions for some - " 'formatoptions' flags; both of the above, plus: - " - " c - Automatically wrap comments at 'textwidth' (which I allow the filetypes - " to set for me) - " t - Automatically wrap text at 'textwidth' (as above) - " - " We need user-defined commands to do this. - " - if !has('user_commands') - finish - endif + " a: Reformat paragraphs to 'textwidth' on all insert or delete operations + nnoremap <silent> + \ <Leader>a + \ :<C-U>ToggleOptionFlagLocal formatoptions a<CR> - " 'c' and 't' have both been around since at least 6.1 + " c: Reformat comments to 'textwidth' nnoremap <silent> \ <Leader>c \ :<C-U>ToggleOptionFlagLocal formatoptions c<CR> + + " j: Delete comment leaders when joining lines + nnoremap <silent> + \ <Leader>j + \ :<C-U>ToggleOptionFlagLocal formatoptions j<CR> + + " t: Reformat non-comment text to 'textwidth' nnoremap <silent> \ <Leader>t \ :<C-U>ToggleOptionFlagLocal formatoptions t<CR> - - " Figure out if we have the 'a' flag for 'formatoptions', to reapply - " 'textwidth' wrapping to the current paragraph on every insertion or - " deletion; keep in a script variable - let s:formatoptions_has_a = v:version > 610 - \ || v:version == 610 && has('patch142') - - " 'a' needs testing - if s:formatoptions_has_a - nnoremap <silent> - \ <Leader>a - \ :<C-U>ToggleOptionFlagLocal formatoptions a<CR> - else - nnoremap <silent> - \ <Leader>a - \ :<C-U>echoerr 'No formatoptions a-flag'<CR> - endif - - " 'j' needs testing - if s:formatoptions_has_j - nnoremap <silent> - \ <Leader>j - \ :<C-U>ToggleOptionFlagLocal formatoptions j<CR> - else - nnoremap <silent> - \ <Leader>j - \ :<C-U>echoerr 'No formatoptions j-flag'<CR> - endif - endif diff --git a/vim/plugin/toggle_option_flag.vim b/vim/plugin/toggle_option_flag.vim index 1d4b11ce..dcc26cce 100644 --- a/vim/plugin/toggle_option_flag.vim +++ b/vim/plugin/toggle_option_flag.vim @@ -12,19 +12,16 @@ if exists('g:loaded_toggle_option_flag') endif 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 :execute anything funny - if a:option =~# '\m\L' - echoerr 'Illegal option name' - return - endif +" Show an error-highlighted message and beep, but without a real :echoerr +function! s:Error(message) + execute 'normal! \<Esc>' + echohl ErrorMsg + echomsg a:message + echohl None +endfunction - " Choose which set command to use - let l:set = a:local - \ ? 'setlocal' - \ : 'set' +" Test whether an option currently has a flag as part of its value +function! s:Has(option, flag) " Horrible :execute to get the option's current setting into a variable " (I couldn't get {curly braces} indirection to work) @@ -33,29 +30,67 @@ 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 flag and current setting so that they'll still - " match at the start and end. Otherwise, use them as-is. + " comma-separated. Extend the flag and value 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 . ',' + let l:search_value = ',' . l:current . ',' else let l:search_flag = a:flag - let l:search_current = l:current + let l:search_value = l:current endif + " Return whether + return stridx(l:search_value, l:search_flag) > -1 + +endfunction + +" Internal function to do the toggling +function! s:Toggle(option, flag, local) + + " Check for spurious option strings, we don't want to :execute anything funny + if a:option =~# '\m\L' + call s:Error('Illegal option name') + return 0 + endif + + " Check the option actually exists + if !exists('&' . a:option) + call s:Error('No such option: ' . a:option) + return 0 + endif + + " Choose which set command to use + let l:set = a:local + \ ? 'setlocal' + \ : 'set' + + " Find whether the flag is set before the change + let l:before = s:Has(a:option, a:flag) + " 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 + let l:operation = l:before \ ? '-=' \ : '+=' - " 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 . '?' + " Try to set the option; suppress errors, we'll check our work + silent! execute l:set + \ . ' ' + \ . a:option . l:operation . escape(a:flag, '\ ') + + " Find whether the flag is set after the change + let l:after = s:Has(a:option, a:flag) + + " If we made a difference, report the new value; if we didn't, admit it + if l:before != l:after + execute l:set . ' ' . a:option . '?' + else + call s:Error('Unable to toggle '.a:option.' flag '.a:flag) + endif - " Run the set and show command strings - execute l:cmd_set - execute l:cmd_show + " Return value is whether we made a change + return l:before != l:after endfunction |