vimrc/systemd.vim

50 lines
1.5 KiB
VimL
Raw Normal View History

2021-09-06 07:30:45 +02:00
let s:matches = system('systemd --dump-configuration --no-pager | grep -o -P "(^\w+[=])|([\[]\w+[\]])"')
let s:matches = split(s:matches, "\n")
let s:allkeys = system('systemd --dump-configuration --no-pager | grep -o -P "(^[\[]\w+[\]])" | sed -e "s/[]]//g"')
let s:allkeys = split(s:allkeys, "\n")
"let g:AutoPairs = {'(':')', '{':'}',"'":"'",'"':'"', '`':'`'}
"let g:asyncomplete_triggers = {'*': ['.', '>', ':', '['] }
function s:get_key()
let l:line = search('[\w*\]', "bnW")
if l:line == 0
return 0
endif
let l:key = getbufline(bufnr("%"), l:line)[0]
return l:key
endfunction
function s:get_matches()
let l:key = s:get_key()
if l:key == 0
let l:from = index(s:matches, l:key) + 1
let l:until = match(s:matches, '\[\w\+\]', l:from)
let l:until = l:until - 1
return s:matches[l:from:l:until]
endif
return s:allkeys
endfunction
function! s:systemd_completor(opt, ctx) abort
let l:col = a:ctx['col']
let l:typed = a:ctx['typed']
if match(l:typed, '[') != -1
let l:matches = s:allkeys
else
let l:matches = s:get_matches()
endif
echom l:typed
let l:kw = matchstr(l:typed, '\v\S+$')
let l:kwlen = len(l:kw)
let l:startcol = l:col - l:kwlen
let l:startcol = 1
call asyncomplete#complete(a:opt['name'], a:ctx, l:startcol, l:matches)
echo 'done'
endfunction
au User asyncomplete_setup call asyncomplete#register_source({
\ 'name': 'systemd',
\ 'allowlist': ['systemd'],
\ 'completor': function('s:systemd_completor'),
\ })