From 81b56a907c3d76a24a93f62a28c430d66c20b706 Mon Sep 17 00:00:00 2001 From: Wang Shidong Date: Sat, 8 Jun 2019 15:09:53 +0800 Subject: [PATCH] Remove some code && fix windows support (#2874) --- autoload/SpaceVim.vim | 24 +- autoload/SpaceVim/autocmds.vim | 2 +- .../{zvim => SpaceVim/commands}/debug.vim | 6 +- autoload/SpaceVim/layers/tools/mpv.vim | 53 ++++ autoload/SpaceVim/layers/ui.vim | 4 +- autoload/SpaceVim/mapping.vim | 28 ++ autoload/SpaceVim/mapping/leader.vim | 4 +- autoload/SpaceVim/plugins.vim | 265 +++++++++++++++++- autoload/SpaceVim/plugins/projectmanager.vim | 19 ++ autoload/SpaceVim/util.vim | 92 ++++++ autoload/zvim/mpv.vim | 34 --- autoload/zvim/plug.vim | 217 -------------- autoload/zvim/util.vim | 204 -------------- config/commands.vim | 4 +- config/main.vim | 9 +- config/plugins/ctrlp.vim | 4 +- config/plugins/denite.vim | 4 +- config/plugins/unite.vim | 4 +- test/init.vader | 6 +- 19 files changed, 488 insertions(+), 495 deletions(-) rename autoload/{zvim => SpaceVim/commands}/debug.vim (57%) create mode 100644 autoload/SpaceVim/layers/tools/mpv.vim delete mode 100644 autoload/zvim/mpv.vim delete mode 100644 autoload/zvim/plug.vim delete mode 100644 autoload/zvim/util.vim diff --git a/autoload/SpaceVim.vim b/autoload/SpaceVim.vim index 67c20e5f8..7dde3d348 100644 --- a/autoload/SpaceVim.vim +++ b/autoload/SpaceVim.vim @@ -1157,6 +1157,20 @@ function! SpaceVim#end() abort let &softtabstop = g:spacevim_default_indent let &shiftwidth = g:spacevim_default_indent +let g:unite_source_menu_menus = + \ get(g:,'unite_source_menu_menus',{}) +let g:unite_source_menu_menus.CustomKeyMaps = {'description': + \ 'Custom mapped keyboard shortcuts [unite]'} +let g:unite_source_menu_menus.CustomKeyMaps.command_candidates = + \ get(g:unite_source_menu_menus.CustomKeyMaps,'command_candidates', []) +let g:unite_source_menu_menus.MyStarredrepos = {'description': + \ 'All github repos starred by me ls'} +let g:unite_source_menu_menus.MyStarredrepos.command_candidates = + \ get(g:unite_source_menu_menus.MyStarredrepos,'command_candidates', []) +let g:unite_source_menu_menus.MpvPlayer = {'description': + \ 'Musics list lm'} +let g:unite_source_menu_menus.MpvPlayer.command_candidates = + \ get(g:unite_source_menu_menus.MpvPlayer,'command_candidates', []) if g:spacevim_realtime_leader_guide nnoremap :LeaderGuide get(g:, 'mapleader', '\') @@ -1167,17 +1181,17 @@ function! SpaceVim#end() abort call SpaceVim#plugins#projectmanager#RootchandgeCallback() - call zvim#util#source_rc('general.vim') + call SpaceVim#util#loadConfig('general.vim') call SpaceVim#autocmds#init() if has('nvim') - call zvim#util#source_rc('neovim.vim') + call SpaceVim#util#loadConfig('neovim.vim') endif - call zvim#util#source_rc('commands.vim') + call SpaceVim#util#loadConfig('commands.vim') filetype plugin indent on syntax on endfunction @@ -1208,8 +1222,8 @@ endfunction function! SpaceVim#begin() abort - call zvim#util#source_rc('functions.vim') - call zvim#util#source_rc('init.vim') + call SpaceVim#util#loadConfig('functions.vim') + call SpaceVim#util#loadConfig('init.vim') " Before loading SpaceVim, We need to parser argvs. let s:status = s:parser_argv() diff --git a/autoload/SpaceVim/autocmds.vim b/autoload/SpaceVim/autocmds.vim index a849837b4..a02d94d26 100644 --- a/autoload/SpaceVim/autocmds.vim +++ b/autoload/SpaceVim/autocmds.vim @@ -48,7 +48,7 @@ function! SpaceVim#autocmds#init() abort autocmd FileType cs set comments=sO:*\ -,mO:*\ \ ,exO:*/,s1:/*,mb:*,ex:*/,:///,:// autocmd FileType vim set comments=sO:\"\ -,mO:\"\ \ ,eO:\"\",:\" autocmd Filetype qf setlocal nobuflisted - autocmd FileType python,coffee call zvim#util#check_if_expand_tab() + autocmd FileType python,coffee call SpaceVim#util#check_if_expand_tab() au StdinReadPost * call s:disable_welcome() autocmd InsertEnter * call s:fixindentline() autocmd BufEnter,FileType * call SpaceVim#mapping#space#refrashLSPC() diff --git a/autoload/zvim/debug.vim b/autoload/SpaceVim/commands/debug.vim similarity index 57% rename from autoload/zvim/debug.vim rename to autoload/SpaceVim/commands/debug.vim index e72944f40..083e8ac85 100644 --- a/autoload/zvim/debug.vim +++ b/autoload/SpaceVim/commands/debug.vim @@ -1,13 +1,13 @@ let s:debug_message = [] -function! zvim#debug#completion_debug(ArgLead, CmdLine, CursorPos) abort +function! SpaceVim#commands#debug#completion_debug(ArgLead, CmdLine, CursorPos) abort call add(s:debug_message, 'arglead:['.a:ArgLead .'] cmdline:[' .a:CmdLine .'] cursorpos:[' .a:CursorPos .']') endfunction -function! zvim#debug#get_message() abort +function! SpaceVim#commands#debug#get_message() abort return join(s:debug_message, "\n") endfunction -function! zvim#debug#clean_message() abort +function! SpaceVim#commands#debug#clean_message() abort let s:debug_message = [] return s:debug_message endfunction diff --git a/autoload/SpaceVim/layers/tools/mpv.vim b/autoload/SpaceVim/layers/tools/mpv.vim new file mode 100644 index 000000000..7d1d8ee88 --- /dev/null +++ b/autoload/SpaceVim/layers/tools/mpv.vim @@ -0,0 +1,53 @@ +"============================================================================= +" mpv.vim --- mpv layer for SpaceVim +" Copyright (c) 2016-2017 Wang Shidong & Contributors +" Author: Wang Shidong < wsdjeg at 163.com > +" URL: https://spacevim.org +" License: GPLv3 +"============================================================================= + + +let s:JOB = SpaceVim#api#import('job') + + +function! SpaceVim#layers#tools#mpv#config() abort + +endfunction + +function! SpaceVim#layers#tools#mpv#loadMusics() abort + 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 SpaceVim#layers#tools#mpv#play('" . m . "')"]) + endfor +endfunction + + +let s:playId = 0 +fu! s:handler(id, data, event) abort + if a:event ==# 'exit' + echom 'job ' . a:id . ' exit with code:' . string(a:data) + let s:playId = 0 + endif +endf +function! s:stop() abort + if s:playId != 0 + call jobstop(s:playId) + let s:playId = 0 + endif + delcommand MStop +endfunction +function! s:play(file,...) abort + if s:playId != 0 + call s:JOB.stop(s:playId) + let s:playId = 0 + endif + let s:playId = s:JOB.start(['mpv','--vid=no',a:file],{ + \ 'on_stdout': function('s:handler'), + \ 'on_stderr': function('s:handler'), + \ 'on_exit': function('s:handler'), + \ }) + command! MStop call zvim#mpv#stop() +endfunction diff --git a/autoload/SpaceVim/layers/ui.vim b/autoload/SpaceVim/layers/ui.vim index 5d201bce8..3c58fa3f1 100644 --- a/autoload/SpaceVim/layers/ui.vim +++ b/autoload/SpaceVim/layers/ui.vim @@ -51,9 +51,9 @@ function! SpaceVim#layers#ui#config() abort endif if !empty(g:spacevim_windows_smartclose) - call SpaceVim#mapping#def('nnoremap ', g:spacevim_windows_smartclose, ':call zvim#util#SmartClose()', + call SpaceVim#mapping#def('nnoremap ', g:spacevim_windows_smartclose, ':call SpaceVim#mapping#SmartClose()', \ 'Smart close windows', - \ 'call zvim#util#SmartClose()') + \ 'call SpaceVim#mapping#SmartClose()') endif " Ui toggles call SpaceVim#mapping#space#def('nnoremap', ['t', '8'], 'call call(' diff --git a/autoload/SpaceVim/mapping.vim b/autoload/SpaceVim/mapping.vim index e5d86737c..86214f2fc 100644 --- a/autoload/SpaceVim/mapping.vim +++ b/autoload/SpaceVim/mapping.vim @@ -278,6 +278,34 @@ function! SpaceVim#mapping#format() abort call setpos('.', save_cursor) endfunction +function! SpaceVim#mapping#BufferEmpty() abort + let l:current = bufnr('%') + if ! getbufvar(l:current, '&modified') + enew + silent! execute 'bdelete '.l:current + endif +endfunction + +fu! SpaceVim#mapping#SmartClose() abort + 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 + elseif getbufvar(winbufnr(i),'&buftype') ==# 'quickfix' + let num = num - 1 + elseif getwinvar(i, '&previewwindow') == 1 && winnr() !=# i + let num = num - 1 + endif + endfor + if num == 1 + else + quit + endif +endf + function! SpaceVim#mapping#gf() abort if &filetype isnot# 'vim' return 0 diff --git a/autoload/SpaceVim/mapping/leader.vim b/autoload/SpaceVim/mapping/leader.vim index 43e8b17db..7eb1e96cc 100644 --- a/autoload/SpaceVim/mapping/leader.vim +++ b/autoload/SpaceVim/mapping/leader.vim @@ -81,9 +81,9 @@ function! SpaceVim#mapping#leader#defindWindowsLeader(key) abort \ ] \ ] nnoremap [Window]x - \ :call zvim#util#BufferEmpty() + \ :call SpaceVim#mapping#BufferEmpty() let lnum = expand('') + s:lnum - 4 - let g:_spacevim_mappings_windows.x = ['call zvim#util#BufferEmpty()', + let g:_spacevim_mappings_windows.x = ['call SpaceVim#mapping#BufferEmpty()', \ 'Empty current buffer', \ [ \ '[WIN x] is to empty current buffer', diff --git a/autoload/SpaceVim/plugins.vim b/autoload/SpaceVim/plugins.vim index 949de03e3..387de6e3a 100644 --- a/autoload/SpaceVim/plugins.vim +++ b/autoload/SpaceVim/plugins.vim @@ -8,12 +8,12 @@ scriptencoding utf-8 function! SpaceVim#plugins#load() abort - if zvim#plug#enable_plug() - call zvim#plug#begin(g:spacevim_plugin_bundle_dir) - call zvim#plug#fetch() + if SpaceVim#plugins#enable_plug() + call SpaceVim#plugins#begin(g:spacevim_plugin_bundle_dir) + call SpaceVim#plugins#fetch() call s:load_plugins() call s:disable_plugins(g:spacevim_disabled_plugins) - call zvim#plug#end() + call SpaceVim#plugins#end() endif endfunction @@ -22,15 +22,15 @@ function! s:load_plugins() abort let g:_spacevim_plugin_layer = group for plugin in s:getLayerPlugins(group) if len(plugin) == 2 - call zvim#plug#add(plugin[0], plugin[1]) - if zvim#plug#tap(split(plugin[0], '/')[-1]) && get(plugin[1], 'loadconf', 0 ) - call zvim#plug#defind_hooks(split(plugin[0], '/')[-1]) + call SpaceVim#plugins#add(plugin[0], plugin[1]) + if SpaceVim#plugins#tap(split(plugin[0], '/')[-1]) && get(plugin[1], 'loadconf', 0 ) + call SpaceVim#plugins#defind_hooks(split(plugin[0], '/')[-1]) endif - if zvim#plug#tap(split(plugin[0], '/')[-1]) && get(plugin[1], 'loadconf_before', 0 ) - call zvim#plug#loadPluginBefore(split(plugin[0], '/')[-1]) + if SpaceVim#plugins#tap(split(plugin[0], '/')[-1]) && get(plugin[1], 'loadconf_before', 0 ) + call SpaceVim#plugins#loadPluginBefore(split(plugin[0], '/')[-1]) endif else - call zvim#plug#add(plugin[0]) + call SpaceVim#plugins#add(plugin[0]) endif endfor call s:loadLayerConfig(group) @@ -38,9 +38,9 @@ function! s:load_plugins() abort unlet g:_spacevim_plugin_layer for plugin in g:spacevim_custom_plugins if len(plugin) == 2 - call zvim#plug#add(plugin[0], plugin[1]) + call SpaceVim#plugins#add(plugin[0], plugin[1]) else - call zvim#plug#add(plugin[0]) + call SpaceVim#plugins#add(plugin[0]) endif endfor endfunction @@ -62,6 +62,34 @@ function! s:loadLayerConfig(layer) abort endfunction +let s:plugins_argv = ['-update', '-openurl'] + +function! SpaceVim#plugins#complete_plugs(ArgLead, CmdLine, CursorPos) abort + call SpaceVim#commands#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") +endfunction + +function! SpaceVim#plugins#Plugin(...) abort + 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) +endfunction function! s:disable_plugins(plugin_list) abort if g:spacevim_plugin_manager ==# 'dein' for name in a:plugin_list @@ -78,4 +106,217 @@ function! SpaceVim#plugins#get(...) abort endfunction +function! s:install_manager() abort + " Fsep && Psep + if has('win16') || has('win32') || has('win64') + let s:Psep = ';' + let s:Fsep = '\' + else + let s:Psep = ':' + let s:Fsep = '/' + endif + " auto install plugin manager + if g:spacevim_plugin_manager ==# 'neobundle' + "auto install neobundle + if filereadable(expand(g:spacevim_plugin_bundle_dir) + \ . 'neobundle.vim'. s:Fsep. 'README.md') + let g:_spacevim_neobundle_installed = 1 + else + if executable('git') + exec '!git clone ' + \ .'https://github.com/' + \ .'Shougo/neobundle.vim' + \ . ' ' + \ . fnameescape(g:spacevim_plugin_bundle_dir) + \ . 'neobundle.vim' + let g:_spacevim_neobundle_installed = 1 + else + echohl WarningMsg + echom 'You need install git!' + echohl None + endif + endif + exec 'set runtimepath+=' + \ . fnameescape(g:spacevim_plugin_bundle_dir) + \ . 'neobundle.vim' + elseif g:spacevim_plugin_manager ==# 'dein' + "auto install dein + if filereadable(expand(g:spacevim_plugin_bundle_dir) + \ . join(['repos', 'github.com', + \ 'Shougo', 'dein.vim', 'README.md'], + \ s:Fsep)) + let g:_spacevim_dein_installed = 1 + else + if executable('git') + exec '!git clone https://github.com/Shougo/dein.vim "' + \ . expand(g:spacevim_plugin_bundle_dir) + \ . join(['repos', 'github.com', + \ 'Shougo', 'dein.vim"'], s:Fsep) + let g:_spacevim_dein_installed = 1 + else + echohl WarningMsg + echom 'You need install git!' + echohl None + endif + endif + exec 'set runtimepath+='. fnameescape(g:spacevim_plugin_bundle_dir) + \ . join(['repos', 'github.com', 'Shougo', + \ 'dein.vim'], s:Fsep) + elseif g:spacevim_plugin_manager ==# 'vim-plug' + "auto install vim-plug + if filereadable(expand('~/.cache/vim-plug/autoload/plug.vim')) + let g:_spacevim_vim_plug_installed = 1 + else + if executable('curl') + exec '!curl -fLo ' + \ . '~/.cache/vim-plug/autoload/plug.vim' + \ . ' --create-dirs ' + \ . 'https://raw.githubusercontent.com/' + \ . 'junegunn/vim-plug/master/plug.vim' + let g:_spacevim_vim_plug_installed = 1 + else + echohl WarningMsg + echom 'You need install curl!' + echohl None + endif + endif + exec 'set runtimepath+=~/.cache/vim-plug/' + endif +endf + +if get(g:,'spacevim_enable_plugins', 1) + call s:install_manager() +endif + +function! SpaceVim#plugins#begin(path) abort + let g:unite_source_menu_menus.AddedPlugins = + \ {'description': + \ 'All the Added plugins' + \ . ' lp'} + let g:unite_source_menu_menus.AddedPlugins.command_candidates = [] + if g:spacevim_plugin_manager ==# 'neobundle' + call neobundle#begin(a:path) + elseif g:spacevim_plugin_manager ==# 'dein' + call dein#begin(a:path) + elseif g:spacevim_plugin_manager ==# 'vim-plug' + call plug#begin(a:path) + endif +endfunction + +function! SpaceVim#plugins#end() abort + if g:spacevim_plugin_manager ==# 'neobundle' + call neobundle#end() + if g:spacevim_checkinstall == 1 + silent! let g:_spacevim_checking_flag = neobundle#exists_not_installed_bundles() + if g:_spacevim_checking_flag + augroup SpaceVimCheckInstall + au! + au VimEnter * SPInstall + augroup END + endif + endif + elseif g:spacevim_plugin_manager ==# 'dein' + call dein#end() + if g:spacevim_checkinstall == 1 + silent! let g:_spacevim_checking_flag = dein#check_install() + if g:_spacevim_checking_flag + augroup SpaceVimCheckInstall + au! + au VimEnter * SPInstall + augroup END + endif + endif + call dein#call_hook('source') + elseif g:spacevim_plugin_manager ==# 'vim-plug' + call plug#end() + endif +endfunction + +function! SpaceVim#plugins#defind_hooks(bundle) abort + if g:spacevim_plugin_manager ==# 'neobundle' + let s:hooks = neobundle#get_hooks(a:bundle) + func! s:hooks.on_source(bundle) abort + call SpaceVim#util#loadConfig('plugins/' . split(a:bundle['name'],'\.')[0] . '.vim') + endf + elseif g:spacevim_plugin_manager ==# 'dein' + call dein#config(g:dein#name, { + \ 'hook_source' : "call SpaceVim#util#loadConfig('plugins/" . split(g:dein#name,'\.')[0] . ".vim')" + \ }) + endif +endfunction + +function! SpaceVim#plugins#fetch() abort + if g:spacevim_plugin_manager ==# 'neobundle' + NeoBundleFetch 'Shougo/neobundle.vim' + elseif g:spacevim_plugin_manager ==# 'dein' + call dein#add('Shougo/dein.vim') + endif +endfunction + +let s:plugins = [] + +fu! s:parser(args) abort + return a:args +endf +let g:_spacevim_plugins = [] +function! SpaceVim#plugins#add(repo,...) abort + let g:spacevim_plugin_name = '' + if g:spacevim_plugin_manager ==# 'neobundle' + exec 'NeoBundle "'.a:repo.'"'.','.join(a:000,',') + let g:spacevim_plugin_name = split(a:repo, '/')[-1] + elseif g:spacevim_plugin_manager ==# 'dein' + if len(a:000) > 0 + call dein#add(a:repo,s:parser(a:000[0])) + else + call dein#add(a:repo) + endif + let g:spacevim_plugin_name = g:dein#name + call add(g:_spacevim_plugins, g:dein#name) + elseif g:spacevim_plugin_manager ==# 'vim-plug' + if len(a:000) > 0 + exec "Plug '".a:repo."', ".join(a:000,',') + else + exec "Plug '".a:repo."'" + endif + let g:spacevim_plugin_name = split(a:repo, '/')[-1] + endif + let str = get(g:,'_spacevim_plugin_layer', 'custom plugin') + let str = '[' . str . ']' + let str = str . repeat(' ', 25 - len(str)) + exec 'call add(g:unite_source_menu_menus' + \ . '.AddedPlugins.command_candidates, ["'. str . '[' + \ . a:repo + \ . (len(a:000) > 0 ? (']' + \ . repeat(' ', 40 - len(a:repo)) + \ . '[lazy loaded] [' . string(a:000[0])) : '') + \ . ']","OpenBrowser https://github.com/' + \ . a:repo + \ . '"])' + call add(s:plugins, a:repo) +endfunction + +function! SpaceVim#plugins#tap(plugin) abort + if g:spacevim_plugin_manager ==# 'neobundle' + return neobundle#tap(a:plugin) + elseif g:spacevim_plugin_manager ==# 'dein' + return dein#tap(a:plugin) + endif +endfunction + +function! SpaceVim#plugins#enable_plug() abort + return g:_spacevim_neobundle_installed + \ || g:_spacevim_dein_installed + \ || g:_spacevim_vim_plug_installed +endfunction + +function! SpaceVim#plugins#loadPluginBefore(plugin) abort + if matchend(a:plugin, "\\.vim") == len(a:plugin) + call SpaceVim#util#loadConfig('plugins_before/' . a:plugin) + elseif matchend(a:plugin, "\\.nvim") == len(a:plugin) + call SpaceVim#util#loadConfig('plugins_before/' . a:plugin[:-6] . '.vim') + else + call SpaceVim#util#loadConfig('plugins_before/' . a:plugin . '.vim') + endif +endfunction + " vim:set et sw=2: diff --git a/autoload/SpaceVim/plugins/projectmanager.vim b/autoload/SpaceVim/plugins/projectmanager.vim index b3aee23e0..dc47f14c8 100644 --- a/autoload/SpaceVim/plugins/projectmanager.vim +++ b/autoload/SpaceVim/plugins/projectmanager.vim @@ -191,5 +191,24 @@ function! s:compare(d1, d2) abort return len(split(a:d2, '/')) - len(split(a:d1, '/')) endfunction +function! SpaceVim#plugins#projectmanager#complete_project(ArgLead, CmdLine, CursorPos) abort + call SpaceVim#commands#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") +endfunction + +function! SpaceVim#plugins#projectmanager#OpenProject(p) abort + let dir = get(g:, 'spacevim_src_root', '~') . a:p + exe 'CtrlP '. dir +endfunction + " vim:set et nowrap sw=2 cc=80: diff --git a/autoload/SpaceVim/util.vim b/autoload/SpaceVim/util.vim index 36d015785..1937a753d 100644 --- a/autoload/SpaceVim/util.vim +++ b/autoload/SpaceVim/util.vim @@ -6,6 +6,9 @@ " License: GPLv3 "============================================================================= +let s:SYSTEM = SpaceVim#api#import('system') + + function! SpaceVim#util#globpath(path, expr) abort if has('patch-7.4.279') return globpath(a:path, a:expr, 1, 1) @@ -22,6 +25,47 @@ function! SpaceVim#util#findFileInParent(what, where) abort return file endfunction +fu! SpaceVim#util#loadConfig(file) abort + if filereadable(g:_spacevim_root_dir. '/config/' . a:file) + execute 'source ' . g:_spacevim_root_dir . '/config/' . a:file + endif +endf + +fu! SpaceVim#util#check_if_expand_tab() abort + 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' + else + return + endif + 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 +endf + function! SpaceVim#util#findDirInParent(what, where) abort let old_suffixesadd = &suffixesadd let &suffixesadd = '' @@ -156,4 +200,52 @@ fu! SpaceVim#util#CopyToClipboard(...) abort endif endf +fu! SpaceVim#util#Generate_ignore(ignore,tool, ...) abort + 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') + if get(a:000, 0, 0) == 1 + call add(ignore, "'!" . ig . "'") + else + call add(ignore, '!' . ig) + endif + endfor + endif + return ignore +endf + +function! SpaceVim#util#UpdateHosts(...) abort + 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 +endfunction + +function! SpaceVim#util#listDirs(dir) abort + 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 [] +endfunction + " vim:set et sw=2 cc=80: diff --git a/autoload/zvim/mpv.vim b/autoload/zvim/mpv.vim deleted file mode 100644 index dd39552bd..000000000 --- a/autoload/zvim/mpv.vim +++ /dev/null @@ -1,34 +0,0 @@ -let s:playId = 0 -fu! s:handler(id, data, event) abort - if a:event ==# 'exit' - echom 'job ' . a:id . ' exit with code:' . string(a:data) - let s:playId = 0 - endif -endf -function! zvim#mpv#stop() abort - if s:playId != 0 - call jobstop(s:playId) - let s:playId = 0 - endif - delcommand MStop -endfunction -function! zvim#mpv#play(file,...) abort - if has('nvim') - if s:playId != 0 - call jobstop(s:playId) - let s:playId = 0 - endif - let s:playId = jobstart(['mpv','--vid=no',a:file],{ - \ 'on_stdout': function('s:handler'), - \ 'on_stderr': function('s:handler'), - \ 'on_exit': function('s:handler'), - \ }) - else - if type(s:playId) == 8 && job_status(s:playId) ==# 'run' - call job_stop(s:playId) - let s:playId =0 - endif - let s:playId = job_start(['mpv','--vid=no',a:file]) - endif - command! MStop call zvim#mpv#stop() -endfunction diff --git a/autoload/zvim/plug.vim b/autoload/zvim/plug.vim deleted file mode 100644 index 1070f515b..000000000 --- a/autoload/zvim/plug.vim +++ /dev/null @@ -1,217 +0,0 @@ -let s:save_cpo = &cpo -set cpo&vim -function! s:install_manager() abort - " Fsep && Psep - if has('win16') || has('win32') || has('win64') - let s:Psep = ';' - let s:Fsep = '\' - else - let s:Psep = ':' - let s:Fsep = '/' - endif - " auto install plugin manager - if g:spacevim_plugin_manager ==# 'neobundle' - "auto install neobundle - if filereadable(expand(g:spacevim_plugin_bundle_dir) - \ . 'neobundle.vim'. s:Fsep. 'README.md') - let g:_spacevim_neobundle_installed = 1 - else - if executable('git') - exec '!git clone ' - \ .'https://github.com/' - \ .'Shougo/neobundle.vim' - \ . ' ' - \ . fnameescape(g:spacevim_plugin_bundle_dir) - \ . 'neobundle.vim' - let g:_spacevim_neobundle_installed = 1 - else - echohl WarningMsg - echom 'You need install git!' - echohl None - endif - endif - exec 'set runtimepath+=' - \ . fnameescape(g:spacevim_plugin_bundle_dir) - \ . 'neobundle.vim' - elseif g:spacevim_plugin_manager ==# 'dein' - "auto install dein - if filereadable(expand(g:spacevim_plugin_bundle_dir) - \ . join(['repos', 'github.com', - \ 'Shougo', 'dein.vim', 'README.md'], - \ s:Fsep)) - let g:_spacevim_dein_installed = 1 - else - if executable('git') - exec '!git clone https://github.com/Shougo/dein.vim "' - \ . expand(g:spacevim_plugin_bundle_dir) - \ . join(['repos', 'github.com', - \ 'Shougo', 'dein.vim"'], s:Fsep) - let g:_spacevim_dein_installed = 1 - else - echohl WarningMsg - echom 'You need install git!' - echohl None - endif - endif - exec 'set runtimepath+='. fnameescape(g:spacevim_plugin_bundle_dir) - \ . join(['repos', 'github.com', 'Shougo', - \ 'dein.vim'], s:Fsep) - elseif g:spacevim_plugin_manager ==# 'vim-plug' - "auto install vim-plug - if filereadable(expand('~/.cache/vim-plug/autoload/plug.vim')) - let g:_spacevim_vim_plug_installed = 1 - else - if executable('curl') - exec '!curl -fLo ' - \ . '~/.cache/vim-plug/autoload/plug.vim' - \ . ' --create-dirs ' - \ . 'https://raw.githubusercontent.com/' - \ . 'junegunn/vim-plug/master/plug.vim' - let g:_spacevim_vim_plug_installed = 1 - else - echohl WarningMsg - echom 'You need install curl!' - echohl None - endif - endif - exec 'set runtimepath+=~/.cache/vim-plug/' - endif -endf - -if get(g:,'spacevim_enable_plugins', 1) - call s:install_manager() -endif - -function! zvim#plug#begin(path) abort - let g:unite_source_menu_menus.AddedPlugins = - \ {'description': - \ 'All the Added plugins' - \ . ' lp'} - let g:unite_source_menu_menus.AddedPlugins.command_candidates = [] - if g:spacevim_plugin_manager ==# 'neobundle' - call neobundle#begin(a:path) - elseif g:spacevim_plugin_manager ==# 'dein' - call dein#begin(a:path) - elseif g:spacevim_plugin_manager ==# 'vim-plug' - call plug#begin(a:path) - endif -endfunction - -function! zvim#plug#end() abort - if g:spacevim_plugin_manager ==# 'neobundle' - call neobundle#end() - if g:spacevim_checkinstall == 1 - silent! let g:_spacevim_checking_flag = neobundle#exists_not_installed_bundles() - if g:_spacevim_checking_flag - augroup SpaceVimCheckInstall - au! - au VimEnter * SPInstall - augroup END - endif - endif - elseif g:spacevim_plugin_manager ==# 'dein' - call dein#end() - if g:spacevim_checkinstall == 1 - silent! let g:_spacevim_checking_flag = dein#check_install() - if g:_spacevim_checking_flag - augroup SpaceVimCheckInstall - au! - au VimEnter * SPInstall - augroup END - endif - endif - call dein#call_hook('source') - elseif g:spacevim_plugin_manager ==# 'vim-plug' - call plug#end() - endif -endfunction - -function! zvim#plug#defind_hooks(bundle) abort - if g:spacevim_plugin_manager ==# 'neobundle' - let s:hooks = neobundle#get_hooks(a:bundle) - func! s:hooks.on_source(bundle) abort - call zvim#util#source_rc('plugins/' . split(a:bundle['name'],'\.')[0] . '.vim') - endf - elseif g:spacevim_plugin_manager ==# 'dein' - call dein#config(g:dein#name, { - \ 'hook_source' : "call zvim#util#source_rc('plugins/" . split(g:dein#name,'\.')[0] . ".vim')" - \ }) - endif -endfunction - -function! zvim#plug#fetch() abort - if g:spacevim_plugin_manager ==# 'neobundle' - NeoBundleFetch 'Shougo/neobundle.vim' - elseif g:spacevim_plugin_manager ==# 'dein' - call dein#add('Shougo/dein.vim') - endif -endfunction - -let s:plugins = [] - -fu! s:parser(args) abort - return a:args -endf -let g:_spacevim_plugins = [] -function! zvim#plug#add(repo,...) abort - let g:spacevim_plugin_name = '' - if g:spacevim_plugin_manager ==# 'neobundle' - exec 'NeoBundle "'.a:repo.'"'.','.join(a:000,',') - let g:spacevim_plugin_name = split(a:repo, '/')[-1] - elseif g:spacevim_plugin_manager ==# 'dein' - if len(a:000) > 0 - call dein#add(a:repo,s:parser(a:000[0])) - else - call dein#add(a:repo) - endif - let g:spacevim_plugin_name = g:dein#name - call add(g:_spacevim_plugins, g:dein#name) - elseif g:spacevim_plugin_manager ==# 'vim-plug' - if len(a:000) > 0 - exec "Plug '".a:repo."', ".join(a:000,',') - else - exec "Plug '".a:repo."'" - endif - let g:spacevim_plugin_name = split(a:repo, '/')[-1] - endif - let str = get(g:,'_spacevim_plugin_layer', 'custom plugin') - let str = '[' . str . ']' - let str = str . repeat(' ', 25 - len(str)) - exec 'call add(g:unite_source_menu_menus' - \ . '.AddedPlugins.command_candidates, ["'. str . '[' - \ . a:repo - \ . (len(a:000) > 0 ? (']' - \ . repeat(' ', 40 - len(a:repo)) - \ . '[lazy loaded] [' . string(a:000[0])) : '') - \ . ']","OpenBrowser https://github.com/' - \ . a:repo - \ . '"])' - call add(s:plugins, a:repo) -endfunction - -function! zvim#plug#tap(plugin) abort - if g:spacevim_plugin_manager ==# 'neobundle' - return neobundle#tap(a:plugin) - elseif g:spacevim_plugin_manager ==# 'dein' - return dein#tap(a:plugin) - endif -endfunction - -function! zvim#plug#enable_plug() abort - return g:_spacevim_neobundle_installed - \ || g:_spacevim_dein_installed - \ || g:_spacevim_vim_plug_installed -endfunction - -function! zvim#plug#loadPluginBefore(plugin) abort - if matchend(a:plugin, "\\.vim") == len(a:plugin) - call zvim#util#source_rc('plugins_before/' . a:plugin) - elseif matchend(a:plugin, "\\.nvim") == len(a:plugin) - call zvim#util#source_rc('plugins_before/' . a:plugin[:-6] . '.vim') - else - call zvim#util#source_rc('plugins_before/' . a:plugin . '.vim') - endif -endfunction - -let &cpo = s:save_cpo -unlet s:save_cpo diff --git a/autoload/zvim/util.vim b/autoload/zvim/util.vim deleted file mode 100644 index ba7bb6e1a..000000000 --- a/autoload/zvim/util.vim +++ /dev/null @@ -1,204 +0,0 @@ -scriptencoding utf-8 -let s:SYSTEM = SpaceVim#api#import('system') -let s:save_cpo = &cpo -set cpo&vim -let g:unite_source_menu_menus = - \ get(g:,'unite_source_menu_menus',{}) -let g:unite_source_menu_menus.CustomKeyMaps = {'description': - \ 'Custom mapped keyboard shortcuts [unite]'} -let g:unite_source_menu_menus.CustomKeyMaps.command_candidates = - \ get(g:unite_source_menu_menus.CustomKeyMaps,'command_candidates', []) -let g:unite_source_menu_menus.MyStarredrepos = {'description': - \ 'All github repos starred by me ls'} -let g:unite_source_menu_menus.MyStarredrepos.command_candidates = - \ get(g:unite_source_menu_menus.MyStarredrepos,'command_candidates', []) -let g:unite_source_menu_menus.MpvPlayer = {'description': - \ 'Musics list lm'} -let g:unite_source_menu_menus.MpvPlayer.command_candidates = - \ get(g:unite_source_menu_menus.MpvPlayer,'command_candidates', []) -fu! zvim#util#defineMap(type,key,value,desc,...) abort - 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]) - -endf -fu! zvim#util#source_rc(file) abort - if filereadable(g:_spacevim_root_dir. '/config/' . a:file) - execute 'source ' . g:_spacevim_root_dir . '/config/' . a:file - endif -endf - -fu! zvim#util#SmartClose() abort - 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 - elseif getbufvar(winbufnr(i),'&buftype') ==# 'quickfix' - let num = num - 1 - elseif getwinvar(i, '&previewwindow') == 1 && winnr() !=# i - let num = num - 1 - endif - endfor - if num == 1 - else - quit - endif -endf - -fu! zvim#util#check_if_expand_tab() abort - 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' - else - return - endif - 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 -endf - -function! zvim#util#BufferEmpty() abort - let l:current = bufnr('%') - if ! getbufvar(l:current, '&modified') - enew - silent! execute 'bdelete '.l:current - endif -endfunction - -function! zvim#util#loadMusics() abort - 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 -endfunction - -function! zvim#util#listDirs(dir) abort - 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 [] -endfunction - -let s:plugins_argv = ['-update', '-openurl'] - -function! zvim#util#complete_plugs(ArgLead, CmdLine, CursorPos) abort - 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") -endfunction - -function! zvim#util#Plugin(...) abort - 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) -endfunction - -function! zvim#util#complete_project(ArgLead, CmdLine, CursorPos) abort - 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") -endfunction - -function! zvim#util#OpenProject(p) abort - let dir = get(g:, 'spacevim_src_root', '~') . a:p - exe 'CtrlP '. dir -endfunction - -function! zvim#util#UpdateHosts(...) abort - 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 -endfunction -fu! zvim#util#Generate_ignore(ignore,tool, ...) abort - 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') - if get(a:000, 0, 0) == 1 - call add(ignore, "'!" . ig . "'") - else - call add(ignore, '!' . ig) - endif - endfor - endif - return ignore -endf - -let &cpo = s:save_cpo -unlet s:save_cpo diff --git a/config/commands.vim b/config/commands.vim index b60095b38..c7aa351f9 100644 --- a/config/commands.vim +++ b/config/commands.vim @@ -3,9 +3,9 @@ " defined already. command! DiffOrig vert new | set bt=nofile | r # | 0d_ | diffthis \ | wincmd p | diffthis -command! -nargs=* -complete=custom,zvim#util#complete_plugs Plugin :call zvim#util#Plugin() +command! -nargs=* -complete=custom,SpaceVim#plugins#complete_plugs Plugin :call SpaceVim#plugins#Plugin() "command for open project -command! -nargs=+ -complete=custom,zvim#util#complete_project OpenProject :call zvim#util#OpenProject() +command! -nargs=+ -complete=custom,SpaceVim#plugins#projectmanager#complete_project OpenProject :call SpaceVim#plugins#projectmanager#OpenProject() command! -nargs=* -complete=custom,SpaceVim#plugins#pmd#complete PMD :call SpaceVim#plugins#pmd#run() diff --git a/config/main.vim b/config/main.vim index ea34b7a9a..c99497ef5 100644 --- a/config/main.vim +++ b/config/main.vim @@ -55,13 +55,8 @@ if has('nvim') else let &rtp = g:_spacevim_root_dir . ',' . $VIMRUNTIME endif -try - call SpaceVim#begin() -catch - " Update the rtp only when SpaceVim is not contained in runtimepath. - let &runtimepath .= ',' . fnamemodify(g:_spacevim_root_dir, ':p:h') - call SpaceVim#begin() -endtry + +call SpaceVim#begin() call SpaceVim#custom#load() diff --git a/config/plugins/ctrlp.vim b/config/plugins/ctrlp.vim index 36f8495fa..fbaeff3c3 100644 --- a/config/plugins/ctrlp.vim +++ b/config/plugins/ctrlp.vim @@ -20,9 +20,9 @@ let g:ctrlp_custom_ignore = get(g:, 'ctrlp_custom_ignore', { \ }) if executable('rg') && !exists('g:ctrlp_user_command') let g:ctrlp_user_command = 'rg %s --no-ignore --hidden --files -g "" ' - \ . join(zvim#util#Generate_ignore(get(g:, 'spacevim_wildignore', ''),'rg', SpaceVim#api#import('system').isWindows ? 0 : 1)) + \ . join(SpaceVim#util#Generate_ignore(get(g:, 'spacevim_wildignore', ''),'rg', SpaceVim#api#import('system').isWindows ? 0 : 1)) elseif executable('ag') && !exists('g:ctrlp_user_command') - let g:ctrlp_user_command = 'ag --hidden -i -g "" ' . join(zvim#util#Generate_ignore(g:spacevim_wildignore,'ag')) . ' %s' + let g:ctrlp_user_command = 'ag --hidden -i -g "" ' . join(SpaceVim#util#Generate_ignore(g:spacevim_wildignore,'ag')) . ' %s' elseif s:SYS.isWindows let g:ctrlp_user_command = \ 'dir %s /-n /b /s /a-d | findstr /v /l ".jpg \\tmp\\"' " Windows diff --git a/config/plugins/denite.vim b/config/plugins/denite.vim index 7d9b184d6..1398b1300 100644 --- a/config/plugins/denite.vim +++ b/config/plugins/denite.vim @@ -38,13 +38,13 @@ if !s:sys.isWindows " Note: It is slower than ag call denite#custom#var('file/rec', 'command', \ ['rg', '--hidden', '--files', '--glob', '!.git', '--glob', ''] - \ + zvim#util#Generate_ignore(g:spacevim_wildignore, 'rg') + \ + SpaceVim#util#Generate_ignore(g:spacevim_wildignore, 'rg') \ ) elseif executable('ag') " Change file/rec command. call denite#custom#var('file/rec', 'command', \ ['ag' , '--nocolor', '--nogroup', '-g', ''] - \ + zvim#util#Generate_ignore(g:spacevim_wildignore, 'ag') + \ + SpaceVim#util#Generate_ignore(g:spacevim_wildignore, 'ag') \ ) endif else diff --git a/config/plugins/unite.vim b/config/plugins/unite.vim index 799a8f57e..5f2e8d68b 100644 --- a/config/plugins/unite.vim +++ b/config/plugins/unite.vim @@ -259,7 +259,9 @@ function! s:view_github_starred_repos() abort Unite -silent -ignorecase -winheight=17 -start-insert menu:MyStarredrepos endif endfunction -call zvim#util#loadMusics() +if SpaceVim#layers#isLoaded('tools#mpv') + call SpaceVim#layers#tools#mpv#loadMusics() +endif augroup unite_buffer_feature autocmd FileType unite call s:unite_my_settings() augroup END diff --git a/test/init.vader b/test/init.vader index 8a2d7554f..4ab44edc9 100644 --- a/test/init.vader +++ b/test/init.vader @@ -1,5 +1,9 @@ Execute ( SpaceVim api: SpaceVim main code ): unlock g:_spacevim_root_dir - source init.vim + if has('nvim') + source init.vim + else + source vimrc + endif SPInstall AssertEqual fnamemodify(g:_spacevim_root_dir, ':.'), ''