sgur
3/3/2016 - 6:04 AM

commandlinefu.com から検索してきて quickfix や previewwindow で表示する

commandlinefu.com から検索してきて quickfix や previewwindow で表示する

" Description:
" commandlinefu.com から利用例を検索し previewwindow か quickfix に放流する
" Requirement:
" - webapi-vim <https://github.com/mattn/webapi-vim>
" - http://... で始まる URL を edit できるプラグイン (ex. netrw)
" Usage:
" :CmdFuPreviewUsing {cmd}
"   {cmd} を利用した例を取得し previewwindow に表示
" :CmdFuQuickfixUsing {cmd}
"   {cmd} を利用した例を取得し quickfix に表示
" :CmdFuPreviewMatching  {term}
"   {term} にマッチした例を取得し previewwindow に表示
" :CmdFuQuickfixMatching {term}
"   {term} にマッチした例を取得し quickfix に表示
"
" :CmdFu [-quickfix] [-matching] {term}
"   -quickfix: quickfix に表示 (デフォルト: 未指定時は previewwindow に表示)
"   -matching: 単語にマッチする例を検索 (デフォルト: 未指定時はコマンドの使用例)scriptencoding utf-8


if exists('g:loaded_commandlinefu') && g:loaded_commandlinefu
  finish
endif
let g:loaded_commandlinefu = 1

if !exists('#BufReadCmd#http://*')
  echohl ErrorMsg | echomsg '"http://*" handler required' | echohl NONE
  finish
endif

try
  call webapi#base64#b64encode('')
  call webapi#json#decode('{}')
catch /^Vim\%((\a\+)\)\=:E117/
  echohl ErrorMsg | echomsg 'webapi-vim required' | echohl NONE
  finish
endtry


" Internal {{{1

function! s:glob_shellcmds() abort "{{{
  let raw_globbed = has('win32')
        \ ? globpath(join(split($PATH, ';'), ','), '*.exe', 1, 1)
        \ : globpath(join(split($PATH, ':'), ','), '*',     1, 1)
  let sub_pattern = has('win32')
        \ ? ':t:gs?\.exe??'
        \ : ':t'
  return uniq(sort(map(filter(raw_globbed, 'executable(v:val)'), 'fnamemodify(v:val, sub_pattern)')))
endfunction "}}}

function! s:result(path) abort "{{{
  let url = printf(s:url_format, a:path, 'json')
  let result = webapi#http#get(url)
  if result.status is# '200'
    return call(s:json_decode, [result.content])
  endif
endfunction "}}}

function! s:using(cmd) abort "{{{
  return printf('using/%s', a:cmd)
endfunction "}}}

function! s:matching(term) abort "{{{
  return printf('matching/%s/%s', a:term, webapi#base64#b64encode(a:term))
endfunction "}}}

function! s:quickfix_using(cmd) abort "{{{
  let path = s:using(a:cmd)
  call setqflist(map(s:result(path), "{
        \   'filename': printf(s:url_format, path, 'plaintext')
        \ , 'pattern': v:val.summary
        \ , 'col': 1
        \ }"))
  copen
endfunction "}}}

function! s:quickfix_matching(term) abort "{{{
  let path = s:matching(a:term)
  let path = printf('matching/%s/%s', a:term, webapi#base64#b64encode(a:term))
  call setqflist(map(s:result(path), "{
        \   'filename': printf(s:url_format, path, 'plaintext')
        \ , 'pattern': v:val.summary
        \ , 'col': 1
        \ }"))
  copen
endfunction "}}}

function! s:preview_using(cmd) abort "{{{
  silent execute 'pedit'
        \ '+/' . escape(a:cmd, ' ')
        \ printf(s:url_format, printf('using/%s', a:cmd), 'plaintext')
endfunction "}}}

function! s:preview_matching(term) abort "{{{
  silent execute 'pedit'
        \ '+/' . escape(a:term, ' ')
        \ printf(s:url_format, printf('matching/%s/%s', a:term, webapi#base64#b64encode(a:term)), 'plaintext')
endfunction "}}}

function! s:commandline_fu(...) abort "{{{
  let command = 'preview'
  let method = 'using'
  let args = copy(a:000)
  let opt_quickfix = index(args, '-quickfix')
  if opt_quickfix != -1
    call remove(args, opt_quickfix)
    let command = 'quickfix'
  endif
  let opt_matching = index(args, '-matching')
  if opt_matching != -1
    call remove(args, opt_matching)
    let method = 'matching'
  endif
  call s:{command}_{method}(args[0])
endfunction "}}}

function! s:commandline_fu_complete(arglead, cmdline, cursorpos) abort "{{{
  let opt_quickfix = a:cmdline =~# '-quickfix' ? [] : ['-quickfix']
  let opt_matching = a:cmdline =~# '-matching' ? [] : ['-matching']
  return filter(opt_quickfix + opt_matching + s:shellcmds, 'stridx(v:val, a:arglead) == 0')
endfunction "}}}


" Interface {{{1

command! -nargs=1 -complete=shellcmd CmdFuQuickfixUsing  call s:quickfix_using(<q-args>)
command! -nargs=1 -complete=shellcmd CmdFuQuickfixMatching  call s:quickfix_matching(<q-args>)
command! -nargs=1 -complete=shellcmd CmdFuPreviewUsing  call s:preview_using(<q-args>)
command! -nargs=1 -complete=shellcmd CmdFuPreviewMatching  call s:preview_matching(<q-args>)

command! -nargs=+ -complete=customlist,s:commandline_fu_complete CmdFu  call s:commandline_fu(<f-args>)

" Initialization {{{1

let s:json_decode = exists('*json_decode')
      \ ? function('json_decode')
      \ : function('webapi#json#decode')
let s:url_format = 'http://www.commandlinefu.com/commands/%s/%s'
let s:shellcmds = s:glob_shellcmds()


" 1}}}
scriptencoding utf-8


if exists('g:loaded_commandlinefu') && g:loaded_commandlinefu
  finish
endif
let g:loaded_commandlinefu = 1

if !exists('#BufReadCmd#http://*')
  echohl WarningMsg | echomsg '"http://*" handler required' | echohl NONE
  finish
endif

try
  call webapi#base64#b64encode('')
  call webapi#json#decode('{}')
catch /^Vim\%((\a\+)\)\=:E117/
  echohl WarningMsg | echomsg 'webapi-vim required' | echohl NONE
  finish
endtry


" Internal {{{1

function! s:glob_shellcmds() abort "{{{
  let raw_globbed = has('win32')
        \ ? globpath(join(split($PATH, ';'), ','), '*.exe', 1, 1)
        \ : globpath(join(split($PATH, ':'), ','), '*',     1, 1)
  let sub_pattern = has('win32')
        \ ? ':t:gs?\.exe??'
        \ : ':t'
  return uniq(sort(map(filter(raw_globbed, 'executable(v:val)'), 'fnamemodify(v:val, sub_pattern)')))
endfunction "}}}

function! s:result(path) abort "{{{
  let url = printf(s:url_format, a:path, 'json')
  let result = webapi#http#get(url)
  if result.status is# '200'
    return call(s:json_decode, [result.content])
  endif
endfunction "}}}

function! s:using(cmd) abort "{{{
  return printf('using/%s', a:cmd)
endfunction "}}}

function! s:matching(term) abort "{{{
  return printf('matching/%s/%s', a:term, webapi#base64#b64encode(a:term))
endfunction "}}}

function! s:quickfix_using(cmd) abort "{{{
  let path = s:using(a:cmd)
  call setqflist(map(s:result(path), "{
        \   'filename': printf(s:url_format, path, 'plaintext')
        \ , 'pattern': v:val.summary
        \ , 'col': 1
        \ }"))
  copen
endfunction "}}}

function! s:quickfix_matching(term) abort "{{{
  let path = s:matching(a:term)
  let path = printf('matching/%s/%s', a:term, webapi#base64#b64encode(a:term))
  call setqflist(map(s:result(path), "{
        \   'filename': printf(s:url_format, path, 'plaintext')
        \ , 'pattern': v:val.summary
        \ , 'col': 1
        \ }"))
  copen
endfunction "}}}

function! s:preview_using(cmd) abort "{{{
  silent execute 'pedit'
        \ '+/' . escape(a:cmd, ' ')
        \ printf(s:url_format, printf('using/%s', a:cmd), 'plaintext')
endfunction "}}}

function! s:preview_matching(term) abort "{{{
  silent execute 'pedit'
        \ '+/' . escape(a:term, ' ')
        \ printf(s:url_format, printf('matching/%s/%s', a:term, webapi#base64#b64encode(a:term)), 'plaintext')
endfunction "}}}

function! s:commandline_fu(...) abort "{{{
  let command = 'preview'
  let method = 'using'
  let args = copy(a:000)
  let opt_quickfix = index(args, '-quickfix')
  if opt_quickfix != -1
    call remove(args, opt_quickfix)
    let command = 'quickfix'
  endif
  let opt_matching = index(args, '-matching')
  if opt_matching != -1
    call remove(args, opt_matching)
    let method = 'matching'
  endif
  call s:{command}_{method}(args[0])
endfunction "}}}

function! s:commandline_fu_complete(arglead, cmdline, cursorpos) abort "{{{
  let opt_quickfix = a:cmdline =~# '-quickfix' ? [] : ['-quickfix']
  let opt_matching = a:cmdline =~# '-matching' ? [] : ['-matching']
  return filter(opt_quickfix + opt_matching + s:shellcmds, 'stridx(v:val, a:arglead) == 0')
endfunction "}}}


" Interface {{{1

command! -nargs=1 -complete=shellcmd CmdFuQuickfixUsing  call s:quickfix_using(<q-args>)
command! -nargs=1 -complete=shellcmd CmdFuQuickfixMatching  call s:quickfix_matching(<q-args>)
command! -nargs=1 -complete=shellcmd CmdFuPreviewUsing  call s:preview_using(<q-args>)
command! -nargs=1 -complete=shellcmd CmdFuPreviewMatching  call s:preview_matching(<q-args>)

command! -nargs=+ -complete=customlist,s:commandline_fu_complete CmdFu  call s:commandline_fu(<f-args>)

" Initialization {{{1

let s:json_decode = exists('*json_decode')
      \ ? function('json_decode')
      \ : function('webapi#json#decode')
let s:url_format = 'http://www.commandlinefu.com/commands/%s/%s'
let s:shellcmds = s:glob_shellcmds()


" 1}}}