104 lines
3.2 KiB
VimL
104 lines
3.2 KiB
VimL
|
" @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)
|
||
|
" @Last Change: 2014-07-01.
|
||
|
" @Revision: 63
|
||
|
|
||
|
|
||
|
" :def: function! tlib#arg#Get(n, var, ?default="", ?test='')
|
||
|
" Set a positional argument from a variable argument list.
|
||
|
" See tlib#string#RemoveBackslashes() for an example.
|
||
|
function! tlib#arg#Get(n, var, ...) "{{{3
|
||
|
let default = a:0 >= 1 ? a:1 : ''
|
||
|
let atest = a:0 >= 2 ? a:2 : ''
|
||
|
" TLogVAR default, atest
|
||
|
if !empty(atest)
|
||
|
let atest = ' && (a:'. a:n .' '. atest .')'
|
||
|
endif
|
||
|
let test = printf('a:0 >= %d', a:n) . atest
|
||
|
return printf('let %s = %s ? a:%d : %s', a:var, test, a:n, string(default))
|
||
|
endf
|
||
|
|
||
|
|
||
|
" :def: function! tlib#arg#Let(list, ?default='')
|
||
|
" Set a positional arguments from a variable argument list.
|
||
|
" See tlib#input#List() for an example.
|
||
|
function! tlib#arg#Let(list, ...) "{{{3
|
||
|
let default = a:0 >= 1 ? a:1 : ''
|
||
|
let list = map(copy(a:list), 'type(v:val) == 3 ? v:val : [v:val, default]')
|
||
|
let args = map(range(1, len(list)), 'call("tlib#arg#Get", [v:val] + list[v:val - 1])')
|
||
|
return join(args, ' | ')
|
||
|
endf
|
||
|
|
||
|
|
||
|
" :def: function! tlib#arg#Key(dict, list, ?default='')
|
||
|
" See |:TKeyArg|.
|
||
|
function! tlib#arg#Key(list, ...) "{{{3
|
||
|
let default = a:0 >= 1 ? a:1 : ''
|
||
|
let dict = a:list[0]
|
||
|
let list = map(copy(a:list[1:-1]), 'type(v:val) == 3 ? v:val : [v:val, default]')
|
||
|
let args = map(list, '"let ". v:val[0] ." = ". string(get(dict, v:val[0], v:val[1]))')
|
||
|
" TLogVAR dict, list, args
|
||
|
return join(args, ' | ')
|
||
|
endf
|
||
|
|
||
|
|
||
|
" :def: function! tlib#arg#StringAsKeyArgs(string, ?keys=[], ?evaluate=0, ?sep=':')
|
||
|
function! tlib#arg#StringAsKeyArgs(string, ...) "{{{1
|
||
|
TVarArg ['keys', {}], ['evaluate', 0], ['sep', ':']
|
||
|
let keyargs = {}
|
||
|
let args = split(a:string, '\\\@<! ')
|
||
|
let arglist = map(args, 'matchlist(v:val, ''^\%(\(\w\+\)'. sep .'\(.*\)\|\(.*\)\)$'')')
|
||
|
" TLogVAR a:string, args, arglist
|
||
|
let pos = 0
|
||
|
for matchlist in arglist
|
||
|
if !empty(matchlist[3])
|
||
|
let keyargs[pos] = matchlist[3]
|
||
|
let pos += 1
|
||
|
else
|
||
|
let [match, key, val; rest] = matchlist
|
||
|
if empty(keys) || has_key(keys, key)
|
||
|
let val = substitute(val, '\\\\', '\\', 'g')
|
||
|
if evaluate
|
||
|
let val = eval(val)
|
||
|
endif
|
||
|
let keyargs[key] = val
|
||
|
else
|
||
|
echom 'Unknown key: '. key .'='. val
|
||
|
endif
|
||
|
endif
|
||
|
endfor
|
||
|
return keyargs
|
||
|
endf
|
||
|
|
||
|
|
||
|
function! tlib#arg#StringAsKeyArgsEqual(string) "{{{1
|
||
|
return tlib#arg#StringAsKeyArgs(a:string, [], 0, '=')
|
||
|
endf
|
||
|
|
||
|
|
||
|
|
||
|
""" Command line {{{1
|
||
|
|
||
|
" :def: function! tlib#arg#Ex(arg, ?chars='%#! ')
|
||
|
" Escape some characters in a string.
|
||
|
"
|
||
|
" Use |fnamescape()| if available.
|
||
|
"
|
||
|
" EXAMPLES: >
|
||
|
" exec 'edit '. tlib#arg#Ex('foo%#bar.txt')
|
||
|
function! tlib#arg#Ex(arg, ...) "{{{3
|
||
|
if exists('*fnameescape') && a:0 == 0
|
||
|
return fnameescape(a:arg)
|
||
|
else
|
||
|
" let chars = '%# \'
|
||
|
let chars = '%#! '
|
||
|
if a:0 >= 1
|
||
|
let chars .= a:1
|
||
|
endif
|
||
|
return escape(a:arg, chars)
|
||
|
endif
|
||
|
endf
|
||
|
|
||
|
|