Created
August 30, 2010 13:46
-
-
Save mattn/557424 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
if &cp || (exists('g:loaded_quickdan_vim') && g:loaded_quickdan_vim) | |
finish | |
endif | |
let g:loaded_quickdan_vim = 1 | |
function! s:quickdanLang(vimLang) | |
let l:filetypeMap = { | |
\ 'perl': 'pl', | |
\ 'perl6': 'p6', | |
\ 'awk': 'awk', | |
\ 'basic': 'bas', | |
\ 'brainfuck': 'bf', | |
\ 'c': 'c', | |
\ 'elisp': 'el', | |
\ 'lisp': 'lsp', | |
\ 'scheme': 'scm', | |
\ 'haskell': 'hs', | |
\ 'io': 'io', | |
\ 'javascript': 'js', | |
\ 'lua': 'lua', | |
\ 'm4': 'm4', | |
\ 'ocaml': 'ml', | |
\ 'php': 'php', | |
\ 'python': 'py', | |
\ 'python3': 'py3', | |
\ 'ruby': 'rb', | |
\ 'ruby19': 'rb19', | |
\ 'postscript': 'ps', | |
\ 'tcl': 'tcl', | |
\} | |
return get(l:filetypeMap, a:vimLang, 'auto') | |
endfunction | |
function! s:nr2byte(nr) | |
if a:nr < 0x80 | |
return nr2char(a:nr) | |
elseif a:nr < 0x800 | |
return nr2char(a:nr/64+192).nr2char(a:nr%64+128) | |
else | |
return nr2char(a:nr/4096%16+224).nr2char(a:nr/64%64+128).nr2char(a:nr%64+128) | |
endif | |
endfunction | |
function! s:nr2enc_char(charcode) | |
if &encoding == 'utf-8' | |
return nr2char(a:charcode) | |
endif | |
let char = s:nr2byte(a:charcode) | |
if strlen(char) > 1 | |
let char = strtrans(iconv(char, 'utf-8', &encoding)) | |
endif | |
return char | |
endfunction | |
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 .= ch | |
elseif ch == ' ' | |
let outstr .= '+' | |
else | |
let outstr .= '%' . substitute('0' . s:nr2hex(char2nr(ch)), '^.*\(..\)$', '\1', '') | |
endif | |
let i = i + 1 | |
endwhile | |
return outstr | |
endfunction | |
function! s:item2query(items, sep) | |
let ret = '' | |
if type(a:items) == 4 | |
for key in keys(a:items) | |
if strlen(ret) | let ret .= a:sep | endif | |
let ret .= key . "=" . s:encodeURIComponent(a:items[key]) | |
endfor | |
elseif type(a:items) == 3 | |
for item in a:items | |
if strlen(ret) | let ret .= a:sep | endif | |
let ret .= item | |
endfor | |
else | |
let ret = a:items | |
endif | |
return ret | |
endfunction | |
function! s:doHttp(url, getdata, postdata, cookie, returnheader) | |
let url = a:url | |
let getdata = s:item2query(a:getdata, '&') | |
let postdata = s:item2query(a:postdata, '&') | |
let cookie = s:item2query(a:cookie, '; ') | |
if strlen(getdata) | |
let url .= "?" . getdata | |
endif | |
let command = "curl -s -k" | |
if a:returnheader | |
let command .= " -i" | |
endif | |
if strlen(cookie) | |
let command .= " -H \"Cookie: " . cookie . "\"" | |
endif | |
let command .= " \"" . url . "\"" | |
if strlen(postdata) | |
let file = tempname() | |
exec 'redir! > '.file | |
silent echo postdata | |
redir END | |
let quote = &shellxquote == '"' ? "'" : '"' | |
let res = system(command . " -d @" . quote.file.quote) | |
call delete(file) | |
else | |
let res = system(command) | |
endif | |
return res | |
endfunction | |
function! s:quickdan(src) | |
let url = 'http://api.dan.co.jp/lleval.cgi' | |
let res = s:doHttp(url, { 's': a:src, 'c': '', 'l': s:quickdanLang(&ft) }, {}, {}, 0) | |
try | |
return eval(iconv(res, "utf-8", &encoding)) | |
catch | |
return { 'stdout': '', 'stderr': 'unkonwn error(maybe timeout)' } | |
endtry | |
endfunction | |
function! g:QuickDan() | |
let src = join(getline('0', '$'), "\n") | |
let res = s:quickdan(src) | |
echohl WarningMsg | |
echo res['stderr'] | |
echohl None | |
echo res['stdout'] | |
endfunction | |
command! QuickDan call g:QuickDan() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment