translate.vim 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. let s:default_options = get(g:, 'translate#default_options', '-no-ansi -no-auto -no-warn -brief')
  2. let s:default_languages = get(g:, 'translate#default_languages', {})
  3. let s:base_cmd = 'trans ' . s:default_options
  4. let s:is_win = has('win32') || has('win64')
  5. " {{{ Exposed
  6. function! translate#run(source_target) abort
  7. if !s:check_executable() | return | endif
  8. let l:regtype = getregtype('a')
  9. let l:regtext = getreg('a')
  10. silent! %yank a
  11. let l:translation = s:translate(a:source_target, @a)
  12. call translate#open_trans_buf(l:translation)
  13. call setreg('a', l:regtext, l:regtype)
  14. endfunction
  15. function! translate#visual(source_target) range abort
  16. if !s:check_executable() | return | endif
  17. let l:regtype = getregtype('a')
  18. let l:regtext = getreg('a')
  19. silent! normal! gv"ay
  20. let l:translation = s:translate(a:source_target, @a)
  21. call translate#open_trans_buf(l:translation)
  22. call setreg('a', l:regtext, l:regtype)
  23. endfunction
  24. function! translate#replace(source_target) range abort
  25. if !s:check_executable() | return | endif
  26. let l:regtype = getregtype('a')
  27. let l:regtext = getreg('a')
  28. silent! normal! gv"ay
  29. let @a = s:translate(a:source_target, @a)
  30. silent! normal! gv"ap
  31. call setreg('a', l:regtext, l:regtype)
  32. endfunction
  33. function! translate#replace_operator(type) abort
  34. call translate#operator(a:type, 1)
  35. endfunction
  36. function! translate#operator(type, ...) abort
  37. if !s:check_executable() | return | endif
  38. let l:replace = get(a:, 1, 0)
  39. let l:regtype = getregtype('a')
  40. let l:regtext = getreg('a')
  41. if a:type ==# 'char'
  42. silent! normal! `[v`]"ay
  43. elseif a:type ==# 'line'
  44. silent! normal! '[V']"ay
  45. else
  46. " Forget about blockwise selection for now
  47. return
  48. endif
  49. let @a = s:translate('', @a)
  50. if l:replace !=# ''
  51. silent! normal! gv"ap
  52. else
  53. call translate#open_trans_buf(@a)
  54. endif
  55. call setreg('a', l:regtext, l:regtype)
  56. endfunction
  57. function! translate#open_trans_buf(text) abort
  58. let l:winnr = winnr()
  59. call translate#clear_trans_buf()
  60. silent! botright 8new Translation
  61. set buftype=nofile
  62. set bufhidden=hide
  63. set noswapfile
  64. let s:trans_buf = bufnr('%')
  65. let @a = a:text
  66. silent! put! a
  67. execute('resize ' . line('$'))
  68. silent! normal! gg
  69. if (l:winnr != winnr())
  70. wincmd p
  71. endif
  72. endfunction
  73. function! translate#clear_trans_buf() abort
  74. if exists('s:trans_buf') && bufexists(s:trans_buf)
  75. sil! exe 'bd! ' . s:trans_buf
  76. unlet s:trans_buf
  77. endif
  78. endfunction
  79. "}}}
  80. "{{{ Helpers
  81. function! s:translate(source_target, text) abort
  82. echo 'Translating...'
  83. let l:source_target = s:get_source_target(a:text, a:source_target)
  84. redraw | echo 'Translating ' . l:source_target . '...'
  85. let l:cmd = s:base_cmd . ' ' . l:source_target
  86. let l:result = system(l:cmd, a:text)[:-2]
  87. redraw | echo ''
  88. return l:result
  89. endfunction
  90. function! s:get_source_target(text, source_target) abort
  91. if (a:source_target !=# '')
  92. return a:source_target
  93. endif
  94. let l:source_lang = system(s:base_cmd .' -id ' . shellescape(a:text))[:-2]
  95. if (!has_key(s:default_languages, l:source_lang))
  96. return ''
  97. endif
  98. let l:target_lang = s:default_languages[l:source_lang]
  99. return l:source_lang . ':' . l:target_lang
  100. endfunction
  101. function! s:msg_error(str) abort
  102. echohl ErrorMsg
  103. echo a:str
  104. echohl None
  105. endfunction
  106. function! s:check_executable() abort
  107. " translate-shell works on windows via WSL or cygwin
  108. " Thus we must set vim shell accordingly.
  109. " However executable() doesn't seem to check to right $PATH
  110. " So bypassing that check on windows for now
  111. if s:is_win
  112. return 1
  113. endif
  114. if !executable('trans')
  115. call s:msg_error('translate-shell not found. Please install it.')
  116. return 0
  117. endif
  118. return 1
  119. endfunction
  120. function! s:is_trans_buf_open() abort
  121. return exists('s:trans_buf') && bufnr('%') == s:trans_buf
  122. endfunction
  123. "}}}
  124. augroup translate
  125. autocmd!
  126. autocmd bufenter * if (winnr("$") == 1 && s:is_trans_buf_open()) | q! | endif
  127. augroup END