01 map :call Eng2Ger(expand("")) 02 map :call Ger2Eng() 03 04 let dictpath = '~/stuff/eng2ger.vok' 05 let vocpath = '~/stuff/vimvoc.txt' 06 07 function Ger2Eng() 08 let word = inputdialog("Translate de -> eng: ") 09 call Extract(word, "want_eng") 10 endfunction 11 12 function Eng2Ger(word) 13 let word = a:word 14 call Extract(word, "want_ger") 15 endfunction 16 17 function Extract(word, lang) 18 let word = a:word 19 let lang = a:lang 20 let wordpair = system('grep "^' . word . '\b \-\|\- \b'. word . '$" ' . g:dictpath) 21 if(v:shell_error) 22 echohl ErrorMsg 23 echo "Error occurred!" 24 echohl None 25 else 26 let match = matchstr(wordpair, "[^\n]*") 27 call Translate(match, word, lang) 28 endif 29 endfunction 30 31 function Translate(match, word, lang) 32 let match = a:match 33 let word = a:word 34 let lang = a:lang 35 if(lang == "want_eng") 36 let result = substitute(match, "\\ (" . word . "\\) -- ", submatch(1), "") 37 call ConfirmDialog(result, match) 38 elseif(lang == "want_ger") 39 let result = substitute(match, " -- \\(" . word . "\\)", submatch(1), "") 40 call ConfirmDialog(result, match) 41 endif 42 endfunction 43 44 function ConfirmDialog(result, match) 45 let result = a:result 46 let match = a:match 47 let choice = confirm(result, "&Insert\n&Save to File\n&Cancel", 3, "Question") 48 if(choice == 1) 49 execute "normal ea " . result 50 elseif(choice == 2) 51 execute "e " . g:vocpath 52 execute "normal o" . match 53 execute "w" . g:vocpath . "|bd" 54 elseif(choice == 3) 55 execute "normal e" 56 endif 57 endfunction