Tuesday, May 5, 2015

Re: How to determine whether an option is boolean?

> 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

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment