play.vim 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. if !exists("g:go_play_open_browser")
  2. let g:go_play_open_browser = 1
  3. endif
  4. function! go#play#Share(count, line1, line2) abort
  5. if !executable('curl')
  6. echohl ErrorMsg | echomsg "vim-go: require 'curl' command" | echohl None
  7. return
  8. endif
  9. let content = join(getline(a:line1, a:line2), "\n")
  10. let share_file = tempname()
  11. call writefile(split(content, "\n"), share_file, "b")
  12. let command = "curl -s -X POST https://play.golang.org/share --data-binary '@".share_file."'"
  13. let snippet_id = go#util#System(command)
  14. " we can remove the temp file because it's now posted.
  15. call delete(share_file)
  16. if go#util#ShellError() != 0
  17. echo 'A error has occurred. Run this command to see what the problem is:'
  18. echo command
  19. return
  20. endif
  21. let url = "http://play.golang.org/p/".snippet_id
  22. " copy to clipboard
  23. if has('unix') && !has('xterm_clipboard') && !has('clipboard')
  24. let @" = url
  25. else
  26. let @+ = url
  27. endif
  28. if g:go_play_open_browser != 0
  29. call go#tool#OpenBrowser(url)
  30. endif
  31. echo "vim-go: snippet uploaded: ".url
  32. endfunction
  33. function! s:get_visual_content() abort
  34. let save_regcont = @"
  35. let save_regtype = getregtype('"')
  36. silent! normal! gvy
  37. let content = @"
  38. call setreg('"', save_regcont, save_regtype)
  39. return content
  40. endfunction
  41. " modified version of
  42. " http://stackoverflow.com/questions/1533565/how-to-get-visually-selected-text-in-vimscript
  43. " another function that returns the content of visual selection, it's not used
  44. " but might be useful in the future
  45. function! s:get_visual_selection() abort
  46. let [lnum1, col1] = getpos("'<")[1:2]
  47. let [lnum2, col2] = getpos("'>")[1:2]
  48. " check if the the visual mode is used before
  49. if lnum1 == 0 || lnum2 == 0 || col1 == 0 || col2 == 0
  50. return
  51. endif
  52. let lines = getline(lnum1, lnum2)
  53. let lines[-1] = lines[-1][: col2 - (&selection == 'inclusive' ? 1 : 2)]
  54. let lines[0] = lines[0][col1 - 1:]
  55. return join(lines, "\n")
  56. endfunction
  57. " vim: sw=2 ts=2 et