Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 11 additions & 19 deletions autoload/branch_sessions.vim
Original file line number Diff line number Diff line change
@@ -1,38 +1,41 @@
function! branch_sessions#Start(...) abort
function! branch_sessions#Start() abort
if !exists(':Obsession')
return s:Error('vim-obsession not installed.')
endif
let l:dir = s:Directory()
let l:file = a:0 ? a:1 . '.vim' : s:File()
let l:file = s:File()
execute 'Obsession ' . l:dir . l:file
endfunction

function! branch_sessions#Load(...) abort
let l:dir = s:Directory()
let l:file = a:0 ? a:1 . '.vim' : s:File()
let l:file = s:File()
if !filereadable(l:dir . l:file)
return s:Error('No session found for ' . l:file)
if !a:0 || (a:0 && !a:1)
call s:Error('No session found for ' . l:file)
endif
return
endif
echomsg 'Loading session ' . l:file
execute 'source ' . l:dir . l:file
endfunction

function! branch_sessions#Mksession(bang, ...) abort
function! branch_sessions#Mksession(bang) abort
let l:command = 'mksession'
if a:bang || get(g:, 'branch_sessions_mksession_bang')
let l:command .= '!'
endif
let l:dir = s:Directory()
let l:file = a:0 ? a:1 . '.vim' : s:File()
let l:file = s:File()
try
execute l:command . ' ' . l:dir . l:file
catch /^Vim\%((\a\+)\)\=:E189:/
call s:Error(l:file . " exists. Add '!' to overwrite.")
endtry
endfunction

function! branch_sessions#Delete(...) abort
let l:session = a:0 ? a:1 : s:Branch()
function! branch_sessions#Delete() abort
let l:session = s:Branch()
if l:session == s:Branch() && exists(':Obsession')
Obsession!
return
Expand All @@ -46,17 +49,6 @@ function! branch_sessions#Delete(...) abort
endif
endfunction

function! branch_sessions#Completion(...) abort
let l:dir = s:Directory()
if !isdirectory(l:dir)
return ''
endif
let l:list = map(
\ glob(l:dir . '**/*.vim', 0, 1), {_, v -> substitute(v, '.vim$', '', '') }
\ )
return join(map(l:list, {_, v -> substitute(v, l:dir, '', '')}), "\n")
endfunction

function! s:File() abort
let l:current_dir = fnamemodify(getcwd(), ':t')
let l:branch = s:Branch()
Expand Down
24 changes: 11 additions & 13 deletions doc/branch_sessions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,31 @@

COMMANDS *branch-sessions-commands*

:Mksession[!] [{session}] Like |:mksession|, but create a session file based
:Mksession[!] Like |:mksession|, but create a session file based
on current directory. If the current directory is a
git repository, make a directory named after the
current directory and a session file named after
the current git branch. If {session} is specified,
that name will be used instead. Session files are
kept in "$HOME/.cache/vim_sessions", but the
directory can be changed with
|g:branch_sessions_directory|. To mimic the
behavior of |:mksession|, if the session file
already exists, it will result in an error, but you
can call with "!" to overwrite the current session
file. This can be tedious, so you can set
the current git branch. Session files are kept in
"$HOME/.cache/vim_sessions", but the directory can
be changed with |g:branch_sessions_directory|. To
mimic the behavior of |:mksession|, if the session
file already exists, it will result in an error,
but you can call with "!" to overwrite the current
session file. This can be tedious, so you can set
|g:branch_sessions_mksession_bang| to 1 to always
call |:mksession| with a "!".

*:SessionStart*
:SessionStart [{session}] Like |:Mksession|, but continue tracking any
:SessionStart Like |:Mksession|, but continue tracking any
changes. Must have tpope's vim-obsession installed.

*:SessionLoad*
:SessionLoad [{session}] Load the session file for the current directory. If
:SessionLoad Load the session file for the current directory. If
the current directory is a git repository, load the
session file for the current directory and branch.

*:SessionDelete*
:SessionDelete [{session}] Delete the session file for the current directory.
:SessionDelete Delete the session file for the current directory.
If the current directory is a git repository, delete
the session file for the current directory and
branch.
Expand Down
19 changes: 11 additions & 8 deletions plugin/branch_sessions.vim
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
command! -nargs=? -complete=custom,branch_sessions#Completion SessionLoad
\ call branch_sessions#Load(<f-args>)
command! -bang -nargs=? -complete=custom,branch_sessions#Completion Mksession
\ call branch_sessions#Mksession(<bang>0, <f-args>)
command! -nargs=? -complete=custom,branch_sessions#Completion SessionStart
\ call branch_sessions#Start(<f-args>)
command! -nargs=? -complete=custom,branch_sessions#Completion SessionDelete
\ call branch_sessions#Delete(<f-args>)
command! SessionLoad call branch_sessions#Load()
command! -bang Mksession call branch_sessions#Mksession(<bang>0)
command! SessionStart call branch_sessions#Start()
command! SessionDelete call branch_sessions#Delete()

if get(g:, 'branch_sessions_autoload_session')
augroup branch_sessions
autocmd!
autocmd VimEnter,DirChanged * call branch_sessions#Load(1)
augroup END
endif