Skip to content

Instantly share code, notes, and snippets.

@bootleq
Created December 10, 2011 12:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bootleq/1455020 to your computer and use it in GitHub Desktop.
Save bootleq/1455020 to your computer and use it in GitHub Desktop.
Vim 陽春 google 字典,反正已經沒用了,google 停了這服務
nnoremap <silent> <LocalLeader>K :DictSearch<CR>
xnoremap <silent> <LocalLeader>K :<C-U>call SaveReg()<CR>gvy:let b:temp_reg=@"<CR>
\ :call RestoreReg()<CR>:call DictSearch(b:temp_reg)<CR>
command! -nargs=* DictSearch call DictSearch(<f-args>)
command! -nargs=0 DictSearchOff call <SID>dict_search_off()
function! DictSearch(...)
let save_isk = &l:iskeyword
let &l:iskeyword = '@'
let word = a:0 > 0 ? a:1 : expand('<cword>')
let mode = a:0 > 1 ? a:2 : ''
let page = a:0 > 2 ? a:3 : get(b:, 'dict_search_current_page', 0)
let split_method = a:0 > 3 ? a:4 : '17new'
let &l:iskeyword = save_isk
let current_word = get(b:, 'dict_search_current_word', '')
if !empty(current_word)
let line = getline('.')
if line =~ '\v^\s+相關詞組 »$'
let mode = 'rph'
let [word, split_method] = [current_word, 'vnew']
elseif line =~ '\v^\s+網路上的翻譯 »$'
let mode = 'wtr'
let [word, split_method] = [current_word, 'vnew']
elseif line =~ '\v^\s+字典結果 »$'
let [word, split_method] = [current_word, 'vnew']
elseif line =~ '\v^\s+下一頁 »$'
let page = page + 1
let [word, split_method] = [current_word, 'vnew']
elseif line =~ '\v^\s*« 上一頁'
let page = page + (match(line, '-') > col('.') ? 1 : -1)
let [word, split_method] = [current_word, 'vnew']
endif
endif
let buffer_name = printf("[DictSearch%s] %s%s",
\ empty(mode) ? "" : " (" . mode . ")",
\ word,
\ empty(page) ? "" : " (" . page . ")"
\ )
if bufnr(buffer_name) > -1
execute "sbuffer " . bufnr(buffer_name)
return
endif
let result = wwwrenderer#render('http://www.google.com.tw/m/search?site=dictionary&q='
\ . word
\ . (empty(mode) ? '' : printf("&%s=1", mode))
\ . (empty(page) ? '' : printf("&start=%s", page * 10))
\ )
execute split_method
execute "silent file " . escape(buffer_name, ' \')
let b:dict_search_current_word = word
let b:dict_search_current_page = page
set filetype=dictsearch
silent $put =result
silent call TidySpaces()
for needle in ['搜尋結果']
let gabage = search('\m\s\+' . needle . '\s»\_.*\%$', 'bcnW')
if gabage > 0
silent execute gabage . ",$d_"
break
endif
endfor
silent! %s/\v\zs\s+\ze(下一頁|相關詞組|網路上的翻譯|字典結果) »$/ /g
silent! %s/\v\zs\s+\ze« (上一頁)//g
" Remove 1) duplicate 2) beginning/ending blank lines.
silent %s/\m^\(.*\)\(\n\1\)\+$/\1/
silent g/\m\(\%^\|\%$\)\n/d_
normal! gg
endfunction
function! s:dict_search_filetype_setting()
setlocal noswapfile buftype=nofile bufhidden=wipe nobuflisted nowrap cursorline number foldcolumn=0
nmap <silent><buffer> <LocalLeader><CR> <LocalLeader>K
xmap <silent><buffer> <LocalLeader><CR> <LocalLeader>K
syntax match ModeLink /\v(下一頁|相關詞組|網路上的翻譯|字典結果) »$/
syntax match ModeLink /\v^\s*\zs« (上一頁)/
highlight link ModeLink String
" TODO b:undo_ftplugin option
endfunction
function! s:dict_search_quit()
if &filetype == 'dictsearch'
execute "quit"
endif
endfunction
function! s:dict_search_off()
let win_count = winnr('$')
silent windo call <SID>dict_search_quit()
execute 'wincmd t'
return win_count > winnr('$')
endfunction
augroup DictSearch
autocmd FileType dictsearch call s:dict_search_filetype_setting()
augroup END
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment