Skip to content

Commit

Permalink
Option to automatically save sessions every few minutes (issue #26)
Browse files Browse the repository at this point in the history
Issue #26 on GitHub:

  autosave the current session every few minutes
  #26
  • Loading branch information
xolox committed May 5, 2013
1 parent 166f1f7 commit 96e547e
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 12 deletions.
8 changes: 7 additions & 1 deletion README.md
Expand Up @@ -6,7 +6,7 @@ To persist your current editing session you can execute the `:SaveSession` comma

![Screenshot of auto-save prompt](http://peterodding.com/code/vim/session/autosave.png)

When you start Vim without editing any files and the default session exists, you'll be prompted whether you want to restore the default session:
If you want, the plug-in can also automatically save your session every few minutes (see the `g:session_autosave_periodic` option). When you start Vim without editing any files and the default session exists, you'll be prompted whether you want to restore the default session:

![Screenshot of auto-open prompt](http://peterodding.com/code/vim/session/autoopen.png)

Expand Down Expand Up @@ -109,6 +109,12 @@ By default this option is set to `'prompt'`. This means that when you start Vim

By default this option is set to `'prompt'`. When you've opened a session and you quit Vim, the session plug-in will ask whether you want to save the changes to your session. Set this option to `'yes'` to always automatically save open sessions when you quit Vim. To completely disable automatic saving you can set this option to `'no'`.

### The `g:session_autosave_periodic` option

This option sets the interval in minutes for automatic, periodic saving of active sessions. The default is zero which disables the feature.

Note that when the plug-in automatically saves a session (because you enabled this feature) the plug-in will not prompt for your permission.

### The `g:session_verbose_messages` option

The session load/save prompts are quite verbose by default because they explain how to disable the prompts. If you find the additional explanation distracting you can lower the verbosity by setting this option to 0 (false) in your [vimrc script] [vimrc].
Expand Down
32 changes: 30 additions & 2 deletions autoload/xolox/session.vim
@@ -1,9 +1,9 @@
" Vim script
" Author: Peter Odding
" Last Change: May 4, 2013
" Last Change: May 5, 2013
" URL: http://peterodding.com/code/vim/session/

let g:xolox#session#version = '1.7.3'
let g:xolox#session#version = '1.8'

call xolox#misc#compat#check('session', 2)

Expand Down Expand Up @@ -246,6 +246,7 @@ endfunction
" Automatic commands to manage the default session. {{{1

function! xolox#session#auto_load() " {{{2
" Automatically load the default / last used session when Vim starts.
if g:session_autoload == 'no'
return
endif
Expand Down Expand Up @@ -313,7 +314,31 @@ function! xolox#session#auto_save() " {{{2
endif
endfunction

function! xolox#session#auto_save_periodic() " {{{2
" Automatically save the session every few minutes?
let interval = g:session_autosave_periodic * 60
let next_save = s:session_last_flushed + interval
if next_save < localtime()
call xolox#misc#msg#debug("session.vim %s: Skipping this beat of 'updatetime' (it's not our time yet).", g:xolox#session#version)
else
call xolox#misc#msg#debug("session.vim %s: This is our beat of 'updatetime'!", g:xolox#session#version)
let name = s:get_name('', 0)
if !empty(name)
call xolox#session#save_cmd(name, '')
endif
endif
endfunction

function! s:flush_session()
let s:session_last_flushed = localtime()
endfunction

if !exists('s:session_last_flushed')
call s:flush_session()
endif

function! xolox#session#auto_unlock() " {{{2
" Automatically unlock all sessions when Vim quits.
let i = 0
while i < len(s:lock_files)
let lock_file = s:lock_files[i]
Expand Down Expand Up @@ -359,6 +384,7 @@ function! xolox#session#open_cmd(name, bang) abort " {{{2
call s:lock_session(path)
execute 'source' fnameescape(path)
call s:last_session_persist(name)
call s:flush_session()
call xolox#misc#timer#stop("session.vim %s: Opened %s session in %s.", g:xolox#session#version, string(name), starttime)
call xolox#misc#msg#info("session.vim %s: Opened %s session from %s.", g:xolox#session#version, string(name), fnamemodify(path, ':~'))
endif
Expand Down Expand Up @@ -395,6 +421,7 @@ function! xolox#session#save_cmd(name, bang) abort " {{{2
call xolox#misc#msg#warn(msg, g:xolox#session#version, string(name), friendly_path)
else
call s:last_session_persist(name)
call s:flush_session()
call xolox#misc#timer#stop("session.vim %s: Saved %s session in %s.", g:xolox#session#version, string(name), starttime)
call xolox#misc#msg#info("session.vim %s: Saved %s session to %s.", g:xolox#session#version, string(name), friendly_path)
let v:this_session = path
Expand Down Expand Up @@ -458,6 +485,7 @@ function! xolox#session#close_cmd(bang, silent, save_allowed) abort " {{{2
execute s:oldcwd
unlet s:oldcwd
endif
call s:flush_session()
if v:this_session == ''
if !a:silent
let msg = "session.vim %s: Closed session."
Expand Down
28 changes: 20 additions & 8 deletions doc/session.txt
Expand Up @@ -20,12 +20,13 @@ Contents ~
4. The |g:session_extension| option
5. The |g:session_autoload| option
6. The |g:session_autosave| option
7. The |g:session_verbose_messages| option
8. The |g:session_default_to_last| option
9. The |g:session_persist_globals| option
10. The |g:session_restart_environment| option
11. The |g:session_command_aliases| option
12. The |g:loaded_session| option
7. The |g:session_autosave_periodic| option
8. The |g:session_verbose_messages| option
9. The |g:session_default_to_last| option
10. The |g:session_persist_globals| option
11. The |g:session_restart_environment| option
12. The |g:session_command_aliases| option
13. The |g:loaded_session| option
5. Compatibility with other plug-ins |session-compatibility-with-other-plug-ins|
6. Known issues |session-known-issues|
7. Contact |session-contact|
Expand All @@ -52,8 +53,10 @@ prompted whether you want to save the open session before quitting Vim:

Screenshot of auto-save prompt, see reference [1]

When you start Vim without editing any files and the default session exists,
you'll be prompted whether you want to restore the default session:
If you want, the plug-in can also automatically save your session every few
minutes (see the |g:session_autosave_periodic| option). When you start Vim
without editing any files and the default session exists, you'll be prompted
whether you want to restore the default session:

Screenshot of auto-open prompt, see reference [2]

Expand Down Expand Up @@ -236,6 +239,15 @@ changes to your session. Set this option to 'yes' to always automatically save
open sessions when you quit Vim. To completely disable automatic saving you
can set this option to 'no'.

-------------------------------------------------------------------------------
The *g:session_autosave_periodic* option

This option sets the interval in minutes for automatic, periodic saving of
active sessions. The default is zero which disables the feature.

Note that when the plug-in automatically saves a session (because you enabled
this feature) the plug-in will not prompt for your permission.

-------------------------------------------------------------------------------
The *g:session_verbose_messages* option

Expand Down
10 changes: 9 additions & 1 deletion plugin/session.vim
@@ -1,6 +1,6 @@
" Vim script
" Author: Peter Odding
" Last Change: May 2, 2013
" Last Change: May 5, 2013
" URL: http://peterodding.com/code/vim/session/

" Support for automatic update using the GLVS plug-in.
Expand Down Expand Up @@ -37,6 +37,13 @@ if !exists('g:session_autosave')
let g:session_autosave = 'prompt'
endif

" Periodically save the active session automatically? Set this to the
" auto-save interval in minutes. The value zero disables the feature
" (this is the default).
if !exists('g:session_autosave_periodic')
let g:session_autosave_periodic = 0
endif

" Define the verbosity of messages.
if !exists('g:session_verbose_messages')
let g:session_verbose_messages = 1
Expand Down Expand Up @@ -92,6 +99,7 @@ unlet s:directory
augroup PluginSession
autocmd!
au VimEnter * nested call xolox#session#auto_load()
au CursorHold,CursorHoldI * call xolox#session#auto_save_periodic()
au VimLeavePre * call xolox#session#auto_save()
au VimLeavePre * call xolox#session#auto_unlock()
augroup END
Expand Down

0 comments on commit 96e547e

Please sign in to comment.