Hello,
* On Wed, Dec 06, 2006 at 04:11:24PM -0800, Eggum, DavidX S <[EMAIL PROTECTED]>
wrote:
> I've found two tasks that are done in vim scripts routinely and it
> would be nice to streamline them:
>
> Here's the first one: "If some variable isn't set yet, then set it to
> a default value". The code always looks something like this:
>
> if !exists("g:MyScriptSetting")
> let g:MyScriptSetting = "default_value"
> endif
>
> How about adding this:
> [...]
> get({string} [, {default}])
> value from variable {string}. When this variable does not
> exist, return {default}. Return zero when {default} is
> omitted.
Well, I've started to refactor my plugins to isolate a similar similar
function in an autoload plugin. My function also takes another argument:
a list of scopes in which the variable can be defined.
" Function: lh#option#Get({name}, {default} [, {scope}])
" @return b:{name} if it exists, of g:{name} if it exists, or {default}
" otherwise
" The order of the variables checked can be specified through the optional
" argument {scope}
Then, in my script, I systematically call this function, and never
directly use a variable. Indeed, I have no way of knowing if the
variable is b:goo, g:foo, w:foo or even s:foo.
The support of non global options is very important to me as I mainly
maintain ftplugins, and work on the files from various projects that
don't share the same configuration (like different targets used in the
makefiles).
A typical use is:
let l:foo = lh#option#Get('foo', s:foo_default, 'wbg')
Which will return the value of the first variable found among w:foo,
b:foo and g:foo. Or s:foo_default otherwise.
So far, {default} is not optional as I have both integer and string
variables, and I can't easily tell the exact type of the variable.
Having such function as builtin would be nice. I can easily cope without
it. But still, it would be nice.
--
Luc Hermitte
http://hermitte.free.fr/vim/