Skip to content

Commit

Permalink
chore: add google_cgi.vim to separate functions
Browse files Browse the repository at this point in the history
  • Loading branch information
kawarimidoll committed Nov 26, 2023
1 parent 4f5294f commit d2a4181
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 21 deletions.
19 changes: 19 additions & 0 deletions google_cgi.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
function! s:url_encode(str)
return range(0, strlen(a:str)-1)
\ ->map({i -> a:str[i] =~ '[-.~]\|\w' ? a:str[i] : printf("%%%02x", char2nr(a:str[i]))})
\ ->join('')
endfunction

function! google_cgi#henkan(str) abort
let url_base = 'http://www.google.com/transliterate?langpair=ja-Hira|ja&text='
let encoded = s:url_encode(a:str)
" echomsg encoded
let result = system($"curl -s '{url_base}{encoded}'")
" echomsg result
try
return json_decode(result)->map({_,v->v[1][0]})->join('')
catch
echomsg v:exception
return ''
endtry
endfunction
22 changes: 1 addition & 21 deletions k.vim
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,6 @@ function! s:capital(char) abort
return substitute(a:char, '.', '\U\0', '')
endfunction

function! s:url_encode(str)
return range(0, strlen(a:str)-1)
\ ->map({i -> a:str[i] =~ '[-.~]\|\w' ? a:str[i] : printf("%%%02x", char2nr(a:str[i]))})
\ ->join('')
endfunction

let s:is_enable = v:false
let s:keys_to_remaps = []
let s:keys_to_unmaps = []
Expand Down Expand Up @@ -466,20 +460,6 @@ function! k#kakutei(fallback_key) abort
return pumvisible() ? "\<c-y>" : ''
endfunction

function! k#google_henkan(str) abort
let url_base = 'http://www.google.com/transliterate?langpair=ja-Hira|ja&text='
let encoded = s:url_encode(a:str)
" echomsg encoded
let result = system($"curl -s '{url_base}{encoded}'")
" echomsg result
try
return json_decode(result)->map({_,v->v[1][0]})->join('')
catch
echomsg v:exception
return ''
endtry
endfunction

function! s:complete_done_pre(complete_info, completed_item) abort
" echomsg a:complete_info a:completed_item

Expand All @@ -500,7 +480,7 @@ function! s:complete_done_pre(complete_info, completed_item) abort

if has_key(user_data, 'google_trans')
let gt = user_data.google_trans
let henkan_result = k#google_henkan(gt.yomi)
let henkan_result = google_cgi#henkan(gt.yomi)
if henkan_result ==# ''
echomsg 'Google変換で結果が得られませんでした。'
return
Expand Down

0 comments on commit d2a4181

Please sign in to comment.