Skip to content

Instantly share code, notes, and snippets.

@tungd
Created November 11, 2011 15:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tungd/1358346 to your computer and use it in GitHub Desktop.
Save tungd/1358346 to your computer and use it in GitHub Desktop.
vim-ref ch source
" A ref source for ch. https://github.com/visionmedia/ch
" Version: 0.0.1
" Heavily based on man source by:
" Author : thinca <thinca+vim@gmail.com>
let s:save_cpo = &cpo
set cpo&vim
scriptencoding utf-8
" config. {{{1
if !exists('g:ref_ch_cmd') " {{{2
let g:ref_ch_cmd = executable('ch') ? 'ch' : ''
endif
let s:source = {'name': 'ch'} " {{{1
function! s:source.available()
return !empty(self.option('cmd'))
endfunction
function! s:source.get_body(query)
let [query, sec] = s:parse(a:query)
let q = sec =~ '\d' ? [sec, query] : [query]
try
let use_vimproc = g:ref_use_vimproc
let g:ref_use_vimproc = 0
let res = ref#system(ref#to_list(self.option('cmd')) + q)
finally
let g:ref_use_vimproc = use_vimproc
endtry
if !res.result
let body = res.stdout
if &termencoding != '' && &encoding != '' && &termencoding !=# &encoding
let encoded = iconv(body, &termencoding, &encoding)
if encoded != ''
let body = encoded
endif
endif
let body = substitute(body, '\r', '', 'g')
return body
endif
let list = self.complete(a:query)
if !empty(list)
return list
endif
throw matchstr(res.stderr, '^\_s*\zs.\{-}\ze\_s*$')
endfunction
function! s:source.opened(query)
call s:syntax()
endfunction
function! s:source.get_keyword()
return ref#get_text_on_cursor('[[:alnum:]_.:+-]\+\%((\d)\)\?')
endfunction
function! s:source.complete(query)
let [query, sec] = s:parse(a:query)
let sec -= 0 " to number
return filter(copy(self.cache(sec, self)),
\ 'v:val =~# "^\\V" . query')
endfunction
function! s:source.normalize(query)
let [query, sec] = s:parse(a:query)
return query . (sec == '' ? '' : '(' . sec . ')')
endfunction
function! s:source.call(name)
let list = []
if a:name is 0
for n in range(1, 9)
let list += self.cache(n, self)
endfor
else
let chpath = self.option('chpath')
let pat = '.*/\zs.*\ze\.sheet$'
if isdirectory(chpath)
let list += map(split(glob(chpath . '/*'), "\n"), 'matchstr(v:val, pat)')
endif
endif
return ref#uniq(list)
endfunction
function! s:source.option(opt)
if a:opt ==# 'chpath'
return expand('~/cli/ch/sheets')
endif
return g:ref_ch_{a:opt}
endfunction
function! s:parse(query)
let l = matchlist(a:query, '\([^[:space:]()]\+\)\s*(\(\d\))$')
if !empty(l)
return l[1 : 2]
endif
let l = matchlist(a:query, '\(\d\)\s\+\(\S*\)')
if !empty(l)
return [l[2], l[1]]
endif
return [a:query, '']
endfunction
function! s:syntax()
endfunction
function! ref#ch#define()
return copy(s:source)
endfunction
let &cpo = s:save_cpo
unlet s:save_cpo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment