1 " vi:set ts=8 sts=2 sw=2 tw=0 nowrap:
3 " dicwin.vim - Dictionary window
5 " Maintainer: MURAOKA Taro <koron@tka.att.ne.jp>
6 " Last Change: 04-Aug-2007.
7 " Commands: <C-k><C-k> Search word under cursor.
8 " <C-k>/ Search prompted word.
9 " <C-k>c Close dictionary window.
10 " <C-k>n Search next. (with last word)
11 " <C-k>p Search previous. (with last word)
12 " <C-k>w Jump to dictionary window (if exists)
13 " Require: &runtimepath/dict/gene.txt or &runtimepath/gene.txt
15 " URL where you can get 'gene.txt':
16 " http://www.namazu.org/~tsuchiya/sdic/data/gene.html
18 if exists('plugin_dicwin_disable')
21 let s:myname = expand('<sfile>:t:r')
23 " Search default dictionary
24 if !exists('dicwin_dictpath')
25 let s:dict = 'gene.txt'
26 let g:dicwin_dictpath = globpath(&rtp, 'dict/'.s:dict)
27 if g:dicwin_dictpath == ''
28 let dicwin_dictpath = globpath(&rtp, s:dict)
32 " Windows return '\\' as directory separator in globpath(), replace it.
33 let dicwin_dictpath = substitute(dicwin_dictpath, '\\', '/', 'g')
36 let s:lastpattern = ''
39 let s:use_mapleader = 0
40 if !exists('g:mapleader')
41 let g:mapleader = "\<C-k>"
42 let s:use_mapleader = 1
44 nnoremap <silent> <Leader>k :call <SID>OpenDictionary(g:dicwin_dictpath, expand('<cword>'))<CR>
45 nnoremap <silent> <Leader>n :call <SID>Search(g:dicwin_dictpath, 0)<CR>
46 nnoremap <silent> <Leader>p :call <SID>Search(g:dicwin_dictpath, 1)<CR>
47 nnoremap <silent> <Leader>w :call <SID>GoDictWindow()<CR>
48 nnoremap <silent> <Leader>c :call <SID>Close()<CR>
49 nnoremap <silent> <Leader>/ :call <SID>Query()<CR>
50 nnoremap <silent> <Leader><C-k> :call <SID>OpenDictionary(g:dicwin_dictpath, expand('<cword>'))<CR>
51 nnoremap <silent> <Leader><C-n> :call <SID>Search(g:dicwin_dictpath, 0)<CR>
52 nnoremap <silent> <Leader><C-p> :call <SID>Search(g:dicwin_dictpath, 1)<CR>
53 nnoremap <silent> <Leader><C-w> :call <SID>GoDictWindow()<CR>
54 if s:use_mapleader > 0
60 " WinEnter/WinLeave hooks
62 function! s:DicWinEnter()
64 exe "normal! 8\<C-W>_"
71 function! s:DicWinLeave()
77 function! s:DicWinUnload()
78 if exists('w:dicwin_dicwin')
84 " GenerateSearchPatternEnglish(word)
86 function! s:GenerateSearchPatternEnglish(word)
90 let pat = substitute(pat, 'ies$', '\\(ies\\=\\|y\\)', '')
95 let pat = substitute(pat, 'ied$', '\\(y\\|ied\\|ie\\)\\=', '')
97 let pat = substitute(pat, 'ed$', '\\(ed\\|e\\)\\=', '')
99 if pat =~ '\(.\)\1ing$'
100 let pat = substitute(pat, '\(.\)ing$', '\1\\=\\(ing\\|e\\)\\=', '')
102 let pat = substitute(pat, 'ing$', '\\(ing\\|e\\)\\=', '')
104 elseif pat =~ 'able$'
105 let pat = substitute(pat, 'able$', '\\(able\\|e\\)', '')
106 elseif pat =~ '[.,]$'
107 let pat = substitute(pat, '[,.]$', '', '')
113 let wquery = input(s:myname. ': Input search word: ')
114 return s:OpenDictionary(g:dicwin_dictpath, wquery)
119 " Close window of dictionary [a:dic].
122 call s:PrevWindowMark()
123 if s:GoDictWindow() >= 0
125 call s:PrevWindowRevert()
130 " dir: 0:next / 1:previous
131 " Search next/previous word in dictionary.
132 function! s:Search(dic, dir)
133 if filereadable(a:dic) != 1
136 " Check existance of previous searched word
137 if s:lastpattern == '' || s:lastword == ''
139 echo s:myname. ': No given word before.'
143 " Initialize variables for direction
150 let dirname = 'previous'
151 let restart = 'BOTTOM'
154 call s:PrevWindowMark()
155 call s:OpenDictionaryWindow(a:dic)
156 let line_before = line('.')
157 execute 'silent! normal! ' .cmd. '^' .s:lastpattern. "\\>\<CR>"
158 let line_after = line('.')
160 if line_before == line_after
162 echo s:myname. ': No other "' .s:lastword. '".'
163 elseif (a:dir == 0 && line_before < line_after) || (a:dir != 0 && line_before > line_after)
164 echo s:myname. ': Found ' .dirname. ' "' .s:lastword. '" in line ' .line_after. '.'
167 echo s:myname. ': Search from ' .restart. ' and found ' .dirname. ' "' .s:lastword. '".'
170 " Revert previous window
171 call s:PrevWindowRevert()
174 " OpenDictionary(dic, word)
175 " Open window of dictionary [a:dic] and search a:word.
177 function! s:OpenDictionary(dic, word)
178 if filereadable(a:dic) != 1
182 if a:word ==# s:lastword
183 call s:Search(a:dic, 0)
186 " Remember previous window and open dictionary window
187 call s:PrevWindowMark()
188 call s:OpenDictionaryWindow(a:dic)
190 let s:lastword = a:word
191 let s:lastpattern = s:GenerateSearchPatternEnglish(a:word)
192 execute "silent! normal! gg/^" . s:lastpattern . "\\%( [+~]\\|$\\)\<CR>"
195 echo s:myname . ": Found \"" . s:lastword . '".'
198 echo s:myname . ": Can't find \"" . s:lastword . '".'
202 " output 'no word message'
204 echo s:myname . ": No word at under cursor."
207 " Leave dictionary window
208 call s:PrevWindowRevert()
211 function! s:OpenDictionaryWindow(name)
212 if filereadable(a:name) != 1
215 if s:GoDictWindow() < 0
216 execute "augroup Dictionary"
218 execute "autocmd WinEnter " . a:name . " call <SID>DicWinEnter()"
219 execute "autocmd WinLeave " . a:name . " call <SID>DicWinLeave()"
220 execute "autocmd BufUnload " . a:name . " call <SID>DicWinUnload()"
221 execute "augroup END"
222 execute 'silent normal! :sview ' . a:name ."\<CR>"
223 let w:dicwin_dicwin = 1
225 let s:lastpattern = ''
230 function! s:GoDictWindow()
231 return s:GoMarkedWindow('dicwin_dicwin')
234 function! s:GetMarkedWindow(name)
236 while winbufnr(winnum) >= 0
237 if getwinvar(winnum, a:name) > 0
240 let winnum = winnum + 1
245 function! s:GoMarkedWindow(name)
246 let winnum = s:GetMarkedWindow(a:name)
247 if winnum > 0 && winnr() != winnum
248 execute winnum.'wincmd w'
253 function! s:PrevWindowMark()
254 let w:dicwin_prevwin = 1
257 function! s:PrevWindowRevert()
258 if s:GoMarkedWindow('dicwin_prevwin') > 0
259 unlet w:dicwin_prevwin