forked from rafi/vim-config
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sessions.vim
127 lines (104 loc) · 3.27 KB
/
sessions.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
" Session Management
" ---
"
" Behaviors:
" - Save active session when quitting vim completely
"
" Commands:
" - SessionSave [name]: Create and activate new session
" - SessionLoad [name]: Clear buffers and load selected session
" - SessionClose: Save session and clear all buffers
" - SessionDetach: Stop persisting session, leave buffers open
"
" If [name] is empty, the current working-directory is used.
"
" Options:
" - g:session_directory defaults to DATA_PATH/session (see config/vimrc)
if exists('g:loaded_sessionsplugin')
finish
endif
let g:loaded_sessionsplugin = 1
" Options
" ---
let g:session_directory = get(g:, 'session_directory', $DATA_PATH . '/session')
" Commands
" ---
" Save and persist session
command! -nargs=? -complete=customlist,<SID>session_list SessionSave
\ call s:session_save(<q-args>)
" Load and persist session
command! -nargs=? -complete=customlist,<SID>session_list SessionLoad
\ call s:session_load(<q-args>)
" Close session, but leave buffers opened
command! SessionDetach call s:session_detach()
" Close session and all buffers
command! SessionClose call s:session_close()
" Save session on quit if one is loaded
augroup plugin_sessions
autocmd!
" If session is loaded, write session file on quit
autocmd VimLeavePre * call s:session_save_current()
" autocmd SessionLoadPost * ++once unsilent
" \ echomsg 'Loaded "' . fnamemodify(v:this_session, ':t:r') . '" session'
augroup END
" Private functions
" ---
function! s:session_save(name)
if ! isdirectory(g:session_directory)
call mkdir(g:session_directory, 'p')
endif
let file_name = empty(a:name) ? s:project_name() : a:name
let file_path = g:session_directory . '/' . file_name . '.vim'
execute 'mksession! ' . fnameescape(file_path)
let v:this_session = file_path
echohl MoreMsg
echo 'Session `' . file_name . '` is now persistent'
echohl None
endfunction
function! s:session_load(name)
call s:session_save_current()
let file_name = empty(a:name) ? s:project_name() : a:name
let file_path = g:session_directory . '/' . file_name . '.vim'
if filereadable(file_path)
call s:buffers_wipeout()
execute 'silent source ' . file_path
else
echohl ErrorMsg
echomsg 'The session "' . file_path . '" doesn''t exist'
echohl None
endif
endfunction
function! s:session_close()
if ! empty(v:this_session) && ! exists('g:SessionLoad')
call s:session_save_current()
call s:session_detach()
call s:buffers_wipeout()
endif
endfunction
function! s:session_save_current()
if ! empty(v:this_session) && ! exists('g:SessionLoad')
execute 'mksession! ' . fnameescape(v:this_session)
endif
endfunction
function! s:session_detach()
if ! empty(v:this_session) && ! exists('g:SessionLoad')
let v:this_session = ''
redrawtabline
redrawstatus
endif
endfunction
function! s:buffers_wipeout()
noautocmd silent! %bwipeout!
endfunction
function! s:session_list(A, C, P)
let glob_pattern = g:session_directory . '/' . fnameescape(a:A) . '*.vim'
return map(split(glob(glob_pattern), '\n'), "fnamemodify(v:val, ':t:r')")
endfunction
function! s:project_name()
let l:cwd = resolve(getcwd())
let l:cwd = substitute(l:cwd, '^' . $HOME . '/', '', '')
let l:cwd = fnamemodify(l:cwd, ':p:gs?/?_?')
let l:cwd = substitute(l:cwd, '^\.', '', '')
return l:cwd
endfunction
" vim: set ts=2 sw=2 tw=80 noet :