From 5cf226229562e92dd4e2fce2fb6134c82669bdd9 Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Tue, 10 Jul 2018 14:50:06 +1200 Subject: Add session_lazy.vim plugin Mostly to properly tie down the way I want sessions to behave while I write my book. No documentation yet! --- vim/autoload/session_lazy.vim | 25 +++++++++++++++++++++++++ vim/plugin/session_lazy.vim | 23 +++++++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 vim/autoload/session_lazy.vim create mode 100644 vim/plugin/session_lazy.vim diff --git a/vim/autoload/session_lazy.vim b/vim/autoload/session_lazy.vim new file mode 100644 index 00000000..59caae37 --- /dev/null +++ b/vim/autoload/session_lazy.vim @@ -0,0 +1,25 @@ +" Choose the filename we'll use for these sessions +let g:session_lazy#active = 0 +if !exists('g:session_lazy#filename') + let g:session_lazy#filename = 'Session.vim' +endif + +" If we started with no arguments, there's no active session, and there's a +" session file sitting right there, read it +function! session_lazy#Thaw() + if !argc() + \ && v:this_session ==# '' + \ && filereadable(g:session_lazy#filename) + let g:session_lazy#active = 1 + execute 'source ' . g:session_lazy#filename + endif +endfunction + +" Before we quit, if we opened this with a session automatically, save it back +" again, into the same file +function! session_lazy#Freeze() + if g:session_lazy#active + \ && g:session_lazy#filename ==# fnamemodify(v:this_session, ':t') + execute 'mksession! ' . g:session_lazy#filename + endif +endfunction diff --git a/vim/plugin/session_lazy.vim b/vim/plugin/session_lazy.vim new file mode 100644 index 00000000..d40cb670 --- /dev/null +++ b/vim/plugin/session_lazy.vim @@ -0,0 +1,23 @@ +" +" session_lazy.vim: Automatically resume a session from the current directory, +" if no other filename arguments were passed to Vim. +" +" Author: Tom Ryder +" License: Same as Vim itself +" +if exists('g:loaded_session_lazy') || &compatible + finish +endif +if !has('autocmd') || v:version < 700 + finish +endif +let g:loaded_session_lazy = 1 + +" Set up +augroup session + autocmd! + autocmd VimEnter * + \ call session_lazy#Thaw() + autocmd VimLeavePre * + \ call session_lazy#Freeze() +augroup END -- cgit v1.2.3