Permalink
Browse files

implement <Plug>(operator-open-googletranslate)

  • Loading branch information...
1 parent 8101c77 commit 7bbf9c2ec02474cc7ecd198074f8042614ce5ebc @haya14busa committed Nov 12, 2016
@@ -0,0 +1,23 @@
+"=============================================================================
+" FILE: autoload/operator/opengoogletranslate.vim
+" AUTHOR: haya14busa
+" License: MIT license
+"=============================================================================
+scriptencoding utf-8
+let s:save_cpo = &cpo
+set cpo&vim
+
+let s:SelectedText = vital#opengoogletranslate#import('Vim.SelectedText')
+
+" operator#opengoogletranslate#do() open Google Translate with selected text.
+"
+" @param {'char'|'line'|'block'} wise
+function! operator#opengoogletranslate#do(wise) abort
+ let input = s:SelectedText.text(a:wise, getpos("'["), getpos("']"))
+ call opengoogletranslate#open(input, '', '')
+endfunction
+
+let &cpo = s:save_cpo
+unlet s:save_cpo
+" __END__
+" vim: expandtab softtabstop=2 shiftwidth=2 foldmethod=marker
@@ -0,0 +1,90 @@
+" ___vital___
+" NOTE: lines between '" ___vital___' is generated by :Vitalize.
+" Do not mofidify the code nor insert new lines before '" ___vital___'
+if v:version > 703 || v:version == 703 && has('patch1170')
+ function! vital#_opengoogletranslate#Vim#SelectedText#import() abort
+ return map({'text': ''}, 'function("s:" . v:key)')
+ endfunction
+else
+ function! s:_SID() abort
+ return matchstr(expand('<sfile>'), '<SNR>\zs\d\+\ze__SID$')
+ endfunction
+ execute join(['function! vital#_opengoogletranslate#Vim#SelectedText#import() abort', printf("return map({'text': ''}, \"function('<SNR>%s_' . v:key)\")", s:_SID()), 'endfunction'], "\n")
+ delfunction s:_SID
+endif
+" ___vital___
+"=============================================================================
+" FILE: autoload/vital/__vital__/Vim/SelectedText.vim
+" AUTHOR: haya14busa
+" License: MIT license
+"=============================================================================
+
+" text() returns selected text from begin to end.
+"
+" @param {'char'|'line'|'block'} wise
+" @return selected text
+function! s:text(wise, begin, end) abort
+ let begin = a:begin
+ let end = a:end
+ if len(begin) > 3
+ let begin = begin[1:2]
+ endif
+ if len(end) > 3
+ let end = end[1:2]
+ endif
+ if s:_is_exclusive() && begin[1] !=# end[1]
+ " Decrement column number for :set selection=exclusive
+ let end[1] -= 1
+ endif
+ if a:wise !=# 'line' && begin ==# end
+ let lines = [s:_get_pos_char(begin)]
+ elseif a:wise ==# 'block'
+ let [min_c, max_c] = s:_sort_num([begin[1], end[1]])
+ let lines = map(range(begin[0], end[0]), '
+ \ getline(v:val)[min_c - 1 : max_c - 1]
+ \ ')
+ elseif a:wise ==# 'line'
+ let lines = getline(begin[0], end[0])
+ else
+ if begin[0] ==# end[0]
+ let lines = [getline(begin[0])[begin[1]-1 : end[1]-1]]
+ else
+ let lines = [getline(begin[0])[begin[1]-1 :]]
+ \ + (end[0] - begin[0] < 2 ? [] : getline(begin[0]+1, end[0]-1))
+ \ + [getline(end[0])[: end[1]-1]]
+ endif
+ endif
+ return join(lines, "\n") . (a:wise ==# 'line' ? "\n" : '')
+endfunction
+
+" @return Boolean
+function! s:_is_exclusive() abort
+ return &selection is# 'exclusive'
+endfunction
+
+"" Return character at given position with multibyte handling.
+"
+" @arg [Number, Number] as coordinate
+" @return String
+function! s:_get_pos_char(pos) abort
+ let [line, col] = a:pos
+ return matchstr(getline(line), '.', col - 1)
+endfunction
+
+" 7.4.341
+" http://ftp.vim.org/vim/patches/7.4/7.4.341
+if v:version > 704 || v:version == 704 && has('patch341')
+ function! s:_sort_num(xs) abort
+ return sort(a:xs, 'n')
+ endfunction
+else
+ function! s:_sort_num_func(x, y) abort
+ return a:x - a:y
+ endfunction
+ function! s:_sort_num(xs) abort
+ return sort(a:xs, 's:_sort_num_func')
+ endfunction
+endif
+
+" __END__
+" vim: expandtab softtabstop=2 shiftwidth=2 foldmethod=marker
@@ -3,3 +3,4 @@ opengoogletranslate
Web.URI
ArgumentParser
+Vim.SelectedText
@@ -0,0 +1,26 @@
+"=============================================================================
+" FILE: plugin/operator/opengoogletranslate.vim
+" AUTHOR: haya14busa
+" License: MIT license
+"=============================================================================
+scriptencoding utf-8
+if expand('%:p') ==# expand('<sfile>:p')
+ unlet! g:loaded_operator_opengoogletranslate
+endif
+if exists('g:loaded_operator_opengoogletranslate')
+ finish
+endif
+let g:loaded_operator_opengoogletranslate = 1
+let s:save_cpo = &cpo
+set cpo&vim
+
+try
+ call operator#user#define('open-googletranslate', 'operator#opengoogletranslate#do')
+catch /^Vim\%((\a\+)\)\=:E117/
+ " vim-operator-user not found.
+endtry
+
+let &cpo = s:save_cpo
+unlet s:save_cpo
+" __END__
+" vim: expandtab softtabstop=2 shiftwidth=2 foldmethod=marker

0 comments on commit 7bbf9c2

Please sign in to comment.