Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove need for python, simplify range functionality #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 33 additions & 25 deletions plugin/instacode.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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(<line1>, <line2>)
if !hasmapto("<Esc>:Instacode<CR>")
vnoremap <leader>ic <Esc>:Instacode<CR>
xnoremap <leader>ic :Instacode<CR>
endif

let &cpo = s:save_cpo
Expand Down