Permalink
Browse files

cleanup

  • Loading branch information...
1 parent c71518b commit 87cf5aa93f83402b53ac38a63d6b138f15c6aee6 @t9md committed Feb 27, 2015
View
@@ -31,7 +31,52 @@ endfunction
" Main:
let s:cw = {}
-function! s:cw.get_env() "{{{1
+function! s:cw.start(winnums, ...) "{{{1
+ let conf = get(a:000, 0, {})
+ let self.conf = extend(choosewin#config#get(), conf)
+ let self.color = choosewin#color#get()
+ let winnums = self.valid_winnums(a:winnums)
+
+ try
+ call self.state_update(1)
+ call self.init()
+ call self.first_path(winnums)
+
+ call self.tab_replace()
+ call self.label_show(winnums, self.conf['label'])
+ while 1
+ call self.choose(self.win_all(), self.conf['label'])
+ endwhile
+ catch
+ let self.exception = v:exception
+ finally
+ call self.finish()
+ call self.state_update(0)
+ return self.last_status()
+ endtry
+endfunction
+
+function! s:cw.init() "{{{1
+ let self.exception = ''
+ let self.win_dest = ''
+ let self.statusline = {}
+ let self.tab_options = {}
+ let self.env = self.env_get()
+ let self.env_orig = deepcopy(self.env)
+ let self.tablabel = self.conf['tablabel']
+ let self._tablabel_split = s:_.str_split(self.tablabel)
+ if !has_key(self, 'previous')
+ let self.previous = []
+ endif
+ let self.keymap = filter(self.conf['keymap'], "v:val !=# '<NOP>'")
+
+ if self.conf['overlay_enable']
+ let self.overlay = choosewin#overlay#get()
+ endif
+ call self.tablabel_init(self.env.tab.all, self.tablabel)
+endfunction
+
+function! s:cw.env_get() "{{{1
return {
\ 'win': { 'cur': winnr(), 'all': self.win_all() },
\ 'tab': { 'cur': tabpagenr(), 'all': self.tab_all() },
@@ -53,17 +98,6 @@ function! s:cw.statusline_save(winnums) "{{{1
endfor
endfunction
-function! s:cw.blink_cword() "{{{1
- if ! self.conf['blink_on_land']
- return
- endif
- for i in range(2)
- let id = matchadd(self.color.Land, s:cword_pattern) | redraw | sleep 80m
- call matchdelete(id) | redraw | sleep 80m
- endfor
-endfunction
-let s:cword_pattern = '\k*\%#\k*'
-
function! s:cw.statusline_replace(winnums) "{{{1
call self.statusline_save(a:winnums)
@@ -79,6 +113,18 @@ function! s:cw.statusline_restore() "{{{1
endfor
endfunction
+
+function! s:cw.blink_cword() "{{{1
+ if ! self.conf['blink_on_land']
+ return
+ endif
+ for i in range(2)
+ let id = matchadd(self.color.Land, s:cword_pattern) | redraw | sleep 80m
+ call matchdelete(id) | redraw | sleep 80m
+ endfor
+endfunction
+let s:cword_pattern = '\k*\%#\k*'
+
function! s:cw.prepare_label(win, align) "{{{1
let pad = repeat(' ', self.conf['label_padding'])
let label = self.win2label[a:win]
@@ -116,7 +162,7 @@ function! s:cw.tabline() "{{{1
return R
endfunction
-function! s:cw.get_tablabel(tabnum)
+function! s:cw.get_tablabel(tabnum) "{{{1
return get(self._tablabel_split, a:tabnum - 1, '..')
endfunction
@@ -147,28 +193,6 @@ function! s:cw.tablabel_init(tabnums, label) "{{{1
let self.tab2label = s:dict_invert(self.label2tab)
endfunction
-function! s:cw.init() "{{{1
- let self.exception = ''
- let self.statusline = {}
- let self.tablabel = self.conf['tablabel']
- let self._tablabel_split = s:_.str_split(self.tablabel)
- let self.tab_options = {}
- let self.win_dest = ''
- let self.env = self.get_env()
- let self.env_orig = deepcopy(self.env)
- if !has_key(self, 'previous')
- let self.previous = []
- endif
- let self.keymap = filter(
- \ extend(self.keymap_default(), self.conf['keymap']),
- \ "v:val !=# '<NOP>'")
-
- if self.conf['overlay_enable']
- let self.overlay = choosewin#overlay#get()
- endif
- call self.tablabel_init(self.env.tab.all, self.tablabel)
-endfunction
-
function! s:cw.keymap_default() "{{{1
return {
\ '0': 'tab_first',
@@ -194,34 +218,6 @@ function! s:cw.read_input() "{{{1
return nr2char(getchar())
endfunction
-function! s:cw.config() "{{{1
- return {
- \ 'statusline_replace': g:choosewin_statusline_replace,
- \ 'tabline_replace': g:choosewin_tabline_replace,
- \ 'overlay_enable': g:choosewin_overlay_enable,
- \ 'overlay_font_size': g:choosewin_overlay_font_size,
- \ 'overlay_shade': g:choosewin_overlay_shade,
- \ 'overlay_shade_priority': g:choosewin_overlay_shade_priority,
- \ 'overlay_label_priority': g:choosewin_overlay_label_priority,
- \ 'overlay_clear_multibyte': g:choosewin_overlay_clear_multibyte,
- \ 'label_align': g:choosewin_label_align,
- \ 'label_padding': g:choosewin_label_padding,
- \ 'label_fill': g:choosewin_label_fill,
- \ 'blink_on_land': g:choosewin_blink_on_land,
- \ 'return_on_single_win': g:choosewin_return_on_single_win,
- \ 'label': g:choosewin_label,
- \ 'keymap': g:choosewin_keymap,
- \ 'tablabel': g:choosewin_tablabel,
- \ 'hook': g:choosewin_hook,
- \ 'hook_enable': g:choosewin_hook_enable,
- \ 'hook_bypass': g:choosewin_hook_bypass,
- \ 'debug': g:choosewin_debug,
- \ 'auto_choose': 0,
- \ 'noop': 0,
- \ 'swap': 0,
- \ }
-endfunction
-
function! s:cw.tab_choose(num) "{{{1
silent execute 'tabnext ' a:num
let self.env.tab.cur = a:num
@@ -404,30 +400,6 @@ function! s:cw.first_path(winnums) "{{{1
endif
endfunction
-function! s:cw.start(winnums, ...) "{{{1
- let self.conf = extend(self.config(), get(a:000, 0, {}), 'force')
- let self.color = choosewin#color#get()
- let winnums = self.valid_winnums(a:winnums)
-
- try
- call self.state_update(1)
- call self.init()
- call self.first_path(winnums)
-
- call self.tab_replace()
- call self.label_show(winnums, self.conf['label'])
- while 1
- call self.choose(self.win_all(), self.conf['label'])
- endwhile
- catch
- let self.exception = v:exception
- finally
- call self.finish()
- call self.state_update(0)
- return self.last_status()
- endtry
-endfunction
-
function! s:cw.state_update(state) "{{{1
" for statusline plugin
let g:choosewin_active = a:state
@@ -0,0 +1,74 @@
+let s:default = {
+ \ 'statusline_replace': 1,
+ \ 'tabline_replace': 1,
+ \ 'overlay_enable': 0,
+ \ 'overlay_font_size': 'auto',
+ \ 'overlay_shade': 0,
+ \ 'overlay_shade_priority': 100,
+ \ 'overlay_label_priority': 101,
+ \ 'overlay_clear_multibyte': 0,
+ \ 'label_align': 'center',
+ \ 'label_padding': 3,
+ \ 'tablabel': '123456789',
+ \ 'blink_on_land': 1,
+ \ 'return_on_single_win': 0,
+ \ 'label': 'ABCDEFGHIJKLMNOPQRTUVWXYZ',
+ \ 'hook': {},
+ \ 'hook_enable': 0,
+ \ 'hook_bypass': [],
+ \ 'land_char': ';',
+ \ 'active': 0,
+ \ 'debug': 0,
+ \ }
+
+let s:default.keymap = {
+ \ '0': 'tab_first',
+ \ '[': 'tab_prev',
+ \ ']': 'tab_next',
+ \ '$': 'tab_last',
+ \ ';': 'win_land',
+ \ '-': 'previous',
+ \ 's': 'swap',
+ \ 'S': 'swap_stay',
+ \ "\<CR>": 'win_land',
+ \ }
+
+" These are variables cannot set directly via global variable.
+let s:internal = {
+ \ 'swap': 0,
+ \ 'auto_choose': 0,
+ \ 'noop': 0,
+ \ }
+
+" Config:
+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)
+ unlet val
+ endfor
+ return R
+endfunction
+
+function! s:config.get() "{{{1
+ return extend(self.user(), s:internal)
+endfunction
+"}}}
+
+" API:
+function! choosewin#config#get() "{{{1
+ return s:config.get()
+endfunction
+
+
+" Test:
+" if expand("%:p") !=# expand("<sfile>:p")
+ " finish
+" endif
+" echo PP(choosewin#config#get())
+
+" vim: fdm=marker:
+
@@ -28,9 +28,9 @@ endfunction
" Font:
function! s:font_new(data) "{{{1
" Generate Font(=Dictionary) used by Overlay.
- let width = len(a:data[0])
- let height = len(a:data)
let [line_used, col_used, pattern] = s:pattern_gen(a:data)
+ let height = max(line_used) + 1
+ let width = max(col_used)
return {
\ 'width': width,
\ 'height': height,
@@ -54,7 +54,7 @@ function! s:pattern_gen(data) "{{{1
call add(line_used, i)
call add(R, s:_parse_column(i, val))
endfor
- let col_used = s:_.uniq(col_used)
+ let col_used = uniq(col_used)
return [line_used, col_used, '\v' . join(R, '|')]
endfunction
@@ -117,12 +117,11 @@ function! choosewin#font#large() "{{{1
return map(s:read_data(s:font_large),'s:font_new(v:val)')
endfunction
"}}}
-if expand("%:p") !=# expand("<sfile>:p")
- finish
-endif
-" let small_data = s:read_data(s:font_small)
-let large_data = s:read_data(s:font_large)
-" echo small_data
-echo s:pattern_gen(large_data['H'])
-
+"
+" if expand("%:p") !=# expand("<sfile>:p")
+ " finish
+" endif
+" let L = choosewin#font#large()
+" let R = map(L, 'v:val["width"]')
+" echo PP(R)
" vim: foldmethod=marker
@@ -88,7 +88,7 @@ function! s:Overlay.setup(wins, conf) "{{{1
let self.conf = a:conf
let self.wins = a:wins
let self.winnr_org = winnr()
- let self.bufs = s:_.uniq(tabpagebuflist(tabpagenr()))
+ let self.bufs = uniq(tabpagebuflist(tabpagenr()))
let self.options_global = s:_.buffer_options_set(bufnr(''), s:vim_options.global)
for bufnr in self.bufs
@@ -98,6 +98,7 @@ function! s:Overlay.setup(wins, conf) "{{{1
\ 'offset': [],
\ 'options': {},
\ 'undofile': tempname(),
+ \ 'font_width': [],
\ })
endfor
endfunction
@@ -122,16 +123,15 @@ function! s:Overlay.setup_window() "{{{1
let font = self._font_table[font_size][char]
let font_idx += 1
let wv.font = font
- let line_s = line('w0') + max([ 1 + (winheight(0) - s:FONT_MAX[font_size].height)/2, 0 ])
+ let line_s = line('w0') + max([ 1 + (winheight(0) - s:FONT_MAX[font_size].height)/2, 0 ])
let line_e = line_s + font.height - 1
- let col = max([(winwidth(0) - s:FONT_MAX[font_size].width)/2 , 1 ])
let offset = col('.') - wincol()
- let col += offset
+ let col = max([(winwidth(0) - font.width)/2 , 1 ]) + offset
let wv.matchids = []
- " let wv.pattern = s:intrpl(font.pattern, s:vars([line_s, col], font.width, font.height))
let wv.pattern = s:intrpl(font.pattern, s:vars([line_s, col], font))
let w:choosewin = wv
+ let b:choosewin.font_width += [font.width]
let b:choosewin.render_lines += range(line_s, line_e)
let b:choosewin.offset += [offset]
let b:choosewin.winwidth += [winwidth(0)]
@@ -147,16 +147,18 @@ function! s:Overlay.setup_buffer() "{{{1
let b:choosewin.options = s:_.buffer_options_set(bufnr, s:vim_options.buffer)
call s:undobreak()
- let render_lines = s:_.uniq(b:choosewin.render_lines)
+ let render_lines = uniq(b:choosewin.render_lines)
let append = max([max(render_lines) - line('$'), 0 ])
call append(line('$'), map(range(append), '""'))
- call self._fill_space(render_lines, max(b:choosewin.winwidth), max(b:choosewin.offset))
+ call self._fill_space(
+ \ render_lines,
+ \ max(b:choosewin.font_width), max(b:choosewin.winwidth), max(b:choosewin.offset))
endfor
noautocmd execute self.winnr_org 'wincmd w'
endfunction
-function! s:Overlay._fill_space(lines, width, offset) "{{{1
- let width = (a:width + s:FONT_MAX.large.width) / 2 + a:offset
+function! s:Overlay._fill_space(lines, font_width, width, offset) "{{{1
+ let width = (a:width + a:font_width) / 2 + a:offset
for line in a:lines
let line_s = getline(line)
if self.conf['overlay_clear_multibyte'] && s:_.include_multibyte_char(line_s)
Oops, something went wrong.

0 comments on commit 87cf5aa

Please sign in to comment.