1
0
Fork 0
mirror of synced 2024-11-15 13:38:58 -05:00
ultimate-vim/sources_non_forked/tlib/autoload/tlib/var.vim

84 lines
2.4 KiB
VimL
Raw Normal View History

2012-08-16 23:41:25 -04:00
" @Author: Tom Link (micathom AT gmail com?subject=[vim])
" @Website: http://www.vim.org/account/profile.php?user_id=4037
" @License: GPL (see http://www.gnu.org/licenses/gpl.txt)
2015-12-08 08:20:04 -05:00
" @Revision: 30
2012-08-16 23:41:25 -04:00
" Define a variable called NAME if yet undefined.
" You can also use the :TLLet command.
"
" EXAMPLES: >
" exec tlib#var#Let('g:foo', 1)
" TLet g:foo = 1
function! tlib#var#Let(name, val) "{{{3
return printf('if !exists(%s) | let %s = %s | endif', string(a:name), a:name, string(a:val))
" return printf('if !exists(%s) | let %s = %s | endif', string(a:name), a:name, a:val)
endf
" :def: function! tlib#var#EGet(var, namespace, ?default='')
" Retrieve a variable by searching several namespaces.
"
" EXAMPLES: >
" let g:foo = 1
" let b:foo = 2
" let w:foo = 3
" echo eval(tlib#var#EGet('foo', 'vg')) => 1
" echo eval(tlib#var#EGet('foo', 'bg')) => 2
" echo eval(tlib#var#EGet('foo', 'wbg')) => 3
function! tlib#var#EGet(var, namespace, ...) "{{{3
let pre = []
let post = []
for namespace in split(a:namespace, '\zs')
let var = namespace .':'. a:var
call add(pre, printf('exists("%s") ? %s : (', var, var))
call add(post, ')')
endfor
let default = a:0 >= 1 ? a:1 : ''
return join(pre) . string(default) . join(post)
endf
" :def: function! tlib#var#Get(var, namespace, ?default='')
" Retrieve a variable by searching several namespaces.
"
" EXAMPLES: >
" let g:foo = 1
" let b:foo = 2
" let w:foo = 3
" echo tlib#var#Get('foo', 'bg') => 1
" echo tlib#var#Get('foo', 'bg') => 2
" echo tlib#var#Get('foo', 'wbg') => 3
function! tlib#var#Get(var, namespace, ...) "{{{3
2014-08-03 18:02:51 -04:00
let var_ = substitute(a:var, '#', '_', 'g')
2012-08-16 23:41:25 -04:00
for namespace in split(a:namespace, '\zs')
2014-08-03 18:02:51 -04:00
let vname = namespace == 'g' ? a:var : var_
let var = namespace .':'. vname
2012-08-16 23:41:25 -04:00
if exists(var)
2014-08-03 18:02:51 -04:00
return {var}
2012-08-16 23:41:25 -04:00
endif
endfor
return a:0 >= 1 ? a:1 : ''
endf
" :def: function! tlib#var#List(rx, ?prefix='')
" Get a list of variables matching rx.
" EXAMPLE:
" echo tlib#var#List('tlib_', 'g:')
function! tlib#var#List(rx, ...) "{{{3
2015-01-18 07:58:28 -05:00
TVarArg ['prefix', 'g:']
if v:version >= 704
exec 'let varlist = keys('. prefix .')'
else
redir => vars
silent! exec 'let '. prefix
redir END
let varlist = split(vars, '\n')
call map(varlist, 'matchstr(v:val, ''^\S\+'')')
endif
2012-08-16 23:41:25 -04:00
call filter(varlist, 'v:val =~ a:rx')
return varlist
endf