From 28b65e3893f4b822de4a80f21ab75c59a21272b5 Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Sat, 4 Nov 2017 03:55:34 +1300 Subject: Spin 'fo' toggle out into new flag toggler plugin This is an experimental new plugin that provides a command to toggle individual single-character flags in an option with a value of a set of such flags, in my case 'formatoptions'. A fair bit of evil eval()ing via :execute here, but I've tried to control it with some strict patern matching. --- vim/config/format.vim | 35 +++++++---------------------- vim/plugin/option_flags.vim | 54 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 27 deletions(-) create mode 100644 vim/plugin/option_flags.vim (limited to 'vim') diff --git a/vim/config/format.vim b/vim/config/format.vim index 35245d0d..ceff2be1 100644 --- a/vim/config/format.vim +++ b/vim/config/format.vim @@ -5,8 +5,8 @@ if v:version > 703 || v:version ==# 703 && has('patch541') endif " -" Quick way to toggle flags in 'formatoptions' that I often want to change; -" specifically: +" Use option_flags.vim plugin to bind quick toggle actions for some +" 'formatoptions' flags: " " a - Automatically format paragraphs, reapplying the wrap on every text " insertion or deletion; sometimes I want this and sometimes I @@ -16,30 +16,11 @@ endif " to set for me) " t - Automatically wrap text at 'textwidth' (as above) " -" So I just have to type e.g. \a to toggle the auto-format flag on and off; -" very handy -" if has('eval') - - " Declare function - function! s:ToggleFormatFlag(flag) - - " Decide on whether we're adding or removing the flag - if &l:formatoptions =~# a:flag - let l:command = 'setlocal formatoptions-=' . a:flag - else - let l:command = 'setlocal formatoptions+=' . a:flag - endif - - " Execute the command we determined and show the result - silent! execute l:command - setlocal formatoptions? - - endfunction - - " Map leader-letters to corresponding format option flags - nnoremap a :call ToggleFormatFlag('a') - nnoremap c :call ToggleFormatFlag('c') - nnoremap t :call ToggleFormatFlag('t') - + nnoremap a + \ :call option_flags#toggle_local('formatoptions', 'a') + nnoremap c + \ :call option_flags#toggle_local('formatoptions', 'c') + nnoremap t + \ :call option_flags#toggle_local('formatoptions', 't') endif diff --git a/vim/plugin/option_flags.vim b/vim/plugin/option_flags.vim new file mode 100644 index 00000000..10739149 --- /dev/null +++ b/vim/plugin/option_flags.vim @@ -0,0 +1,54 @@ +" +" option_flags.vim: Provide functions 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! +" +" Author: Tom Ryder +" License: Same as Vim itself +" +if has('eval') + + " Public wrapper function to toggle a flag with 'set' + function! option_flags#toggle(option, flag) + call s:toggle(a:option, a:flag, 0) + endfunction + + " Public wrapper function to toggle a flag with 'setlocal' + function! option_flags#toggle_local(option, flag) + call s:toggle(a:option, a:flag, 1) + endfunction + + " 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]' + 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 + if a:local + let l:set = 'setlocal ' + else + let l:set = 'set ' + endif + + " Use eval() to assign -= or += to l:op for the option toggle + " (I couldn't get {curly braces} indirection to work) + let l:op = '' + execute 'let l:op = &'.a:option.' =~# a:flag ? "-=" : "+="' + + " Use eval() to perform the option toggle and then print the value + execute l:set . ' ' . a:option . l:op . a:flag . ' ' . a:option . '?' + + endfunction + +endif -- cgit v1.2.3