-
Notifications
You must be signed in to change notification settings - Fork 249
Expand file tree
/
Copy pathspacevim.vim
More file actions
260 lines (227 loc) · 7.74 KB
/
spacevim.vim
File metadata and controls
260 lines (227 loc) · 7.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
scriptencoding utf-8
let g:spacevim.info = g:spacevim.base. '/core/autoload/spacevim/info.vim'
let g:spacevim.layers_base = '/layers'
let g:spacevim.private_base = '/private'
let g:spacevim.nvim = has('nvim') && exists('*jobwait') && !g:spacevim.os.windows
let g:spacevim.vim8 = exists('*job_start')
let g:spacevim.timer = exists('*timer_start')
let g:spacevim.gui = has('gui_running')
let g:spacevim.tmux = !empty($TMUX)
let g:spacevim.loaded = ['spacevim'] " Enable spacevim layer by default
let g:spacevim.excluded = []
let g:spacevim.plugins = []
let s:plug_options = {}
let s:dot_spacevim = $HOME.'/.spacevim'
let s:private_config = g:spacevim.base.'/private/config.vim'
let s:private_packages = g:spacevim.base.'/private/packages.vim'
let s:TYPE = {
\ 'string': type(''),
\ 'list': type([]),
\ 'dict': type({}),
\ 'funcref': type(function('call'))
\ }
function! spacevim#bootstrap() abort
call spacevim#begin()
call spacevim#end()
endfunction
function! spacevim#begin() abort
" Download vim-plug if unavailable
if !g:spacevim.os.windows
call s:check_vim_plug()
endif
call s:define_command()
call s:cache()
call s:check_dot_spacevim()
endfunction
function! s:check_vim_plug() abort
"let l:plug_path = g:spacevim.nvim ? '~/.local/share/nvim/site/autoload/plug.vim' : '~/.vim/autoload/plug.vim'
"let l:plug_path = '~/.cache/space-vim/site/autoload/plug.vim'
let l:plug_path = g:spacevim_plugvim_filepath
if empty(glob(l:plug_path)) | call spacevim#vim#plug#download(l:plug_path) | endif
endfunction
function! s:define_command() abort
" MP means MyPlugin
command! -nargs=+ -bar MP call s:my_plugin(<args>)
command! -nargs=+ -bar Layer call s:layer(<args>)
command! -nargs=0 -bar SpaceInfo call spacevim#debugging#Info()
command! -nargs=0 -bar LayerCache call spacevim#cache#init()
command! -nargs=0 -bar LayerStatus call spacevim#layer#status()
endfunction
function! s:check_dot_spacevim() abort
if filereadable(expand(s:dot_spacevim))
call s:Source(s:dot_spacevim)
call extend(g:spacevim.loaded, get(g:, 'spacevim_layers', []))
let g:mapleader = get(g:, 'spacevim_leader', "\<Space>")
let g:maplocalleader = get(g:, 'spacevim_localleader', ',')
else
call spacevim#util#err('.spacevim does not exist! Exiting...')
endif
endfunction
function! s:cache() abort
let l:info = g:spacevim.info
if filereadable(l:info)
execute 'source ' . (g:spacevim.os.windows ? s:path(l:info) : l:info)
else
call spacevim#cache#init()
endif
endfunction
function! s:layer(name, ...)
if index(g:spacevim.loaded, a:name) == -1
call add(g:spacevim.loaded, a:name)
endif
if a:0 > 1
return spacevim#util#err('Invalid number of arguments (1..2)')
elseif a:0 == 1
call s:parse_options(a:1)
endif
endfunction
function! s:to_a(v) abort
return type(a:v) == s:TYPE.list ? a:v : [a:v]
endfunction
function! s:parse_options(arg)
let l:type = type(a:arg)
if l:type == s:TYPE.dict
if has_key(a:arg, 'exclude')
call extend(g:spacevim.excluded, s:to_a(a:arg['exclude']))
else
throw 'Invalid option (expected: exclude)'
endif
else
throw 'Invalid argument type (expected: dictionary)'
endif
endfunction
" This is an only one possible extra argument: plug option, dict
function! s:my_plugin(plugin, ...) abort
if index(g:spacevim.plugins, a:plugin) < 0
call add(g:spacevim.plugins, a:plugin)
endif
if a:0 == 1
let s:plug_options[a:plugin] = a:1
if has_key(a:1, 'on_event')
let l:group = 'load/'.a:plugin
let l:name = split(a:plugin, '/')[1]
let l:events = join(s:to_a(a:1.on_event), ',')
let l:load = printf("call plug#load('%s')", l:name)
execute "augroup" l:group
autocmd!
execute 'autocmd' l:events '*' l:load '|' 'autocmd!' l:group
execute 'augroup END'
endif
endif
endfunction
function! s:Source(file) abort
try
execute 'source ' . fnameescape(a:file)
catch
echom v:exception
call spacevim#cache#init()
endtry
endfunction
function! s:path(path) abort
return substitute(a:path, '/', '\', 'g')
endfunction
function! spacevim#end() abort
" Backward compatibility
if exists('*Layers') | call Layers() | endif
call s:register_plugin()
" Make vim-better-default settings can be overrided
silent! runtime! plugin/default.vim
call s:config()
if exists('*UserConfig') | call UserConfig() | endif
call s:spacevim_helptags()
call s:check_missing_plugins()
silent doautocmd <nomodeline> User SpacevimAfterUserConfig
endfunction
" Initialize vim-plug system
function! s:register_plugin() abort
" https://github.com/junegunn/vim-plug/issues/559
call plug#begin(get(g:, 'spacevim_plug_home',
\ g:spacevim.nvim ? '~/.local/share/nvim/plugged' : '~/.vim/plugged/'))
call s:packages()
" Register non-excluded plugins
function! s:filter_and_register(val) abort
if index(g:spacevim.excluded, a:val) < 0
if has_key(s:plug_options, a:val)
call plug#(a:val, s:plug_options[a:val])
else
call plug#(a:val)
endif
endif
endfunction
call extend(g:spacevim.excluded, get(g:, 'spacevim_excluded', []))
call map(copy(g:spacevim.plugins), 's:filter_and_register(v:val)')
if exists('*UserInit') | call UserInit() | endif
call plug#end()
endfunction
function! s:packages() abort
let g:spacevim.speed_up_via_timer = get(g:, 'spacevim_speed_up_via_timer', g:spacevim.timer)
" Load Layer packages
for l:layer in g:spacevim.loaded
try
let l:layer_packages = g:spacevim.manifest[l:layer].dir . '/packages.vim'
catch
call spacevim#cache#init()
endtry
call s:Source(l:layer_packages)
endfor
" Try private Layer packages
if exists('g:spacevim.private')
call map(copy(g:spacevim.private), 's:Source(g:spacevim.base ."/private/".v:val."/packages.vim")')
endif
" Load private packages
if filereadable(expand(s:private_packages)) | call s:Source(s:private_packages) | endif
endfunction
function! s:config() abort
" Load Layer config
cal map(copy(g:spacevim.loaded), 's:Source(g:spacevim.manifest[v:val].dir . "/config.vim")')
" Try private Layer config
if exists('g:spacevim.private')
call map(copy(g:spacevim.private), 's:Source(g:spacevim.base ."/private/".v:val."/config.vim")')
endif
" Load private config
if filereadable(expand(s:private_config)) | call s:Source(s:private_config) | endif
endfunction
function! s:spacevim_helptags() abort
let helptag_file = g:spacevim.base . '/core/doc/spacevim.txt'
let helptag_time = getftime(helptag_file)
let helptag_lastrun_file = g:spacevim.base . '/core/doc/.spacevim_last_helptags_run'
let helptag_lastrun_time = filereadable(helptag_lastrun_file) ? readfile(helptag_lastrun_file) : []
if (len(helptag_lastrun_time) != 1) || (helptag_lastrun_time[0] != helptag_time)
try
execute 'helptags' g:spacevim.base . '/core/doc'
call writefile([helptag_time], helptag_lastrun_file)
catch
echom v:exception
endtry
endif
endfunction
function! s:check_missing_plugins() abort
if g:spacevim.timer
call timer_start(1500, 'spacevim#vim#plug#check')
else
augroup checkPlug
autocmd!
autocmd VimEnter * call spacevim#vim#plug#check()
augroup END
endif
endfunction
" Util for config.vim and packages.vim
function! spacevim#load(layer) abort
return index(g:spacevim.loaded, a:layer) > -1 ? 1 : 0
endfunction
" Return true if any layer in layers is loaded.
function! spacevim#load_any(...) abort
for layer in a:000
if index(g:spacevim.loaded, layer) >= 0
return 1
endif
endfor
return 0
endfunction
function! spacevim#VimPlugPostUpdateHook(make, cmd, info) abort
if spacevim#load('programming')
execute('AsyncRun -mode=term -pos=tab '.(a:make?'-program=make ':'').'@ '.a:cmd)
else
call system(a:cmd)
endif
endfunction