tpope / vim-obsession

obsession.vim: continuously updated session files
http://www.vim.org/scripts/script.php?script_id=4472
1.76k stars 70 forks source link

Default directory for sessions & default session #6

Closed dhruvasagar closed 5 years ago

dhruvasagar commented 11 years ago

Hi Tim,

I like the simplicity of this plugin. But I wanted to see what you have to say about 2 things

  1. Default directory for saving session files.
    • The idea being if I specify one, sessions should be stored in it, otherwise it will stick with the current behavior. What is your opinion on the same ?
  2. Default session.
    • The idea being that we could enable (by setting an option) auto loading of this default session when launching vim. When vim exits, besides writing the session file, we can also update the default session which can just be a symlink to the current session file. Something like this would obviously need to be in a specific directory and hence I guess this feature could also be enabled if the 1st is accepted.

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.

mMontu commented 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
dhruvasagar commented 5 years ago

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.