-
Notifications
You must be signed in to change notification settings - Fork 0
/
bootstrap_RTP.vim
278 lines (249 loc) · 11.3 KB
/
bootstrap_RTP.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
command! -bar -nargs=1 AddRcVim call AddRcVim(<q-args>, 0)
command! -bar -nargs=1 AddRcGvim call AddRcGvim(<q-args>, 0)
command! -bar -nargs=1 AddRcLayer call AddRcLayer(<q-args>, 1)
command! -bar -nargs=+ AddIDEDir call AddIDEDir(<f-args>)
command! -nargs=1 -bar PathAddPP call PathAddPP(<f-args>)
command! -nargs=1 -bar PathAddRTP call PathAddRTP(<f-args>)
command! -nargs=1 -bar PathAddAfterPP call PathAddAfterPP(<f-args>)
command! -nargs=1 -bar PathAddAfterRTP call PathAddAfterRTP(<f-args>)
command! -nargs=1 -bar Src execute printf("source %s", _cmdPath(<f-args>))
command! -bang -nargs=1 ReadPathE echon join(['# '.<q-args>] + split(eval(<f-args>), ","), "\n")."\n"
command! -bang -nargs=1 ReadPath call append(line('.'), ['# '.<q-args>] + split(eval(<f-args>), ","))
if $VIMS_PRINT_DBG ==# "1"
if has('nvim')
let s:log_debug = "/tmp/nvim_dbg_initial.txt"
else
let s:log_debug = "/tmp/vim_dbg_initial.txt"
endif
call writefile([&rtp, &packpath], s:log_debug)
endif
fun! VimFlavor() abort
if has("nvim")
return "nvim"
else
return "vim"
endif
endfun
fun! AddIDEDir(dir, projectname) abort
call AddRcLayer(a:dir . "/layer", 1)
call AddRcVim(a:dir . "/vimrc", 0)
call AddRcGvim(a:dir . "/gvimrc", 1)
endfun
fun! AddRcVim(rcfile, optional) abort
call add(g:vimruntime.stock_vim_init.vimrc_spec.rc, a:rcfile)
if a:optional == 1
call add(g:vimruntime.stock_vim_init.vimrc_spec.rc_is_optional, a:rcfile)
endif
endfun
fun! AddRcGvim(rcfile, optional) abort
call add(g:vimruntime.stock_vim_init.gvimrc_spec.rc, a:rcfile)
endfun
fun! AddRcLayer(dir, optional) abort
call AddRcVim(a:dir . "/" . "shared" . "/vimrc", a:optional)
call AddRcGvim(a:dir . "/" . "shared" . "/gvimrc", 1)
call AddRcVim(a:dir . "/" . VimFlavor() . "/vimrc", a:optional)
call AddRcGvim(a:dir . "/" . VimFlavor() . "/gvimrc", 1)
call PathAddPP(a:dir . "/" . "shared")
call PathAddPP(a:dir . "/" . VimFlavor())
call PathAddAfterRTP(a:dir . "/shared")
call PathAddPP(a:dir . "/" . VimFlavor())
call PathAddAfterRTP(a:dir . "/" . "shared" . "/after")
call PathAddAfterRTP(a:dir . "/" . VimFlavor() . "/after")
endfun
fun! PathAddPP(arg) abort
call add(g:vimruntime.stock_vim_init.prependPPList, a:arg)
endfun
fun! PathAddRTP(arg) abort
call add(g:vimruntime.stock_vim_init.prependRTPList, a:arg)
endfun
fun! PathAddAfterPP(arg) abort
call add(g:vimruntime.stock_vim_init.appendPPList, a:arg)
endfun
fun! PathAddAfterRTP(arg) abort
call add(g:vimruntime.stock_vim_init.appendRTPList, a:arg)
endfun
" fun! _AdaptToBuildInSourcetree() abort
" " detect if vim is used just after "configure; make" and change runtimepath accordingly
" if ! empty(glob(g:_vim_instance.probable_runtimedir))
" echom "found vim source runtime to be more precise: " . g:_vim_instance.probable_runtimedir
" let $VIMRUNTIME=g:_vim_instance.probable_runtimedir
" let $VIM=g:_vim_instance.probable_install_prefix
" " echom "rtp orig:" . &rtp
" let &runtimepath = substitute(&runtimepath, '\V'.escape(g:_vim_instance.orig_VIMRUNTIME, '\'), g:_vim_instance.probable_runtimedir, 'g')
" let &runtimepath = substitute(&runtimepath, '\V'.escape(g:_vim_instance.orig_VIM, '\'), g:_vim_instance.probable_install_prefix, 'g')
" let &packpath = substitute(&packpath, '\V'.escape(g:_vim_instance.orig_VIMRUNTIME, '\'), g:_vim_instance.probable_runtimedir, 'g')
" let &packpath = substitute(&packpath, '\V'.escape(g:_vim_instance.orig_VIM, '\'), g:_vim_instance.probable_install_prefix, 'g')
" let &helpfile = substitute(&helpfile, '\V'.escape(g:_vim_instance.orig_VIMRUNTIME, '\'), g:_vim_instance.probable_runtimedir, 'g')
" let &helpfile = substitute(&helpfile, '\V'.escape(g:_vim_instance.orig_VIM, '\'), g:_vim_instance.probable_install_prefix, 'g')
" " echom "rtp afte:" . &rtp
" endif
" endf
fun! _path_rel_to(base, rel) abort
return trim(system(printf("readlink -f %s", shellescape(a:base."/".a:rel))))
endf
fun! _cmdPath(...) abort
let args=[]
for a in a:000
if match(a, '^[s]:') > -1
throw "script-level variables are not supported in _cmdPath commands"
elseif match(a, '^[glab]:') > -1
call add(args, eval(a))
else
call add(args, a)
endif
endfor
return join(args, "/")
endf
fun! _PathSettingPrepend(setting, part) abort
" echom printf("DBG: _PathSettingPrepend('%s', '%s')", string(a:setting), string(a:part))
if a:setting ==# "runtimepath" || a:setting ==# "rtp"
let oldsetting = &runtimepath
let &runtimepath=a:part.",".oldsetting
elseif a:setting ==# "packpath" || a:setting ==# "pp"
let oldsetting = &packpath
let &packpath=a:part.",".oldsetting
else
echoe "unknown setting: ".a:setting
endif
endf
fun! _KeepPathPart(part, pathname) abort
let p = expand(a:part)
let matchesHOMEVIM = 0
if has("nvim")
let tomatchHOMEVIM = fnamemodify(expand("$HOME")."/.config/nvim", ':p:h')
else
let tomatchHOMEVIM = fnamemodify(expand("$HOME")."/.vim", ':p:h')
endif
let tomatchVIMRUNTIME = fnamemodify(expand("$VIMRUNTIME"), ':p:h:h')
let exists=0
" echom "looking if ".p." matches ".tomatchHOMEVIM
if stridx(p, tomatchHOMEVIM) == 0
let matchesHOMEVIM=1
" echom "matched HOMEVIM!"
endif
" echom "looking if ".p." matches ".tomatchVIMRUNTIME
if isdirectory(p)
let exists = 1
" echom "exists!"
endif
" if ! matchesHOMEVIM && exists
if ! matchesHOMEVIM
return 1
else
return 0
endif
endf
fun! _VimRuntimeLog(msg, ...) abort "{{{
" optional parameter: 1 if is fatal -- then quit
call writefile([a:msg], vimruntime.logfile, 'a')
if a:0 > 0 && a:1 == 1
echoerr a:msg
q!
endif
endf
if ! exists('g:_vim_instance')
let g:_vim_instance={}
let g:_vim_instance.cmd = v:argv[0]
if g:_vim_instance.cmd[0] != '/'
let g:_vim_instance.executable = resolve(systemlist("which ".g:_vim_instance.cmd)[0])
else
let g:_vim_instance.executable = resolve(g:_vim_instance.cmd)
endif
" echom "DBG: probable vim executable=" . g:_vim_instance.executable
let g:_vim_instance.probable_install_prefix = fnamemodify(g:_vim_instance.executable, ":p:h")."/.."
let _probable_vim_runtimedirs=[]
let _probable_nvim_runtimedirs=[]
call add(_probable_vim_runtimedirs, g:_vim_instance.probable_install_prefix . "/share/vim/vim92")
call add(_probable_vim_runtimedirs, g:_vim_instance.probable_install_prefix . "/share/vim/vim91")
call add(_probable_vim_runtimedirs, g:_vim_instance.probable_install_prefix . "/share/vim/vim90")
call add(_probable_vim_runtimedirs, g:_vim_instance.probable_install_prefix . "/share/vim/vim82")
call add(_probable_vim_runtimedirs, g:_vim_instance.probable_install_prefix . "/share/vim/vim81")
call add(_probable_vim_runtimedirs, g:_vim_instance.probable_install_prefix . "/share/vim/vim80")
call add(_probable_vim_runtimedirs, g:_vim_instance.probable_install_prefix . "/runtime")
call add(_probable_nvim_runtimedirs, g:_vim_instance.probable_install_prefix . "/share/nvim/runtime")
let __probable_runtimedirs = []
if has('nvim')
let __probable_runtimedirs = _probable_nvim_runtimedirs
else
let __probable_runtimedirs = _probable_vim_runtimedirs
endif
for probable_runtimedir in __probable_runtimedirs
if isdirectory(probable_runtimedir)
if has('nvim')
if ! empty(glob(probable_runtimedir . "/filetype.lua"))
let g:_vim_instance.probable_runtimedir = probable_runtimedir
break
endif
else
if ! empty(glob(probable_runtimedir . "/filetype.vim"))
let g:_vim_instance.probable_runtimedir = probable_runtimedir
break
endif
endif
endif
endfor
if ! exists('g:_vim_instance.probable_runtimedir')
echoerr "ERROR: could not find a probable runtime directory. tried: ".join(__probable_runtimedirs, ", ")
endif
let g:_vim_instance.orig_VIMRUNTIME = $VIMRUNTIME
let g:_vim_instance.orig_VIM = $VIM
" echom "DBG: checking if nvim... " . has('nvim')
" if has('nvim')
" echom "DBG: checking: isdir: " . ($VIMRUNTIME . "/runtime") . ": " . isdirectory($VIMRUNTIME . "/runtime")
" if isdirectory($VIMRUNTIME . "/runtime")
" let $VIMRUNTIME=$VIMRUNTIME . "/runtime"
" echom "setting VIMRUNTIME to " . $VIMRUNTIME . "as a bugfix."
" let runtimepathelements = split(&runtimepath, ',')
" let newruntimepathelements = []
" for rtp in runtimepathelements
" if rtp ==# g:_vim_instance.orig_VIMRUNTIME
" let rtp_new = $VIMRUNTIME
" else
" let rtp_new = rtp
" endif
" endfor
" let &runtimepath = join(newruntimepathelements, ',')
" endif
" endif
" call _AdaptToBuildInSourcetree()
let g:vimruntime={}
let g:vimruntime.logfile = expand('<sfile>:p:h') . '/vimruntime.log'
let g:vimruntime.stock_vim_init={}
let g:vimruntime.stock_vim_init.scriptfile = expand('<sfile>:p')
" These are records of the original paths
let g:vimruntime.stock_vim_init.origRTP = &runtimepath
let g:vimruntime.stock_vim_init.origPP = &packpath
" These are for vimrc/gvimrc sourcing
let g:vimruntime.stock_vim_init.vimrc_spec = {}
let g:vimruntime.stock_vim_init.vimrc_spec.rc = []
let g:vimruntime.stock_vim_init.vimrc_spec.rc_is_optional = []
let g:vimruntime.stock_vim_init.gvimrc_spec = {}
let g:vimruntime.stock_vim_init.gvimrc_spec.rc = []
" These are for rewriting the stock RTP
let g:vimruntime.stock_vim_init.newRTPList = []
let g:vimruntime.stock_vim_init.newPPList = []
let g:vimruntime.stock_vim_init.appendRTPList = []
let g:vimruntime.stock_vim_init.prependRTPList = []
let g:vimruntime.stock_vim_init.appendPPList = []
let g:vimruntime.stock_vim_init.prependPPList = []
if strlen(g:_vim_instance.probable_runtimedir) > 0
if ! has('nvim')
call add(g:vimruntime.stock_vim_init.newRTPList, g:_vim_instance.probable_runtimedir)
else
" call add(g:vimruntime.stock_vim_init.newRTPList, "/home/user/neovim/runtime")
endif
endif
for p in split(g:vimruntime.stock_vim_init.origRTP, ",")
" echom "checking ".p." for inclusion in ".g:_vim_instance.probable_runtimedir
if _KeepPathPart(p, 'runtimepath')
call add(g:vimruntime.stock_vim_init.newRTPList, p)
else
" echom 'not keeping path part: ' . p
endif
endfor
for p in split(g:vimruntime.stock_vim_init.origPP, ",")
if _KeepPathPart(p, 'packpath')
call add(g:vimruntime.stock_vim_init.newPPList, p)
endif
endfor
endif