-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathworkspaces.vim
More file actions
520 lines (397 loc) · 14.4 KB
/
workspaces.vim
File metadata and controls
520 lines (397 loc) · 14.4 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
let s:config = ctrlspace#context#Configuration()
let s:modes = ctrlspace#modes#Modes()
let s:workspaces = []
function! ctrlspace#workspaces#Workspaces() abort
return s:workspaces
endfunction
function! ctrlspace#workspaces#SetWorkspaceNames() abort
let filename = ctrlspace#util#WorkspaceFile()
let s:workspaces = []
call s:modes.Workspace.SetData("LastActive", "")
if filereadable(filename)
for line in readfile(filename)
if line =~? "CS_WORKSPACE_BEGIN: "
call add(s:workspaces, line[20:])
elseif line =~? "CS_LAST_WORKSPACE: "
call s:modes.Workspace.SetData("LastActive", line[19:])
endif
endfor
endif
endfunction
function! ctrlspace#workspaces#SetActiveWorkspaceName(name, ...) abort
if a:0 > 0
let digest = a:1
else
let digest = s:modes.Workspace.Data.Active.Digest
end
call s:modes.Workspace.SetData("Active", { "Name": a:name, "Digest": digest, "Root": ctrlspace#roots#CurrentProjectRoot() })
call s:modes.Workspace.SetData("LastActive", a:name)
let filename = ctrlspace#util#WorkspaceFile()
let lines = []
if filereadable(filename)
for line in readfile(filename)
if !(line =~? "CS_LAST_WORKSPACE: ")
call add(lines, line)
endif
endfor
endif
if !empty(a:name)
call insert(lines, "CS_LAST_WORKSPACE: " . a:name)
endif
call writefile(lines, filename)
endfunction
function! ctrlspace#workspaces#ActiveWorkspace() abort
let aw = s:modes.Workspace.Data.Active
let aw.Status = 0
if !empty(aw.Name) && aw.Root ==# ctrlspace#roots#CurrentProjectRoot()
let aw.Status = 1
if aw.Digest !=# ctrlspace#workspaces#CreateDigest()
let aw.Status = 2
endif
endif
return aw
endfunction
function! ctrlspace#workspaces#NewWorkspace() abort
tabe
tabo!
call ctrlspace#buffers#DeleteHiddenNonameBuffers(1)
call ctrlspace#buffers#DeleteForeignBuffers(1)
call s:modes.Workspace.SetData("Active", { "Name": "", "Digest": "", "Root": "" })
endfunction
function! ctrlspace#workspaces#SelectedWorkspaceName() abort
return s:modes.Workspace.Enabled ? s:workspaces[ctrlspace#window#SelectedIndex()] : ""
endfunction
function! ctrlspace#workspaces#RenameWorkspace(name) abort
let newName = ctrlspace#ui#GetInput("Rename workspace '" . a:name . "' to: ", a:name)
if empty(newName)
return 0
endif
for existingName in s:workspaces
if newName ==# existingName
call ctrlspace#ui#Msg("Workspace '" . newName . "' already exists.")
return 0
endif
endfor
let filename = ctrlspace#util#WorkspaceFile()
let lines = []
let workspaceStartMarker = "CS_WORKSPACE_BEGIN: " . a:name
let workspaceEndMarker = "CS_WORKSPACE_END: " . a:name
let lastWorkspaceMarker = "CS_LAST_WORKSPACE: " . a:name
if filereadable(filename)
for line in readfile(filename)
if line ==# workspaceStartMarker
let line = "CS_WORKSPACE_BEGIN: " . newName
elseif line ==# workspaceEndMarker
let line = "CS_WORKSPACE_END: " . newName
elseif line ==# lastWorkspaceMarker
let line = "CS_LAST_WORKSPACE: " . newName
endif
call add(lines, line)
endfor
endif
call writefile(lines, filename)
if s:modes.Workspace.Data.Active.Name ==# a:name && s:modes.Workspace.Data.Active.Root ==# ctrlspace#roots#CurrentProjectRoot()
call ctrlspace#workspaces#SetActiveWorkspaceName(newName)
endif
call ctrlspace#workspaces#SetWorkspaceNames()
call ctrlspace#window#Kill(0, 0)
call ctrlspace#window#Toggle(1)
call ctrlspace#ui#DelayedMsg("Workspace '" . a:name . "' has been renamed to '" . newName . "'.")
return 1
endfunction
function! ctrlspace#workspaces#DeleteWorkspace(name) abort
if !ctrlspace#ui#Confirmed("Delete workspace '" . a:name . "'?")
return 0
endif
let filename = ctrlspace#util#WorkspaceFile()
let lines = []
let inWorkspace = 0
let workspaceStartMarker = "CS_WORKSPACE_BEGIN: " . a:name
let workspaceEndMarker = "CS_WORKSPACE_END: " . a:name
if filereadable(filename)
for oldLine in readfile(filename)
if oldLine ==# workspaceStartMarker
let inWorkspace = 1
endif
if !inWorkspace
call add(lines, oldLine)
endif
if oldLine ==# workspaceEndMarker
let inWorkspace = 0
endif
endfor
endif
call writefile(lines, filename)
if s:modes.Workspace.Data.Active.Name ==# a:name && s:modes.Workspace.Data.Active.Root ==# ctrlspace#roots#CurrentProjectRoot()
call ctrlspace#workspaces#SetActiveWorkspaceName(a:name, "")
endif
call ctrlspace#workspaces#SetWorkspaceNames()
if empty(s:workspaces)
call ctrlspace#window#Kill(0, 1)
else
call ctrlspace#window#Kill(0, 0)
call ctrlspace#window#Toggle(1)
endif
call ctrlspace#ui#DelayedMsg("Workspace '" . a:name . "' has been deleted.")
return 1
endfunction
" bang == 0) load
" bang == 1) append
function! ctrlspace#workspaces#LoadWorkspace(bang, name) abort
if !ctrlspace#roots#ProjectRootFound()
return 0
endif
call ctrlspace#util#HandleVimSettings("start")
let cwdSave = fnamemodify(".", ":p:h")
silent! exe "cd " . fnameescape(ctrlspace#roots#CurrentProjectRoot())
let filename = ctrlspace#util#WorkspaceFile()
if !filereadable(filename)
silent! exe "cd " . fnameescape(cwdSave)
return 0
endif
let oldLines = readfile(filename)
if empty(a:name)
let name = ""
for line in oldLines
if line =~? "CS_LAST_WORKSPACE: "
let name = line[19:]
break
endif
endfor
if empty(name)
silent! exe "cd " . fnameescape(cwdSave)
return 0
endif
else
let name = a:name
endif
let startMarker = "CS_WORKSPACE_BEGIN: " . name
let endMarker = "CS_WORKSPACE_END: " . name
let lines = []
let inWorkspace = 0
for ol in oldLines
if ol ==# startMarker
let inWorkspace = 1
elseif ol ==# endMarker
let inWorkspace = 0
elseif inWorkspace
let ol = substitute(ol, "let t:ctrlspace_label", "let t:CtrlSpaceLabel", "")
let ol = substitute(ol, "let t:ctrlspace_autotab", "let t:CtrlSpaceAutotab", "")
call add(lines, ol)
endif
endfor
if empty(lines)
call ctrlspace#ui#Msg("Workspace '" . name . "' not found in file '" . filename . "'.")
call ctrlspace#workspaces#SetWorkspaceNames()
silent! exe "cd " . fnameescape(cwdSave)
return 0
endif
call s:execWorkspaceCommands(a:bang, name, lines)
if !a:bang
let s:modes.Workspace.Data.Active.Digest = ctrlspace#workspaces#CreateDigest()
let msg = "Workspace '" . name . "' has been loaded."
else
let s:modes.Workspace.Data.Active.Digest = ""
let msg = "Workspace '" . name . "' has been appended."
endif
call ctrlspace#ui#Msg(msg)
call ctrlspace#ui#DelayedMsg(msg)
silent! exe "cd " . fnameescape(cwdSave)
call ctrlspace#util#HandleVimSettings("stop")
return 1
endfunction
function! s:execWorkspaceCommands(bang, name, lines) abort
let commands = []
if !a:bang
call ctrlspace#ui#Msg("Loading workspace '" . a:name . "'...")
call add(commands, "tabe")
call add(commands, "tabo!")
call add(commands, "call ctrlspace#buffers#DeleteHiddenNonameBuffers(1)")
call add(commands, "call ctrlspace#buffers#DeleteForeignBuffers(1)")
call ctrlspace#workspaces#SetActiveWorkspaceName(a:name)
else
let curTab = tabpagenr()
call ctrlspace#ui#Msg("Appending workspace '" . a:name . "'...")
call add(commands, "tabe")
endif
call writefile(a:lines, "CS_SESSION")
call add(commands, "source CS_SESSION")
call add(commands, "redraw!")
if a:bang
call add(commands, "normal! " . curTab . "gt")
endif
for c in commands
silent exe c
endfor
call ctrlspace#util#ChDir(fnamemodify(".", ":p:h"))
call delete("CS_SESSION")
endfunction
function! ctrlspace#workspaces#SaveWorkspace(name) abort
if !ctrlspace#roots#ProjectRootFound()
return 0
endif
call ctrlspace#util#HandleVimSettings("start")
let cwdSave = fnamemodify(".", ":p:h")
let root = ctrlspace#roots#CurrentProjectRoot()
silent! exe "cd " . fnameescape(root)
if empty(a:name)
if !empty(s:modes.Workspace.Data.Active.Name) && s:modes.Workspace.Data.Active.Root ==# root
let name = s:modes.Workspace.Data.Active.Name
else
silent! exe "cd " . fnameescape(cwdSave)
call ctrlspace#util#HandleVimSettings("stop")
call ctrlspace#ui#Msg("Nothing to save.")
return 0
endif
else
let name = a:name
endif
let filename = ctrlspace#util#WorkspaceFile()
let lastTab = tabpagenr("$")
let lines = []
let inWorkspace = 0
let startMarker = "CS_WORKSPACE_BEGIN: " . name
let endMarker = "CS_WORKSPACE_END: " . name
if filereadable(filename)
for oldLine in readfile(filename)
if oldLine ==# startMarker
let inWorkspace = 1
endif
if !inWorkspace
call add(lines, oldLine)
endif
if oldLine ==# endMarker
let inWorkspace = 0
endif
endfor
endif
call add(lines, startMarker)
let ssopSave = &ssop
set ssop=winsize,tabpages,buffers,sesdir
let tabData = []
for t in range(1, lastTab)
let data = {
\ "label": ctrlspace#util#Gettabvar(t, "CtrlSpaceLabel"),
\ "autotab": ctrlspace#util#GettabvarWithDefault(t, "CtrlSpaceAutotab", 0)
\ }
let ctrlspaceList = ctrlspace#api#Buffers(t)
let bufs = []
for [nr, bname] in items(ctrlspaceList)
let bufname = fnamemodify(bname, ":.")
if !filereadable(bufname)
continue
endif
call add(bufs, bufname)
endfor
let data.bufs = bufs
call add(tabData, data)
endfor
silent! exe "mksession! CS_SESSION"
if !filereadable("CS_SESSION")
silent! exe "cd " . fnameescape(cwdSave)
silent! exe "set ssop=" . ssopSave
call ctrlspace#util#HandleVimSettings("stop")
call ctrlspace#ui#Msg("Workspace '" . name . "' cannot be saved at this moment.")
return 0
endif
let tabIndex = 0
let config = ctrlspace#context#Configuration()
if filereadable(config.WorkspaceFile)
call add(lines, "cd " . fnameescape(root))
let useWorkspaceFile = 1
else
let useWorkspaceFile = 0
endif
for cmd in readfile("CS_SESSION")
if useWorkspaceFile && cmd ==# 'exe "cd " . escape(expand("<sfile>:p:h"), '' '')'
continue
elseif cmd =~# "^lcd "
continue
elseif cmd =~# "^badd\>"
let baddList = matchlist(cmd, '\v^badd \+\d+ (\f+)$')
if exists("baddList[1]") && filereadable(baddList[1])
call add(lines, cmd)
endif
elseif
\ ((cmd =~# '^edit \f\+') && (tabIndex == 0))
\ || ( (has('patch-8.1.149') || has('nvim-0.5')) && cmd ==# 'tabnext')
\ || (!(has('patch-8.1.149') || has('nvim-0.5')) && cmd =~# '^tabedit \f\+')
" NB: check patch-8.1.149 for backend vim mksession change
" NB: check nvim-0.5 for port of patch-8.1.149 from Vim to Nvim
let data = tabData[tabIndex]
if cmd =~# '^tabedit \f\+'
call add(lines, 'tabedit')
elseif cmd ==# 'tabnext'
call add(lines, cmd)
endif
for b in data.bufs
call add(lines, 'edit ' . fnameescape(b))
endfor
if cmd =~# '^tabedit \f\+'
" turn 'tabedit ...' into 'edit ...'
call add(lines, cmd[3:])
endif
if !empty(data.label)
call add(lines, "let t:CtrlSpaceLabel = '" . substitute(data.label, "'", "''","g") . "'")
endif
if !empty(data.autotab)
call add(lines, "let t:CtrlSpaceAutotab = " . data.autotab)
endif
let tabIndex += 1
else
call add(lines, cmd)
endif
endfor
call add(lines, endMarker)
call writefile(lines, filename)
call delete("CS_SESSION")
call ctrlspace#workspaces#SetActiveWorkspaceName(name, ctrlspace#workspaces#CreateDigest())
call ctrlspace#workspaces#SetWorkspaceNames()
silent! exe "cd " . fnameescape(cwdSave)
silent! exe "set ssop=" . ssopSave
call ctrlspace#util#HandleVimSettings("stop")
let msg = "Workspace '" . name . "' has been saved."
call ctrlspace#ui#Msg(msg)
call ctrlspace#ui#DelayedMsg(msg)
return 1
endfunction
function! ctrlspace#workspaces#CreateDigest() abort
let useNossl = exists("b:nosslSave") && b:nosslSave
if useNossl
set nossl
endif
let cpoSave = &cpo
set cpo&vim
let lines = []
for t in range(1, tabpagenr("$"))
let line = [t, ctrlspace#util#Gettabvar(t, "CtrlSpaceLabel")]
let bufs = []
let visibles = []
let tabBuffers = ctrlspace#api#Buffers(t)
for bname in values(tabBuffers)
let bufname = fnamemodify(bname, ":p")
if !filereadable(bufname)
continue
endif
call add(bufs, bufname)
endfor
for visibleBuf in tabpagebuflist(t)
if exists("tabBuffers[visibleBuf]")
let bufname = fnamemodify(tabBuffers[visibleBuf], ":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
let digest = join(lines, "&&&")
if useNossl
set ssl
endif
let &cpo = cpoSave
return digest
endfunction