forked from rafi/vim-config
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsessions.vim
More file actions
90 lines (77 loc) · 2.4 KB
/
sessions.vim
File metadata and controls
90 lines (77 loc) · 2.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
" 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
"
" 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')
" 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>)
" Save session on quit if one is loaded
augroup plugin_sessions
autocmd!
" If session is loaded, write session file on quit
autocmd VimLeavePre *
\ if ! empty(v:this_session) && ! exists('g:SessionLoad')
\ | execute 'mksession! ' . fnameescape(v:this_session)
\ | endif
augroup END
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)
let file_name = empty(a:name) ? s:project_name() : a:name
let file_path = g:session_directory.'/'.file_name.'.vim'
if ! empty(v:this_session) && ! exists('g:SessionLoad')
\ | execute 'mksession! '.fnameescape(v:this_session)
\ | endif
if filereadable(file_path)
noautocmd silent! %bwipeout!
execute 'silent! source '.file_path
echomsg 'Loaded "'.file_path.'" session'
else
echohl ErrorMsg
echomsg 'The session "'.file_path.'" doesn''t exist'
echohl None
endif
endfunction
function! s:session_list(A, C, P)
return map(
\ split(glob(g:session_directory.'/*.vim'), '\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 :