-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathwindow.vim
More file actions
600 lines (484 loc) · 13.3 KB
/
Copy pathwindow.vim
File metadata and controls
600 lines (484 loc) · 13.3 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
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
let s:config = ctrlspace#context#Configuration()
let s:modes = ctrlspace#modes#Modes()
function! ctrlspace#window#MaxHeight()
let maxFromConfig = s:config.MaxHeight
if maxFromConfig
return maxFromConfig
else
return &lines / 3
endif
endfunction
function! ctrlspace#window#Toggle(internal)
if !a:internal
call s:resetWindow()
endif
" if we get called and the list is open --> close it
let pbuf = ctrlspace#context#PluginBuffer()
if bufexists(pbuf)
if bufwinnr(pbuf) != -1
call ctrlspace#window#Kill(pbuf, 1)
return
else
call ctrlspace#window#Kill(pbuf, 0)
if !a:internal
let t:CtrlSpaceStartWindow = winnr()
let t:CtrlSpaceWinrestcmd = winrestcmd()
let t:CtrlSpaceActivebuf = bufnr("")
endif
endif
elseif !a:internal
" make sure zoom window is closed
silent! exe "pclose"
let t:CtrlSpaceStartWindow = winnr()
let t:CtrlSpaceWinrestcmd = winrestcmd()
let t:CtrlSpaceActivebuf = bufnr("")
endif
if s:modes.Zoom.Enabled
let t:CtrlSpaceActivebuf = bufnr("")
endif
" create the buffer first & set it up
silent! exe "noautocmd botright pedit CtrlSpace"
silent! exe "noautocmd wincmd P"
silent! exe "resize" s:config.Height
" zoom start window in Zoom Mode
if s:modes.Zoom.Enabled
silent! exe t:CtrlSpaceStartWindow . "wincmd w"
vert resize | resize
silent! exe "noautocmd wincmd P"
endif
call s:setUpBuffer()
if s:modes.Help.Enabled
call ctrlspace#help#DisplayHelp(s:filler())
call ctrlspace#util#SetStatusline()
return
endif
let [b:patterns, b:indices, b:size, b:text] = ctrlspace#engine#Content()
" set up window height
if b:size > s:config.Height
let maxHeight = ctrlspace#window#MaxHeight()
if b:size < maxHeight
silent! exe "resize " . b:size
else
silent! exe "resize " . maxHeight
endif
endif
silent! exe "set updatetime=" . s:config.SearchTiming
call s:displayContent()
call ctrlspace#util#SetStatusline()
" display search patterns
for pattern in b:patterns
" escape ~ sign because of E874: (NFA) Could not pop the stack !
call matchadd("CtrlSpaceSearch", "\\c" .substitute(pattern, '\~', '\\~', "g"))
endfor
call s:setActiveLine()
normal! zb
endfunction
function! ctrlspace#window#GoToBufferListPosition(direction)
let bufferList = ctrlspace#api#BufferList(tabpagenr())
let currentBuffer = bufnr("%")
let currentIndex = -1
let bufferListLen = len(bufferList)
for index in range(bufferListLen)
if bufferList[index]["index"] == currentBuffer
let currentIndex = index
break
endif
endfor
if currentIndex == -1
return
endif
if a:direction == "down"
let targetIndex = currentIndex + 1
if targetIndex == bufferListLen
let targetIndex = 0
endif
else
let targetIndex = currentIndex - 1
if targetIndex < 0
let targetIndex = bufferListLen - 1
endif
endif
silent! exe ":b " . bufferList[targetIndex]["index"]
endfunction
function! ctrlspace#window#GoToStartWindow()
silent! exe t:CtrlSpaceStartWindow . "wincmd w"
if winrestcmd() != t:CtrlSpaceWinrestcmd
silent! exe t:CtrlSpaceWinrestcmd
if winrestcmd() != t:CtrlSpaceWinrestcmd
wincmd =
endif
endif
endfunction
function! ctrlspace#window#Kill(pluginBuffer, final)
" added workaround for strange Vim behavior when, when kill starts with some delay
" (in a wrong buffer). This happens in some Nop modes (in a File List view).
if (exists("s:killingNow") && s:killingNow) || (!a:pluginBuffer && &ft != "ctrlspace")
return
endif
let s:killingNow = 1
if exists("b:updatetimeSave")
silent! exe "set updatetime=" . b:updatetimeSave
endif
if exists("b:timeoutlenSave")
silent! exe "set timeoutlen=" . b:timeoutlenSave
endif
if exists("b:mouseSave")
silent! exe "set mouse=" . b:mouseSave
endif
" shellslash support for win32
if exists("b:nosslSave") && b:nosslSave
set nossl
endif
if a:pluginBuffer
silent! exe ':' . a:pluginBuffer . 'bwipeout'
else
bwipeout
endif
if a:final
call ctrlspace#util#HandleVimSettings("stop")
if s:modes.Search.Data.Restored
call ctrlspace#search#AppendToSearchHistory()
endif
call ctrlspace#window#GoToStartWindow()
if s:modes.Zoom.Enabled
exec ":b " . s:modes.Zoom.Data.Buffer
call s:modes.Zoom.SetData("Buffer", 0)
call s:modes.Zoom.Disable()
call ctrlspace#buffers#DeleteForeignBuffers(1)
call ctrlspace#buffers#DeleteHiddenNonameBuffers(1)
endif
set guicursor-=n:block-CtrlSpaceSelected-blinkon0
endif
unlet s:killingNow
endfunction
function! ctrlspace#window#QuitVim()
if !s:config.SaveWorkspaceOnExit
let aw = ctrlspace#workspaces#ActiveWorkspace()
if aw.Status == 2 && !ctrlspace#ui#Confirmed("Current workspace ('" . aw.Name . "') not saved. Proceed anyway?")
return
endif
endif
if !ctrlspace#ui#ProceedIfModified()
return
endif
call ctrlspace#window#Kill(0, 1)
qa!
endfunction
function! ctrlspace#window#MoveSelectionBar(where)
if b:size < 1
return
endif
let newpos = 0
if !exists("b:lastline")
let b:lastline = 0
endif
setlocal modifiable
" the mouse was pressed: remember which line
" and go back to the original location for now
if a:where == "mouse"
let newpos = line(".")
call s:goto(b:lastline)
endif
" exchange the first char (>) with a space
call setline(line("."), " " . strpart(getline(line(".")), 1))
" go where the user want's us to go
if a:where == "up"
call s:goto(line(".") - 1)
elseif a:where == "down"
call s:goto(line(".") + 1)
elseif a:where == "mouse"
call s:goto(newpos)
elseif a:where == "pgup"
let newpos = line(".") - winheight(0)
if newpos < 1
let newpos = 1
endif
call s:goto(newpos)
elseif a:where == "pgdown"
let newpos = line(".") + winheight(0)
if newpos > line("$")
let newpos = line("$")
endif
call s:goto(newpos)
elseif a:where == "half_pgup"
let newpos = line(".") - winheight(0) / 2
if newpos < 1
let newpos = 1
endif
call s:goto(newpos)
elseif a:where == "half_pgdown"
let newpos = line(".") + winheight(0) / 2
if newpos > line("$")
let newpos = line("$")
endif
call s:goto(newpos)
else
call s:goto(a:where)
endif
" and mark this line with a >
call setline(line("."), ">" . strpart(getline(line(".")), 1))
" remember this line, in case the mouse is clicked
" (which automatically moves the cursor there)
let b:lastline = line(".")
setlocal nomodifiable
endfunction
function! ctrlspace#window#MoveCursor(where)
if a:where == "up"
call s:goto(line(".") - 1)
elseif a:where == "down"
call s:goto(line(".") + 1)
elseif a:where == "mouse"
call s:goto(line("."))
elseif a:where == "pgup"
let newpos = line(".") - winheight(0)
if newpos < 1
let newpos = 1
endif
call s:goto(newpos)
elseif a:where == "pgdown"
let newpos = line(".") + winheight(0)
if newpos > line("$")
let newpos = line("$")
endif
call s:goto(newpos)
elseif a:where == "half_pgup"
let newpos = line(".") - winheight(0) / 2
if newpos < 1
let newpos = 1
endif
call s:goto(newpos)
elseif a:where == "half_pgdown"
let newpos = line(".") + winheight(0) / 2
if newpos > line("$")
let newpos = line("$")
endif
call s:goto(newpos)
else
call s:goto(a:where)
endif
endfunction
function! ctrlspace#window#SelectedIndex()
return b:indices[line(".") - 1]
endfunction
function! ctrlspace#window#GoToWindow()
let nr = ctrlspace#window#SelectedIndex()
if bufwinnr(nr) != -1
call ctrlspace#window#Kill(0, 1)
silent! exe bufwinnr(nr) . "wincmd w"
return 1
endif
return 0
endfunction
" tries to set the cursor to a line of the buffer list
function! s:goto(line)
if b:size < 1
return
endif
if a:line < 1
call s:goto(b:size - a:line)
elseif a:line > b:size
call s:goto(a:line - b:size)
else
call cursor(a:line, 1)
endif
endfunction
function! s:resetWindow()
call s:modes.Help.Disable()
call s:modes.Nop.Disable()
call s:modes.Search.Disable()
call s:modes.NextTab.Disable()
call s:modes.Buffer.Enable()
call s:modes.Buffer.SetData("SubMode", "single")
call s:modes.Search.SetData("NewSearchPerformed", 0)
call s:modes.Search.SetData("Restored", 0)
call s:modes.Search.SetData("Letters", [])
call s:modes.Search.SetData("HistoryIndex", -1)
call s:modes.Workspace.SetData("LastBrowsed", 0)
call s:modes.Workspace.SetData("SubMode", "load")
call ctrlspace#roots#SetCurrentProjectRoot(ctrlspace#roots#FindProjectRoot())
call s:modes.Bookmark.SetData("Active", ctrlspace#bookmarks#FindActiveBookmark())
call s:modes.Search.RemoveData("LastSearchedDirectory")
if ctrlspace#roots#LastProjectRoot() != ctrlspace#roots#CurrentProjectRoot()
call ctrlspace#files#ClearAll()
call ctrlspace#roots#SetLastProjectRoot(ctrlspace#roots#CurrentProjectRoot())
call ctrlspace#workspaces#SetWorkspaceNames()
endif
set guicursor+=n:block-CtrlSpaceSelected-blinkon0
call ctrlspace#util#HandleVimSettings("start")
endfunction
function! s:setUpBuffer()
setlocal noswapfile
setlocal buftype=nofile
setlocal bufhidden=delete
setlocal nobuflisted
setlocal nomodifiable
setlocal nowrap
setlocal nonumber
if exists('+relativenumber')
setlocal norelativenumber
endif
setlocal nocursorcolumn
setlocal nocursorline
setlocal nospell
setlocal nolist
setlocal cc=
setlocal filetype=ctrlspace
call ctrlspace#context#SetPluginBuffer(bufnr("%"))
let root = ctrlspace#roots#CurrentProjectRoot()
if !empty(root)
silent! exe "lcd " . fnameescape(root)
endif
if &timeout
let b:timeoutlenSave = &timeoutlen
set timeoutlen=10
endif
let b:updatetimeSave = &updatetime
" shellslash support for win32
if has("win32") && !&ssl
let b:nosslSave = 1
set ssl
endif
augroup CtrlSpaceUpdateSearch
au!
au CursorHold <buffer> call ctrlspace#search#UpdateSearchResults()
augroup END
augroup CtrlSpaceLeave
au!
au BufLeave <buffer> call ctrlspace#window#Kill(0, 1)
augroup END
" set up syntax highlighting
if has("syntax")
syn clear
syn match CtrlSpaceNormal / .*/
syn match CtrlSpaceSelected /> .*/hs=s+1
endif
call clearmatches()
if !s:config.UseMouseAndArrowsInTerm && !has("gui_running")
" Block unnecessary escape sequences!
noremap <silent><buffer><esc>[ :call ctrlspace#keys#MarkKeyEscSequence()<CR>
let b:mouseSave = &mouse
set mouse=
endif
for k in ctrlspace#keys#KeyNames()
let key = strlen(k) > 1 ? ("<" . k . ">") : k
if k == '"'
let k = '\' . k
endif
silent! exe "noremap <silent><buffer> " . key . " :call ctrlspace#keys#Keypressed(\"" . k . "\")<CR>"
endfor
endfunction
function! s:setActiveLine()
if !empty(s:modes.Search.Data.Letters) && s:modes.Search.Data.NewSearchPerformed
call ctrlspace#window#MoveSelectionBar(line("$"))
if !s:modes.Search.Enabled
call s:modes.Search.SetData("NewSearchPerformed", 0)
endif
else
let clv = ctrlspace#modes#CurrentListView()
if clv.Name ==# "Workspace"
if clv.Data.LastBrowsed
let activeLine = clv.Data.LastBrowsed
else
let activeLine = 1
let aw = ctrlspace#workspaces#ActiveWorkspace()
if aw.Status
let currWsp = aw.Name
elseif !empty(clv.Data.LastActive)
let currWsp = clv.Data.LastActive
else
let currWsp = ""
endif
if !empty(currWsp)
let workspaces = ctrlspace#workspaces#Workspaces()
for i in range(b:size)
if currWsp ==# workspaces[b:indices[i]]
let activeLine = i + 1
break
endif
endfor
endif
endif
elseif clv.Name ==# "Tab"
let activeLine = tabpagenr()
elseif clv.Name ==# "Bookmark"
let activeLine = 1
if !empty(clv.Data.Active)
let bookmarks = ctrlspace#bookmarks#Bookmarks()
for i in range(b:size)
if clv.Data.Active.Name ==# bookmarks[b:indices[i]].Name
let activeLine = i + 1
break
endif
endfor
endif
elseif clv.Name ==# "File"
let activeLine = line("$")
else
let activeLine = 0
let maxCounter = 0
let lastLine = 0
for i in range(b:size)
if b:indices[i] == t:CtrlSpaceActivebuf
let activeLine = i + 1
break
endif
let currentJumpCounter = ctrlspace#util#GetbufvarWithDefault(b:indices[i], "CtrlSpaceJumpCounter", 0)
if currentJumpCounter > maxCounter
let maxCounter = currentJumpCounter
let lastLine = i + 1
endif
endfor
if !activeLine
let activeLine = (lastLine > 0) ? lastLine : b:size - 1
endif
endif
call ctrlspace#window#MoveSelectionBar(activeLine)
endif
endfunction
function! s:filler()
" generate a variable to fill the buffer afterwards
" (we need this for "full window" color :)
if !exists("s:filler['" . &columns . "']")
let fill = "\n"
let i = 0
while i < &columns
let i += 1
let fill = ' ' . fill
endwhile
if !exists("s:filler")
let s:filler = {}
endif
let s:filler[string(&columns)] = fill
endif
return s:filler[string(&columns)]
endfunction
function! s:fillBufferSpace()
let fill = s:filler()
while winheight(0) > line(".")
silent! put =fill
endwhile
endfunction
function! s:displayContent()
setlocal modifiable
if b:size > 0
silent! put! =b:text
normal! GkJ
call s:fillBufferSpace()
call s:modes.Nop.Disable()
else
let emptyListMessage = " List empty"
let sizes = ctrlspace#context#SymbolSizes()
if &columns < (strwidth(emptyListMessage) + 2)
let emptyListMessage = strpart(emptyListMessage, 0, &columns - 2 - sizes.Dots) . s:config.Symbols.Dots
endif
while strwidth(emptyListMessage) < &columns
let emptyListMessage .= ' '
endwhile
silent! put! =emptyListMessage
normal! GkJ
call s:fillBufferSpace()
normal! 0
call s:modes.Nop.Enable()
endif
setlocal nomodifiable
endfunction