2016-12-27 09:46:49 -05:00
|
|
|
function! go#play#Share(count, line1, line2) abort
|
2016-06-26 07:12:36 -04:00
|
|
|
if !executable('curl')
|
|
|
|
echohl ErrorMsg | echomsg "vim-go: require 'curl' command" | echohl None
|
|
|
|
return
|
|
|
|
endif
|
2014-10-31 17:30:24 -04:00
|
|
|
|
2016-06-26 07:12:36 -04:00
|
|
|
let content = join(getline(a:line1, a:line2), "\n")
|
|
|
|
let share_file = tempname()
|
|
|
|
call writefile(split(content, "\n"), share_file, "b")
|
2014-10-31 17:30:24 -04:00
|
|
|
|
2018-06-14 06:31:12 -04:00
|
|
|
let l:cmd = ['curl', '-s', '-X', 'POST', 'https://play.golang.org/share',
|
|
|
|
\ '--data-binary', '@' . l:share_file]
|
|
|
|
let [l:snippet_id, l:err] = go#util#Exec(l:cmd)
|
2014-10-31 17:30:24 -04:00
|
|
|
|
2016-06-26 07:12:36 -04:00
|
|
|
" we can remove the temp file because it's now posted.
|
|
|
|
call delete(share_file)
|
2014-10-31 17:30:24 -04:00
|
|
|
|
2018-06-14 06:31:12 -04:00
|
|
|
if l:err != 0
|
|
|
|
echom 'A error has occurred. Run this command to see what the problem is:'
|
|
|
|
echom go#util#Shelljoin(l:cmd)
|
2016-06-26 07:12:36 -04:00
|
|
|
return
|
|
|
|
endif
|
2014-10-31 17:30:24 -04:00
|
|
|
|
2016-06-26 07:12:36 -04:00
|
|
|
let url = "http://play.golang.org/p/".snippet_id
|
2014-10-31 17:30:24 -04:00
|
|
|
|
2016-06-26 07:12:36 -04:00
|
|
|
" copy to clipboard
|
|
|
|
if has('unix') && !has('xterm_clipboard') && !has('clipboard')
|
|
|
|
let @" = url
|
|
|
|
else
|
|
|
|
let @+ = url
|
|
|
|
endif
|
2014-10-31 17:30:24 -04:00
|
|
|
|
2018-06-14 06:31:12 -04:00
|
|
|
if go#config#PlayOpenBrowser()
|
2016-06-26 07:12:36 -04:00
|
|
|
call go#tool#OpenBrowser(url)
|
|
|
|
endif
|
2014-10-31 17:30:24 -04:00
|
|
|
|
2016-06-26 07:12:36 -04:00
|
|
|
echo "vim-go: snippet uploaded: ".url
|
2014-10-31 17:30:24 -04:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
|
2016-12-27 09:46:49 -05:00
|
|
|
function! s:get_visual_content() abort
|
2016-06-26 07:12:36 -04:00
|
|
|
let save_regcont = @"
|
|
|
|
let save_regtype = getregtype('"')
|
|
|
|
silent! normal! gvy
|
|
|
|
let content = @"
|
|
|
|
call setreg('"', save_regcont, save_regtype)
|
|
|
|
return content
|
2014-10-31 17:30:24 -04:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
" modified version of
|
|
|
|
" http://stackoverflow.com/questions/1533565/how-to-get-visually-selected-text-in-vimscript
|
|
|
|
" another function that returns the content of visual selection, it's not used
|
|
|
|
" but might be useful in the future
|
2016-12-27 09:46:49 -05:00
|
|
|
function! s:get_visual_selection() abort
|
2016-06-26 07:12:36 -04:00
|
|
|
let [lnum1, col1] = getpos("'<")[1:2]
|
|
|
|
let [lnum2, col2] = getpos("'>")[1:2]
|
|
|
|
|
|
|
|
" check if the the visual mode is used before
|
|
|
|
if lnum1 == 0 || lnum2 == 0 || col1 == 0 || col2 == 0
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
|
|
|
|
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")
|
2014-10-31 17:30:24 -04:00
|
|
|
endfunction
|
|
|
|
|
2016-06-26 07:12:36 -04:00
|
|
|
" vim: sw=2 ts=2 et
|