Created
March 13, 2009 02:25
-
-
Save mattn/78403 to your computer and use it in GitHub Desktop.
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
"============================================================================= | |
" File: dan-the-perl.vim | |
" Author: Yasuhiro Matsumoto <mattn.jp@gmail.com> | |
" Last Change: 14-Mar-2009 | |
" | |
if &cp || (exists('g:loaded_dan_the_perl_vim') && g:loaded_dan_the_perl_vim) | |
finish | |
endif | |
let g:loaded_dan_the_perl_vim = 1 | |
function! s:nr2hex(nr) | |
let n = a:nr | |
let r = "" | |
while n | |
let r = '0123456789ABCDEF'[n % 16] . r | |
let n = n / 16 | |
endwhile | |
return r | |
endfunction | |
function! s:encodeURIComponent(instr) | |
let instr = iconv(a:instr, &enc, "utf-8") | |
let len = strlen(instr) | |
let i = 0 | |
let outstr = '' | |
while i < len | |
let ch = instr[i] | |
if ch =~# '[0-9A-Za-z-._~!''()*]' | |
let outstr = outstr . ch | |
elseif ch == ' ' | |
let outstr = outstr . '+' | |
else | |
let outstr = outstr . '%' . substitute('0' . s:nr2hex(char2nr(ch)), '^.*\(..\)$', '\1', '') | |
endif | |
let i = i + 1 | |
endwhile | |
return outstr | |
endfunction | |
function! DanThePerl(line1, line2) | |
let content = join(getline(a:line1, a:line2), "\n") | |
let quote = &shellxquote == '"' ? "'" : '"' | |
let url = 'http://api.dan.co.jp/perleval.cgi?c=callback&s='.s:encodeURIComponent(content) | |
let res = system('curl -s '.quote.url.quote) | |
let res = substitute(res, '^callback(\(.*\));.*$', '\1', '') | |
let json = eval(res) | |
if has_key(json, 'result') | |
echo json['result'] | |
elseif has_key(json, 'error') && len(json['error']) | |
echo json['error'] | |
else | |
echoerr "unknown error" | |
endif | |
unlet json | |
endfunction | |
command! -nargs=? -range=% DanThePerl :call DanThePerl(<line1>, <line2>) | |
nnoremap <silent> !dan :DanThePerl<cr> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment