Permalink
Browse files

refactoring done

  • Loading branch information...
1 parent 69b0c7c commit ddabfeb095a77a5db0e55f78200c10d3ae060614 @t9md committed Mar 2, 2015
Showing with 73 additions and 105 deletions.
  1. +60 −62 autoload/choosewin.vim
  2. +1 −1 autoload/choosewin/action.vim
  3. +8 −8 autoload/choosewin/color.vim
  4. +4 −5 autoload/choosewin/config.vim
  5. +0 −29 plugin/choosewin.vim
View
@@ -18,6 +18,7 @@ endfunction
" Wins:
let s:wins = {}
+
function! s:wins.get() "{{{1
return self._data
endfunction
@@ -27,68 +28,65 @@ function! s:wins.set(wins) "{{{1
let self._data = filter(a:wins, 'index(s:win_all(), v:val) isnot -1')
return self
endfunction
+"}}}
" Main:
let s:cw = {}
function! s:cw.start(wins, ...) "{{{1
- let self.conf = extend(choosewin#config#get(), get(a:000, 0, {}))
- let self.color = choosewin#color#get()
- let self.action = choosewin#action#init(self)
- let self.wins = s:wins.set(a:wins)
- let status = []
+ call self.init(a:wins, get(a:000, 0, {}))
+
try
+ let status = []
" Some status bar plugin need to know if choosewin active or not.
let g:choosewin_active = 1
- if empty(self.wins.get())
+ if empty(self.wins.get()) ||
+ \ ( len(self.wins.get()) is 1 && self.conf['return_on_single_win'] )
throw 'RETURN'
endif
- if len(self.wins.get()) is 1
- if self.conf['return_on_single_win']
- throw 'RETURN'
- endif
-
- if self.conf['auto_choose']
- call self.action.do_win(self.wins.get()[0])
- endif
+ if len(self.wins.get()) is 1 && self.conf['auto_choose']
+ call self.action.do_win(self.wins.get()[0])
endif
call self.setup()
call self.choose()
+
catch /\v^(CHOSE \d+|SWAP)$/
if self.conf['noop'] && v:exception =~# '^CHOSE'
- if self.env_org.tab isnot tabpagenr()
+ let tab = tabpagenr()
+ let win = str2nr(matchstr(v:exception, '\v^CHOSE \zs\d+'))
+ let status = [ tab, win ]
+ if tab isnot self.env_org.tab
call self.action.do_tab(self.env_org.tab)
endif
- let win = split(v:exception, '\v\s+')[-1]
- let status = [ tabpagenr(), str2nr(win) ]
else
let status = [ tabpagenr(), winnr() ]
endif
let self.previous = [ self.env_org.tab, self.env_org.win ]
catch /\v^(RETURN|CANCELED)$/
- let status = []
catch
let self.exception = v:exception
- let status = []
finally
call self.finish()
let g:choosewin_active = 0
return status
endtry
endfunction
-function! s:cw.setup() "{{{1
+function! s:cw.init(wins, conf) "{{{1
+ let self.wins = s:wins.set(a:wins)
+ let self.conf = extend(choosewin#config#get(), a:conf)
+ let self.action = choosewin#action#init(self)
+ let self.color = choosewin#color#get()
let self.exception = ''
let self.tab_options = {}
let self.statusline = {}
- let self.env_org = { 'win': winnr(), 'tab': tabpagenr(), }
- let self.label2tab = s:_.dict_create(self.conf['tablabel'], s:tab_all())
+ let self.env_org = {'win': winnr(), 'tab': tabpagenr()}
+endfunction
- if !has_key(self, 'previous')
- let self.previous = []
- endif
+function! s:cw.setup() "{{{1
+ let self.label2tab = s:_.dict_create(self.conf['tablabel'], s:tab_all())
if self.conf['overlay_enable']
let self.overlay = choosewin#overlay#get()
@@ -99,27 +97,6 @@ function! s:cw.setup() "{{{1
endif
endfunction
-function! s:cw.prepare_label(win, align) "{{{1
- let pad = repeat(' ', self.conf['label_padding'])
- let label = self.win2label[a:win]
- let win_s = pad . label . pad
- let color = self.color[ winnr() is a:win ? "LabelCurrent" : "Label" ]
-
- if a:align is 'left'
- return printf('%%#%s# %s %%#%s# %%= ', color, win_s, self.color.Other)
- endif
-
- if a:align is 'right'
- return printf('%%#%s# %%= %%#%s# %s ', self.color.Other, color, win_s)
- endif
-
- if a:align is 'center'
- let padding = repeat(' ', winwidth(a:win)/2-len(win_s))
- return printf('%%#%s# %s %%#%s# %s %%#%s# %%= ',
- \ self.color.Other, padding, color, win_s, self.color.Other)
- endif
-endfunction
-
function! s:cw.choose() "{{{1
while 1
call self.label_show()
@@ -153,6 +130,20 @@ function! s:cw.choose() "{{{1
endwhile
endfunction
+function! s:cw.finish() "{{{1
+ if !empty(self.tab_options)
+ call s:_.buffer_options_set(bufnr(''), self.tab_options)
+ endif
+ echo ''
+ redraw
+ if self.conf['blink_on_land']
+ call s:_.blink(2, self.color.Land, '\k*\%#\k*')
+ endif
+ if !empty(self.exception)
+ call s:_.message(self.exception)
+ endif
+endfunction
+
function! s:cw.call_hook(hook_point, arg) "{{{1
let HOOK = get(self.conf['hook'], a:hook_point, 0)
if s:_.is_Funcref(HOOK)
@@ -161,7 +152,9 @@ function! s:cw.call_hook(hook_point, arg) "{{{1
return a:arg
endif
endfunction
+"}}}
+" Label:
function! s:cw.label_show() "{{{1
if self.conf['hook_enable'] && index(self.conf['hook_bypass'], 'filter_window' ) is -1
let wins_new = self.call_hook('filter_window', copy(self.wins.get()))
@@ -197,6 +190,27 @@ function! s:cw.label_clear() "{{{1
call self.overlay.restore()
endif
endfunction
+
+function! s:cw.prepare_label(win, align) "{{{1
+ let pad = repeat(' ', self.conf['label_padding'])
+ let label = self.win2label[a:win]
+ let win_s = pad . label . pad
+ let color = self.color[ winnr() is a:win ? "LabelCurrent" : "Label" ]
+
+ if a:align is 'left'
+ return printf('%%#%s# %s %%#%s# %%= ', color, win_s, self.color.Other)
+ endif
+
+ if a:align is 'right'
+ return printf('%%#%s# %%= %%#%s# %s ', self.color.Other, color, win_s)
+ endif
+
+ if a:align is 'center'
+ let padding = repeat(' ', winwidth(a:win)/2-len(win_s))
+ return printf('%%#%s# %s %%#%s# %s %%#%s# %%= ',
+ \ self.color.Other, padding, color, win_s, self.color.Other)
+ endif
+endfunction
"}}}
" Tabline:
@@ -221,22 +235,6 @@ function! s:cw.get_tablabel(num) "{{{1
endfunction
"}}}
-" Restore:
-function! s:cw.finish() "{{{1
- if !empty(self.tab_options)
- call s:_.buffer_options_set(bufnr(''), self.tab_options)
- endif
- echo ''
- redraw
- if self.conf['blink_on_land']
- call s:_.blink(2, self.color.Land, '\k*\%#\k*')
- endif
- if !empty(self.exception)
- call s:_.message(self.exception)
- endif
-endfunction
-"}}}
-
" API:
function! choosewin#start(...) "{{{1
return call(s:cw.start, a:000, s:cw)
@@ -79,7 +79,7 @@ function! s:ac.do_tab_close() "{{{1
endfunction
function! s:ac.do_previous() "{{{1
- if empty(self.app.previous)
+ if !has_key(self.app, 'previous')
throw 'NO_PREVIOUS_WINDOW'
endif
@@ -7,18 +7,18 @@ function! s:Color.init() "{{{1
let config = choosewin#config#get()
let self.mgr = choosewin#hlmanager#new('ChooseWin')
- let color_Label = self.mgr.register(config.color_label)
+ let color_Label = self.mgr.register(config['color_label'])
let color = {
\ "Label": color_Label,
- \ "LabelCurrent": self.mgr.register(config.color_label_current),
- \ "Overlay": self.mgr.register(config.color_overlay),
- \ "OverlayCurrent": self.mgr.register(config.color_overlay_current),
- \ "Shade": self.mgr.register(config.color_shade),
+ \ "LabelCurrent": self.mgr.register(config['color_label_current']),
+ \ "Overlay": self.mgr.register(config['color_overlay']),
+ \ "OverlayCurrent": self.mgr.register(config['color_overlay_current']),
+ \ "Shade": self.mgr.register(config['color_shade']),
\ }
- let color.Other = g:choosewin_label_fill
- \ ? color_Label : self.mgr.register(config.color_other)
- let color.Land = self.mgr.register(config.color_land)
+ let color.Other = config['label_fill']
+ \ ? color_Label : self.mgr.register(config['color_other'])
+ let color.Land = self.mgr.register(config['color_land'])
let self.color = color
endfunction
@@ -44,7 +44,7 @@ let s:keymap = {
\ }
" These are variables cannot set directly via global variable.
-let s:internal = {
+let s:api_options = {
\ 'swap': 0,
\ 'swap_stay': 0,
\ 'auto_choose': 0,
@@ -57,15 +57,15 @@ let s:config = {}
function! s:config.user() "{{{1
let R = {}
let prefix = 'choosewin_'
- for [name, val] in items(s:default)
- let R[name] = get(g:, prefix . name, val)
+ for [name, default] in items(s:default)
+ let R[name] = get(g:, prefix . name, default)
unlet val
endfor
return R
endfunction
function! s:config.get() "{{{1
- let conf = extend(self.user(), s:internal)
+ let conf = extend(self.user(), s:api_options)
call extend(conf['keymap'], s:keymap, 'keep')
call filter(conf['keymap'], "v:val isnot '<NOP>'")
return conf
@@ -79,4 +79,3 @@ endfunction
"}}}
" vim: fdm=marker:
-
View
@@ -10,35 +10,6 @@ let s:old_cpo = &cpo
set cpo&vim
" Main:
-let s:options = {
- \ 'g:choosewin_label_fill': 0,
- \ 'g:choosewin_color_label':
- \ { 'gui': ['DarkGreen', 'white', 'bold'], 'cterm': [ 22, 15,'bold'] },
- \ 'g:choosewin_color_label_current':
- \ { 'gui': ['LimeGreen', 'black', 'bold'], 'cterm': [ 40, 16, 'bold'] },
- \ 'g:choosewin_color_overlay':
- \ { 'gui': ['DarkGreen', 'DarkGreen' ], 'cterm': [ 22, 22 ] },
- \ 'g:choosewin_color_overlay_current':
- \ { 'gui': ['LimeGreen', 'LimeGreen' ], 'cterm': [ 40, 40 ] },
- \ 'g:choosewin_color_other':
- \ { 'gui': ['gray20', 'black'], 'cterm': [ 240, 0] },
- \ 'g:choosewin_color_land':
- \ { 'gui':[ 'LawnGreen', 'Black', 'bold,underline'], 'cterm': ['magenta', 'white'] },
- \ 'g:choosewin_color_shade':
- \ { 'gui':[ '', '#777777'], 'cterm': ['', 'grey'] },
- \ }
-
-function! s:options_set(options)
- for [varname, value] in items(a:options)
- if !exists(varname)
- let {varname} = value
- endif
- unlet value
- endfor
-endfunction
-
-call s:options_set(s:options)
-
augroup plugin-choosewin
autocmd!
autocmd ColorScheme,SessionLoadPost * call choosewin#color#refresh()

0 comments on commit ddabfeb

Please sign in to comment.