How to determine whether an option is boolean?

15 views
Skip to first unread message

lith

unread,
May 5, 2015, 5:21:59 AM5/5/15
to vim...@googlegroups.com
Hi!

Some vim options are boolean, i.e. integers with the possible values 0 and 1. You can, e.g., set `ai` and `noai` or use `let &ai = 1`. Other options are numeric/integer or string values.

type(&option) can only be used to distinguish string from bool/int options. exist('&noai') returns 0, so it cannot be used to distinguish bool from int options. Is there another way to determine whether an option is a bool (an int that takes only the values 0 or 1) or an int?

Regards,
Tom

Christian Brabandt

unread,
May 5, 2015, 5:47:31 AM5/5/15
to vim...@googlegroups.com
Some hacks, I can think of:
- parsing $VIMRUNTIME/doc/options.txt
- try/catch block around :set inv<option>

BTW: What is the problem with treating boolean options like integer?

Best,
Christian

Paul Isambert

unread,
May 5, 2015, 5:48:32 AM5/5/15
to vim...@googlegroups.com
> Some vim options are boolean, i.e. integers with the possible values 0 and 1. You can, e.g., set `ai` and `noai` or use `let &ai = 1`. Other options are numeric/integer or string values.
>
> type(&option) can only be used to distinguish string from bool/int options. exist('&noai') returns 0, so it cannot be used to distinguish bool from int options. Is there another way to determine whether an option is a bool (an int that takes only the values 0 or 1) or an int?

Off my head:

function! IsBoolean (option)
if !exists("&" . a:option)
return 0
endif
let bool = 1
exe "let old = &" . a:option
try
exe "set no" . a:option
catch
let bool = 0
endtry
exe "let &" . a:option . " = " . old
return bool
endfunction

I’m not sure setting and resetting the option is 100% harmless for all
options, though.

Best,
Paul
Reply all
Reply to author
Forward
0 new messages