Closed dhruvasagar closed 5 years ago
Given there are no news about this, I'm pasting a code adapted from vim-session in case anyone else is looking for this functionality:
command! -bar -bang -nargs=? -complete=customlist,s:obsession_open_complete
\ ObsessionOpen call s:obsession_open(<q-args>, <q-bang>)
function! s:obsession_open(name, bang)
let l:name = a:name
if empty(a:name)
let l:name = 'default'
endif
let l:session = l:name.'.vim'
if !filereadable(g:session_directory.l:session)
echohl warningmsg
echom "Session ".session." not found!"
echohl None
return
endif
if !filereadable(g:session_directory.l:session.'.lock') || a:bang == '!'
if writefile([], g:session_directory.l:session.'.lock') != 0
echom "Failed to write session lock"
return
endif
augroup ObsessionDefault
au!
exe 'autocmd VimLeavePre * call delete('''.g:session_directory.l:session.'.lock'')'
augroup END
exe 'source '.g:session_directory.l:session
else
echohl warningmsg
echom "Session ".l:name." is locked. Use ':ObsessionOpen! ".l:name."' to override"
echohl None
endif
endfunction
function! s:obsession_open_complete(arg, line, pos)
let l:files = globpath(g:session_directory, a:arg.'*.vim', 0, 1) " retrieve session files
return map(l:files, function('s:obsession_open_complete_helper')) " leave only base name
endfunction
function! s:obsession_open_complete_helper(index, value)
return fnamemodify(a:value, ':t:r')
endfunction
function! s:session_is_empty()
" Check that the user has started Vim without editing any files
let l:current_buffer_is_empty = (&modified == 0 && getline(1, '$') == [''])
let l:buffer_list_is_empty = (bufname('%') == '' && empty(filter(range(1, bufnr('$')),
\ 'buflisted(v:val) && v:val != ' . bufnr(''))))
let l:buffer_list_is_persistent = (stridx(&viminfo, '%') >= 0)
return (l:current_buffer_is_empty) && (l:buffer_list_is_empty || l:buffer_list_is_persistent)
endfunction
if !v:vim_did_enter
let g:session_directory = $HOME . '/.vim_sessions/'
augroup ObsessionInit
au!
autocmd VimEnter * nested
\ if s:session_is_empty() |
\ ObsessionOpen |
\ endif |
\ au! ObsessionInit
augroup END
endif
I created dhruvasagar/vim-prosession as an extension to vim-obsession, you can try that if you'd like. I don't believe this issue is particularly relevant anymore unless there is interest in trying to bring this feature within vim-obsession.
Hi Tim,
I like the simplicity of this plugin. But I wanted to see what you have to say about 2 things
I was originally thinking of just adding this feature but considered taking your opinion on the same. I would be glad to contribute if you think this can be good.