From ec4fcfb751b8453c57ed00ccc1ddd4a53e48ecbe Mon Sep 17 00:00:00 2001 From: Evan Gray Date: Sun, 18 Aug 2024 13:22:35 -0500 Subject: [PATCH] Add variable to autoload sessions Closes #3 Adds variable to autoload session on VimEnter and DirChanged Remove the ability to specify a session --- autoload/branch_sessions.vim | 30 +++++++++++------------------- doc/branch_sessions.txt | 24 +++++++++++------------- plugin/branch_sessions.vim | 19 +++++++++++-------- 3 files changed, 33 insertions(+), 40 deletions(-) diff --git a/autoload/branch_sessions.vim b/autoload/branch_sessions.vim index 1fb54be..af60a9a 100644 --- a/autoload/branch_sessions.vim +++ b/autoload/branch_sessions.vim @@ -1,29 +1,32 @@ -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:/ @@ -31,8 +34,8 @@ function! branch_sessions#Mksession(bang, ...) abort 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 @@ -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() diff --git a/doc/branch_sessions.txt b/doc/branch_sessions.txt index 8abf74e..8dddd04 100644 --- a/doc/branch_sessions.txt +++ b/doc/branch_sessions.txt @@ -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. diff --git a/plugin/branch_sessions.vim b/plugin/branch_sessions.vim index f1e31a5..a11ecdc 100644 --- a/plugin/branch_sessions.vim +++ b/plugin/branch_sessions.vim @@ -1,8 +1,11 @@ -command! -nargs=? -complete=custom,branch_sessions#Completion SessionLoad - \ call branch_sessions#Load() -command! -bang -nargs=? -complete=custom,branch_sessions#Completion Mksession - \ call branch_sessions#Mksession(0, ) -command! -nargs=? -complete=custom,branch_sessions#Completion SessionStart - \ call branch_sessions#Start() -command! -nargs=? -complete=custom,branch_sessions#Completion SessionDelete - \ call branch_sessions#Delete() +command! SessionLoad call branch_sessions#Load() +command! -bang Mksession call branch_sessions#Mksession(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