Vim 陽春 Dr.eye 字典,沒在用了,太慢
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
command! -nargs=* DictSearch call DictSearch(<f-args>) | |
" 來源:Dr.eye 字典 移動版 | |
" 例: http://www.dreye.com/mws/dict.php?w=word&ua=dc_cont | |
let s:dict_search_modes = { | |
\ 'keys': ['cont', 'var', 'phra', 'diff', 'der' ], | |
\ 'values': ['字義', '變化', '片語', '辨析', '衍生'] | |
\ } | |
function! DictSearch(...) | |
let word = a:0 > 0 ? a:1 : '' | |
let mode = a:0 > 1 ? a:2 : 'cont' | |
let page = a:0 > 2 ? a:3 : get(b:, 'dict_search_current_page', 0) | |
let split_method = a:0 > 3 ? a:4 : '17new' | |
" <cword> CamelCase/under_scored 拆解,並轉為小寫 | |
if empty(word) | |
let save_isk = &l:iskeyword | |
let &l:iskeyword = '@' | |
let word = expand('<cword>') | |
let &l:iskeyword = save_isk | |
if word =~ '\u\l' | |
let pattern = printf('\v%%<%sc\u?\l*%%>%sc', | |
\ col('.') + 1, | |
\ col('.') | |
\ ) | |
let camel = matchstr(getline('.'), pattern) | |
if !empty(camel) | |
let word = tolower(camel) | |
endif | |
endif | |
endif | |
" 選單處理 | |
let nav_line = 1 | |
let current_word = get(b:, 'dict_search_current_word', '') | |
if !empty(current_word) && line('.') == nav_line | |
for mode_word in s:dict_search_modes.values | |
if word == mode_word | |
let mode = get(s:dict_search_modes.keys, index(s:dict_search_modes.values, word)) | |
let [word, split_method] = [current_word, 'vnew'] | |
break | |
endif | |
endfor | |
endif | |
" 定義 buffer name | |
let mode_human = get( | |
\ s:dict_search_modes.values, | |
\ index(s:dict_search_modes.keys, mode), | |
\ '?' | |
\ ) | |
let buffer_name = printf("[DictSearch%s] %s%s", | |
\ empty(mode) ? "" : " (" . mode_human . ")", | |
\ word, | |
\ empty(page) ? "" : " (" . page . ")" | |
\ ) | |
" 切換至既有 buffer | |
if bufnr(buffer_name) > -1 | |
execute "sbuffer " . bufnr(buffer_name) | |
return | |
endif | |
echomsg printf("%s (%s)查詢中...", word, mode_human) | |
let start_time = reltime() | |
" 取得 web 結果 | |
" FIXME render 的第二個參數目前無效 | |
" FIXME `page` 選項目前沒用到 | |
let result = wwwrenderer#render('http://www.dreye.com/mws/dict.php?w=' | |
\ . word | |
\ . (empty(mode) ? '' : printf("&ua=dc_%s", mode)) | |
\ . (empty(page) ? '' : printf("&start=%s", page * 10)) | |
\ , ['td',{'class': 'mainbody_icon'}] | |
\ ) | |
let query_time = reltimestr(reltime(start_time, reltime())) | |
" 分割視窗 | |
let save_eadirection = &eadirection | |
let save_equalalways = &equalalways | |
let &eadirection = 'hor' | |
let &equalalways = 1 | |
execute split_method | |
let &eadirection = save_eadirection | |
let &equalalways = save_equalalways | |
" 輸出內容 | |
execute "silent file " . escape(buffer_name, ' \') | |
let b:dict_search_current_word = word | |
let b:dict_search_current_page = page | |
set filetype=dictsearch | |
if executable('opencc') | |
let result = system(printf("echo '%s' | opencc", result)) | |
endif | |
silent $put =result | |
" 格式整理 | |
silent call TidySpaces() | |
%s/\v\_.*字義變化片語辨析衍生\ze//e | |
%s/\v^(KK|DJ): \[\]$\n\n//ge | |
%s/\v^\n{2,}$//ge | |
" 建立選單 | |
let nav = printf("%s (%s)\n\n", | |
\ join(s:dict_search_modes.values, ' '), | |
\ word | |
\ ) | |
0put=nav | |
call matchadd("NavBarCurrent", printf('\v%%1l%s', mode_human)) | |
normal! gg | |
echomsg printf("查詢時間:%s 秒", query_time) | |
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 NavBar /\v%1l\K+/ | |
syntax match DictSearchH1 /\v%1l\(.*$/ | |
syntax match GrammarType /\v^(n|v\.aux|vt|vi|a)\./ | |
syntax match GrammarType /\v^(形變|動變|名複):/ | |
syntax match DictListItem /\v^\d\./ | |
syntax match DictSection /\v^(同義|同義參見):/ | |
highlight link NavBar Statement | |
highlight link NavBarCurrent Title | |
highlight link GrammarType Type | |
highlight link DictListItem Number | |
highlight link DictSection Structure | |
" TODO handle 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