-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathctrlspace.vim
More file actions
5700 lines (4803 loc) · 168 KB
/
ctrlspace.vim
File metadata and controls
5700 lines (4803 loc) · 168 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
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
" Vim-CtrlSpace - Vim Workspace Controller
" Maintainer: Szymon Wrozynski
" Version: 4.2.14
"
" The MIT License (MIT)
" Copyright (c) 2013-2015 Szymon Wrozynski <[email protected]> and Contributors
" Original BufferList plugin code - copyright (c) 2005 Robert Lillack <[email protected]>
" Permission is hereby granted, free of charge, to any person obtaining a copy
" of this software and associated documentation files (the "Software"), to deal
" in the Software without restriction, including without limitation the rights
" to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
" copies of the Software, and to permit persons to whom the Software is
" furnished to do so, subject to the following conditions:
" The above copyright notice and this permission notice shall be included in
" all copies or substantial portions of the Software.
" THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
" IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
" FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
" AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
" LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
" OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
" THE SOFTWARE.
"
" Usage:
" https://github.com/szw/vim-ctrlspace/blob/master/README.md
scriptencoding utf-8
if exists('g:ctrlspace_loaded')
finish
endif
let g:ctrlspace_loaded = 1
function! <SID>define_config_variable(name, default_value)
if !exists("g:ctrlspace_" . a:name)
let g:{"ctrlspace_" . a:name} = a:default_value
endif
endfunction
function! <SID>define_symbols()
if g:ctrlspace_unicode_font
let symbols = {
\ "cs": "⌗",
\ "tab": "∙",
\ "all": "፨",
\ "vis": "★",
\ "file": "⊚",
\ "tabs": "○",
\ "c_tab": "●",
\ "ntm": "⁺",
\ "load": "|∷|",
\ "save": "[∷]",
\ "zoom": "⌕",
\ "s_left": "›",
\ "s_right": "‹",
\ "bm": "♥",
\ "help": "?",
\ "iv": "☆",
\ "ia": "★",
\ "im": "+",
\ "dots": "…"
\ }
else
let symbols = {
\ "cs": "#",
\ "tab": "TAB",
\ "all": "ALL",
\ "vis": "VIS",
\ "file": "FILE",
\ "tabs": "-",
\ "c_tab": "+",
\ "ntm": "+",
\ "load": "|::|",
\ "save": "[::]",
\ "zoom": "*",
\ "s_left": "[",
\ "s_right": "]",
\ "bm": "BM",
\ "help": "?",
\ "iv": "-",
\ "ia": "*",
\ "im": "+",
\ "dots": "..."
\ }
endif
return symbols
endfunction
call <SID>define_config_variable("height", 1)
call <SID>define_config_variable("max_height", 0)
call <SID>define_config_variable("set_default_mapping", 1)
call <SID>define_config_variable("default_mapping_key", "<C-Space>")
call <SID>define_config_variable("use_ruby_bindings", 1)
call <SID>define_config_variable("glob_command", "")
call <SID>define_config_variable("use_tabline", 1)
call <SID>define_config_variable("use_mouse_and_arrows_in_term", 0)
call <SID>define_config_variable("statusline_function", "ctrlspace#statusline()")
call <SID>define_config_variable("cache_files", 1)
call <SID>define_config_variable("save_workspace_on_exit", 0)
call <SID>define_config_variable("save_workspace_on_switch", 0)
call <SID>define_config_variable("load_last_workspace_on_start", 0)
call <SID>define_config_variable("cache_dir", expand($HOME))
" make empty to disable
call <SID>define_config_variable("project_root_markers", [".git", ".hg", ".svn", ".bzr", "_darcs", "CVS"])
call <SID>define_config_variable("unicode_font", 1)
call <SID>define_config_variable("symbols", <SID>define_symbols())
call <SID>define_config_variable("ignored_files", '\v(tmp|temp)[\/]') " in addition to 'wildignore' option
call <SID>define_config_variable("max_files", 500)
call <SID>define_config_variable("max_search_results", 200)
call <SID>define_config_variable("search_timing", [50, 500])
call <SID>define_config_variable("search_resonators", ['.', '/', '\', '_', '-'])
command! -nargs=* -range CtrlSpace :call <SID>start_ctrlspace_and_feedkeys(<q-args>)
command! -nargs=0 -range CtrlSpaceGoUp :call <SID>go_outside_list("up")
command! -nargs=0 -range CtrlSpaceGoDown :call <SID>go_outside_list("down")
command! -nargs=0 -range CtrlSpaceTabLabel :call <SID>new_tab_label(0)
command! -nargs=0 -range CtrlSpaceClearTabLabel :call <SID>remove_tab_label(0)
command! -nargs=* -range CtrlSpaceSaveWorkspace :call <SID>save_workspace_externally(<q-args>)
command! -nargs=0 -range CtrlSpaceNewWorkspace :call <SID>new_workspace_externally()
command! -nargs=* -range -bang CtrlSpaceLoadWorkspace :call <SID>load_workspace_externally(<bang>0, <q-args>)
command! -nargs=* -range -complete=dir CtrlSpaceAddProjectRoot :call <SID>add_project_root_ui(<q-args>)
command! -nargs=* -range -complete=dir CtrlSpaceRemoveProjectRoot :call <SID>remove_project_root_ui(<q-args>)
hi def link CtrlSpaceNormal Normal
hi def link CtrlSpaceSelected Visual
hi def link CtrlSpaceSearch IncSearch
hi def link CtrlSpaceStatus StatusLine
function! <SID>set_default_mapping(key, action)
let s:default_key = a:key
if !empty(s:default_key)
if s:default_key ==? "<C-Space>" && !has("gui_running") && !has("win32")
let s:default_key = "<Nul>"
endif
silent! exe 'nnoremap <unique><silent>' . s:default_key . ' ' . a:action
endif
endfunction
if g:ctrlspace_set_default_mapping
call <SID>set_default_mapping(g:ctrlspace_default_mapping_key, ":CtrlSpace<CR>")
endif
let s:files = []
let s:zoom_mode = 0
let s:key_esc_sequence = 0
let s:active_workspace_name = ""
let s:active_workspace_digest = ""
let s:workspace_names = []
let s:last_active_workspace = ""
let s:update_search_results = 0
let s:last_project_root = ""
let s:project_root = ""
let s:symbol_sizes = {}
let s:CS_SEP = "|CS_###_CS|"
let s:plugin_buffer = -1
function! <SID>init_project_roots_and_bookmarks()
let cache_file = g:ctrlspace_cache_dir . "/.cs_cache"
let s:project_roots = {}
let s:bookmarks = []
if filereadable(cache_file)
for line in readfile(cache_file)
if line =~# "CS_PROJECT_ROOT: "
let s:project_roots[line[17:]] = 1
endif
if line =~# "CS_BOOKMARK: "
let parts = split(line[13:], s:CS_SEP)
let bookmark = { "name": ((len(parts) > 1) ? parts[1] : parts[0]), "directory": parts[0], "jump_counter": 0 }
call add(s:bookmarks, bookmark)
let s:project_roots[bookmark.directory] = 1
endif
endfor
endif
endfunction
call <SID>init_project_roots_and_bookmarks()
function! <SID>add_project_root_ui(directory)
let directory = <SID>normalize_directory(empty(a:directory) ? getcwd() : a:directory)
if !isdirectory(directory)
call <SID>msg("Invalid directory: '" . directory . "'")
return
endif
let roots = copy(s:project_roots)
for bookmark in s:bookmarks
let roots[bookmark.directory] = 1
endfor
if exists("roots[directory]")
call <SID>msg("Directory is already a permanent project root!")
return
endif
call <SID>add_project_root(directory)
call <SID>msg("Directory '" . directory . "' has been added as a permanent project root.")
endfunction
function! <SID>remove_project_root_ui(directory)
let directory = <SID>normalize_directory(empty(a:directory) ? getcwd() : a:directory)
if !exists("s:project_roots[directory]")
call <SID>msg("Directory '" . directory . "' is not a permanent project root!" )
return
endif
call <SID>remove_project_root(directory)
call <SID>msg("The project root '" . directory . "' has been removed.")
endfunction
function! <SID>remove_project_root(directory)
let directory = <SID>normalize_directory(a:directory)
if exists("s:project_roots[directory]")
unlet s:project_roots[directory]
endif
let lines = []
let cache_file = g:ctrlspace_cache_dir . "/.cs_cache"
if filereadable(cache_file)
for old_line in readfile(cache_file)
if old_line !~# "CS_PROJECT_ROOT: "
call add(lines, old_line)
endif
endfor
endif
for root in keys(s:project_roots)
call add(lines, "CS_PROJECT_ROOT: " . root)
endfor
call writefile(lines, cache_file)
endfunction
function! <SID>add_project_root(directory)
let directory = <SID>normalize_directory(a:directory)
let s:project_roots[directory] = 1
let lines = []
let bm_roots = {}
let cache_file = g:ctrlspace_cache_dir . "/.cs_cache"
for bookmark in s:bookmarks
let bm_roots[bookmark.directory] = 1
endfor
if filereadable(cache_file)
for old_line in readfile(cache_file)
if old_line !~# "CS_PROJECT_ROOT: "
call add(lines, old_line)
endif
endfor
endif
for root in keys(s:project_roots)
if !exists("bm_roots[root]")
call add(lines, "CS_PROJECT_ROOT: " . root)
endif
endfor
call writefile(lines, cache_file)
endfunction
function! <SID>add_to_bookmarks(directory, name)
let directory = <SID>normalize_directory(a:directory)
let jump_counter = 0
for i in range(0, len(s:bookmarks) - 1)
if s:bookmarks[i].directory == directory
let jump_counter = s:bookmarks[i].jump_counter
call remove(s:bookmarks, i)
break
endif
endfor
let bookmark = { "name": a:name, "directory": directory, "jump_counter": jump_counter }
call add(s:bookmarks, bookmark)
let lines = []
let bm_roots = {}
let cache_file = g:ctrlspace_cache_dir . "/.cs_cache"
if filereadable(cache_file)
for old_line in readfile(cache_file)
if (old_line !~# "CS_BOOKMARK: ") && (old_line !~# "CS_PROJECT_ROOT: ")
call add(lines, old_line)
endif
endfor
endif
for bm in s:bookmarks
call add(lines, "CS_BOOKMARK: " . bm.directory . s:CS_SEP . bm.name)
let bm_roots[bm.directory] = 1
endfor
for root in keys(s:project_roots)
if !exists("bm_roots[root]")
call add(lines, "CS_PROJECT_ROOT: " . root)
endif
endfor
call writefile(lines, cache_file)
let s:project_roots[bookmark.directory] = 1
return bookmark
endfunction
function! <SID>init_key_names()
let lowercase_letters = "q w e r t y u i o p a s d f g h j k l z x c v b n m"
let uppercase_letters = toupper(lowercase_letters)
let control_letters_list = []
for l in split(lowercase_letters, " ")
call add(control_letters_list, "C-" . l)
endfor
let control_letters = join(control_letters_list, " ")
let numbers = "1 2 3 4 5 6 7 8 9 0"
let special_chars = "Space CR BS Tab S-Tab / ? ; : , . < > [ ] { } ( ) ' ` ~ + - _ = ! @ # $ % ^ & * C-f C-b C-u C-d C-h C-w " .
\ "Bar BSlash MouseDown MouseUp LeftDrag LeftRelease 2-LeftMouse " .
\ "Down Up Home End Left Right PageUp PageDown " .
\ 'F1 F2 F3 F4 F5 F6 F7 F8 F9 F10 F11 F12 "'
if !g:ctrlspace_use_mouse_and_arrows_in_term || has("gui_running")
let special_chars .= " Esc"
endif
let special_chars .= (has("gui_running") || has("win32")) ? " C-Space" : " Nul"
let s:key_names = split(join([lowercase_letters, uppercase_letters, control_letters, numbers, special_chars], " "), " ")
" won't work with leader mappings
if exists("s:default_key")
for i in range(0, len(s:key_names) - 1)
let full_key_name = (strlen(s:key_names[i]) > 1) ? ("<" . s:key_names[i] . ">") : s:key_names[i]
if full_key_name ==# s:default_key
call remove(s:key_names, i)
break
endif
endfor
endif
endfunction
call <SID>init_key_names()
au BufEnter * call <SID>add_tab_buffer()
let s:jump_counter = 0
au BufEnter * call <SID>add_jump()
au TabEnter * let s:jump_counter += 1 | let t:ctrlspace_tablist_jump_counter = s:jump_counter
if g:ctrlspace_save_workspace_on_exit
au VimLeavePre * if !empty(s:active_workspace_name) | call <SID>save_workspace_externally("") | endif
endif
if g:ctrlspace_load_last_workspace_on_start
au VimEnter * nested if (argc() == 0) && !empty(<SID>find_project_root()) | call <SID>load_workspace_externally(0, "") | endif
endif
function! ctrlspace#statusline()
hi def link User1 CtrlSpaceStatus
let statusline = "%1*" . g:ctrlspace_symbols.cs . " " . ctrlspace#statusline_mode_segment(" ")
if !&showtabline
let statusline .= " %=%1* %<" . ctrlspace#statusline_tab_segment()
endif
return statusline
endfunction
function! <SID>go_outside_list(direction)
let buffer_list = ctrlspace#bufferlist(tabpagenr())
let current_buffer = bufnr("%")
let current_index = -1
let buffer_list_len = len(buffer_list)
for index in range(0, buffer_list_len - 1)
if buffer_list[index]["number"] == current_buffer
let current_index = index
break
endif
endfor
if current_index == -1
return
endif
if a:direction == "down"
let target_index = current_index + 1
if target_index == buffer_list_len
let target_index = 0
endif
else
let target_index = current_index - 1
if target_index < 0
let target_index = buffer_list_len - 1
endif
endif
silent! exe ":b " . buffer_list[target_index]["number"]
endfunction
function! ctrlspace#bufferlist(tabnr)
let buffer_list = []
let tabnr = tabpagenr()
let single_list = gettabvar(tabnr, "ctrlspace_list")
let visible_buffers = tabpagebuflist(tabnr)
if type(single_list) != 4
return
endif
for i in keys(single_list)
let i = str2nr(i)
let bufname = bufname(i)
if !strlen(bufname) && (getbufvar(i, '&modified') || (index(visible_buffers, i) != -1))
let bufname = '[' . i . '*No Name]'
endif
if strlen(bufname) && getbufvar(i, '&modifiable') && getbufvar(i, '&buflisted')
call add(buffer_list, { "number": i, "raw": bufname })
endif
endfor
call sort(buffer_list, function("s:compare_raw_names"))
return buffer_list
endfunction
function! ctrlspace#buffers(tabnr)
let buffer_list = {}
let ctrlspace_list = gettabvar(a:tabnr, "ctrlspace_list")
let visible_buffers = tabpagebuflist(a:tabnr)
if type(ctrlspace_list) != 4
return buffer_list
endif
for i in keys(ctrlspace_list)
let i = str2nr(i)
let bufname = bufname(i)
if !strlen(bufname) && (getbufvar(i, '&modified') || (index(visible_buffers, i) != -1))
let bufname = '[' . i . '*No Name]'
endif
if strlen(bufname) && getbufvar(i, '&modifiable') && getbufvar(i, '&buflisted')
let buffer_list[i] = bufname
endif
endfor
return buffer_list
endfunction
function! ctrlspace#statusline_tab_segment()
let current_tab = tabpagenr()
let winnr = tabpagewinnr(current_tab)
let buflist = tabpagebuflist(current_tab)
let bufnr = buflist[winnr - 1]
let bufname = bufname(bufnr)
let bufs_number = ctrlspace#tab_buffers_number(current_tab)
let title = ctrlspace#tab_title(current_tab, bufnr, bufname)
if !g:ctrlspace_unicode_font && !empty(bufs_number)
let bufs_number = ":" . bufs_number
end
let tabinfo = string(current_tab) . bufs_number . " "
if ctrlspace#tab_modified(current_tab)
let tabinfo .= "+ "
endif
let tabinfo .= title
return tabinfo
endfunction
function! <SID>create_status_tabline()
let current = tabpagenr()
let line = ""
for i in range(1, tabpagenr("$"))
let line .= (current == i ? g:ctrlspace_symbols.c_tab : g:ctrlspace_symbols.tabs)
endfor
return line
endfunction
function! ctrlspace#statusline_mode_segment(...)
let statusline_elements = []
if s:workspace_mode == 1
call add(statusline_elements, g:ctrlspace_symbols.load)
elseif s:workspace_mode == 2
call add(statusline_elements, g:ctrlspace_symbols.save)
elseif s:tablist_mode
call add(statusline_elements, <SID>create_status_tabline())
elseif s:bookmark_mode
call add(statusline_elements, g:ctrlspace_symbols.bm)
else
if s:file_mode
let symbol = g:ctrlspace_symbols.file
elseif s:single_mode == 2
let symbol = g:ctrlspace_symbols.vis
elseif s:single_mode == 1
let symbol = g:ctrlspace_symbols.tab
else
let symbol = g:ctrlspace_symbols.all
endif
if s:next_tab_mode
let symbol .= g:ctrlspace_symbols.ntm . ctrlspace#tab_buffers_number(tabpagenr() + 1)
endif
call add(statusline_elements, symbol)
endif
if !empty(s:search_letters) || s:search_mode
let search_element = g:ctrlspace_symbols.s_left . join(s:search_letters, "")
if s:search_mode
let search_element .= "_"
endif
let search_element .= g:ctrlspace_symbols.s_right
call add(statusline_elements, search_element)
endif
if s:zoom_mode
call add(statusline_elements, g:ctrlspace_symbols.zoom)
endif
if s:help_mode
call add(statusline_elements, g:ctrlspace_symbols.help)
endif
let separator = (a:0 > 0) ? a:1 : " "
return join(statusline_elements, separator)
endfunction
function! ctrlspace#tab_buffers_number(tabnr)
let buffers_number = len(ctrlspace#buffers(a:tabnr))
let number_to_show = ""
if buffers_number > 1
if g:ctrlspace_unicode_font
let small_numbers = ["⁰", "¹", "²", "³", "⁴", "⁵", "⁶", "⁷", "⁸", "⁹"]
let number_str = string(buffers_number)
for i in range(0, len(number_str) - 1)
let number_to_show .= small_numbers[str2nr(number_str[i])]
endfor
else
let number_to_show = string(buffers_number)
endif
endif
return number_to_show
endfunction
function! ctrlspace#tab_title(tabnr, bufnr, bufname)
let bufname = a:bufname
let bufnr = a:bufnr
let title = gettabvar(a:tabnr, "ctrlspace_label")
if empty(title)
if getbufvar(bufnr, "&ft") == "ctrlspace"
if s:zoom_mode && exists("s:zoom_mode_original_buffer")
let bufnr = s:zoom_mode_original_buffer
else
let bufnr = winbufnr(t:ctrlspace_start_window)
endif
let bufname = bufname(bufnr)
endif
if empty(bufname)
let title = "[" . bufnr . "*No Name]"
else
let title = "[" . fnamemodify(bufname, ':t') . "]"
endif
endif
return title
endfunction
function! ctrlspace#guitablabel()
let winnr = tabpagewinnr(v:lnum)
let buflist = tabpagebuflist(v:lnum)
let bufnr = buflist[winnr - 1]
let bufname = bufname(bufnr)
let title = ctrlspace#tab_title(v:lnum, bufnr, bufname)
let bufs_number = ctrlspace#tab_buffers_number(v:lnum)
if !g:ctrlspace_unicode_font && !empty(bufs_number)
let bufs_number = ":" . bufs_number
end
let label = '' . v:lnum . bufs_number . ' '
if ctrlspace#tab_modified(v:lnum)
let label .= '+ '
endif
let label .= title . ' '
return label
endfunction
function! ctrlspace#tabline()
let last_tab = tabpagenr("$")
let current_tab = tabpagenr()
let tabline = ''
for t in range(1, last_tab)
let winnr = tabpagewinnr(t)
let buflist = tabpagebuflist(t)
let bufnr = buflist[winnr - 1]
let bufname = bufname(bufnr)
let bufs_number = ctrlspace#tab_buffers_number(t)
let title = ctrlspace#tab_title(t, bufnr, bufname)
if !g:ctrlspace_unicode_font && !empty(bufs_number)
let bufs_number = ":" . bufs_number
end
let tabline .= '%' . t . 'T'
let tabline .= (t == current_tab ? '%#TabLineSel#' : '%#TabLine#')
let tabline .= ' ' . t . bufs_number . ' '
if ctrlspace#tab_modified(t)
let tabline .= '+ '
endif
let tabline .= title . ' '
endfor
let tabline .= '%#TabLineFill#%T'
if last_tab > 1
let tabline .= '%='
let tabline .= '%#TabLine#%999XX'
endif
return tabline
endfunction
function! ctrlspace#bufnr()
return bufexists(s:plugin_buffer) ? s:plugin_buffer : -1
endfunction
function! <SID>new_tab_label(tabnr)
let tabnr = a:tabnr > 0 ? a:tabnr : tabpagenr()
let label = <SID>get_input("Label for tab " . tabnr . ": ", gettabvar(tabnr, "ctrlspace_label"))
if !empty(label)
call <SID>set_tab_label(tabnr, label, 0)
endif
endfunction
function! <SID>remove_tab_label(tabnr)
let tabnr = a:tabnr > 0 ? a:tabnr : tabpagenr()
call <SID>set_tab_label(tabnr, "", 0)
endfunction
function! ctrlspace#tab_modified(tabnr)
for b in map(keys(ctrlspace#buffers(a:tabnr)), "str2nr(v:val)")
if getbufvar(b, '&modified')
return 1
endif
endfor
return 0
endfunction
function! <SID>msg(message)
echo g:ctrlspace_symbols.cs . " " . a:message
endfunction
function! <SID>delayed_msg(...)
if !empty(a:000)
let s:delayed_message = a:1
elseif exists("s:delayed_message") && !empty(s:delayed_message)
redraw
call <SID>msg(s:delayed_message)
unlet s:delayed_message
endif
endfunction
function! <SID>max_height()
if g:ctrlspace_max_height
return g:ctrlspace_max_height
else
return &lines / 3
endif
endfunction
function! <SID>internal_file_path(name)
let full_part = empty(s:project_root) ? "" : (s:project_root . "/")
if !empty(g:ctrlspace_project_root_markers)
for candidate in g:ctrlspace_project_root_markers
let candidate_path = full_part . candidate
if isdirectory(candidate_path)
return candidate_path . "/" . a:name
endif
endfor
endif
return full_part . "." . a:name
endfunction
function! <SID>workspace_file()
return <SID>internal_file_path("cs_workspaces")
endfunction
function! <SID>files_cache()
return empty(g:ctrlspace_cache_files) ? "" : <SID>internal_file_path("cs_files")
endfunction
function! <SID>save_first_workspace()
let labels = []
for t in range(1, tabpagenr("$"))
let label = gettabvar(t, "ctrlspace_label")
if !empty(label)
call add(labels, gettabvar(t, "ctrlspace_label"))
endif
endfor
call <SID>save_workspace(join(labels, " "))
endfunction
function! <SID>create_workspace_digest()
let use_nossl = exists("b:nossl_save") && b:nossl_save
if use_nossl
set nossl
endif
let lines = []
for t in range(1, tabpagenr("$"))
let line = [t, gettabvar(t, "ctrlspace_label")]
let bufs = []
let visibles = []
let tab_buffers = ctrlspace#buffers(t)
for bname in values(tab_buffers)
let bufname = fnamemodify(bname, ":p")
if !filereadable(bufname)
continue
endif
call add(bufs, bufname)
endfor
for visible_buf in tabpagebuflist(t)
if exists("tab_buffers[visible_buf]")
let bufname = fnamemodify(tab_buffers[visible_buf], ":p")
if !filereadable(bufname)
continue
endif
call add(visibles, bufname)
endif
endfor
call add(line, join(bufs, "|"))
call add(line, join(visibles, "|"))
call add(lines, join(line, ","))
endfor
if use_nossl
set ssl
endif
return join(lines, "&&&")
endfunction
function! <SID>save_workspace(name)
let name = <SID>get_input("Save current workspace as: ", a:name)
if empty(name)
return 0
endif
call <SID>kill(0, 1)
call <SID>save_workspace_externally(name)
return 1
endfunction
function <SID>save_workspace_externally(name)
if !<SID>project_root_found()
return
endif
call <SID>handle_vim_settings("start")
let cwd_save = fnamemodify(".", ":p:h")
silent! exe "cd " . fnameescape(s:project_root)
if empty(a:name)
if !empty(s:active_workspace_name)
let name = s:active_workspace_name
else
silent! exe "cd " . fnameescape(cwd_save)
call <SID>handle_vim_settings("stop")
return
endif
else
let name = a:name
endif
let filename = <SID>workspace_file()
let last_tab = tabpagenr("$")
let lines = []
let in_workspace = 0
let workspace_start_marker = "CS_WORKSPACE_BEGIN: " . name
let workspace_end_marker = "CS_WORKSPACE_END: " . name
if filereadable(filename)
for old_line in readfile(filename)
if old_line ==# workspace_start_marker
let in_workspace = 1
endif
if !in_workspace
call add(lines, old_line)
endif
if old_line ==# workspace_end_marker
let in_workspace = 0
endif
endfor
endif
call add(lines, workspace_start_marker)
let ssop_save = &ssop
set ssop=winsize,tabpages,buffers,sesdir
let tab_data = []
for t in range(1, last_tab)
let data = {
\ "label": gettabvar(t, "ctrlspace_label"),
\ "autotab": <SID>gettabvar_with_default(t, "ctrlspace_autotab", 0)
\ }
let ctrlspace_list = ctrlspace#buffers(t)
let bufs = []
for [nr, bname] in items(ctrlspace_list)
let bufname = fnamemodify(bname, ":.")
if !filereadable(bufname)
continue
endif
call add(bufs, bufname)
endfor
let data.bufs = bufs
call add(tab_data, data)
endfor
silent! exe "mksession! CS_SESSION"
if !filereadable("CS_SESSION")
silent! exe "cd " . fnameescape(cwd_save)
silent! exe "set ssop=" . ssop_save
call <SID>handle_vim_settings("stop")
call <SID>msg("The workspace '" . name . "' cannot be saved at this moment.")
return
endif
let tab_index = 0
for cmd in readfile("CS_SESSION")
if ((cmd =~# "^edit") && (tab_index == 0)) || (cmd =~# "^tabnew") || (cmd =~# "^tabedit")
let data = tab_data[tab_index]
if tab_index > 0
call add(lines, cmd)
endif
for b in data.bufs
call add(lines, "edit " . b)
endfor
if !empty(data.label)
call add(lines, "let t:ctrlspace_label = '" . substitute(data.label, "'", "''","g") . "'")
endif
if !empty(data.autotab)
call add(lines, "let t:ctrlspace_autotab = " . data.autotab)
endif
if tab_index == 0
call add(lines, cmd)
elseif cmd =~# "^tabedit"
call add(lines, cmd[3:]) "make edit from tabedit
endif
let tab_index += 1
else
let badd_list = matchlist(cmd, "\\m^badd \+\\d* \\(.*\\)$")
if !(exists("badd_list[1]") && !empty(badd_list[1]) && !filereadable(badd_list[1]))
call add(lines, cmd)
endif
endif
endfor
call add(lines, workspace_end_marker)
call writefile(lines, filename)
call delete("CS_SESSION")
call <SID>set_active_workspace_name(name)
let s:active_workspace_digest = <SID>create_workspace_digest()
call <SID>set_workspace_names()
silent! exe "cd " . fnameescape(cwd_save)
silent! exe "set ssop=" . ssop_save
call <SID>handle_vim_settings("stop")
call <SID>msg("The workspace '" . name . "' has been saved.")
endfunction
function! <SID>delete_workspace(name)
if !<SID>confirmed("Delete workspace '" . a:name . "'?")
return
endif
let filename = <SID>workspace_file()
let lines = []
let in_workspace = 0
let workspace_start_marker = "CS_WORKSPACE_BEGIN: " . a:name
let workspace_end_marker = "CS_WORKSPACE_END: " . a:name
if filereadable(filename)
for old_line in readfile(filename)
if old_line ==# workspace_start_marker
let in_workspace = 1
endif
if !in_workspace
call add(lines, old_line)
endif
if old_line ==# workspace_end_marker
let in_workspace = 0
endif
endfor
endif
call writefile(lines, filename)
if s:active_workspace_name ==# a:name
call <SID>set_active_workspace_name("")