m (Flagged as duplicate + added to Options category.)
Line 1: Line 1:
  +
{{duplicate|920}}
 
{{review}}
 
{{review}}
 
{{Tip
 
{{Tip
Line 11: Line 12:
 
Here's a little function I put together to make some of my mappings easier to read, understand and change.
 
Here's a little function I put together to make some of my mappings easier to read, understand and change.
   
  +
<pre>
 
 
 
function ToggleFlag(option,flag)
 
function ToggleFlag(option,flag)
 
 
exec ('let lopt = &amp;' . a:option)
 
exec ('let lopt = &amp;' . a:option)
 
 
if lopt =~ (".*" . a:flag . ".*")
 
if lopt =~ (".*" . a:flag . ".*")
 
exec ('set ' . a:option . '-=' . a:flag)
 
exec ('set ' . a:option . '-=' . a:flag)
 
 
 
else
 
else
 
exec ('set ' . a:option . '+=' . a:flag)
 
exec ('set ' . a:option . '+=' . a:flag)
 
 
 
endif
 
endif
 
 
endfunction
 
endfunction
  +
</pre>
 
 
 
 
Examples of use:
 
Examples of use:
  +
<pre>
 
 
 
 
map &lt;silent&gt; &lt;F8&gt; :call ToggleFlag("guioptions","m")&lt;CR&gt;
 
map &lt;silent&gt; &lt;F8&gt; :call ToggleFlag("guioptions","m")&lt;CR&gt;
 
 
map &lt;silent&gt; &lt;F9&gt; :call ToggleFlag("guioptions","T")&lt;CR&gt;
 
map &lt;silent&gt; &lt;F9&gt; :call ToggleFlag("guioptions","T")&lt;CR&gt;
  +
</pre>
 
   
   
 
Can anyone see anyway to improve it?
 
Can anyone see anyway to improve it?
 
* e.g. remove the leading exec... "if &amp;{a:option}..." doesn't work.
 
 
* e.g. a regex match doesn't seem the cleanest of checks, though I prefer it to setting a variable for each possible flag.
e.g. remove the leading exec... "if &amp;{a:option}..." doesn't work.
 
 
e.g. a regex match doesn't seem the cleanest of checks, though I prefer it to setting a variable for each possible flag.
 
 
}}
 
}}
   
 
== Comments ==
 
== Comments ==
 
Hmmm. The following might be more flexible (I think it should work for any flag-style option.
 
Hmmm. The following might be more flexible (I think it should work for any flag-style option.
  +
<pre>
 
 
function ToggleFlag(option,flag)
 
function ToggleFlag(option,flag)
 
exec ('let tf_old = &amp;' . a:option)
 
exec ('let tf_old = &amp;' . a:option)
Line 56: Line 42:
 
exec ('let tf_new = &amp;' . a:option)
 
exec ('let tf_new = &amp;' . a:option)
 
if (tf_o == tf_t)
 
if (tf_o == tf_t)
exec ('set ' . a:option . '+=' . a:flag)
+
exec ('set ' . a:option . '+=' . a:flag)
 
endif
 
endif
 
endfunction
 
endfunction
  +
</pre>
 
 
And here's another for valued rather than flag options, such as foldcolumn:
 
And here's another for valued rather than flag options, such as foldcolumn:
  +
<pre>
 
 
function CycleNum(option,min,inc,max)
 
function CycleNum(option,min,inc,max)
 
exec ('let tz_value = ((&amp;'.a:option.'+'.a:inc.'-'.a:min.')%('.a:max.'+'.a:inc.'))+'.a:min)
 
exec ('let tz_value = ((&amp;'.a:option.'+'.a:inc.'-'.a:min.')%('.a:max.'+'.a:inc.'))+'.a:min)
Line 69: Line 55:
 
exec ('set '.a:option.'='.tz_value)
 
exec ('set '.a:option.'='.tz_value)
 
endfunction
 
endfunction
  +
</pre>
 
 
e.g.
 
e.g.
  +
<pre>
 
map &lt;silent&gt; &lt;F6&gt; :call CycleNum("foldcolumn",0,2,4)&lt;CR&gt;
 
map &lt;silent&gt; &lt;F6&gt; :call CycleNum("foldcolumn",0,2,4)&lt;CR&gt;
  +
</pre>
 
 
robin at isometry dot net
 
robin at isometry dot net
 
, May 12, 2003 17:53
 
, May 12, 2003 17:53
 
----
 
----
 
Here are my revised functions, and mappings:
 
Here are my revised functions, and mappings:
  +
<pre>
 
 
" my function to cycle a numeric option
 
" my function to cycle a numeric option
 
function CycleNum(option,min,inc,max)
 
function CycleNum(option,min,inc,max)
Line 86: Line 73:
 
exec ('setlocal '.a:option.'='.tz_value)
 
exec ('setlocal '.a:option.'='.tz_value)
 
endfunction
 
endfunction
  +
 
" my function to toggle an option flag
 
" my function to toggle an option flag
 
function ToggleFlag(option,flag)
 
function ToggleFlag(option,flag)
Line 110: Line 98:
 
" Cycle shiftwidth
 
" Cycle shiftwidth
 
noremap &lt;silent&gt; &lt;M-t&gt;w :call CycleNum("shiftwidth",4,4,8)&lt;BAR&gt;set shiftwidth?&lt;CR&gt;
 
noremap &lt;silent&gt; &lt;M-t&gt;w :call CycleNum("shiftwidth",4,4,8)&lt;BAR&gt;set shiftwidth?&lt;CR&gt;
  +
</pre>
 
 
robin at isometry dot net
 
robin at isometry dot net
 
, May 16, 2003 1:10
 
, May 16, 2003 1:10
 
----
 
----
 
<!-- parsed by vimtips.py in 0.503925 seconds-->
 
<!-- parsed by vimtips.py in 0.503925 seconds-->
  +
  +
[[Category:Options]]

Revision as of 12:20, 12 September 2007

Duplicate tip

This tip is very similar to the following:

These tips need to be merged – see the merge guidelines.

Previous TipNext Tip

Tip: #472 - Handy option flag toggler

Created: May 12, 2003 9:52 Complexity: basic Author: robin at isometry dot net Version: 6.0 Karma: 14/5 Imported from: Tip#472

Here's a little function I put together to make some of my mappings easier to read, understand and change.

function ToggleFlag(option,flag) 
 exec ('let lopt = &' . a:option) 
 if lopt =~ (".*" . a:flag . ".*") 
   exec ('set ' . a:option . '-=' . a:flag) 
 else 
   exec ('set ' . a:option . '+=' . a:flag) 
 endif 
endfunction 

Examples of use:

map <silent> <F8> :call ToggleFlag("guioptions","m")<CR> 
map <silent> <F9> :call ToggleFlag("guioptions","T")<CR> 


Can anyone see anyway to improve it?

  • e.g. remove the leading exec... "if &{a:option}..." doesn't work.
  • e.g. a regex match doesn't seem the cleanest of checks, though I prefer it to setting a variable for each possible flag.

Comments

Hmmm. The following might be more flexible (I think it should work for any flag-style option.

function ToggleFlag(option,flag) 
 exec ('let tf_old = &' . a:option) 
 exec ('set ' . a:option . '-=' . a:flag) 
 exec ('let tf_new = &' . a:option) 
 if (tf_o == tf_t) 
   exec ('set ' . a:option . '+=' . a:flag) 
 endif 
endfunction 

And here's another for valued rather than flag options, such as foldcolumn:

function CycleNum(option,min,inc,max) 
 exec ('let tz_value = ((&'.a:option.'+'.a:inc.'-'.a:min.')%('.a:max.'+'.a:inc.'))+'.a:min) 
 if (tz_value < a:min) " in case inc<0 
 let tz_value = tz_value+a:max 
 endif 
 exec ('set '.a:option.'='.tz_value) 
endfunction 

e.g.

map <silent> <F6> :call CycleNum("foldcolumn",0,2,4)<CR>

robin at isometry dot net , May 12, 2003 17:53


Here are my revised functions, and mappings:

" my function to cycle a numeric option 
function CycleNum(option,min,inc,max) 
 exec ('let tz_value = (((&'.a:option.'-'.a:min.')+'.a:inc.')%(('.a:max.'-'.a:min.')+'.a:inc.'))+'.a:min) 
 if (tz_value < a:min) " in case inc<0 
 let tz_value = tz_value+a:max 
 endif 
 exec ('setlocal '.a:option.'='.tz_value) 
endfunction 

" my function to toggle an option flag 
function ToggleFlag(option,flag) 
 exec ('let tf_o = &'.a:option) 
 exec ('setlocal '.a:option.'-='.a:flag) 
 exec ('let tf_t = &'.a:option) 
 if (tf_o == tf_t) 
 exec ('setlocal '.a:option.'+='.a:flag) 
 endif 
endfunction 

" Toggle folding column 
noremap <silent> <F7> :call CycleNum("foldcolumn",0,2,6)<BAR>set foldcolumn?<CR> 
imap <F7> <C-O><F7> 

" Toggle window appearance 
noremap <silent> <F8> :call ToggleFlag("guioptions","m")<BAR>set guioptions?<CR> 
imap <F8> <C-O><F8> 
noremap <silent> <F9> :call ToggleFlag("guioptions","T")<BAR>set guioptions?<CR> 
imap <F9> <C-O><F9> 

" Cycle tabstop 
noremap <silent> <M-t>s :call CycleNum("tabstop",4,4,8)<BAR>set tabstop?<CR> 
" Cycle shiftwidth 
noremap <silent> <M-t>w :call CycleNum("shiftwidth",4,4,8)<BAR>set shiftwidth?<CR>

robin at isometry dot net , May 16, 2003 1:10


Community content is available under CC-BY-SA unless otherwise noted.