Created
November 11, 2011 15:55
-
-
Save tungd/1358346 to your computer and use it in GitHub Desktop.
vim-ref ch source
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
" A ref source for ch. https://github.com/visionmedia/ch | |
" Version: 0.0.1 | |
" Heavily based on man source by: | |
" Author : thinca <[email protected]> | |
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