-
Notifications
You must be signed in to change notification settings - Fork 0
/
session.vim
117 lines (90 loc) · 2.8 KB
/
session.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
" directory to save session files
let s:root = get(g:, "session_root", $HOME . "/.vim/sessions")
" do not load when file was passed on the cli
let s:loadable = argc() == 0
function! s:openSession(file)
" do not source active session twice
if a:file == v:this_session
return s:error("This session is already in use.")
endif
" check whether session file exists
if !filereadable(a:file)
return s:error("Session file is not exist.")
endif
" save active session
if s:isActiveSession()
call s:saveSession()
endif
" remove all buffers
try
%bdelete
catch
redraw
return s:error("There are modified buffers.")
endtry
execute "source " . a:file
let g:SESSION = v:this_session
endfunction
function! s:saveSession(...)
let file = get(a:000, 0, "") == "" ? v:this_session : s:getSessionPath(a:000[0])
if file == ""
return s:error("You must have active session or specify one.")
endif
if !isdirectory(s:root)
call mkdir(s:root, "p")
endif
" save active session
if s:isActiveSession() && file != v:this_session
execute "mksession! " . v:this_session
endif
execute "mksession! " . file
let v:this_session = file
let g:SESSION = v:this_session
endfunction
function! s:closeSession()
if !s:isActiveSession()
return s:error("There is no active session.")
endif
" save active session
call s:saveSession()
let v:this_session = ""
let g:SESSION = v:this_session
endfunction
function! s:removeSession(...)
let file = len(a:000) == 0 ? v:this_session : s:getSessionPath(a:000[0])
if file == ""
return s:error("You must have active session or specify one.")
endif
" remove active session
if file == g:SESSION
let v:this_session = ""
let g:SESSION = v:this_session
endif
" check whether session file exists
if !filereadable(file)
return s:error("Session file is not exist.")
endif
call delete(file)
endfunction
function! s:getSessionPath(name)
return s:root . "/" . a:name
endfunction
function! s:isActiveSession()
return v:this_session != ""
endfunction
function! s:getSuggestions(a, b, c)
let sessions = substitute(glob(s:root . '/*'), '\\', '/', 'g')
return substitute(sessions, '\(^\|\n\)' . s:root . '/', '\1', 'g')
endfunction
function! s:error(message)
echo a:message
endfunction
" autocommands
autocmd VimEnter * nested execute s:loadable && get(g:, "SESSION", "") != "" && s:openSession(g:SESSION)
autocmd VimLeavePre * execute s:isActiveSession() && s:saveSession()
autocmd StdinReadPre * let s:loadable = 0
" commands
command! -nargs=1 -complete=custom,<SID>getSuggestions Session call <SID>openSession(<SID>getSessionPath(<f-args>))
command! -nargs=? -complete=custom,<SID>getSuggestions SessionSave call <SID>saveSession(<f-args>)
command! SessionClose call <SID>closeSession()
command! -nargs=? -complete=custom,<SID>getSuggestions SessionRemove call <SID>removeSession(<f-args>)