forked from rafi/vim-config
-
Notifications
You must be signed in to change notification settings - Fork 3
/
init.vim
404 lines (348 loc) · 10.4 KB
/
init.vim
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
" Configuration and plugin-manager manager :)
" ---
" Maintainer: Rafael Bodill
" See: github.com/rafi/vim-config
"
" Plugin-manager agnostic initialization and user configuration parsing
" Set custom augroup
augroup user_events
autocmd!
augroup END
" Initializes options
let s:default_manager = 'dein'
let s:package_manager = get(g:, 'etc_package_manager', s:default_manager)
if empty(s:package_manager) || s:package_manager ==# 'none'
finish
endif
" Enables 24-bit RGB color in the terminal
if has('termguicolors')
if empty($COLORTERM) || $COLORTERM =~# 'truecolor\|24bit'
set termguicolors
endif
endif
if ! has('nvim')
set t_Co=256
" Set Vim-specific sequences for RGB colors
" Fixes 'termguicolors' usage in vim+tmux
" :h xterm-true-color
let &t_8f = "\<Esc>[38;2;%lu;%lu;%lum"
let &t_8b = "\<Esc>[48;2;%lu;%lu;%lum"
endif
" Disable vim distribution plugins
" let g:loaded_gzip = 1
" let g:loaded_tar = 1
" let g:loaded_tarPlugin = 1
" let g:loaded_zip = 1
" let g:loaded_zipPlugin = 1
let g:loaded_getscript = 1
let g:loaded_getscriptPlugin = 1
let g:loaded_vimball = 1
let g:loaded_vimballPlugin = 1
" let g:loaded_matchit = 1
" let g:loaded_matchparen = 1
let g:loaded_2html_plugin = 1
let g:loaded_logiPat = 1
let g:loaded_rrhelper = 1
let g:no_gitrebase_maps = 1
let g:no_man_maps = 1 " See share/nvim/runtime/ftplugin/man.vim
let g:loaded_netrw = 1
let g:loaded_netrwPlugin = 1
let g:loaded_netrwSettings = 1
let g:loaded_netrwFileHandlers = 1
" Set main configuration directory as parent directory
let $VIM_PATH =
\ get(g:, 'etc_vim_path',
\ exists('*stdpath') ? stdpath('config') :
\ ! empty($MYVIMRC) ? fnamemodify(expand($MYVIMRC, 1), ':h') :
\ ! empty($VIM_PATH) ? expand($VIM_PATH, 1) :
\ fnamemodify(resolve(expand('<sfile>:p')), ':h:h')
\ )
" Set data directory
let $VIM_DATA_PATH = exists('*stdpath') ? stdpath('data') :
\ expand(($XDG_DATA_HOME ? $XDG_DATA_HOME : '~/.local/share') . '/nvim', 1)
" Collection of user plugin list config file-paths
let s:config_paths = get(g:, 'etc_config_paths', [
\ $VIM_PATH . '/config/plugins.yaml',
\ $VIM_PATH . '/config/plugins.local.yaml',
\ $VIM_PATH . '/config/local.plugins.yaml',
\ $VIM_PATH . '/usr/vimrc.yaml',
\ $VIM_PATH . '/usr/vimrc.json',
\ $VIM_PATH . '/vimrc.yaml',
\ $VIM_PATH . '/vimrc.json',
\ ])
" Filter non-existent config paths
call filter(s:config_paths, 'filereadable(v:val)')
function! s:main()
if has('vim_starting')
" When using VIMINIT trick for exotic MYVIMRC locations, add path now.
if &runtimepath !~# $VIM_PATH
set runtimepath^=$VIM_PATH
set runtimepath+=$VIM_PATH/after
endif
" Ensure data directories
for s:path in [
\ $VIM_DATA_PATH . '/backup',
\ $VIM_DATA_PATH . '/sessions',
\ $VIM_DATA_PATH . '/swap',
\ $VIM_DATA_PATH . '/undo',
\ $VIM_PATH . '/spell' ]
if ! isdirectory(s:path)
call mkdir(s:path, 'p', 0770)
endif
endfor
" Try setting up the custom virtualenv created by ./venv.sh
let l:virtualenv = $VIM_DATA_PATH . '/venv/bin/python'
if empty(l:virtualenv) || ! filereadable(l:virtualenv)
" Fallback to old virtualenv location
let l:virtualenv = $VIM_DATA_PATH . '/venv/neovim3/bin/python'
endif
" Python interpreter settings
if filereadable(l:virtualenv)
if has('nvim')
let g:python3_host_prog = l:virtualenv
elseif has('pythonx')
execute 'set pythonthreehome=' . fnamemodify(l:virtualenv, ':h:h')
if has('python3')
set pyxversion=3
elseif has('python')
set pyxversion=2
endif
endif
endif
endif
" Initializes chosen package manager
call s:use_{s:package_manager}()
endfunction
" Use dein as a plugin manager
function! s:use_dein()
let l:cache_path = $VIM_DATA_PATH . '/dein'
if has('vim_starting')
let g:dein#auto_recache = v:true
" let g:dein#lazy_rplugins = v:true
let g:dein#install_progress_type = 'echo'
let g:dein#install_message_type = 'echo'
let g:dein#install_max_processes = 10
let g:dein#enable_notification = v:true
" Add dein to vim's runtimepath
if &runtimepath !~# '/dein.vim'
let s:dein_dir = l:cache_path . '/repos/github.com/Shougo/dein.vim'
" Clone dein if first-time setup
if ! isdirectory(s:dein_dir)
execute '!git clone https://github.com/Shougo/dein.vim' s:dein_dir
if v:shell_error
call s:error('dein installation has failed! is git installed?')
finish
endif
endif
execute 'set runtimepath+='.substitute(
\ fnamemodify(s:dein_dir, ':p') , '/$', '', '')
endif
endif
" Initialize dein.vim (package manager)
if dein#load_state(l:cache_path)
let l:rc = s:parse_config_files()
if empty(l:rc)
call s:error('Empty plugin list')
return
endif
" Start propagating file paths and plugin presets
call dein#begin(l:cache_path, extend([expand('<sfile>')], s:config_paths))
for plugin in l:rc
" If vim already started, don't re-add existing ones
if has('vim_starting')
\ || ! has_key(g:dein#_plugins, fnamemodify(plugin['repo'], ':t'))
call dein#add(plugin['repo'], extend(plugin, {}, 'keep'))
endif
endfor
" Add any local ./dev plugins
if isdirectory($VIM_PATH . '/dev')
call dein#local($VIM_PATH . '/dev', { 'frozen': 1, 'merged': 0 })
endif
call dein#end()
" Save cached state for faster startups
if ! g:dein#_is_sudo
call dein#save_state()
endif
" Update or install plugins if a change detected
if dein#check_install()
call dein#install()
endif
endif
filetype plugin indent on
syntax enable
endfunction
function! s:use_plug() abort
" vim-plug package-manager initialization
let l:cache_root = $VIM_DATA_PATH . '/plug'
let l:cache_init = l:cache_root . '/init.vimplug'
let l:cache_repos = l:cache_root . '/repos'
augroup user_plugin_vimplug
autocmd!
augroup END
if &runtimepath !~# '/init.vimplug'
if ! isdirectory(l:cache_init)
silent !curl -fLo $VIM_DATA_PATH/plug/init.vimplug/autoload/plug.vim
\ --create-dirs
\ https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
autocmd user_plugin_vimplug VimEnter * PlugInstall --sync | source $MYVIMRC
endif
execute 'set runtimepath+='.substitute(
\ fnamemodify(l:cache_init, ':p') , '/$', '', '')
endif
let l:rc = s:parse_config_files()
if empty(l:rc)
call s:error('Empty plugin list')
return
endif
call plug#begin(l:cache_repos)
for plugin in l:rc
call plug#(plugin['repo'], extend(plugin, {}, 'keep'))
endfor
call plug#end()
endfunction
function! s:parse_config_files()
let l:merged = []
try
" Merge all lists of plugins together
for l:cfg_file in s:config_paths
let l:merged = extend(l:merged, s:load_config(l:cfg_file))
endfor
catch /.*/
call s:error(
\ 'Unable to read configuration files at ' . string(s:config_paths))
echoerr v:exception
endtry
" If there's more than one config file source,
" de-duplicate plugins by repo key.
if len(s:config_paths) > 1
call s:dedupe_plugins(l:merged)
endif
return l:merged
endfunction
function! s:dedupe_plugins(list)
let l:list = reverse(a:list)
let l:i = 0
let l:seen = {}
while i < len(l:list)
let l:key = list[i]['repo']
if l:key !=# '' && has_key(l:seen, l:key)
call remove(l:list, i)
else
if l:key !=# ''
let l:seen[l:key] = 1
endif
let l:i += 1
endif
endwhile
return reverse(l:list)
endfunction
" General utilities, mainly for dealing with user configuration parsing
" ---
function! s:error(msg)
for l:mes in s:str2list(a:msg)
echohl WarningMsg | echomsg '[config/init] ' . l:mes | echohl None
endfor
endfunction
function! s:debug(msg)
for l:mes in s:str2list(a:msg)
echohl WarningMsg | echomsg '[config/init] ' . l:mes | echohl None
endfor
endfunction
function! s:load_config(filename)
" Parse YAML/JSON config file
if a:filename =~# '\.json$'
" Parse JSON with built-in json_decode
let l:json = readfile(a:filename)
return has('nvim') ? json_decode(l:json) : json_decode(join(l:json))
elseif a:filename =~# '\.ya\?ml$'
" Parse YAML with common command-line utilities
return s:load_yaml(a:filename)
endif
call s:error('Unknown config file format ' . a:filename)
return ''
endfunction
function! s:str2list(expr)
" Convert string to list
return type(a:expr) ==# v:t_list ? a:expr : split(a:expr, '\n')
endfunction
" YAML related
" ---
let s:convert_tool = ''
function! s:load_yaml(filename)
if empty(s:convert_tool)
let s:convert_tool = s:find_yaml2json_method()
endif
if s:convert_tool ==# 'ruby'
let l:cmd = "ruby -e 'require \"json\"; require \"yaml\"; ".
\ "print JSON.generate YAML.load \$stdin.read'"
elseif s:convert_tool ==# 'python'
let l:cmd = "python -c 'import sys,yaml,json; y=yaml.safe_load(sys.stdin.read()); print(json.dumps(y))'"
elseif s:convert_tool ==# 'yq'
let l:cmd = 'yq e -j -I 0'
else
let l:cmd = s:convert_tool
endif
try
let l:raw = readfile(a:filename)
return json_decode(system(l:cmd, l:raw))
catch /.*/
call s:error([
\ string(v:exception),
\ 'Error loading ' . a:filename,
\ 'Caught: ' . string(v:exception),
\ ])
endtry
endfunction
function! s:find_yaml2json_method()
if exists('*json_decode')
" Try different tools to convert YAML into JSON:
if executable('yj') && s:test_yaml2json('yj')
" See https://github.com/sclevine/yj
return 'yj'
elseif executable('yq') && s:test_yaml2json('yq')
" See https://github.com/mikefarah/yq
return 'yq'
elseif executable('yaml2json') && s:test_yaml2json('yaml2json')
" See https://github.com/bronze1man/yaml2json
return 'yaml2json'
" Or, try ruby. Which is installed on every macOS by default
" and has yaml built-in.
elseif executable('ruby') && s:test_ruby_yaml()
return 'ruby'
" Or, fallback to use python3 and PyYAML
elseif executable('python') && s:test_python_yaml()
return 'python'
endif
call s:error([
\ 'Unable to find a proper YAML parsing utility.',
\ 'Please run: pip3 install --user PyYAML',
\ ])
else
call s:error('"json_decode" unsupported. Upgrade to latest Neovim or Vim')
endif
endfunction
function! s:test_yaml2json(cmd)
" Test yaml2json capabilities
try
let result = system(a:cmd, "---\na: 1.5")
if v:shell_error != 0
return 0
endif
let result = json_decode(result)
return result.a == 1.5
catch
endtry
return 0
endfunction
function! s:test_ruby_yaml()
" Test Ruby YAML capabilities
call system("ruby -e 'require \"json\"; require \"yaml\"'")
return v:shell_error == 0
endfunction
function! s:test_python_yaml()
" Test Python YAML capabilities
call system("python -c 'import sys,yaml,json'")
return v:shell_error == 0
endfunction
call s:main()
" vim: set ts=2 sw=2 tw=80 noet :