2016-12-26 21:11:19 +08:00
scriptencoding utf -8
2018-02-25 22:14:26 +08:00
let s :SYSTEM = SpaceVim #api #import ( 'system' )
2016-12-26 21:11:19 +08:00
let s :save_cpo = &cpo
set cpo &vim
let g :unite_source_menu_menus =
2018-02-25 22:14:26 +08:00
\ get ( g :, 'unite_source_menu_menus' , {})
2016-12-26 21:11:19 +08:00
let g :unite_source_menu_menus .CustomKeyMaps = {'description' :
2018-02-25 22:14:26 +08:00
\ 'Custom mapped keyboard shortcuts [unite]<SPACE>' }
2016-12-26 21:11:19 +08:00
let g :unite_source_menu_menus .CustomKeyMaps .command_candidates =
2018-02-25 22:14:26 +08:00
\ get ( g :unite_source_menu_menus .CustomKeyMaps , 'command_candidates' , [])
2016-12-26 21:11:19 +08:00
let g :unite_source_menu_menus .MyStarredrepos = {'description' :
2018-02-25 22:14:26 +08:00
\ 'All github repos starred by me <leader>ls' }
2016-12-26 21:11:19 +08:00
let g :unite_source_menu_menus .MyStarredrepos .command_candidates =
2018-02-25 22:14:26 +08:00
\ get ( g :unite_source_menu_menus .MyStarredrepos , 'command_candidates' , [])
2016-12-26 21:11:19 +08:00
let g :unite_source_menu_menus .MpvPlayer = {'description' :
2018-02-25 22:14:26 +08:00
\ 'Musics list <leader>lm' }
2016-12-26 21:11:19 +08:00
let g :unite_source_menu_menus .MpvPlayer .command_candidates =
2018-02-25 22:14:26 +08:00
\ get ( g :unite_source_menu_menus .MpvPlayer , 'command_candidates' , [])
2016-12-26 21:11:19 +08:00
fu ! zvim #util #defineMap ( type , key , value , desc , ...) abort
2018-02-25 22:14:26 +08:00
exec a :type . ' ' . a :key . ' ' . a :value
let description = '➤ '
\. a :desc
\. repeat ( ' ' , 80 - len ( a :desc ) - len ( a :key ) )
\. a :key
let cmd = len ( a :000 ) > 0 ? a :000 [0 ] : a :value
call add ( g :unite_source_menu_menus .CustomKeyMaps .command_candidates , [description , cmd ])
2016-12-26 21:11:19 +08:00
endf
fu ! zvim #util #source_rc ( file ) abort
2019-02-03 00:07:31 +08:00
if filereadable ( g :_spacevim_root_dir . '/config/' . a :file )
execute 'source ' . g :_spacevim_root_dir . '/config/' . a :file
2018-02-25 22:14:26 +08:00
endif
2016-12-26 21:11:19 +08:00
endf
fu ! zvim #util #SmartClose ( ) abort
2018-02-25 22:14:26 +08:00
let ignorewin = get ( g :, 'spacevim_smartcloseignorewin' , [])
let ignoreft = get ( g :, 'spacevim_smartcloseignoreft' , [])
let win_count = winnr ( '$' )
let num = win_count
for i in range ( 1 , win_count )
if index ( ignorewin , bufname ( winbufnr ( i ) ) ) ! = -1 | | index ( ignoreft , getbufvar ( bufname ( winbufnr ( i ) ) , '&filetype' ) ) ! = -1
let num = num - 1
endif
if getbufvar ( winbufnr ( i ) , '&buftype' ) = = # 'quickfix'
let num = num - 1
2016-12-26 21:11:19 +08:00
endif
2018-02-25 22:14:26 +08:00
endfor
if num = = 1
else
quit
endif
2016-12-26 21:11:19 +08:00
endf
fu ! s :findFileInParent ( what , where ) abort " {{{2
2018-02-25 22:14:26 +08:00
let old_suffixesadd = &suffixesadd
let &suffixesadd = ''
let file = findfile ( a :what , escape ( a :where , ' ' ) . ';' )
let &suffixesadd = old_suffixesadd
return file
2016-12-26 21:11:19 +08:00
endf " }}}2
fu ! s :findDirInParent ( what , where ) abort " {{{2
2018-02-25 22:14:26 +08:00
let old_suffixesadd = &suffixesadd
let &suffixesadd = ''
let dir = finddir ( a :what , escape ( a :where , ' ' ) . ';' )
let &suffixesadd = old_suffixesadd
return dir
2016-12-26 21:11:19 +08:00
endf " }}}2
fu ! zvim #util #CopyToClipboard ( ...) abort
2018-02-25 22:14:26 +08:00
if a :0
2018-10-28 20:52:12 +08:00
echom 1
2018-02-25 22:14:26 +08:00
if executable ( 'git' )
let repo_home = fnamemodify ( s :findDirInParent ( '.git' , expand ( '%:p' ) ) , ':p:h:h' )
if repo_home ! = # '' | | ! isdirectory ( repo_home )
let branch = split ( systemlist ( 'git -C ' . repo_home . ' branch -a |grep "*"' ) [0 ], ' ' ) [1 ]
let remotes = filter ( systemlist ( 'git -C ' . repo_home . ' remote -v' ) , "match(v:val,'^origin') >= 0 && match(v:val,'fetch') > 0" )
if len ( remotes ) > 0
let remote = remotes [0 ]
if stridx ( remote , '@' ) > -1
let repo_url = 'https://github.com/' . split ( split ( remote , ' ' ) [0 ], ':' ) [1 ]
let repo_url = strpart ( repo_url , 0 , len ( repo_url ) - 4 )
else
let repo_url = split ( remote , ' ' ) [0 ]
let repo_url = strpart ( repo_url , stridx ( repo_url , 'http' ) , len ( repo_url ) - 4 - stridx ( repo_url , 'http' ) )
endif
let f_url = repo_url . '/blob/' . branch . '/' . strpart ( expand ( '%:p' ) , len ( repo_home ) + 1 , len ( expand ( '%:p' ) ) )
if s :SYSTEM .isWindows
let f_url = substitute ( f_url , '\' , '/' , 'g' )
endif
if a :1 = = 2
let current_line = line ( '.' )
let f_url .= '#L' . current_line
elseif a :1 = = 3
let f_url .= '#L' . getpos ( "'<" ) [1 ] . '-L' . getpos ( "'>" ) [1 ]
endif
try
let @+ = f_url
echo 'Copied to clipboard'
catch /^Vim\%((\a\+)\)\=:E354/
if has ( 'nvim' )
2018-08-23 06:08:37 +08:00
echohl WarningMsg | echom 'Cannot find clipboard, for more info see :h clipboard' | echohl None
2016-12-26 21:11:19 +08:00
else
2018-08-23 06:08:37 +08:00
echohl WarningMsg | echom 'You need to compile your vim with +clipboard feature' | echohl None
2016-12-26 21:11:19 +08:00
endif
2018-02-25 22:14:26 +08:00
endtry
2016-12-26 21:11:19 +08:00
else
2018-02-25 22:14:26 +08:00
echohl WarningMsg | echom 'This git repo has no remote host' | echohl None
2016-12-26 21:11:19 +08:00
endif
2018-02-25 22:14:26 +08:00
else
echohl WarningMsg | echom 'This file is not in a git repo' | echohl None
endif
2016-12-26 21:11:19 +08:00
else
2018-08-23 06:08:37 +08:00
echohl WarningMsg | echom 'You need to install git!' | echohl None
2016-12-26 21:11:19 +08:00
endif
2018-02-25 22:14:26 +08:00
else
try
let @+ = expand ( '%:p' )
echo 'Copied to clipboard ' . @+
catch /^Vim\%((\a\+)\)\=:E354/
if has ( 'nvim' )
echohl WarningMsg | echom 'Can not find clipboard, for more info see :h clipboard' | echohl None
else
2018-08-23 06:08:37 +08:00
echohl WarningMsg | echom 'You need to compile you vim with +clipboard feature' | echohl None
2018-02-25 22:14:26 +08:00
endif
endtry
endif
2016-12-26 21:11:19 +08:00
endf
fu ! zvim #util #check_if_expand_tab ( ) abort
2018-02-25 22:14:26 +08:00
let has_noexpandtab = search ( '^\t' , 'wn' )
let has_expandtab = search ( '^ ' , 'wn' )
if has_noexpandtab && has_expandtab
let idx = inputlist ( ['ERROR: current file exists both expand and noexpand TAB, python can only use one of these two mode in one file.\nSelect Tab Expand Type:' ,
\ '1. expand (tab=space, recommended)' ,
\ '2. noexpand (tab=\t, currently have risk)' ,
\ '3. do nothing (I will handle it by myself)' ])
let tab_space = printf ( '%*s' , &tabstop , '' )
if idx = = 1
let has_noexpandtab = 0
let has_expandtab = 1
silent exec '%s/\t/' . tab_space . '/g'
elseif idx = = 2
let has_noexpandtab = 1
let has_expandtab = 0
silent exec '%s/' . tab_space . '/\t/g'
2016-12-26 21:11:19 +08:00
else
2018-02-25 22:14:26 +08:00
return
2016-12-26 21:11:19 +08:00
endif
2018-02-25 22:14:26 +08:00
endif
if has_noexpandtab = = 1 && has_expandtab = = 0
echomsg 'substitute space to TAB...'
set noexpandtab
echomsg 'done!'
elseif has_noexpandtab = = 0 && has_expandtab = = 1
echomsg 'substitute TAB to space...'
set expandtab
echomsg 'done!'
else
" it may be a new file
" we use original vim setting
endif
2016-12-26 21:11:19 +08:00
endf
function ! zvim #util #BufferEmpty ( ) abort
2018-02-25 22:14:26 +08:00
let l :current = bufnr ( '%' )
if ! getbufvar ( l :current , '&modified' )
enew
silent ! execute 'bdelete ' .l :current
endif
2016-12-26 21:11:19 +08:00
endfunction
function ! zvim #util #loadMusics ( ) abort
2018-02-25 22:14:26 +08:00
let musics = SpaceVim #util #globpath ( '~/Musics' , '*.mp3' )
let g :unite_source_menu_menus .MpvPlayer .command_candidates = []
for m in musics
call add ( g :unite_source_menu_menus .MpvPlayer .command_candidates ,
\ [fnamemodify ( m , ':t' ) ,
\ "call zvim#mpv#play('" . m . "')" ])
endfor
2016-12-26 21:11:19 +08:00
endfunction
function ! zvim #util #listDirs ( dir ) abort
2018-02-25 22:14:26 +08:00
let dir = fnamemodify ( a :dir , ':p' )
if isdirectory ( dir )
let cmd = printf ( 'ls -F %s | grep /$' , dir )
return map ( systemlist ( cmd ) , 'v:val[:-2]' )
endif
return []
2016-12-26 21:11:19 +08:00
endfunction
let s :plugins_argv = ['-update' , '-openurl' ]
function ! zvim #util #complete_plugs ( ArgLead , CmdLine , CursorPos ) abort
2018-02-25 22:14:26 +08:00
call zvim #debug #completion_debug ( a :ArgLead , a :CmdLine , a :CursorPos )
if a :CmdLine = ~ # 'Plugin\s*$' | | a :ArgLead = ~ # '^-[a-zA-Z]*'
return join ( s :plugins_argv , "\n" )
endif
return join ( plugins #list ( ) , "\n" )
2016-12-26 21:11:19 +08:00
endfunction
function ! zvim #util #Plugin ( ...) abort
2018-02-25 22:14:26 +08:00
let adds = []
let updates = []
let flag = 0
for a in a :000
if flag = = 1
call add ( adds , a )
elseif flag = = 2
call add ( updates , a )
endif
if a = = # '-update'
let flag = 1
elseif a = = # '-openurl'
let flag = 2
endif
endfor
echo string ( adds ) . "\n" . string ( updates )
2016-12-26 21:11:19 +08:00
endfunction
function ! zvim #util #complete_project ( ArgLead , CmdLine , CursorPos ) abort
2018-02-25 22:14:26 +08:00
call zvim #debug #completion_debug ( a :ArgLead , a :CmdLine , a :CursorPos )
let dir = get ( g :, 'spacevim_src_root' , '~' )
"return globpath(dir, '*')
let result = split ( globpath ( dir , '*' ) , "\n" )
let ps = []
for p in result
if isdirectory ( p ) && isdirectory ( p . '\' . '.git' )
call add ( ps , fnamemodify ( p , ':t' ) )
endif
endfor
return join ( ps , "\n" )
2016-12-26 21:11:19 +08:00
endfunction
function ! zvim #util #OpenProject ( p ) abort
2018-02-25 22:14:26 +08:00
let dir = get ( g :, 'spacevim_src_root' , '~' ) . a :p
exe 'CtrlP ' . dir
2016-12-26 21:11:19 +08:00
endfunction
function ! zvim #util #UpdateHosts ( ...) abort
2018-02-25 22:14:26 +08:00
if len ( a :000 ) = = 0
let url = get ( g :, 'spacevim_hosts_url' , '' )
else
let url = a :1
endif
let hosts = systemlist ( 'curl -s ' . url )
if s :SYSTEM .isWindows
let local_hosts = $SystemRoot . expand ( '\System32\drivers\etc\hosts' )
else
let local_hosts = '/etc/hosts'
endif
if writefile ( hosts , local_hosts , 'a' ) = = -1
echo 'failed!'
else
echo 'successfully!'
endif
2016-12-26 21:11:19 +08:00
endfunction
2017-05-29 22:52:32 +08:00
fu ! zvim #util #Generate_ignore ( ignore , tool , ...) abort
2018-02-25 22:14:26 +08:00
let ignore = []
if a :tool = = # 'ag'
for ig in split ( a :ignore , ',' )
call add ( ignore , '--ignore' )
call add ( ignore , "'" . ig . "'" )
endfor
elseif a :tool = = # 'rg'
for ig in split ( a :ignore , ',' )
call add ( ignore , '-g' )
2018-12-09 10:04:55 +08:00
if get ( a :000 , 0 , 0 ) = = 1
2018-02-25 22:14:26 +08:00
call add ( ignore , "'!" . ig . "'" )
else
call add ( ignore , '!' . ig )
endif
endfor
endif
return ignore
2016-12-26 21:11:19 +08:00
endf
let &cpo = s :save_cpo
unlet s :save_cpo