m (Flagged as duplicate + added to Options category.)
(Change to TipImported template + severe manual clean)
Line 1: Line 1:
 
{{duplicate|920}}
 
{{duplicate|920}}
 
{{review}}
 
{{review}}
  +
{{TipImported
{{Tip
 
 
|id=472
 
|id=472
  +
|previous=471
|title=Handy option flag toggler
 
  +
|next=473
|created=May 12, 2003 9:52
+
|created=May 12, 2003
 
|complexity=basic
 
|complexity=basic
|author=robin at isometry dot net
+
|author=robin
 
|version=6.0
 
|version=6.0
 
|rating=14/5
 
|rating=14/5
 
}}
|text=
 
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>
 
<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>
 
</pre>
  +
Examples of use:
+
Examples of use:
 
<pre>
 
<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>
 
</pre>
   
 
==Comments==
 
The following might be more flexible (I think it should work for any flag-style option).
   
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.
 
}}
 
 
== Comments ==
 
Hmmm. The following might be more flexible (I think it should work for any flag-style option.
 
<pre>
 
function ToggleFlag(option,flag)
 
exec ('let tf_old = &amp;' . a:option)
 
exec ('set ' . a:option . '-=' . a:flag)
 
exec ('let tf_new = &amp;' . a:option)
 
if (tf_o == tf_t)
 
exec ('set ' . a:option . '+=' . a:flag)
 
endif
 
endfunction
 
</pre>
 
And here's another for valued rather than flag options, such as foldcolumn:
 
<pre>
 
function CycleNum(option,min,inc,max)
 
exec ('let tz_value = ((&amp;'.a:option.'+'.a:inc.'-'.a:min.')%('.a:max.'+'.a:inc.'))+'.a:min)
 
if (tz_value &lt; a:min) " in case inc&lt;0
 
let tz_value = tz_value+a:max
 
endif
 
exec ('set '.a:option.'='.tz_value)
 
endfunction
 
</pre>
 
e.g.
 
<pre>
 
map &lt;silent&gt; &lt;F6&gt; :call CycleNum("foldcolumn",0,2,4)&lt;CR&gt;
 
</pre>
 
robin at isometry dot net
 
, May 12, 2003 17:53
 
----
 
Here are my revised functions, and mappings:
 
 
<pre>
 
<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)
exec ('let tz_value = (((&amp;'.a:option.'-'.a:min.')+'.a:inc.')%(('.a:max.'-'.a:min.')+'.a:inc.'))+'.a:min)
+
exec ('let tz_value = (((&amp;'.a:option.'-'.a:min.')+'.a:inc.')%(('.a:max.'-'.a:min.')+'.a:inc.'))+'.a:min)
if (tz_value &lt; a:min) " in case inc&lt;0
+
if (tz_value &lt; a:min) " in case inc&lt;0
let tz_value = tz_value+a:max
+
let tz_value = tz_value+a:max
  +
endif
endif
 
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)
exec ('let tf_o = &amp;'.a:option)
+
exec ('let tf_o = &amp;'.a:option)
exec ('setlocal '.a:option.'-='.a:flag)
+
exec ('setlocal '.a:option.'-='.a:flag)
exec ('let tf_t = &amp;'.a:option)
+
exec ('let tf_t = &amp;'.a:option)
if (tf_o == tf_t)
+
if (tf_o == tf_t)
exec ('setlocal '.a:option.'+='.a:flag)
+
exec ('setlocal '.a:option.'+='.a:flag)
  +
endif
endif
 
endfunction
+
endfunction
   
" Toggle folding column
+
" Toggle folding column
noremap &lt;silent&gt; &lt;F7&gt; :call CycleNum("foldcolumn",0,2,6)&lt;BAR&gt;set foldcolumn?&lt;CR&gt;
+
noremap &lt;silent&gt; &lt;F7&gt; :call CycleNum("foldcolumn",0,2,6)&lt;BAR&gt;set foldcolumn?&lt;CR&gt;
imap &lt;F7&gt; &lt;C-O&gt;&lt;F7&gt;
+
imap &lt;F7&gt; &lt;C-O&gt;&lt;F7&gt;
   
" Toggle window appearance
+
" Toggle window appearance
noremap &lt;silent&gt; &lt;F8&gt; :call ToggleFlag("guioptions","m")&lt;BAR&gt;set guioptions?&lt;CR&gt;
+
noremap &lt;silent&gt; &lt;F8&gt; :call ToggleFlag("guioptions","m")&lt;BAR&gt;set guioptions?&lt;CR&gt;
imap &lt;F8&gt; &lt;C-O&gt;&lt;F8&gt;
+
imap &lt;F8&gt; &lt;C-O&gt;&lt;F8&gt;
noremap &lt;silent&gt; &lt;F9&gt; :call ToggleFlag("guioptions","T")&lt;BAR&gt;set guioptions?&lt;CR&gt;
+
noremap &lt;silent&gt; &lt;F9&gt; :call ToggleFlag("guioptions","T")&lt;BAR&gt;set guioptions?&lt;CR&gt;
imap &lt;F9&gt; &lt;C-O&gt;&lt;F9&gt;
+
imap &lt;F9&gt; &lt;C-O&gt;&lt;F9&gt;
   
" Cycle tabstop
+
" Cycle tabstop
noremap &lt;silent&gt; &lt;M-t&gt;s :call CycleNum("tabstop",4,4,8)&lt;BAR&gt;set tabstop?&lt;CR&gt;
+
noremap &lt;silent&gt; &lt;M-t&gt;s :call CycleNum("tabstop",4,4,8)&lt;BAR&gt;set tabstop?&lt;CR&gt;
" 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>
 
</pre>
robin at isometry dot net
 
, May 16, 2003 1:10
 
----
 
<!-- parsed by vimtips.py in 0.503925 seconds-->
 
   
 
----
 
[[Category:Options]]
 
[[Category:Options]]

Revision as of 06:38, 4 November 2007

Duplicate tip

This tip is very similar to the following:

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

Tip 472 Printable Monobook Previous Next

created May 12, 2003 · complexity basic · author robin · version 6.0


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>

Comments

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

" 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>

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