Fixed vim and zsh
This commit is contained in:
529
vim/plugins/GoldenView/autoload/GoldenView.vim
Normal file
529
vim/plugins/GoldenView/autoload/GoldenView.vim
Normal file
@ -0,0 +1,529 @@
|
||||
" =============== ============================================================
|
||||
" Name : GoldenView
|
||||
" Description : Always have a nice view for vim split windows
|
||||
" Author : Zhao Cai <caizhaoff@gmail.com>
|
||||
" HomePage : https://github.com/zhaocai/GoldenView.Vim
|
||||
" Date Created : Tue 18 Sep 2012 10:25:23 AM EDT
|
||||
" Last Modified : Fri 19 Oct 2012 05:55:17 PM EDT
|
||||
" Tag : [ vim, window, golden-ratio ]
|
||||
" Copyright : © 2012 by Zhao Cai,
|
||||
" Released under current GPL license.
|
||||
" =============== ============================================================
|
||||
|
||||
" ============================================================================
|
||||
" Initialization And Profile: ⟨⟨⟨1
|
||||
" ============================================================================
|
||||
function! GoldenView#ExtendProfile(name, def)
|
||||
let default = get(s:goldenview__profile, a:name,
|
||||
\ copy(s:goldenview__profile['default']))
|
||||
|
||||
let s:goldenview__profile[a:name] = extend(default, a:def)
|
||||
endfunction
|
||||
|
||||
function! GoldenView#Init()
|
||||
if exists('g:goldenview__initialized') && g:goldenview__initialized == 1
|
||||
return
|
||||
endif
|
||||
|
||||
let s:goldenview__golden_ratio = 1.618
|
||||
lockvar s:goldenview__golden_ratio
|
||||
|
||||
set equalalways
|
||||
set eadirection=ver
|
||||
|
||||
|
||||
let s:goldenview__profile = {
|
||||
\ 'reset' : {
|
||||
\ 'focus_window_winheight' : &winheight ,
|
||||
\ 'focus_window_winwidth' : &winwidth ,
|
||||
\ 'other_window_winheight' : &winminheight ,
|
||||
\ 'other_window_winwidth' : &winminwidth ,
|
||||
\ },
|
||||
\ 'default' : {
|
||||
\ 'focus_window_winheight' : function('GoldenView#GoldenHeight') ,
|
||||
\ 'focus_window_winwidth' : function('GoldenView#TextWidth') ,
|
||||
\ 'other_window_winheight' : function('GoldenView#GoldenMinHeight') ,
|
||||
\ 'other_window_winwidth' : function('GoldenView#GoldenMinWidth') ,
|
||||
\ },
|
||||
\
|
||||
\ }
|
||||
|
||||
call GoldenView#ExtendProfile('golden-ratio', {
|
||||
\ 'focus_window_winwidth' : function('GoldenView#GoldenWidth') ,
|
||||
\ })
|
||||
|
||||
let s:goldenview__ignore_nrule = GoldenView#zl#rule#norm(
|
||||
\ g:goldenview__ignore_urule, {
|
||||
\ 'logic' : 'or',
|
||||
\ }
|
||||
\ )
|
||||
|
||||
let s:goldenview__restore_nrule = GoldenView#zl#rule#norm(
|
||||
\ g:goldenview__restore_urule, {
|
||||
\ 'logic' : 'or',
|
||||
\ }
|
||||
\ )
|
||||
let g:goldenview__initialized = 1
|
||||
endfunction
|
||||
|
||||
|
||||
|
||||
" ============================================================================
|
||||
" Auto Resize: ⟨⟨⟨1
|
||||
" ============================================================================
|
||||
function! GoldenView#ToggleAutoResize()
|
||||
if exists('s:goldenview__auto_resize') && s:goldenview__auto_resize == 1
|
||||
call GoldenView#DisableAutoResize()
|
||||
call GoldenView#zl#print#moremsg('GoldenView Auto Resize: Off')
|
||||
else
|
||||
call GoldenView#EnableAutoResize()
|
||||
call GoldenView#zl#print#moremsg('GoldenView Auto Resize: On')
|
||||
endif
|
||||
endfunction
|
||||
|
||||
|
||||
function! GoldenView#EnableAutoResize()
|
||||
|
||||
call GoldenView#Init()
|
||||
|
||||
let active_profile = s:goldenview__profile[g:goldenview__active_profile]
|
||||
call s:set_focus_window(active_profile)
|
||||
call s:set_other_window(active_profile)
|
||||
|
||||
augroup GoldenView
|
||||
au!
|
||||
" Enter
|
||||
autocmd VimResized * call GoldenView#Enter({'event' : 'VimResized'})
|
||||
autocmd BufWinEnter * call GoldenView#Enter({'event' : 'BufWinEnter'})
|
||||
autocmd WinEnter * call GoldenView#Enter({'event' : 'WinEnter'})
|
||||
|
||||
" Leave
|
||||
autocmd WinLeave * call GoldenView#Leave()
|
||||
augroup END
|
||||
let s:goldenview__auto_resize = 1
|
||||
|
||||
endfunction
|
||||
|
||||
|
||||
function! GoldenView#DisableAutoResize()
|
||||
au! GoldenView
|
||||
call GoldenView#ResetResize()
|
||||
|
||||
let s:goldenview__auto_resize = 0
|
||||
endfunction
|
||||
|
||||
function! GoldenView#Leave(...)
|
||||
|
||||
" GoldenViewTrace 'WinLeave', a:000
|
||||
|
||||
" Do nothing if there is no split window
|
||||
" --------------------------------------
|
||||
if winnr('$') < 2
|
||||
return
|
||||
endif
|
||||
|
||||
call GoldenView#Diff()
|
||||
|
||||
if GoldenView#IsIgnore()
|
||||
" Record the last size of ignored windows. Restore there sizes if affected
|
||||
" by GoldenView.
|
||||
|
||||
" For new split, the size does not count, which is highly possible
|
||||
" to be resized later. Should use the size with WinLeave event.
|
||||
"
|
||||
call GoldenView#initialize_tab_variable()
|
||||
let t:goldenview['bufs'][bufnr('%')] = {
|
||||
\ 'winnr' : winnr() ,
|
||||
\ 'winwidth' : winwidth(0) ,
|
||||
\ 'winheight' : winheight(0) ,
|
||||
\ }
|
||||
let t:goldenview['cmdheight'] = &cmdheight
|
||||
end
|
||||
endfunction
|
||||
|
||||
function! GoldenView#Diff()
|
||||
" Diff Mode: auto-resize to equal size
|
||||
if ! exists('b:goldenview_diff')
|
||||
let b:goldenview_diff = 0
|
||||
endif
|
||||
if &diff
|
||||
if ! b:goldenview_diff
|
||||
for nr in GoldenView#zl#list#uniq(tabpagebuflist())
|
||||
if getbufvar(nr, '&diff')
|
||||
call setbufvar(nr, 'goldenview_diff', 1)
|
||||
endif
|
||||
endfor
|
||||
exec 'wincmd ='
|
||||
endif
|
||||
return 1
|
||||
else
|
||||
if b:goldenview_diff
|
||||
let b:goldenview_diff = 0
|
||||
endif
|
||||
endif
|
||||
return 0
|
||||
endfunction
|
||||
|
||||
function! GoldenView#Enter(...)
|
||||
|
||||
if GoldenView#Diff()
|
||||
return
|
||||
endif
|
||||
|
||||
if &lazyredraw
|
||||
return
|
||||
endif
|
||||
|
||||
return call('GoldenView#Resize', a:000)
|
||||
endfunction
|
||||
|
||||
function! GoldenView#Resize(...)
|
||||
"--------- ------------------------------------------------
|
||||
" Desc : resize focused window
|
||||
"
|
||||
" Args : {'event' : event}
|
||||
" Return : none
|
||||
"
|
||||
" Raise : none from this function
|
||||
"
|
||||
" Pitfall :
|
||||
" - Can not set winminwith > winwidth
|
||||
" - AutoCmd Sequence:
|
||||
" - `:copen` :
|
||||
" 1. WinEnter (&ft inherited from last buffer)
|
||||
" 2. BufWinEnter (&ft == '')
|
||||
" 3. BufWinEnter (&ft == 'qf', set winfixheight)
|
||||
" - `:split`
|
||||
" 1. WinLeave current window
|
||||
" 2. WinEnter new split window with current buffer
|
||||
" 3. `split` return, user script may change the buffer
|
||||
" type, width, etc.
|
||||
"
|
||||
"
|
||||
"--------- ------------------------------------------------
|
||||
|
||||
" GoldenViewTrace 'GoldenView Resize', a:000
|
||||
|
||||
let opts = {'is_force' : 0}
|
||||
if a:0 >= 1 && GoldenView#zl#var#is_dict(a:1)
|
||||
call extend(opts, a:1)
|
||||
endif
|
||||
|
||||
let winnr_diff = s:winnr_diff()
|
||||
if winnr_diff > 0
|
||||
" Plus Split Window:
|
||||
" ++++++++++++++++++
|
||||
|
||||
" GoldenViewTrace '+++ winnr +++', a:000
|
||||
return
|
||||
|
||||
elseif winnr_diff < 0
|
||||
" Minus Split Window:
|
||||
" -------------------
|
||||
|
||||
call GoldenView#initialize_tab_variable()
|
||||
let saved_lazyredraw = &lazyredraw
|
||||
set lazyredraw
|
||||
let current_winnr = winnr()
|
||||
|
||||
" Restore: original size based on "g:goldenview__restore_urule"
|
||||
" ------------------------------------------------------------
|
||||
for winnr in range(1, winnr('$'))
|
||||
let bufnr = winbufnr(winnr)
|
||||
let bufsaved = get(t:goldenview['bufs'], bufnr, {})
|
||||
|
||||
" Ignored Case: same buffer displayed in multiply windows
|
||||
" -------------------------------------------------------
|
||||
if ! empty(bufsaved) && bufsaved['winnr'] == winnr
|
||||
silent noautocmd exec winnr 'wincmd w'
|
||||
|
||||
if GoldenView#IsRestore()
|
||||
silent exec 'vertical resize ' . bufsaved['winwidth']
|
||||
silent exec 'resize ' . bufsaved['winheight']
|
||||
|
||||
" GoldenViewTrace 'restore buffer:'. nr, a:000
|
||||
endif
|
||||
endif
|
||||
endfor
|
||||
|
||||
if &cmdheight != t:goldenview['cmdheight']
|
||||
exec 'set cmdheight=' . t:goldenview['cmdheight']
|
||||
endif
|
||||
|
||||
silent exec current_winnr 'wincmd w'
|
||||
|
||||
redraw
|
||||
let &lazyredraw = saved_lazyredraw
|
||||
|
||||
" GoldenViewTrace '--- winnr ---', a:000
|
||||
return
|
||||
endif
|
||||
|
||||
if ! opts['is_force']
|
||||
|
||||
" Do nothing if there is no split window
|
||||
if winnr('$') < 2
|
||||
return
|
||||
endif
|
||||
|
||||
if GoldenView#IsIgnore()
|
||||
" GoldenViewTrace 'Ignored', a:000
|
||||
return
|
||||
endif
|
||||
|
||||
endif
|
||||
|
||||
|
||||
let active_profile = s:goldenview__profile[g:goldenview__active_profile]
|
||||
call s:set_focus_window(active_profile)
|
||||
" GoldenViewTrace 'Set Focuse', a:000
|
||||
|
||||
|
||||
|
||||
" reset focus windows minimal size
|
||||
let &winheight = &winminheight
|
||||
let &winwidth = &winminwidth
|
||||
|
||||
" GoldenViewTrace 'Reset Focus', a:000
|
||||
endfunction
|
||||
|
||||
function! GoldenView#IsIgnore()
|
||||
return GoldenView#zl#rule#is_true(s:goldenview__ignore_nrule)
|
||||
endfunction
|
||||
|
||||
function! GoldenView#IsRestore()
|
||||
return GoldenView#zl#rule#is_true(s:goldenview__restore_nrule)
|
||||
endfunction
|
||||
|
||||
|
||||
function! GoldenView#ResetResize()
|
||||
let reset_profile = s:goldenview__profile[g:goldenview__reset_profile]
|
||||
call s:set_other_window(reset_profile, {'force' : 1})
|
||||
call s:set_focus_window(reset_profile, {'force' : 1})
|
||||
endfunction
|
||||
|
||||
|
||||
function! GoldenView#GoldenHeight(...)
|
||||
return float2nr(&lines / s:goldenview__golden_ratio)
|
||||
endfunction
|
||||
|
||||
|
||||
function! GoldenView#GoldenWidth(...)
|
||||
return float2nr(&columns / s:goldenview__golden_ratio)
|
||||
endfunction
|
||||
|
||||
|
||||
function! GoldenView#GoldenMinHeight(...)
|
||||
return float2nr(GoldenView#GoldenHeight()/(5*s:goldenview__golden_ratio))
|
||||
endfunction
|
||||
|
||||
|
||||
function! GoldenView#GoldenMinWidth(...)
|
||||
return float2nr(GoldenView#GoldenWidth()/(3*s:goldenview__golden_ratio))
|
||||
endfunction
|
||||
|
||||
|
||||
function! GoldenView#TextWidth(...)
|
||||
let tw = &l:textwidth
|
||||
|
||||
if tw != 0
|
||||
return float2nr(tw * 4/3)
|
||||
else
|
||||
let tw = float2nr(80 * 4/3)
|
||||
let gw = GoldenView#GoldenWidth()
|
||||
return tw > gw ? gw : tw
|
||||
endif
|
||||
endfunction
|
||||
|
||||
|
||||
function! s:set_focus_window(profile,...)
|
||||
let opts = {
|
||||
\ 'force' : 0
|
||||
\ }
|
||||
if a:0 >= 1 && GoldenView#zl#var#is_dict(a:1)
|
||||
call extend(opts, a:1)
|
||||
endif
|
||||
|
||||
try
|
||||
if !&winfixwidth || opts['force']
|
||||
let &winwidth =
|
||||
\ s:eval(a:profile, a:profile['focus_window_winwidth'])
|
||||
endif
|
||||
if !&winfixheight || opts['force']
|
||||
let &winheight =
|
||||
\ s:eval(a:profile, a:profile['focus_window_winheight'])
|
||||
endif
|
||||
catch /^Vim\%((\a\+)\)\=:E36/ " Not enough room
|
||||
call GoldenView#zl#print#warning('GoldenView: ' . v:exception)
|
||||
endtry
|
||||
endfunction
|
||||
|
||||
function! s:set_other_window(profile,...)
|
||||
let opts = {
|
||||
\ 'force' : 0
|
||||
\ }
|
||||
if a:0 >= 1 && GoldenView#zl#var#is_dict(a:1)
|
||||
call extend(opts, a:1)
|
||||
endif
|
||||
|
||||
try
|
||||
if !&winfixwidth || opts['force']
|
||||
let &winminwidth =
|
||||
\ s:eval(a:profile, a:profile['other_window_winwidth'])
|
||||
endif
|
||||
if !&winfixheight || opts['force']
|
||||
let &winminheight =
|
||||
\ s:eval(a:profile, a:profile['other_window_winheight'])
|
||||
endif
|
||||
catch /^Vim\%((\a\+)\)\=:E36/ " Not enough room
|
||||
call GoldenView#zl#print#warning('GoldenView: ' . v:exception)
|
||||
endtry
|
||||
endfunction
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
" ============================================================================
|
||||
" Split: ⟨⟨⟨1
|
||||
" ============================================================================
|
||||
function! GoldenView#Split()
|
||||
call GoldenView#zl#window#split_nicely()
|
||||
endfunction
|
||||
|
||||
|
||||
|
||||
" ============================================================================
|
||||
" Switch: ⟨⟨⟨1
|
||||
" ============================================================================
|
||||
function! GoldenView#SwitchMain(...)
|
||||
let opts = {
|
||||
\ 'from_bufnr' : bufnr('%') ,
|
||||
\}
|
||||
if a:0 >= 1 && type(a:1) == type({})
|
||||
call extend(opts, a:1)
|
||||
endif
|
||||
|
||||
let window_count = winnr('$')
|
||||
if window_count < 2
|
||||
return
|
||||
endif
|
||||
|
||||
let current_winnr = winnr()
|
||||
let switched = 0
|
||||
|
||||
let saved_lazyredraw = &lazyredraw
|
||||
set lazyredraw
|
||||
for i in range(1, window_count)
|
||||
silent noautocmd exec i 'wincmd w'
|
||||
|
||||
if ! GoldenView#IsIgnore()
|
||||
let switched = GoldenView#zl#window#switch_buffer(
|
||||
\ opts['from_bufnr'], winbufnr(i))
|
||||
break
|
||||
endif
|
||||
endfor
|
||||
|
||||
redraw
|
||||
let &lazyredraw = saved_lazyredraw
|
||||
|
||||
if switched
|
||||
call GoldenView#Resize({'event' : 'WinEnter'})
|
||||
else
|
||||
silent noautocmd exec current_winnr 'wincmd w'
|
||||
endif
|
||||
endfunction
|
||||
|
||||
" ============================================================================
|
||||
" Helper Functions: ⟨⟨⟨1
|
||||
" ============================================================================
|
||||
|
||||
function! s:eval(profile, val)
|
||||
if GoldenView#zl#var#is_number(a:val)
|
||||
return a:val
|
||||
elseif GoldenView#zl#var#is_funcref(a:val)
|
||||
return a:val(a:profile)
|
||||
else
|
||||
try
|
||||
return eval(a:val)
|
||||
catch /^Vim\%((\a\+)\)\=:E/
|
||||
throw 'GoldenView: invalid profile value type!'
|
||||
endtry
|
||||
endif
|
||||
endfunction
|
||||
|
||||
function! GoldenView#initialize_tab_variable()
|
||||
if !exists('t:goldenview')
|
||||
let t:goldenview = {
|
||||
\ 'nrwin' : winnr('$') ,
|
||||
\ 'cmdheight' : &cmdheight ,
|
||||
\ 'bufs' : {} ,
|
||||
\ }
|
||||
endif
|
||||
endfunction
|
||||
|
||||
function! s:winnr_diff()
|
||||
call GoldenView#initialize_tab_variable()
|
||||
|
||||
let nrwin = winnr('$')
|
||||
if nrwin != t:goldenview['nrwin']
|
||||
let diff = nrwin - t:goldenview['nrwin']
|
||||
let t:goldenview['nrwin'] = nrwin
|
||||
return diff
|
||||
else
|
||||
return 0
|
||||
endif
|
||||
endfunction
|
||||
|
||||
|
||||
|
||||
|
||||
" ============================================================================
|
||||
" Debug: ⟨⟨⟨1
|
||||
" ============================================================================
|
||||
|
||||
function! GoldenView#Info()
|
||||
return {
|
||||
\ 'buffer' : {
|
||||
\ 'filetype' : &ft ,
|
||||
\ 'buftype' : &buftype ,
|
||||
\ 'bufname' : bufname('%') ,
|
||||
\ 'winwidth' : winwidth(0) ,
|
||||
\ 'winheight' : winheight(0) ,
|
||||
\ },
|
||||
\ 'goldenview' : get(t:, 'goldenview', GoldenView#initialize_tab_variable()),
|
||||
\ 'setting' : {
|
||||
\ 'win_count' : winnr('$') ,
|
||||
\ 'lazyredraw' : &lazyredraw ,
|
||||
\ 'cmdheight' : &cmdheight ,
|
||||
\ 'winfixwidth' : &winfixwidth ,
|
||||
\ 'winfixheight' : &winfixheight ,
|
||||
\ 'winwidth' : &winwidth ,
|
||||
\ 'winminwidth' : &winminwidth ,
|
||||
\ 'winheight' : &winheight ,
|
||||
\ 'winminheight' : &winminheight ,
|
||||
\ }
|
||||
\}
|
||||
endfunction
|
||||
|
||||
|
||||
function! GoldenView#Trace(...)
|
||||
" -------- - -----------------------------------------------
|
||||
" Example : >
|
||||
" GoldenViewTrace 'WinLeave', a:000
|
||||
" -------- - -----------------------------------------------
|
||||
|
||||
call GoldenView#initialize_tab_variable()
|
||||
let info = GoldenView#Info()
|
||||
let info['context'] = get(g:,'GoldenView_zl_context','')
|
||||
let info['args'] = a:000
|
||||
call GoldenView#zl#log#debug(info)
|
||||
endfunction
|
||||
|
||||
command! -nargs=* -complete=expression GoldenViewTrace
|
||||
\ exec GoldenView#zl#vim#context() | call GoldenView#Trace(<args>)
|
||||
|
||||
" ============================================================================
|
||||
" Modeline: ⟨⟨⟨1
|
||||
" ============================================================================
|
||||
" vim: set ft=vim ts=4 sw=4 tw=78 fdm=syntax fmr=⟨⟨⟨,⟩⟩⟩ fdl=1 :
|
126
vim/plugins/GoldenView/autoload/GoldenView/zl/list.vim
Normal file
126
vim/plugins/GoldenView/autoload/GoldenView/zl/list.vim
Normal file
@ -0,0 +1,126 @@
|
||||
" =============== ============================================================
|
||||
" Synopsis : list helper functions
|
||||
" Author : Zhao Cai <caizhaoff@gmail.com>
|
||||
" HomePage : https://github.com/zhaocai/zl.vim
|
||||
" Version : 0.1
|
||||
" Date Created : Sat 03 Sep 2011 03:54:00 PM EDT
|
||||
" Last Modified : Thu 20 Sep 2012 04:25:10 PM EDT
|
||||
" Tag : [ vim, list ]
|
||||
" Copyright : (c) 2012 by Zhao Cai,
|
||||
" Released under current GPL license.
|
||||
" =============== ============================================================
|
||||
|
||||
|
||||
|
||||
" ============================================================================
|
||||
" Sort: ⟨⟨⟨1
|
||||
" ============================================================================
|
||||
|
||||
function! GoldenView#zl#list#unique_sort(list, ...)
|
||||
"--------- ------------------------------------------------
|
||||
" Args : list, [func]
|
||||
" Return : unique sorted list
|
||||
" Raise :
|
||||
"
|
||||
" Refer : http://vim.wikia.com/wiki/Unique_sorting
|
||||
"--------- ------------------------------------------------
|
||||
let list = copy(a:list)
|
||||
|
||||
if exists('a:1') && type(a:1) == type(function('function'))
|
||||
call sort(list, a:1)
|
||||
else
|
||||
call sort(list)
|
||||
endif
|
||||
if len(list) <= 1 | return list | endif
|
||||
let result = [ list[0] ]
|
||||
let last = list[0]
|
||||
let i = 1
|
||||
while i < len(list)
|
||||
if last != list[i]
|
||||
let last = list[i]
|
||||
call add(result, last)
|
||||
endif
|
||||
let i += 1
|
||||
endwhile
|
||||
return result
|
||||
endfunction
|
||||
|
||||
|
||||
|
||||
function! GoldenView#zl#list#sort_by(list, expr)
|
||||
"--------- ------------------------------------------------
|
||||
" Desc : sort list by expr
|
||||
"
|
||||
" Args : v:val is used in {expr}
|
||||
" Return : sroted list
|
||||
" Raise :
|
||||
"
|
||||
" Example : >
|
||||
" let list = [{'a' : 1}, {'a' : 22}, {'a' : 3}]
|
||||
" echo GoldenView#zl#list#sort_by(list, 'v:val["a"]')
|
||||
"
|
||||
" Refer : vital.vim
|
||||
"--------- ------------------------------------------------
|
||||
|
||||
let pairs = map(a:list, printf('[v:val, %s]', a:expr))
|
||||
return map(GoldenView#zl#list#sort(pairs,
|
||||
\ 'a:a[1] ==# a:b[1] ? 0 : a:a[1] ># a:b[1] ? 1 : -1'),
|
||||
\ 'v:val[0]')
|
||||
endfunction
|
||||
|
||||
function! s:_compare(a, b)
|
||||
return eval(s:expr)
|
||||
endfunction
|
||||
|
||||
function! GoldenView#zl#list#sort(list, expr)
|
||||
"--------- ------------------------------------------------
|
||||
" Desc : sort list with expr to compare two values.
|
||||
"
|
||||
" Args : a:a and a:b can be used in {expr}
|
||||
" Return : sroted list
|
||||
" Raise :
|
||||
"
|
||||
" Refer : vital.vim
|
||||
"--------- ------------------------------------------------
|
||||
|
||||
if type(a:expr) == type(function('function'))
|
||||
return sort(a:list, a:expr)
|
||||
endif
|
||||
let s:expr = a:expr
|
||||
return sort(a:list, 's:_compare')
|
||||
endfunction
|
||||
|
||||
|
||||
function! GoldenView#zl#list#uniq(list, ...)
|
||||
let list = a:0
|
||||
\ ? map(copy(a:list), printf('[v:val, %s]', a:1))
|
||||
\ : copy(a:list)
|
||||
let i = 0
|
||||
let seen = {}
|
||||
while i < len(list)
|
||||
let key = string(a:0 ? list[i][1] : list[i])
|
||||
if has_key(seen, key)
|
||||
call remove(list, i)
|
||||
else
|
||||
let seen[key] = 1
|
||||
let i += 1
|
||||
endif
|
||||
endwhile
|
||||
return a:0 ? map(list, 'v:val[0]') : list
|
||||
endfunction
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
" ============================================================================
|
||||
" Modeline: ⟨⟨⟨1
|
||||
" ============================================================================
|
||||
" vim: set ft=vim ts=4 sw=4 tw=78 fdm=marker fmr=⟨⟨⟨,⟩⟩⟩ fdl=1 :
|
310
vim/plugins/GoldenView/autoload/GoldenView/zl/log.vim
Normal file
310
vim/plugins/GoldenView/autoload/GoldenView/zl/log.vim
Normal file
@ -0,0 +1,310 @@
|
||||
" =============== ============================================================
|
||||
" Name : log.vim
|
||||
" Synopsis : vim script library: log
|
||||
" Author : Zhao Cai <caizhaoff@gmail.com>
|
||||
" HomePage : https://github.com/zhaocai/zl.vim
|
||||
" Date Created : Sat 03 Sep 2012 03:54:00 PM EDT
|
||||
" Last Modified : Sat 27 Apr 2013 06:58:13 PM EDT
|
||||
" Tag : [ vim, log ]
|
||||
" Copyright : © 2012 by Zhao Cai,
|
||||
" Released under current GPL license.
|
||||
" =============== ============================================================
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
" ============================================================================
|
||||
" Logger: [[[1
|
||||
" ============================================================================
|
||||
|
||||
|
||||
if !has('ruby')
|
||||
|
||||
function! GoldenView#zl#log#info(...)
|
||||
call GoldenView#zl#print#warning("GoldenView(log): require vim to be built with +ruby.")
|
||||
endfunction
|
||||
function! GoldenView#zl#log#level_info(...)
|
||||
call GoldenView#zl#print#warning("GoldenView(log): require vim to be built with +ruby.")
|
||||
endfunction
|
||||
|
||||
|
||||
function! GoldenView#zl#log#debug(...)
|
||||
call GoldenView#zl#print#warning("GoldenView(log): require vim to be built with +ruby.")
|
||||
endfunction
|
||||
function! GoldenView#zl#log#level_debug(...)
|
||||
call GoldenView#zl#print#warning("GoldenView(log): require vim to be built with +ruby.")
|
||||
endfunction
|
||||
|
||||
|
||||
function! GoldenView#zl#log#warn(...)
|
||||
call GoldenView#zl#print#warning("GoldenView(log): require vim to be built with +ruby.")
|
||||
endfunction
|
||||
function! GoldenView#zl#log#level_warn(...)
|
||||
call GoldenView#zl#print#warning("GoldenView(log): require vim to be built with +ruby.")
|
||||
endfunction
|
||||
|
||||
|
||||
function! GoldenView#zl#log#error(...)
|
||||
call GoldenView#zl#print#warning("GoldenView(log): require vim to be built with +ruby.")
|
||||
endfunction
|
||||
function! GoldenView#zl#log#level_error(...)
|
||||
call GoldenView#zl#print#warning("GoldenView(log): require vim to be built with +ruby.")
|
||||
endfunction
|
||||
|
||||
|
||||
function! GoldenView#zl#log#fatal(...)
|
||||
call GoldenView#zl#print#warning("GoldenView(log): require vim to be built with +ruby.")
|
||||
endfunction
|
||||
function! GoldenView#zl#log#level_fatal(...)
|
||||
call GoldenView#zl#print#warning("GoldenView(log): require vim to be built with +ruby.")
|
||||
endfunction
|
||||
|
||||
|
||||
else
|
||||
|
||||
|
||||
function! <SID>get_logfile()
|
||||
if GoldenView#zl#sys#is_mac()
|
||||
let logfile = expand('~/Library/Logs/vim/GoldenView.log')
|
||||
elseif GoldenView#zl#sys#is_win()
|
||||
let logfile = "C:/windows/temp/vim/GoldenView.log"
|
||||
elseif GoldenView#zl#sys#is_linux()
|
||||
let logfile = expand('/var/log/vim/GoldenView.log')
|
||||
else
|
||||
let logfile = input('Please Input logfile: ', '/var/log/vim/GoldenView.log', 'file')
|
||||
endif
|
||||
let log_dir = fnamemodify(logfile, ':p:h')
|
||||
if !isdirectory(log_dir)
|
||||
call mkdir(log_dir, 'p')
|
||||
endif
|
||||
return logfile
|
||||
endfunction
|
||||
|
||||
let s:GoldenView_zl_ruby_loaded = 0
|
||||
function! <SID>setup_ruby()
|
||||
if s:GoldenView_zl_ruby_loaded == 0
|
||||
|
||||
let logfile = <SID>get_logfile()
|
||||
|
||||
ruby << __RUBY__
|
||||
# ($LOAD_PATH << File.join(Vim.evaluate('g:GoldenView_zl_autoload_path'), 'lib')).uniq!
|
||||
require "logger"
|
||||
require "awesome_print"
|
||||
|
||||
$GoldenView_zl_logger = Logger.new(Vim.evaluate('logfile'), 'weekly')
|
||||
__RUBY__
|
||||
|
||||
autocmd VimLeavePre * call <SID>cleanup_ruby()
|
||||
let s:GoldenView_zl_ruby_loaded = 1
|
||||
endif
|
||||
endfunction
|
||||
|
||||
function! <SID>cleanup_ruby()
|
||||
if s:GoldenView_zl_ruby_loaded
|
||||
ruby $GoldenView_zl_logger.close
|
||||
endif
|
||||
endfunction
|
||||
|
||||
|
||||
" --------------------------------%>--------------------------------
|
||||
" Example:
|
||||
" function! Trace(...) abort
|
||||
" let message = {}
|
||||
" let message['context'] = get(g:,'GoldenView_zl_context','')
|
||||
" let message['args'] = a:000
|
||||
" call GoldenView#zl#log#info(message)
|
||||
" endfunction
|
||||
"
|
||||
" command! -nargs=* -complete=expression Trace
|
||||
" \ exec GoldenView#zl#vim#context() | call GoldenView#zl#log#info(<args>)
|
||||
" --------------------------------%<--------------------------------
|
||||
|
||||
|
||||
|
||||
|
||||
function! GoldenView#zl#log#info(...)
|
||||
call <SID>setup_ruby()
|
||||
ruby << __RUBY__
|
||||
|
||||
args = Vim.evaluate('a:000')
|
||||
|
||||
case args.size
|
||||
when 0
|
||||
return
|
||||
when 1
|
||||
$GoldenView_zl_logger.info("GoldenView") { args[0].ai(:plain => true) }
|
||||
when 2
|
||||
$GoldenView_zl_logger.info(args[0]) { args[1].ai(:plain => true) }
|
||||
else
|
||||
$GoldenView_zl_logger.info(args[0]) { args[1].ai(:plain => true) }
|
||||
end
|
||||
__RUBY__
|
||||
|
||||
endfunction
|
||||
|
||||
function! GoldenView#zl#log#level_info()
|
||||
ruby $GoldenView_zl_logger.level = Logger::INFO
|
||||
endfunction
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
function! GoldenView#zl#log#debug(...)
|
||||
call <SID>setup_ruby()
|
||||
ruby << __RUBY__
|
||||
|
||||
args = Vim.evaluate('a:000')
|
||||
|
||||
case args.size
|
||||
when 0
|
||||
return
|
||||
when 1
|
||||
$GoldenView_zl_logger.debug("GoldenView") { args[0].ai(:plain => true) }
|
||||
when 2
|
||||
$GoldenView_zl_logger.debug(args[0]) { args[1].ai(:plain => true) }
|
||||
else
|
||||
$GoldenView_zl_logger.debug(args[0]) { args[1].ai(:plain => true) }
|
||||
end
|
||||
__RUBY__
|
||||
|
||||
endfunction
|
||||
|
||||
function! GoldenView#zl#log#level_debug()
|
||||
ruby $GoldenView_zl_logger.level = Logger::DEBUG
|
||||
endfunction
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
function! GoldenView#zl#log#warn(...)
|
||||
call <SID>setup_ruby()
|
||||
ruby << __RUBY__
|
||||
|
||||
args = Vim.evaluate('a:000')
|
||||
|
||||
case args.size
|
||||
when 0
|
||||
return
|
||||
when 1
|
||||
$GoldenView_zl_logger.warn("GoldenView") { args[0].ai(:plain => true) }
|
||||
when 2
|
||||
$GoldenView_zl_logger.warn(args[0]) { args[1].ai(:plain => true) }
|
||||
else
|
||||
$GoldenView_zl_logger.warn(args[0]) { args[1].ai(:plain => true) }
|
||||
end
|
||||
__RUBY__
|
||||
|
||||
endfunction
|
||||
|
||||
function! GoldenView#zl#log#level_warn()
|
||||
ruby $GoldenView_zl_logger.level = Logger::WARN
|
||||
endfunction
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
function! GoldenView#zl#log#error(...)
|
||||
call <SID>setup_ruby()
|
||||
ruby << __RUBY__
|
||||
|
||||
args = Vim.evaluate('a:000')
|
||||
|
||||
case args.size
|
||||
when 0
|
||||
return
|
||||
when 1
|
||||
$GoldenView_zl_logger.error("GoldenView") { args[0].ai(:plain => true) }
|
||||
when 2
|
||||
$GoldenView_zl_logger.error(args[0]) { args[1].ai(:plain => true) }
|
||||
else
|
||||
$GoldenView_zl_logger.error(args[0]) { args[1].ai(:plain => true) }
|
||||
end
|
||||
__RUBY__
|
||||
|
||||
endfunction
|
||||
|
||||
function! GoldenView#zl#log#level_error()
|
||||
ruby $GoldenView_zl_logger.level = Logger::ERROR
|
||||
endfunction
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
function! GoldenView#zl#log#fatal(...)
|
||||
call <SID>setup_ruby()
|
||||
ruby << __RUBY__
|
||||
|
||||
args = Vim.evaluate('a:000')
|
||||
|
||||
case args.size
|
||||
when 0
|
||||
return
|
||||
when 1
|
||||
$GoldenView_zl_logger.fatal("GoldenView") { args[0].ai(:plain => true) }
|
||||
when 2
|
||||
$GoldenView_zl_logger.fatal(args[0]) { args[1].ai(:plain => true) }
|
||||
else
|
||||
$GoldenView_zl_logger.fatal(args[0]) { args[1].ai(:plain => true) }
|
||||
end
|
||||
__RUBY__
|
||||
|
||||
endfunction
|
||||
|
||||
function! GoldenView#zl#log#level_fatal()
|
||||
ruby $GoldenView_zl_logger.level = Logger::FATAL
|
||||
endfunction
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
endif
|
||||
|
||||
|
||||
" ============================================================================
|
||||
" Modeline: [[[1
|
||||
" ============================================================================
|
||||
" vim: set ts=4 sw=4 tw=78 fdm=marker fmr=[[[,]]] fdl=1 :
|
||||
|
||||
|
69
vim/plugins/GoldenView/autoload/GoldenView/zl/print.vim
Normal file
69
vim/plugins/GoldenView/autoload/GoldenView/zl/print.vim
Normal file
@ -0,0 +1,69 @@
|
||||
" =============== ============================================================
|
||||
" Name : print.vim
|
||||
" Synopsis : vim script library: print
|
||||
" Author : Zhao Cai <caizhaoff@gmail.com>
|
||||
" HomePage : https://github.com/zhaocai/zl.vim
|
||||
" Date Created : Sat 03 Sep 2011 03:54:00 PM EDT
|
||||
" Last Modified : Thu 20 Sep 2012 04:25:11 PM EDT
|
||||
" Tag : [ vim, print ]
|
||||
" Copyright : © 2012 by Zhao Cai,
|
||||
" Released under current GPL license.
|
||||
" =============== ============================================================
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
" ============================================================================
|
||||
" Echo Message: ⟨⟨⟨1
|
||||
" ============================================================================
|
||||
|
||||
function! GoldenView#zl#print#echomsg(message, ...)
|
||||
"--------- ------------------------------------------------
|
||||
" Desc : echomsg wrapper
|
||||
"
|
||||
" Args :
|
||||
" - message to print
|
||||
" - opts : >
|
||||
" {
|
||||
" 'hl' : 'MoreMsg' ,
|
||||
" }
|
||||
" Return :
|
||||
" Raise :
|
||||
"--------- ------------------------------------------------
|
||||
|
||||
if empty(a:message) | return | endif
|
||||
let opts = {
|
||||
\ 'hl' : 'MoreMsg',
|
||||
\ }
|
||||
if a:0 >= 1 && type(a:1) == type({})
|
||||
call extend(opts, a:1)
|
||||
endif
|
||||
|
||||
execute 'echohl ' . opts.hl
|
||||
for m in split(a:message, "\n")
|
||||
echomsg m
|
||||
endfor
|
||||
echohl NONE
|
||||
endfunction
|
||||
|
||||
function! GoldenView#zl#print#warning(message)
|
||||
call GoldenView#zl#print#echomsg(a:message, {'hl':'WarningMsg'})
|
||||
endfunction
|
||||
function! GoldenView#zl#print#error(message)
|
||||
call GoldenView#zl#print#echomsg(a:message, {'hl':'ErrorMsg'})
|
||||
endfunction
|
||||
function! GoldenView#zl#print#moremsg(message)
|
||||
call GoldenView#zl#print#echomsg(a:message, {'hl':'MoreMsg'})
|
||||
endfunction
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
" ============================================================================
|
||||
" Modeline: ⟨⟨⟨1
|
||||
" ============================================================================
|
||||
" vim: set ts=4 sw=4 tw=78 fdm=marker fmr=⟨⟨⟨,⟩⟩⟩ fdl=1 :
|
||||
|
||||
|
222
vim/plugins/GoldenView/autoload/GoldenView/zl/rc.vim
Normal file
222
vim/plugins/GoldenView/autoload/GoldenView/zl/rc.vim
Normal file
@ -0,0 +1,222 @@
|
||||
" =============== ============================================================
|
||||
" Description : vim script library
|
||||
" Author : Zhao Cai <caizhaoff@gmail.com>
|
||||
" HomePage : https://github.com/zhaocai/zl.vim
|
||||
" Date Created : Mon 03 Sep 2012 09:05:14 AM EDT
|
||||
" Last Modified : Thu 18 Apr 2013 03:16:39 PM EDT
|
||||
" Tag : [ vim, library ]
|
||||
" Copyright : © 2012 by Zhao Cai,
|
||||
" Released under current GPL license.
|
||||
" =============== ============================================================
|
||||
|
||||
" ============================================================================
|
||||
" Initialization: [[[1
|
||||
" ============================================================================
|
||||
|
||||
let s:GOLDENVIEW_ZL_VERSION_CURRENT = 140
|
||||
|
||||
let s:VERSION_FACTOR = str2float('0.01')
|
||||
|
||||
|
||||
function! GoldenView#zl#rc#init() abort
|
||||
if exists('g:loaded_GoldenView_zl')
|
||||
return
|
||||
endif
|
||||
|
||||
if v:version < 700
|
||||
echoerr "zl.vim: requires Vim >= 7"
|
||||
return
|
||||
endif
|
||||
|
||||
|
||||
if !exists("g:GoldenView_zl_force_reload")
|
||||
let g:GoldenView_zl_force_reload = 0
|
||||
endif
|
||||
|
||||
if !exists("g:GoldenView_zl_debug_mode")
|
||||
let g:GoldenView_zl_debug_mode = 0
|
||||
endif
|
||||
|
||||
let g:loaded_GoldenView_zl = s:GOLDENVIEW_ZL_VERSION_CURRENT * s:VERSION_FACTOR
|
||||
|
||||
endfunction
|
||||
|
||||
|
||||
|
||||
function! GoldenView#zl#rc#load_guard(prefix, vim_version, GoldenView_zl_version,exprs,...)
|
||||
"--------- ------------------------------------------------
|
||||
" Desc : gereric script load guard function
|
||||
"
|
||||
" Args :
|
||||
" - prefix : to generate loaded_var_name
|
||||
" - vim_version : minmium vim version requirement
|
||||
" - GoldenView_zl_version : minmium zl.vim version requirement.
|
||||
" set 0 to ignore.
|
||||
" - exprs : assert list of expresses to be true
|
||||
" - ... scope : 'g' , 'b', ...
|
||||
"
|
||||
" Return :
|
||||
" - 1 : unloaded
|
||||
" - 0 : already loaded
|
||||
"
|
||||
" Raise :
|
||||
"
|
||||
" Example : >
|
||||
" try | if !GoldenView#zl#rc#load_guard(expand('<sfile>:t:r'), 703, 140, ['!&cp'])
|
||||
" finish
|
||||
" endif
|
||||
" catch /^Vim\%((\a\+)\)\=:E117/
|
||||
" " E117: Unknown Function
|
||||
" throw 'zl.vim is required!'
|
||||
" endtry
|
||||
"
|
||||
" Details :
|
||||
" g:loaded_{script name} is defined as 1 if:
|
||||
" - vim version > 702
|
||||
" - zl version > 100
|
||||
" - test !&cp is true
|
||||
"
|
||||
" Refer :
|
||||
"--------- ------------------------------------------------
|
||||
|
||||
" call Dfunc('GoldenView#zl#rc#load_guard(' . a:prefix .' '. a:vim_version .' '. a:GoldenView_zl_version .' '. join(a:exprs))
|
||||
|
||||
let l:scope = a:0 >= 1 ? a:1 : 'g'
|
||||
|
||||
let l:loaded_var_name = l:scope . ':loaded_'
|
||||
\ . substitute(a:prefix, '[^0-9a-zA-Z_]', '_', 'g')
|
||||
" call Decho("loaded_var_name: " . l:loaded_var_name)
|
||||
|
||||
if exists(l:loaded_var_name) && g:GoldenView_zl_force_reload == 0
|
||||
" call Decho(l:loaded_var_name . ' loaded: return 0')
|
||||
" call Dret('GoldenView#zl#rc#load_guard')
|
||||
return 0
|
||||
endif
|
||||
|
||||
if a:vim_version > 0 && a:vim_version > v:version
|
||||
echoerr l:loaded_var_name . ' requires Vim version '
|
||||
\ . string(a:vim_version * s:VERSION_FACTOR)
|
||||
return 0
|
||||
elseif a:GoldenView_zl_version > 0
|
||||
if !exists("g:loaded_GoldenView_zl")
|
||||
echoerr 'zl.vim is required but not loaded'
|
||||
return 0
|
||||
endif
|
||||
|
||||
if (a:GoldenView_zl_version > s:GOLDENVIEW_ZL_VERSION_CURRENT)
|
||||
echoerr l:loaded_var_name . ' requires zl library version '
|
||||
\ . string(a:GoldenView_zl_version * s:VERSION_FACTOR)
|
||||
return 0
|
||||
endif
|
||||
endif
|
||||
for expr in a:exprs
|
||||
if !eval(expr)
|
||||
echoerr l:loaded_var_name . ' requires: ' . expr
|
||||
" call Dret('GoldenView#zl#rc#load_guard')
|
||||
return 0
|
||||
endif
|
||||
endfor
|
||||
let {l:loaded_var_name} = 1
|
||||
" call Dret('GoldenView#zl#rc#load_guard')
|
||||
return 1
|
||||
endfunction
|
||||
|
||||
function! GoldenView#zl#rc#script_force_reload(...)
|
||||
"--------- ------------------------------------------------
|
||||
" Desc : Call to ignore GoldenView#zl#rc#load_guard() and source.
|
||||
"
|
||||
" Args : script path or current script by default
|
||||
" Return :
|
||||
" Raise : E484: cannot open file
|
||||
"
|
||||
"--------- ------------------------------------------------
|
||||
let script = a:0 >= 1 ? a:1 : '%'
|
||||
|
||||
let l:saved = g:GoldenView_zl_force_reload
|
||||
let g:GoldenView_zl_force_reload = 1
|
||||
|
||||
exec "so " . script
|
||||
|
||||
let g:GoldenView_zl_force_reload = l:saved
|
||||
endfunction
|
||||
|
||||
|
||||
|
||||
|
||||
function! GoldenView#zl#rc#init_python()
|
||||
if has("python3")
|
||||
let g:GoldenView_zl_py = 'py3'
|
||||
let g:GoldenView_zl_pyeval = 'py3eval'
|
||||
elseif has("python")
|
||||
let g:GoldenView_zl_py = 'py'
|
||||
let g:GoldenView_zl_pyeval = 'pyeval'
|
||||
else
|
||||
let g:GoldenView_zl_py = ''
|
||||
let g:GoldenView_zl_pyeval = ''
|
||||
endif
|
||||
endfunction
|
||||
|
||||
|
||||
|
||||
" ============================================================================
|
||||
" Set Initialization Default Variables: [[[1
|
||||
" ============================================================================
|
||||
function! GoldenView#zl#rc#set_default(var, ...)
|
||||
"--------- ------------------------------------------------
|
||||
" Desc : Set Initialization Default Variables
|
||||
"
|
||||
" Args : ('var', val) || ( {dict} )
|
||||
" Return :
|
||||
" Raise : 'zl.vim ***'
|
||||
"
|
||||
" Pitfall : avoid 's:' variables, which will be defined in
|
||||
" this rc.vim script bur your script
|
||||
"
|
||||
" Example : >
|
||||
" call GoldenView#zl#rc#set_default({
|
||||
" \ 'g:xxx_yyy' : {
|
||||
" \ 'abc' : 1,
|
||||
" \ }
|
||||
" \
|
||||
" \ , 'g:yyy_zzz' : 'bcd'
|
||||
" \ })
|
||||
"--------- ------------------------------------------------
|
||||
|
||||
if type(a:var) == type({})
|
||||
for key in keys(a:var)
|
||||
call <SID>set_default(key, a:var[key])
|
||||
endfor
|
||||
elseif type(a:var) == type("")
|
||||
if a:0 >= 1
|
||||
call <SID>set_default(a:var, a:1)
|
||||
else
|
||||
throw "zl.vim should call with default value for " . a:var
|
||||
endif
|
||||
else
|
||||
throw "zl.vim unsupported type: " . type(a:var)
|
||||
endif
|
||||
endfunction
|
||||
|
||||
function! s:set_default(var,val)
|
||||
if !exists(a:var) || type({a:var}) != type(a:val)
|
||||
let {a:var} = a:val
|
||||
endif
|
||||
endfunction
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
"
|
||||
" Note:
|
||||
" add this call to other files if they need to check g:loaded_GoldenView_zl
|
||||
" or g:GoldenView_zl_install_path
|
||||
"
|
||||
call GoldenView#zl#rc#init()
|
||||
|
||||
|
||||
" ============================================================================
|
||||
" Modeline: [[[1
|
||||
" ============================================================================
|
||||
" vim: set ft=vim ts=4 sw=4 tw=78 fdm=marker fmr=[[[,]]] fdl=1 :
|
94
vim/plugins/GoldenView/autoload/GoldenView/zl/regex.vim
Normal file
94
vim/plugins/GoldenView/autoload/GoldenView/zl/regex.vim
Normal file
@ -0,0 +1,94 @@
|
||||
" =============== ============================================================
|
||||
" Name : regex.vim
|
||||
" Description : vim library: Regular Expression
|
||||
" Author : Zhao Cai <caizhaoff@gmail.com>
|
||||
" HomePage : https://github.com/zhaocai/zl.vim
|
||||
" Date Created : Mon 03 Sep 2012 09:05:14 AM EDT
|
||||
" Last Modified : Thu 20 Sep 2012 04:25:12 PM EDT
|
||||
" Tag : [ vim, library, regex ]
|
||||
" Copyright : © 2012 by Zhao Cai,
|
||||
" Released under current GPL license.
|
||||
" =============== ============================================================
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
" ============================================================================
|
||||
" Regex Escape: ⟨⟨⟨1
|
||||
" ============================================================================
|
||||
function! GoldenView#zl#regex#escape(text, ...) " (text, ?magic='m') ⟨⟨⟨2
|
||||
"--------- ------------------------------------------------
|
||||
" Desc : escape common special characters.
|
||||
"
|
||||
" Args :
|
||||
"
|
||||
" - text to escape
|
||||
" - magic : one of m, M, v, V. See :help 'magic'
|
||||
"
|
||||
" Return : escaped text
|
||||
" Raise :
|
||||
"
|
||||
" Example : echo GoldenView#zl#regex#escape('I|||love&you\\\', 'v')
|
||||
"
|
||||
" Refer :
|
||||
"--------- ------------------------------------------------
|
||||
|
||||
let l:magic = a:0 >= 1 ? a:1 : 'm'
|
||||
|
||||
if l:magic =~# '^\\\?m$'
|
||||
return escape(a:text, '^$.*\[]~')
|
||||
elseif l:magic =~# '^\\\?M$'
|
||||
return escape(a:text, '^$\')
|
||||
elseif l:magic =~# '^\\\?V$'
|
||||
return escape(a:text, '\')
|
||||
elseif l:magic =~# '^\\\?v$'
|
||||
return substitute(a:text, '[^0-9a-zA-Z_]', '\\&', 'g')
|
||||
else
|
||||
throw 'unsupported magic type'
|
||||
return a:text
|
||||
endif
|
||||
endfunction " ⟩⟩⟩
|
||||
|
||||
|
||||
|
||||
" ----------------------------------------------------------------------------
|
||||
" Regex Builder: ⟨⟨⟨1
|
||||
|
||||
|
||||
function! GoldenView#zl#regex#or(list, ...) "⟨⟨⟨
|
||||
let opts =
|
||||
\ { 'is_capturing' : 0
|
||||
\ , 'magic' : 'm'
|
||||
\ }
|
||||
if a:0 >= 1 && GoldenView#zl#var#is_dict(a:1)
|
||||
call extend(opts, a:1)
|
||||
endif
|
||||
|
||||
let begin = opts['is_capturing'] ? '\(' : '\%('
|
||||
let or = '\|'
|
||||
let end = '\)'
|
||||
|
||||
if opts['magic'] =~# '^\\\?v$'
|
||||
let begin = opts['is_capturing'] ? '(' : '%('
|
||||
let or = '|'
|
||||
let end = ')'
|
||||
endif
|
||||
|
||||
return begin
|
||||
\ . join(map(
|
||||
\ a:list, 'GoldenView#zl#regex#escape(v:val, "' . opts['magic'] . '")'),
|
||||
\ or)
|
||||
\ . end
|
||||
endfunction "⟩⟩⟩
|
||||
|
||||
" ⟩⟩⟩
|
||||
|
||||
|
||||
" ============================================================================
|
||||
" Modeline: ⟨⟨⟨1
|
||||
" ============================================================================
|
||||
" vim: set ft=vim ts=4 sw=4 tw=78 fdm=marker fmr=⟨⟨⟨,⟩⟩⟩ fdl=1 :
|
331
vim/plugins/GoldenView/autoload/GoldenView/zl/rule.vim
Normal file
331
vim/plugins/GoldenView/autoload/GoldenView/zl/rule.vim
Normal file
@ -0,0 +1,331 @@
|
||||
" =============== ============================================================
|
||||
" Name : rule.vim
|
||||
" Synopsis : vim script library: rule
|
||||
" Author : Zhao Cai <caizhaoff@gmail.com>
|
||||
" HomePage : https://github.com/zhaocai/zl.vim
|
||||
" Date Created : Sat 03 Sep 2011 03:54:00 PM EDT
|
||||
" Last Modified : Sat 29 Sep 2012 01:03:24 AM EDT
|
||||
" Tag : [ vim, rule ]
|
||||
" Copyright : © 2012 by Zhao Cai,
|
||||
" Released under current GPL license.
|
||||
" =============== ============================================================
|
||||
|
||||
|
||||
|
||||
" ============================================================================
|
||||
" Rule: [[[1
|
||||
" ============================================================================
|
||||
|
||||
" [TODO]( list for at type ) @zhaocai @start(2012-09-27 08:05)
|
||||
|
||||
let s:rule_types = [
|
||||
\ 'filetype', 'buftype', 'mode' , 'cword',
|
||||
\ 'bufname' , 'at' , 'syntax', 'expr' ,
|
||||
\ ]
|
||||
let s:nrule = {
|
||||
\ 'eval_order' : copy(s:rule_types),
|
||||
\ 'eval_negate' : [] ,
|
||||
\ 'logic' : 'or' ,
|
||||
\ 'rule' : {} ,
|
||||
\ }
|
||||
|
||||
function! GoldenView#zl#rule#norm(urule, ...)
|
||||
"--------- ------------------------------------------------
|
||||
" Desc : normalize rules
|
||||
"
|
||||
" Rule :
|
||||
" - "urule" : "Unnormalized RULE", rules written by users.
|
||||
" - "nrule" : "Nnormalized RULE", rules completed with
|
||||
" optional items and internal items.
|
||||
"
|
||||
" Args :
|
||||
" - urule: un-normalized rule
|
||||
" - opts :
|
||||
" - eval_order : order in s:rule_types,
|
||||
" - eval_negate : reverse eval result
|
||||
" - logic :
|
||||
" - {or} : 'v:filetype || v:bufname || ...'
|
||||
" - {and} : 'v:filetype && v:bufname && ...'
|
||||
" - {string} : similar to v:val for filter()
|
||||
"
|
||||
" Return : normalized rules
|
||||
" Raise :
|
||||
"
|
||||
" Example : >
|
||||
"
|
||||
" Refer :
|
||||
"--------- ------------------------------------------------
|
||||
let nrule = deepcopy(s:nrule)
|
||||
|
||||
if a:0 >= 1 && GoldenView#zl#var#is_dict(a:1)
|
||||
call extend(nrule, a:1)
|
||||
endif
|
||||
|
||||
let type_expr = {
|
||||
\ 'buftype' : '&buftype' ,
|
||||
\ 'filetype' : '&ft' ,
|
||||
\ 'bufname' : "bufname('%')" ,
|
||||
\ 'cword' : "expand('<cword>')" ,
|
||||
\ }
|
||||
|
||||
let type_pat = {}
|
||||
for type in ['filetype', 'buftype', 'syntax']
|
||||
if has_key(a:urule, type)
|
||||
let type_pat[type] = '^\%(' . join(a:urule[type], '\|') . '\)$'
|
||||
endif
|
||||
endfor
|
||||
for type in ['bufname', 'cword']
|
||||
if has_key(a:urule, type)
|
||||
let type_pat[type] = '^\%('
|
||||
\ . join(map(a:urule[type], 'GoldenView#zl#regex#escape(v:val)'), '\|')
|
||||
\ . '\)$'
|
||||
endif
|
||||
endfor
|
||||
|
||||
|
||||
" normalize each type of rules
|
||||
for type in ['mode']
|
||||
if has_key(a:urule, type)
|
||||
let nrule.rule[type] = a:urule[type]
|
||||
endif
|
||||
endfor
|
||||
|
||||
for type in ['filetype', 'buftype', 'bufname', 'cword']
|
||||
if has_key(a:urule, type)
|
||||
let nrule.rule[type] =
|
||||
\ {
|
||||
\ 'eval_expr' : 1 ,
|
||||
\ 'expr' : type_expr[type] ,
|
||||
\ 'pat' : type_pat[type] ,
|
||||
\ }
|
||||
endif
|
||||
endfor
|
||||
|
||||
|
||||
for type in ['syntax']
|
||||
if has_key(a:urule, type)
|
||||
let nrule.rule[type] = type_pat[type]
|
||||
endif
|
||||
endfor
|
||||
|
||||
for type in ['mode', 'at']
|
||||
if has_key(a:urule, type)
|
||||
let nrule.rule[type] = a:urule[type]
|
||||
endif
|
||||
endfor
|
||||
|
||||
for type in ['expr']
|
||||
if has_key(a:urule, type)
|
||||
try | let nrule.rule[type] =
|
||||
\ join(
|
||||
\ map(
|
||||
\ map(
|
||||
\ copy(a:urule[type])
|
||||
\ ,"join(v:val,' || ')"
|
||||
\ )
|
||||
\ , "'('.v:val.')'"
|
||||
\ )
|
||||
\ ,' && '
|
||||
\ )
|
||||
catch /^Vim\%((\a\+)\)\=:E714/ " E714: List required
|
||||
throw 'zl(rule): expr rule should be written as list of lists.'
|
||||
endtry
|
||||
endif
|
||||
endfor
|
||||
|
||||
call filter(
|
||||
\ nrule['eval_order']
|
||||
\ , 'has_key(nrule.rule, v:val) && !empty(nrule.rule[v:val])'
|
||||
\ )
|
||||
|
||||
return nrule
|
||||
endfunction
|
||||
|
||||
|
||||
function! GoldenView#zl#rule#is_true(nrule, ...)
|
||||
try
|
||||
return call('GoldenView#zl#rule#logic_'.a:nrule['logic'], [a:nrule] + a:000)
|
||||
catch /^Vim\%((\a\+)\)\=:E129/
|
||||
throw 'zl(rule): undefined logic funcref'
|
||||
endtry
|
||||
endfunction
|
||||
|
||||
|
||||
function! GoldenView#zl#rule#is_false(nrule, ...)
|
||||
return !call('GoldenView#zl#rule#is_true', [a:nrule] + a:000)
|
||||
endfunction
|
||||
|
||||
|
||||
" rule logic
|
||||
function! s:_return(nrule, type, ret)
|
||||
return
|
||||
\ index(a:nrule.eval_negate, a:type) >= 0
|
||||
\ ? !a:ret
|
||||
\ : a:ret
|
||||
endfunction
|
||||
|
||||
function! GoldenView#zl#rule#logic_or(nrule, ...)
|
||||
let opts = {}
|
||||
if a:0 >= 1 && GoldenView#zl#var#is_dict(a:1)
|
||||
call extend(opts, a:1)
|
||||
endif
|
||||
|
||||
for type in a:nrule['eval_order']
|
||||
if s:_return(a:nrule, type, s:eval_{type}(a:nrule['rule'], opts))
|
||||
return 1
|
||||
endif
|
||||
endfor
|
||||
return 0
|
||||
endfunction
|
||||
|
||||
function! GoldenView#zl#rule#logic_and(nrule, ...)
|
||||
let opts = {}
|
||||
if a:0 >= 1 && GoldenView#zl#var#is_dict(a:1)
|
||||
call extend(opts, a:1)
|
||||
endif
|
||||
|
||||
for type in a:nrule['eval_order']
|
||||
if !s:_return(a:nrule, type, s:eval_{type}(a:nrule['rule'], opts))
|
||||
return 0
|
||||
endif
|
||||
endfor
|
||||
return 1
|
||||
endfunction
|
||||
|
||||
function! GoldenView#zl#rule#logic_expr(nrule, ...)
|
||||
let opts = {}
|
||||
if a:0 >= 1 && GoldenView#zl#var#is_dict(a:1)
|
||||
call extend(opts, a:1)
|
||||
endif
|
||||
|
||||
let str = a:nrule['expr']
|
||||
for type in a:nrule['eval_order']
|
||||
let str = substitute(str
|
||||
\ , 'v:'.type
|
||||
\ , string(
|
||||
\ s:_return(a:nrule, type,
|
||||
\ s:eval_{type}(a:nrule['rule'], opts)
|
||||
\ )
|
||||
\ )
|
||||
\ , 'ge'
|
||||
\ )
|
||||
endfor
|
||||
|
||||
try
|
||||
return eval(str)
|
||||
catch /^Vim\%((\a\+)\)\=:E/
|
||||
throw printf('zl(rule): eval(%s) raises %s', str, v:exception)
|
||||
endtry
|
||||
endfunction
|
||||
|
||||
|
||||
|
||||
" nrule eval
|
||||
function! s:eval_filetype(nrule, ...)
|
||||
return call('s:_eval_match', ['filetype', a:nrule] + a:000)
|
||||
endfunction
|
||||
|
||||
function! s:eval_cword(nrule, ...)
|
||||
return call('s:_eval_match', ['cword', a:nrule] + a:000)
|
||||
endfunction
|
||||
|
||||
function! s:eval_buftype(nrule, ...)
|
||||
return call('s:_eval_match', ['buftype', a:nrule] + a:000)
|
||||
endfunction
|
||||
|
||||
function! s:eval_bufname(nrule, ...)
|
||||
return call('s:_eval_match', ['bufname', a:nrule] + a:000)
|
||||
endfunction
|
||||
|
||||
|
||||
function! s:eval_at(nrule, ...)
|
||||
return search(get(a:nrule, 'at', '\%#'), 'bcnW')
|
||||
endfunction
|
||||
|
||||
function! s:eval_mode(nrule, ...)
|
||||
let mode_pat = get(a:nrule, 'mode', [])
|
||||
let mode_expr =
|
||||
\ a:0 >= 1 && GoldenView#zl#var#is_dict(a:1)
|
||||
\ ? get(a:1, 'mode', mode())
|
||||
\ : mode()
|
||||
|
||||
return
|
||||
\ !empty(
|
||||
\ filter(
|
||||
\ mode_pat
|
||||
\ , 'stridx(mode_expr, v:val) == -1'
|
||||
\ )
|
||||
\ )
|
||||
endfunction
|
||||
|
||||
function! s:eval_syntax(nrule, ...)
|
||||
let pat = get(a:nrule, 'syntax', '')
|
||||
|
||||
let opts = {}
|
||||
if a:0 >= 1 && GoldenView#zl#var#is_dict(a:1)
|
||||
call extend(opts, a:1)
|
||||
endif
|
||||
let syn_names = GoldenView#zl#syntax#synstack_names(opts)
|
||||
|
||||
return !empty(filter(syn_names, 'match(v:val, pat) != -1'))
|
||||
endfunction
|
||||
|
||||
function! s:eval_expr(nrule, ...)
|
||||
try
|
||||
return eval(get(a:nrule, 'expr', 1))
|
||||
catch /^Vim\%((\a\+)\)\=:E/
|
||||
return 0
|
||||
endtry
|
||||
endfunction
|
||||
|
||||
function! s:_eval_match(type, nrule, ...)
|
||||
"--------- ------------------------------------------------
|
||||
" Desc : internal match evluation
|
||||
" Rule :
|
||||
" { 'type' :
|
||||
" {
|
||||
" 'eval_expr' : (1|0) ,
|
||||
" 'expr' : {expr} ,
|
||||
" 'pat' : {pat} ,
|
||||
" }
|
||||
" }
|
||||
"
|
||||
" Args : [{type}, {nrule}[, {opts}]]
|
||||
" Return :
|
||||
" - 0 : false
|
||||
" - 1 : true
|
||||
" Raise : zl(rule)
|
||||
"
|
||||
" Refer : vimhelp:match()
|
||||
"--------- ------------------------------------------------
|
||||
|
||||
let rule = copy(get(a:nrule, a:type, {}))
|
||||
if empty(rule)
|
||||
throw 'zl(rule): ' . v:exception
|
||||
endif
|
||||
|
||||
" opt for {expr} from runtime opts
|
||||
if a:0 >= 1 && GoldenView#zl#var#is_dict(a:1) && has_key(a:1, a:type)
|
||||
let rt_rule = a:1[a:type]
|
||||
if GoldenView#zl#var#is_dict(rt_rule)
|
||||
call extend(rule, rt_rule)
|
||||
elseif GoldenView#zl#var#is_string(rt_rule)
|
||||
let rule['expr'] = rt_rule
|
||||
let rule['eval_expr'] = 0
|
||||
endif
|
||||
endif
|
||||
if rule['eval_expr']
|
||||
let rule['expr'] = eval(rule['expr'])
|
||||
endif
|
||||
try
|
||||
return call('match', [rule['expr'], rule['pat']]) != -1
|
||||
catch /^Vim\%((\a\+)\)\=:E/
|
||||
throw 'zl(rule): ' . v:exception
|
||||
endtry
|
||||
endfunction
|
||||
|
||||
|
||||
" ============================================================================
|
||||
" Modeline: [[[1
|
||||
" ============================================================================
|
||||
" vim: set ft=vim ts=4 sw=4 tw=78 fdm=marker fmr=[[[,]]] fdl=1 :
|
78
vim/plugins/GoldenView/autoload/GoldenView/zl/sys.vim
Normal file
78
vim/plugins/GoldenView/autoload/GoldenView/zl/sys.vim
Normal file
@ -0,0 +1,78 @@
|
||||
" =============== ============================================================
|
||||
" Name : sys.vim
|
||||
" Description : vim library
|
||||
" Author : Zhao Cai <caizhaoff@gmail.com>
|
||||
" HomePage : https://github.com/zhaocai/zl.vim
|
||||
" Date Created : Mon 03 Sep 2012 09:05:14 AM EDT
|
||||
" Last Modified : Thu 20 Sep 2012 04:25:15 PM EDT
|
||||
" Tag : [ vim, library ]
|
||||
" Copyright : © 2012 by Zhao Cai,
|
||||
" Released under current GPL license.
|
||||
" =============== ============================================================
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
" ============================================================================
|
||||
" Environment: [[[1
|
||||
" ============================================================================
|
||||
|
||||
function! GoldenView#zl#sys#ostype()
|
||||
"--------- ------------------------------------------------
|
||||
" Args :
|
||||
" Return :
|
||||
" - Mac, Windows, Linux, FreeBSD, SunOS, Unix,
|
||||
" - undefined
|
||||
" Raise :
|
||||
"
|
||||
" Example : >
|
||||
" call GoldenView#zl#rc#set_default('g:os_type', GoldenView#zl#sys#ostype())
|
||||
"--------- ------------------------------------------------
|
||||
if (has("win32") || has("win95") || has("win64") || has("win16"))
|
||||
let s:os_type = "Windows"
|
||||
elseif has('mac')
|
||||
let s:os_type="Mac"
|
||||
elseif has('unix')
|
||||
let arch = system("uname | tr -d '\n'")
|
||||
if ( arch=="Linux" )
|
||||
let s:os_type="Linux"
|
||||
elseif ( arch=="FreeBSD" )
|
||||
let s:os_type="FreeBSD"
|
||||
elseif ( arch=="SunOS" )
|
||||
let s:os_type="SunOS"
|
||||
elseif ( arch=="Darwin" )
|
||||
let s:os_type="Mac"
|
||||
else
|
||||
let s:os_type="Unix"
|
||||
endif
|
||||
endif
|
||||
return s:os_type
|
||||
endfunction
|
||||
|
||||
function! GoldenView#zl#sys#is_cygwin()
|
||||
return s:is_cygwin
|
||||
endfunction
|
||||
|
||||
function! GoldenView#zl#sys#is_win()
|
||||
return s:os_type == 'Windows'
|
||||
endfunction
|
||||
function! GoldenView#zl#sys#is_mac()
|
||||
return s:os_type == 'Mac'
|
||||
endfunction
|
||||
function! GoldenView#zl#sys#is_linux()
|
||||
return s:os_type == 'Linux'
|
||||
endfunction
|
||||
|
||||
let s:os_type = GoldenView#zl#sys#ostype()
|
||||
let s:is_cygwin = has('win32unix')
|
||||
|
||||
" ============================================================================
|
||||
" Modeline: [[[1
|
||||
" ============================================================================
|
||||
" vim: set ft=vim ts=4 sw=4 tw=78 fdm=marker fmr=[[[,]]] fdl=1 :
|
86
vim/plugins/GoldenView/autoload/GoldenView/zl/var.vim
Normal file
86
vim/plugins/GoldenView/autoload/GoldenView/zl/var.vim
Normal file
@ -0,0 +1,86 @@
|
||||
" =============== ============================================================
|
||||
" Name : var.vim
|
||||
" Synopsis : vim script library: variable
|
||||
" Author : Zhao Cai <caizhaoff@gmail.com>
|
||||
" HomePage : https://github.com/zhaocai/zl.vim
|
||||
" Date Created : Sat 03 Sep 2011 03:54:00 PM EDT
|
||||
" Last Modified : Thu 20 Sep 2012 04:25:16 PM EDT
|
||||
" Tag : [ vim, variable ]
|
||||
" Copyright : © 2012 by Zhao Cai,
|
||||
" Released under current GPL license.
|
||||
" =============== ============================================================
|
||||
|
||||
|
||||
|
||||
|
||||
" ============================================================================
|
||||
" Type: [[[1
|
||||
" ============================================================================
|
||||
|
||||
|
||||
"--------- ------------------------------------------------
|
||||
" Desc : Wrapper functions for type()
|
||||
"
|
||||
" Refer : vital.vim
|
||||
"--------- ------------------------------------------------
|
||||
|
||||
let [
|
||||
\ s:__TYPE_NUMBER,
|
||||
\ s:__TYPE_STRING,
|
||||
\ s:__TYPE_FUNCREF,
|
||||
\ s:__TYPE_LIST,
|
||||
\ s:__TYPE_DICT,
|
||||
\ s:__TYPE_FLOAT
|
||||
\] = [
|
||||
\ type(3),
|
||||
\ type(""),
|
||||
\ type(function('tr')),
|
||||
\ type([]),
|
||||
\ type({}),
|
||||
\ has('float') ? type(str2float('0')) : -1
|
||||
\]
|
||||
" __TYPE_FLOAT = -1 when -float
|
||||
" This doesn't match to anything.
|
||||
|
||||
" Number or Float
|
||||
function! GoldenView#zl#var#is_numeric(Value)
|
||||
let _ = type(a:Value)
|
||||
return _ ==# s:__TYPE_NUMBER
|
||||
\ || _ ==# s:__TYPE_FLOAT
|
||||
endfunction
|
||||
" Number
|
||||
function! GoldenView#zl#var#is_integer(Value)
|
||||
return type(a:Value) ==# s:__TYPE_NUMBER
|
||||
endfunction
|
||||
function! GoldenView#zl#var#is_number(Value)
|
||||
return type(a:Value) ==# s:__TYPE_NUMBER
|
||||
endfunction
|
||||
" Float
|
||||
function! GoldenView#zl#var#is_float(Value)
|
||||
return type(a:Value) ==# s:__TYPE_FLOAT
|
||||
endfunction
|
||||
" String
|
||||
function! GoldenView#zl#var#is_string(Value)
|
||||
return type(a:Value) ==# s:__TYPE_STRING
|
||||
endfunction
|
||||
" Funcref
|
||||
function! GoldenView#zl#var#is_funcref(Value)
|
||||
return type(a:Value) ==# s:__TYPE_FUNCREF
|
||||
endfunction
|
||||
" List
|
||||
function! GoldenView#zl#var#is_list(Value)
|
||||
return type(a:Value) ==# s:__TYPE_LIST
|
||||
endfunction
|
||||
" Dictionary
|
||||
function! GoldenView#zl#var#is_dict(Value)
|
||||
return type(a:Value) ==# s:__TYPE_DICT
|
||||
endfunction
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
" ============================================================================
|
||||
" Modeline: [[[1
|
||||
" ============================================================================
|
||||
" vim: set ft=vim ts=4 sw=4 tw=78 fdm=marker fmr=[[[,]]] fdl=1 :
|
50
vim/plugins/GoldenView/autoload/GoldenView/zl/vim.vim
Normal file
50
vim/plugins/GoldenView/autoload/GoldenView/zl/vim.vim
Normal file
@ -0,0 +1,50 @@
|
||||
" =============== ============================================================
|
||||
" Description : vim library: vim
|
||||
" Author : Zhao Cai <caizhaoff@gmail.com>
|
||||
" HomePage : https://github.com/zhaocai/zl.vim
|
||||
" Date Created : Mon 03 Sep 2012 09:05:14 AM EDT
|
||||
" Last Modified : Thu 20 Sep 2012 06:01:48 PM EDT
|
||||
" Tag : [ vim, library, debug ]
|
||||
" Copyright : © 2012 by Zhao Cai,
|
||||
" Released under current GPL license.
|
||||
" =============== ============================================================
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
" ============================================================================
|
||||
" Context: [[[1
|
||||
" ============================================================================
|
||||
function! GoldenView#zl#vim#context()
|
||||
" -------- - -----------------------------------------------
|
||||
" Desc : generate context
|
||||
"
|
||||
" Example : >
|
||||
" function BeTraced(...)
|
||||
" exec GoldenView#zl#vim#context() | call XXX#Trace(a:000)
|
||||
" endfunction
|
||||
"
|
||||
" function XXX#Trace(...)
|
||||
" let context = g:GoldenView_zl_context
|
||||
" "...
|
||||
" endfunction
|
||||
" Refer :
|
||||
" -------- - -----------------------------------------------
|
||||
return
|
||||
\'try
|
||||
\| throw ""
|
||||
\|catch
|
||||
\| let g:GoldenView_zl_context = v:throwpoint
|
||||
\|endtry
|
||||
\'
|
||||
endfunction
|
||||
|
||||
|
||||
" ============================================================================
|
||||
" Modeline: [[[1
|
||||
" ============================================================================
|
||||
" vim: set ft=vim ts=4 sw=4 tw=78 fdm=marker fmr=[[[,]]] fdl=1 :
|
432
vim/plugins/GoldenView/autoload/GoldenView/zl/window.vim
Normal file
432
vim/plugins/GoldenView/autoload/GoldenView/zl/window.vim
Normal file
@ -0,0 +1,432 @@
|
||||
" =============== ============================================================
|
||||
" Name : window.vim
|
||||
" Synopsis : vim script library: window
|
||||
" Author : Zhao Cai <caizhaoff@gmail.com>
|
||||
" HomePage : https://github.com/zhaocai/zl.vim
|
||||
" Version : 0.1
|
||||
" Date Created : Sat 03 Sep 2011 03:54:00 PM EDT
|
||||
" Last Modified : Tue 23 Oct 2012 04:59:06 PM EDT
|
||||
" Tag : [ vim, syntax ]
|
||||
" Copyright : © 2012 by Zhao Cai,
|
||||
" Released under current GPL license.
|
||||
" =============== ============================================================
|
||||
|
||||
|
||||
|
||||
" ============================================================================
|
||||
" Status: ⟨⟨⟨1
|
||||
" ============================================================================
|
||||
call GoldenView#zl#rc#set_default({
|
||||
\ 'g:GoldenView_zl_window__ignore_urule' : {
|
||||
\ 'filetype' : [
|
||||
\ '' ,
|
||||
\ 'qf' , 'vimpager', 'undotree', 'tagbar',
|
||||
\ 'nerdtree', 'vimshell', 'vimfiler', 'voom' ,
|
||||
\ 'tabman' , 'unite' , 'quickrun', 'Decho' ,
|
||||
\ ],
|
||||
\ 'buftype' : [
|
||||
\ 'nofile' ,
|
||||
\ ],
|
||||
\ 'bufname' : [
|
||||
\ 'GoToFile' , 'diffpanel_\d\+' ,
|
||||
\ '__Gundo_Preview__' , '__Gundo__' ,
|
||||
\ '\[LustyExplorer-Buffers\]' , '\-MiniBufExplorer\-' ,
|
||||
\ '_VOOM\d\+$' , '__Urannotate_\d\+__' ,
|
||||
\ '__MRU_Files__' ,
|
||||
\ ],
|
||||
\ },
|
||||
\ })
|
||||
|
||||
let s:GoldenView_zl_window__ignore_nrule = GoldenView#zl#rule#norm(
|
||||
\ g:GoldenView_zl_window__ignore_urule, {
|
||||
\ 'logic' : 'or',
|
||||
\ }
|
||||
\ )
|
||||
|
||||
function! GoldenView#zl#window#is_last_visible()
|
||||
"--------- ------------------------------------------------
|
||||
" Desc : check if no visible buffer left
|
||||
"
|
||||
" Args :
|
||||
"
|
||||
" Return :
|
||||
" - 0 : false
|
||||
" - 1 : true
|
||||
" Raise :
|
||||
"
|
||||
"--------- ------------------------------------------------
|
||||
|
||||
for i in range(tabpagenr('$'))
|
||||
let tabnr = i + 1
|
||||
for bufnr in tabpagebuflist(tabnr)
|
||||
let ft = getbufvar(bufnr, '&ft')
|
||||
let buftype = getbufvar(bufnr, '&buftype')
|
||||
if empty(ft) && empty(buftype)
|
||||
continue
|
||||
endif
|
||||
if ! GoldenView#zl#rule#is_true(s:GoldenView_zl_window__ignore_nrule)
|
||||
return 0
|
||||
endif
|
||||
endfor
|
||||
endfor
|
||||
|
||||
return 1
|
||||
endfunction
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
" ============================================================================
|
||||
" Move: ⟨⟨⟨1
|
||||
" ============================================================================
|
||||
let s:golden_ratio = 1.618
|
||||
function! GoldenView#zl#window#next_window_or_tab()
|
||||
if tabpagenr('$') == 1 && winnr('$') == 1
|
||||
call GoldenView#zl#window#split_nicely()
|
||||
elseif winnr() < winnr("$")
|
||||
wincmd w
|
||||
else
|
||||
tabnext
|
||||
wincmd w
|
||||
endif
|
||||
endfunction
|
||||
|
||||
function! GoldenView#zl#window#previous_window_or_tab()
|
||||
if winnr() > 1
|
||||
wincmd W
|
||||
else
|
||||
tabprevious
|
||||
execute winnr("$") . "wincmd w"
|
||||
endif
|
||||
endfunction
|
||||
|
||||
|
||||
|
||||
" ============================================================================
|
||||
" Size: ⟨⟨⟨1
|
||||
" ============================================================================
|
||||
function! GoldenView#zl#window#golden_ratio_width()
|
||||
return float2nr(&columns / s:golden_ratio)
|
||||
endfunction
|
||||
|
||||
function! GoldenView#zl#window#golden_ratio_height()
|
||||
return float2nr(&lines / s:golden_ratio)
|
||||
endfunction
|
||||
|
||||
function! GoldenView#zl#window#textwidth()
|
||||
return &tw == 0 ? 78 : &tw
|
||||
endfunction
|
||||
|
||||
" ============================================================================
|
||||
" Split: ⟨⟨⟨1
|
||||
" ============================================================================
|
||||
|
||||
function! GoldenView#zl#window#nicely_split_cmd(...)
|
||||
let opts = {
|
||||
\ 'winnr' : 0 ,
|
||||
\}
|
||||
if a:0 >= 1 && type(a:1) == type({})
|
||||
call extend(opts, a:1)
|
||||
endif
|
||||
|
||||
let ww = winwidth(opts['winnr'])
|
||||
let tw = &textwidth
|
||||
|
||||
if tw != 0 && ww > s:golden_ratio * tw
|
||||
return 'vsplit'
|
||||
endif
|
||||
|
||||
if ww > &columns / s:golden_ratio
|
||||
return 'vsplit'
|
||||
endif
|
||||
|
||||
return 'split'
|
||||
endfunction
|
||||
|
||||
function! GoldenView#zl#window#split_nicely()
|
||||
let split_cmd = GoldenView#zl#window#nicely_split_cmd()
|
||||
try
|
||||
exec split_cmd
|
||||
catch /^Vim\%((\a\+)\)\=:E36/
|
||||
if split_cmd == 'split'
|
||||
let &winminheight = &winminheight / 2
|
||||
else
|
||||
let &winminwidth = &winminwidth / 2
|
||||
endif
|
||||
exec split_cmd
|
||||
endtry
|
||||
wincmd p
|
||||
endfunction
|
||||
|
||||
function! GoldenView#zl#window#toggle_split()
|
||||
let prev_name = winnr()
|
||||
silent! wincmd w
|
||||
if prev_name == winnr()
|
||||
split
|
||||
else
|
||||
call GoldenView#zl#buf#quit()
|
||||
endif
|
||||
endfunction
|
||||
|
||||
|
||||
|
||||
" ============================================================================
|
||||
" Sort: ⟨⟨⟨1
|
||||
" ============================================================================
|
||||
function! GoldenView#zl#window#sort_by(...)
|
||||
"--------- ------------------------------------------------
|
||||
" Desc : sort buffer by size, height, or width
|
||||
"
|
||||
" Args :
|
||||
" - opts : > ↓
|
||||
" {
|
||||
" 'by' : size|height|width|winnr|bufnr ,
|
||||
" 'tabnr' : tabpagenr() ,
|
||||
" 'width_weight' : s:golden_ratio ,
|
||||
" 'height_weight' : 1 ,
|
||||
" }
|
||||
"
|
||||
" Return : sorted list of
|
||||
" {
|
||||
" 'bufnr' : bufnr ,
|
||||
" 'winnr' : winnr ,
|
||||
" 'width' : width ,
|
||||
" 'height' : height ,
|
||||
" 'size' : size ,
|
||||
" }
|
||||
"
|
||||
" Raise :
|
||||
"
|
||||
"--------- ------------------------------------------------
|
||||
|
||||
let opts = {
|
||||
\ 'by' : 'size' ,
|
||||
\ 'tabnr' : tabpagenr() ,
|
||||
\ 'width_weight' : s:golden_ratio ,
|
||||
\ 'height_weight' : 1 ,
|
||||
\}
|
||||
if a:0 >= 1 && type(a:1) == type({})
|
||||
call extend(opts, a:1)
|
||||
endif
|
||||
|
||||
let list = []
|
||||
for bufnr in tabpagebuflist(opts['tabnr'])
|
||||
let winnr = bufwinnr(bufnr)
|
||||
let width = winwidth(winnr)
|
||||
let height = winheight(winnr)
|
||||
let size = width * opts['width_weight']
|
||||
\ + height * opts['height_weight']
|
||||
|
||||
call add(list, {
|
||||
\ 'bufnr' : bufnr ,
|
||||
\ 'winnr' : winnr ,
|
||||
\ 'width' : width ,
|
||||
\ 'height' : height ,
|
||||
\ 'size' : size ,
|
||||
\ })
|
||||
endfor
|
||||
|
||||
return GoldenView#zl#list#sort_by(list,'v:val["'.opts['by'].'"]')
|
||||
endfunction
|
||||
|
||||
|
||||
" ============================================================================
|
||||
" Switch: ⟨⟨⟨1
|
||||
" ============================================================================
|
||||
|
||||
function! GoldenView#zl#window#switch_buffer_toggle(...)
|
||||
"--------- ------------------------------------------------
|
||||
" Desc : toggle buffer switch
|
||||
"
|
||||
" Args :
|
||||
" - opts : >
|
||||
" {
|
||||
" 'with' : 'largest' ,
|
||||
" }
|
||||
"
|
||||
" Return :
|
||||
" Raise :
|
||||
"
|
||||
" Example : >
|
||||
" nnoremap <silent> <C-@>
|
||||
" \ :<C-u>call GoldenView#zl#window#switch_buffer_toggle()<CR>
|
||||
"--------- ------------------------------------------------
|
||||
|
||||
let opts = {
|
||||
\ 'with' : 'largest',
|
||||
\}
|
||||
if a:0 >= 1 && type(a:1) == type({})
|
||||
call extend(opts, a:1)
|
||||
endif
|
||||
|
||||
let bufnr = bufnr('%')
|
||||
if exists('b:switch_buffer')
|
||||
\ && bufwinnr(b:switch_buffer['bufnr']) == b:switch_buffer['winnr']
|
||||
call GoldenView#zl#window#switch_buffer(bufnr, b:switch_buffer['bufnr'])
|
||||
else
|
||||
try
|
||||
let fn = 'GoldenView#zl#window#switch_buffer_with_'.opts['with']
|
||||
exec 'call ' . fn . '()'
|
||||
catch /^Vim%((a+))=:E700/
|
||||
throw "zl: function " . fn . 'for ' . opts['with']
|
||||
\ . ' is not implemented!'
|
||||
endtry
|
||||
|
||||
endif
|
||||
endfunction
|
||||
|
||||
|
||||
function! GoldenView#zl#window#switch_buffer_with_sorted_by_size_index(index, ...)
|
||||
"--------- ------------------------------------------------
|
||||
" Desc : switch buffer with the largest window
|
||||
"
|
||||
" Args :
|
||||
" - opts : >
|
||||
" {
|
||||
" 'bufnr' : bufnr('%') ,
|
||||
" 'by' : 'size'|'height'|'width' ,
|
||||
" 'tabnr' : tabpagenr() ,
|
||||
" 'width_weight' : s:golden_ratio ,
|
||||
" 'height_weight' : 1 ,
|
||||
" }
|
||||
"
|
||||
" Return :
|
||||
" Raise :
|
||||
"
|
||||
" Example : >
|
||||
" nnoremap <silent> <C-@>
|
||||
" \ :<C-u>call GoldenView#zl#window#switch_buffer_with_largest()<CR>
|
||||
"--------- ------------------------------------------------
|
||||
|
||||
|
||||
let opts = {
|
||||
\ 'bufnr' : bufnr('%') ,
|
||||
\ 'by' : 'size' ,
|
||||
\ 'tabnr' : tabpagenr() ,
|
||||
\ 'width_weight' : s:golden_ratio ,
|
||||
\ 'height_weight' : 1 ,
|
||||
\}
|
||||
if a:0 >= 1 && type(a:1) == type({})
|
||||
call extend(opts, a:1)
|
||||
endif
|
||||
|
||||
let sorted = GoldenView#zl#window#sort_by(filter(copy(opts),'v:key != "bufnr"'))
|
||||
let bufnr_to = sorted[a:index]['bufnr']
|
||||
call GoldenView#zl#window#switch_buffer(opts['bufnr'], bufnr_to)
|
||||
endfunction
|
||||
|
||||
|
||||
function! GoldenView#zl#window#switch_buffer_with_largest(...)
|
||||
call GoldenView#zl#window#switch_buffer_with_sorted_by_size_index(-1, a:000)
|
||||
endfunction
|
||||
|
||||
function! GoldenView#zl#window#switch_buffer_with_smallest(...)
|
||||
call GoldenView#zl#window#switch_buffer_with_sorted_by_size_index(0, a:000)
|
||||
endfunction
|
||||
|
||||
function! GoldenView#zl#window#switch_buffer(bufnr1, bufnr2)
|
||||
"--------- ------------------------------------------------
|
||||
" Desc : switch buffer window if both are visible
|
||||
"
|
||||
" Args : bufnr1 <-> bufnr2
|
||||
" Return :
|
||||
" - 0 : fail
|
||||
" - 1 : success
|
||||
" Raise :
|
||||
"
|
||||
" Example : >
|
||||
"
|
||||
" Refer :
|
||||
"--------- ------------------------------------------------
|
||||
let winnr1 = bufwinnr(a:bufnr1)
|
||||
let winnr2 = bufwinnr(a:bufnr2)
|
||||
if winnr1 != -1 && winnr2 != -1
|
||||
|
||||
silent noautocmd exec winnr1 'wincmd w'
|
||||
if bufnr('%') != a:bufnr2
|
||||
silent noautocmd exec 'buffer' a:bufnr2
|
||||
let b:switch_buffer = {
|
||||
\ 'bufnr' : a:bufnr1 ,
|
||||
\ 'winnr' : winnr2 ,
|
||||
\ }
|
||||
endif
|
||||
silent noautocmd exec winnr2 'wincmd w'
|
||||
if bufnr('%') != a:bufnr1
|
||||
silent noautocmd exec 'buffer' a:bufnr1
|
||||
|
||||
" need filetype detect (maybe) because bufnr1 disappears for a
|
||||
" moment
|
||||
silent filetype detect
|
||||
|
||||
let b:switch_buffer = {
|
||||
\ 'bufnr' : a:bufnr2 ,
|
||||
\ 'winnr' : winnr1 ,
|
||||
\ }
|
||||
endif
|
||||
|
||||
return 1
|
||||
else
|
||||
return 0
|
||||
endif
|
||||
endfunction
|
||||
|
||||
function! GoldenView#zl#window#alternate_buffer()
|
||||
if bufnr('%') != bufnr('#') && buflisted(bufnr('#'))
|
||||
buffer #
|
||||
else
|
||||
let cnt = 0
|
||||
let pos = 1
|
||||
let current = 0
|
||||
while pos <= bufnr('$')
|
||||
if buflisted(pos)
|
||||
if pos == bufnr('%')
|
||||
let current = cnt
|
||||
endif
|
||||
|
||||
let cnt += 1
|
||||
endif
|
||||
|
||||
let pos += 1
|
||||
endwhile
|
||||
|
||||
if current > cnt / 2
|
||||
bprevious
|
||||
else
|
||||
bnext
|
||||
endif
|
||||
endif
|
||||
endfunction
|
||||
|
||||
" ============================================================================
|
||||
" Scroll: ⟨⟨⟨1
|
||||
" ============================================================================
|
||||
|
||||
function! GoldenView#zl#window#scroll_other_window(direction)
|
||||
execute 'wincmd' (winnr('#') == 0 ? 'w' : 'p')
|
||||
execute (a:direction ? "normal! \<C-d>" : "normal! \<C-u>")
|
||||
wincmd p
|
||||
endfunction
|
||||
|
||||
" ============================================================================
|
||||
" View: ⟨⟨⟨1
|
||||
" ============================================================================
|
||||
function! GoldenView#zl#window#save_view_command(command) range
|
||||
let view = winsaveview()
|
||||
|
||||
let range = ''
|
||||
if a:firstline != a:lastline
|
||||
let range = a:firstline.','.a:lastline
|
||||
endif
|
||||
try
|
||||
keepjumps execute range.a:command
|
||||
finally
|
||||
call winrestview(view)
|
||||
endtry
|
||||
endfunction
|
||||
|
||||
" ============================================================================
|
||||
" Modeline: ⟨⟨⟨1
|
||||
" ============================================================================
|
||||
" vim: set ft=vim ts=4 sw=4 tw=78 fdm=marker fmr=⟨⟨⟨,⟩⟩⟩ fdl=1 :
|
||||
|
Reference in New Issue
Block a user