diff --git a/plugin/instacode.vim b/plugin/instacode.vim index 23b1a73..4114d04 100644 --- a/plugin/instacode.vim +++ b/plugin/instacode.vim @@ -14,36 +14,44 @@ let g:loaded_instacode = 1 let s:save_cpo = &cpo set cpo&vim -" function by StackOverflow user xolox -" http://stackoverflow.com/a/6271254 -function! s:get_visual_selection() - " Why is this not a built-in Vim script function?! - let [lnum1, col1] = getpos("'<")[1:2] - let [lnum2, col2] = getpos("'>")[1:2] - let lines = getline(lnum1, lnum2) - let lines[-1] = lines[-1][: col2 - (&selection == 'inclusive' ? 1 : 2)] - let lines[0] = lines[0][col1 - 1:] - return join(lines, "\n") +" Ripped directly from haskellmode.vim +function! s:url_encode(string) + let pat = '\([^[:alnum:]]\)' + let code = '\=printf("%%%02X",char2nr(submatch(1)))' + let url = substitute(a:string,pat,code,'g') + return url endfunction -function! Instacode() - if getpos("'<")[1] > 0 -python << endpython -import vim, webbrowser, urllib -code = vim.eval('s:get_visual_selection()') -filetype = vim.eval('&ft') -webbrowser.open('http://instacod.es/?post_code=' + urllib.quote_plus(code) + '&post_lang=' + urllib.quote_plus(filetype.title())) -endpython - sleep 500m - redraw! - else - echomsg "You need to visually select something first" - endif +" Open URLs with the system's default application. Works for both local and +" remote paths. +function! s:open_url(url) + let url = shellescape(a:url) + + if has('mac') + silent call system('open '.url) + elseif has('unix') + if executable('xdg-open') + silent call system('xdg-open '.url.' 2>&1 > /dev/null &') + else + echoerr 'You need to install xdg-open to be able to open urls' + return + end + elseif has('win32') || has('win64') + exe '!start rundll32 url.dll,FileProtocolHandler '.url + else + echoerr 'Don''t know how to open a URL on this system' + return + end +endfunction + +function! Instacode(start_lineno, end_lineno) + let code = join(getbufline('%', a:start_lineno, a:end_lineno), "\n") + call s:open_url('http://instacod.es/?post_code='.s:url_encode(code).'&post_lang='.&ft) endfunction -command Instacode :call Instacode() +command -range=% Instacode :call Instacode(, ) if !hasmapto(":Instacode") - vnoremap ic :Instacode + xnoremap ic :Instacode endif let &cpo = s:save_cpo