forked from rafi/vim-config
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nerdtree.vim
353 lines (313 loc) · 8.99 KB
/
nerdtree.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
" NERDTree
" --------
let g:NERDTreeMinimalUI = 1
let g:NERDTreeWinSize = 25
let g:NERDTreeCascadeOpenSingleChildDir = 1
let g:NERDTreeCascadeSingleChildDir = 0
let g:NERDTreeShowHidden = 1
let g:NERDTreeRespectWildIgnore = 0
let g:NERDTreeAutoDeleteBuffer = 0
let g:NERDTreeQuitOnOpen = 1
let g:NERDTreeHijackNetrw = 1
let g:NERDTreeBookmarksFile = $VARPATH.'/treemarks'
let NERDTreeIgnore = [
\ '\.git$', '\.hg$', '\.svn$', '\.stversions$', '\.pyc$', '\.svn$',
\ '\.DS_Store$', '\.sass-cache$', '__pycache__$', '\.egg-info$', '\.cache$'
\ ]
autocmd MyAutoCmd FileType nerdtree call s:nerdtree_settings()
function! s:nerdtree_settings() abort
setlocal expandtab " Enabling vim-indent-guides
vertical resize 25
endfunction
" Close all open buffers on entering a window if the only
" buffer that's left is the NERDTree buffer
autocmd MyAutoCmd WinEnter * if exists('t:NERDTreeBufName') &&
\ bufwinnr(t:NERDTreeBufName) != -1 && winnr("$") == 1 | q | endif
" if the current window is NERDTree, move focus to the next window
autocmd MyAutoCmd TabLeave * call s:NERDTreeUnfocus()
function! s:NERDTreeUnfocus()
if exists('t:NERDTreeBufName') && bufwinnr(t:NERDTreeBufName) == winnr()
let l:winNum = 1
while (l:winNum <= winnr('$'))
let l:buf = winbufnr(l:winNum)
if buflisted(l:buf) && getbufvar(l:buf, '&modifiable') == 1 &&
\ ! empty(getbufvar(l:buf, '&buftype'))
exec l:winNum.'wincmd w'
return
endif
let l:winNum = l:winNum + 1
endwhile
wincmd w
endif
endfunction
" Private helpers {{{
function! s:SID()
if ! exists('s:sid')
let s:sid = matchstr(expand('<sfile>'), '<SNR>\zs\d\+\ze_SID$')
endif
return s:sid
endfunction
let s:SNR = '<SNR>'.s:SID().'_'
function! s:get_containing_dir(node)
let path = a:node.path
if ! path.isDirectory || ! a:node.isOpen
let path = path.getParent()
endif
return path.str()
endfunction
" }}}
" Custom plugins
" ---
" Plugin: Open diff split on current file {{{
" ---
call NERDTreeAddKeyMap({
\ 'key': 'gd',
\ 'callback': s:SNR.'diff',
\ 'quickhelpText': 'open diff on current file',
\ 'scope': 'FileNode' })
function! s:diff(node)
wincmd w
diffthis
execute 'vsplit '.fnameescape(a:node.path.str())
diffthis
endfunction
" }}}
" Plugin: Jump to project root / user home {{{
" ---
call NERDTreeAddKeyMap({
\ 'key': '&',
\ 'callback': s:SNR.'jump_project_root',
\ 'quickhelpText': 'Open current git root' })
call NERDTreeAddKeyMap({
\ 'key': 'gh',
\ 'callback': s:SNR.'jump_home',
\ 'quickhelpText': 'open user home directory' })
function! s:jump_project_root()
let project_dir = badge#root()
if ! empty(project_dir)
call s:_set_root(project_dir)
endif
endfunction
function! s:jump_home()
call s:_set_root($HOME)
endfunction
function! s:_set_root(dir)
let path = g:NERDTreePath.New(a:dir)
let node = g:NERDTreeDirNode.New(path, b:NERDTree)
call b:NERDTree.changeRoot(node)
endfunction
" }}}
" Plugin: Create a new file or dir in path {{{
" ---
call NERDTreeAddKeyMap({
\ 'key': 'N',
\ 'callback': s:SNR.'create_in_path',
\ 'quickhelpText': 'Create file or dir',
\ 'scope': 'Node' })
function! s:create_in_path(node)
if a:node.path.isDirectory && ! a:node.isOpen
call a:node.parent.putCursorHere(0, 0)
endif
call NERDTreeAddNode()
endfunction
" }}}
" Plugin: Find and grep in path using Denite {{{
" ---
call NERDTreeAddKeyMap({
\ 'key': 'gf',
\ 'callback': s:SNR.'find_in_path',
\ 'quickhelpText': 'Search in dir',
\ 'scope': 'Node' })
call NERDTreeAddKeyMap({
\ 'key': 'gr',
\ 'callback': s:SNR.'grep_dir',
\ 'quickhelpText': 'Grep in dir',
\ 'scope': 'Node' })
function! s:find_in_path(node)
execute 'Denite file_rec:'.s:get_containing_dir(a:node)
endfunction
function! s:grep_dir(node)
execute 'Denite -buffer-name=grep grep:'.s:get_containing_dir(a:node)
endfunction
" }}}
" Plugin: Yank path {{{
" ---
call NERDTreeAddKeyMap({
\ 'key': 'yy',
\ 'callback': s:SNR.'yank_path',
\ 'quickhelpText': 'yank current node',
\ 'scope': 'Node' })
function! s:yank_path(node)
let l:path = a:node.path.str()
call setreg('*', l:path)
echomsg 'Yank node: '.l:path
endfunction
" }}}
" Plugin: Toggle width {{{
" ---
call NERDTreeAddKeyMap({
\ 'key': 'w',
\ 'callback': s:SNR.'toggle_width',
\ 'quickhelpText': 'Toggle window width' })
function! s:toggle_width()
let l:max = 0
for l:z in range(1, line('$'))
let l:len = len(getline(l:z))
if l:len > l:max
let l:max = l:len
endif
endfor
exe 'vertical resize '.(l:max == winwidth('.') ? g:NERDTreeWinSize : l:max)
endfunction
" }}}
" Menu-item: Modify .local.vimrc of project {{{
" ---
call NERDTreeAddMenuItem({
\ 'text': '(l)ocal rc',
\ 'shortcut': 'l',
\ 'callback': s:SNR.'modify_localvimrc'})
function! s:modify_localvimrc()
let current_dir = g:NERDTreeDirNode.GetSelected().path.str({'format': 'Cd'})
if empty(current_dir)
echoerr 'Unable to find current directory'
return
endif
let lvimrc_path = current_dir.'/.local.vimrc'
let cwd = getcwd()
if ! filereadable(lvimrc_path)
call writefile([
\ 'lcd <sfile>:h',
\ '" set isk+=!,?',
\ ],
\ lvimrc_path)
endif
wincmd w
execute 'vsplit '.fnameescape(lvimrc_path)
endfunction
" }}}
" Plugin: Smart h/l navigation {{{
" @see https://github.com/jballanc/nerdtree-space-keys
" ---
call NERDTreeAddKeyMap({
\ 'key': 'l',
\ 'callback': s:SNR.'descendTree',
\ 'quickhelpText': 'open tree and go to first child',
\ 'scope': 'DirNode' })
call NERDTreeAddKeyMap({
\ 'key': 'l',
\ 'callback': s:SNR.'openFile',
\ 'quickhelpText': 'open file',
\ 'scope': 'FileNode' })
call NERDTreeAddKeyMap({
\ 'key': 'h',
\ 'callback': s:SNR.'closeOrAscendTree',
\ 'quickhelpText': 'close dir or move to parent dir',
\ 'scope': 'DirNode' })
call NERDTreeAddKeyMap({
\ 'key': 'h',
\ 'callback': s:SNR.'ascendTree',
\ 'quickhelpText': 'move to parent dir',
\ 'scope': 'FileNode' })
function! s:descendTree(dirnode)
call a:dirnode.open()
call b:NERDTree.render()
if a:dirnode.getChildCount() > 0
let chld = a:dirnode.getChildByIndex(0, 1)
call chld.putCursorHere(0, 0)
end
endfunction
function! s:openFile(filenode)
call a:filenode.activate({'reuse': 'all', 'where': 'p'})
endfunction
function! s:closeOrAscendTree(dirnode)
if a:dirnode.isOpen
call a:dirnode.close()
call b:NERDTree.render()
else
call s:ascendTree(a:dirnode)
endif
endfunction
function! s:ascendTree(node)
let parent = a:node.parent
if parent != {}
call parent.putCursorHere(0, 0)
if parent.isOpen
call parent.close()
call b:NERDTree.render()
end
end
endfunction
" }}}
" Plugin: Execute file with system associated utility {{{
" ---
call NERDTreeAddKeyMap({
\ 'key': 'x',
\ 'callback': s:SNR.'execute_system_associated',
\ 'quickhelpText': 'Execute system associated',
\ 'scope': 'FileNode' })
function! s:execute_system_associated(filenode)
let current_dir = getcwd()
let path = a:filenode.parent.path.str()
" if exists(g:nerdtree_plugin_open_cmd)
" let cmd = g:nerdtree_plugin_open_cmd.' '.path
" call system(cmd)
" endif
" Snippet from vital.vim
try
execute (haslocaldir() ? 'lcd' : 'cd') fnameescape(path)
let filename = fnamemodify(a:filenode.path.str(), ':p')
let s:is_unix = has('unix')
let s:is_windows = has('win16') || has('win32') || has('win64') || has('win95')
let s:is_cygwin = has('win32unix')
let s:is_mac = !s:is_windows && !s:is_cygwin
\ && (has('mac') || has('macunix') || has('gui_macvim') ||
\ (!isdirectory('/proc') && executable('sw_vers')))
" As of 7.4.122, the system()'s 1st argument is converted internally by Vim.
" Note that Patch 7.4.122 does not convert system()'s 2nd argument and
" return-value. We must convert them manually.
let s:need_trans = v:version < 704 || (v:version == 704 && !has('patch122'))
" Detect desktop environment.
if s:is_windows
" For URI only.
if s:need_trans
let filename = iconv(filename, &encoding, 'char')
endif
" Note:
" # and % required to be escaped (:help cmdline-special)
silent execute printf(
\ '!start rundll32 url.dll,FileProtocolHandler %s',
\ escape(filename, '#%'),
\)
elseif s:is_cygwin
" Cygwin.
call system(printf('%s %s', 'cygstart',
\ shellescape(filename)))
elseif executable('xdg-open')
" Linux.
call system(printf('%s %s &', 'xdg-open',
\ shellescape(filename)))
elseif exists('$KDE_FULL_SESSION') && $KDE_FULL_SESSION ==# 'true'
" KDE.
call system(printf('%s %s &', 'kioclient exec',
\ shellescape(filename)))
elseif exists('$GNOME_DESKTOP_SESSION_ID')
" GNOME.
call system(printf('%s %s &', 'gnome-open',
\ shellescape(filename)))
elseif executable('exo-open')
" Xfce.
call system(printf('%s %s &', 'exo-open',
\ shellescape(filename)))
elseif s:is_mac && executable('open')
" Mac OS.
call system(printf('%s %s &', 'open',
\ shellescape(filename)))
else
throw 'vital: System.File: open(): Not supported.'
endif
finally
execute (haslocaldir() ? 'lcd' : 'cd') fnameescape(current_dir)
endtry
endfunction
" }}}
" vim: set foldmethod=marker ts=2 sw=2 tw=80 noet :