Skip to content

Commit

Permalink
convert mode "h/j/k/l" key
Browse files Browse the repository at this point in the history
  • Loading branch information
bouzuya committed May 3, 2012
1 parent 8c1a53d commit cfea59e
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 38 deletions.
37 changes: 19 additions & 18 deletions autoload/bim/engine.vim
Expand Up @@ -55,7 +55,7 @@ function! s:d.okurirest()
endfunction

function! s:d.kanji()
return self._kanji
return self.pager().item()
endfunction

function! s:d.fixed()
Expand All @@ -73,25 +73,20 @@ endfunction

function! s:d.input(key)
call self._raw.input(a:key)
let self._pager = self._new_pager()
endfunction

function! s:d.remove_last()
let b = self._raw.is_okuri()
call self._raw.input_backspace()
let a = self._raw.is_okuri()
if b != a
let self._kanji = ''
endif
let self._pager = self._new_pager()
endfunction

function! s:d.convert()
let cand = self.candidate()
if empty(cand)
let self._kanji = ''
return
endif
let index = index(cand, self.kanji())
let self._kanji = get(cand, index + 1, cand[0])
call self.pager().next_item()
endfunction

function! s:d.pager()
return self._pager
endfunction

function! s:d.start_okuri()
Expand All @@ -100,11 +95,12 @@ endfunction

function! s:d.fix()
let result = ''
if strchars(self.kanji()) == 0
let conv = self.kanji()
if strchars(conv) == 0
let result = self.yomigana() . self.okurigana()
else
call bim#dict#add_word(self.yomigana() . self.okuri()[0], self.kanji())
let result = self.kanji() . self.okurigana()
call bim#dict#add_word(self.yomigana() . self.okuri()[0], conv)
let result = conv . self.okurigana()
endif
call self._fix(result)
endfunction
Expand All @@ -131,9 +127,14 @@ endfunction
function! s:d._fix(s)
let self._fixed .= a:s
let self._raw = bim#raw#new()
let self._kanji = ''
let self._pager = self._new_pager()
endfunction

function! s:d._new_pager()
return bim#pager#new(self.candidate(), 7, -1)
endfunction


" _romaji2hiragana({romaji}[, {proc_last}])
function! s:d._romaji2hiragana(romaji, ...)
let proc_last = get(a:000, 0, 0)
Expand Down Expand Up @@ -170,12 +171,12 @@ function! s:d._init()
\ }
call extend(self, {
\ '_raw': bim#raw#new(),
\ '_kanji': '',
\ '_fixed': '',
\ '_modes': modes,
\ '_mode': default_mode,
\ '_default_mode': default_mode
\ })
let self._pager = self._new_pager()
endfunction

let &cpoptions = s:save_cpoptions
Expand Down
4 changes: 4 additions & 0 deletions autoload/bim/handler.vim
Expand Up @@ -113,24 +113,28 @@ endfunction

function! bim#handler#c_h(bim, key)
let b = a:bim
call b.pager().prev_item()
call s:echo(b)
return ''
endfunction

function! bim#handler#c_j(bim, key)
let b = a:bim
call b.pager().next_page()
call s:echo(b)
return ''
endfunction

function! bim#handler#c_k(bim, key)
let b = a:bim
call b.pager().prev_page()
call s:echo(b)
return ''
endfunction

function! bim#handler#c_l(bim, key)
let b = a:bim
call b.pager().next_item()
call s:echo(b)
return ''
endfunction
Expand Down
52 changes: 32 additions & 20 deletions autoload/bim/pager.vim
Expand Up @@ -39,8 +39,10 @@ function! s:pager.next_page()
endif

let self._idx += self._ipp
if self._idx >= len(self._list)
let self._idx = self._idx - len(self._list) - 1
let l = len(self._list)
if self._idx >= l
let v = (l + self._ipp) / self._ipp * self._ipp
let self._idx = self._idx < v ? l - 1 : self._idx % self._ipp
endif
endfunction

Expand All @@ -51,39 +53,31 @@ function! s:pager.prev_page()

let self._idx -= self._ipp
if self._idx < 0
let self._idx = self._idx + len(self._list) + 1
let l = len(self._list)
let v = (l + self._ipp) / self._ipp * self._ipp
let self._idx = v + self._idx >= l ? l - 1 : v + self._idx
endif
endfunction

function! s:pager.pagenum()
return len(self.pages())
function! s:pager.items()
return self._list
endfunction

function! s:pager.idx()
return self._idx
function! s:pager.itemnum()
return len(self._list)
endfunction

function! s:pager.pageidx()
return self._idx / self._ipp
function! s:pager.idx()
return self._idx
endfunction

function! s:pager.item()
if self._idx == -1
if self._idx < 0
return ''
endif
return get(self._list, self._idx, '')
endfunction

function! s:pager.page()
return get(self.pages(), self.pageidx(), [])
endfunction

function! s:pager._init(list, ipp, idx)
let self._list = copy(a:list)
let self._ipp = a:ipp
let self._idx = a:idx
endfunction

function! s:pager.pages()
let l = self._list
let n = self._ipp
Expand All @@ -98,6 +92,24 @@ function! s:pager.pages()
return pages
endfunction

function! s:pager.pagenum()
return len(self.pages())
endfunction

function! s:pager.pageidx()
return self._idx / self._ipp
endfunction

function! s:pager.page()
return get(self.pages(), self.pageidx(), [])
endfunction

function! s:pager._init(list, ipp, idx)
let self._list = copy(a:list)
let self._ipp = a:ipp
let self._idx = a:idx
endfunction

let &cpoptions = s:save_cpoptions
unlet! s:save_cpoptions

17 changes: 17 additions & 0 deletions test/pager.vim
Expand Up @@ -130,6 +130,22 @@ function! s:test12()
call bim#assert#are_equal('', p.item())
endfunction

function! s:test13()
let p = bim#pager#new([
\ 'A', 'B', 'C', 'D', 'E', 'F', 'G',
\ 'H', 'I', 'J', 'K', 'L', 'M', 'N',
\ 'O', 'P', 'Q', 'R'
\ ], 7, 2)
call p.next_page()
call bim#assert#are_equal(9, p.idx())
call bim#assert#are_equal('J', p.item())
call p.next_page()
call bim#assert#are_equal(16, p.idx())
call bim#assert#are_equal('Q', p.item())
call p.next_page()
call bim#assert#are_equal(2, p.idx())
call bim#assert#are_equal('C', p.item())
endfunction

function! s:test()
call s:test1()
Expand All @@ -144,6 +160,7 @@ function! s:test()
call s:test10()
call s:test11()
call s:test12()
call s:test13()
endfunction

call s:test()
Expand Down

0 comments on commit cfea59e

Please sign in to comment.