Skip to content

Commit 96e547e

Browse files
committed
Option to automatically save sessions every few minutes (issue #26)
Issue #26 on GitHub: autosave the current session every few minutes #26
1 parent 166f1f7 commit 96e547e

File tree

4 files changed

+66
-12
lines changed

4 files changed

+66
-12
lines changed

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ To persist your current editing session you can execute the `:SaveSession` comma
66

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

9-
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:
9+
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:
1010

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

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

110110
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'`.
111111

112+
### The `g:session_autosave_periodic` option
113+
114+
This option sets the interval in minutes for automatic, periodic saving of active sessions. The default is zero which disables the feature.
115+
116+
Note that when the plug-in automatically saves a session (because you enabled this feature) the plug-in will not prompt for your permission.
117+
112118
### The `g:session_verbose_messages` option
113119

114120
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].

autoload/xolox/session.vim

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
" Vim script
22
" Author: Peter Odding
3-
" Last Change: May 4, 2013
3+
" Last Change: May 5, 2013
44
" URL: http://peterodding.com/code/vim/session/
55

6-
let g:xolox#session#version = '1.7.3'
6+
let g:xolox#session#version = '1.8'
77

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

@@ -246,6 +246,7 @@ endfunction
246246
" Automatic commands to manage the default session. {{{1
247247

248248
function! xolox#session#auto_load() " {{{2
249+
" Automatically load the default / last used session when Vim starts.
249250
if g:session_autoload == 'no'
250251
return
251252
endif
@@ -313,7 +314,31 @@ function! xolox#session#auto_save() " {{{2
313314
endif
314315
endfunction
315316

317+
function! xolox#session#auto_save_periodic() " {{{2
318+
" Automatically save the session every few minutes?
319+
let interval = g:session_autosave_periodic * 60
320+
let next_save = s:session_last_flushed + interval
321+
if next_save < localtime()
322+
call xolox#misc#msg#debug("session.vim %s: Skipping this beat of 'updatetime' (it's not our time yet).", g:xolox#session#version)
323+
else
324+
call xolox#misc#msg#debug("session.vim %s: This is our beat of 'updatetime'!", g:xolox#session#version)
325+
let name = s:get_name('', 0)
326+
if !empty(name)
327+
call xolox#session#save_cmd(name, '')
328+
endif
329+
endif
330+
endfunction
331+
332+
function! s:flush_session()
333+
let s:session_last_flushed = localtime()
334+
endfunction
335+
336+
if !exists('s:session_last_flushed')
337+
call s:flush_session()
338+
endif
339+
316340
function! xolox#session#auto_unlock() " {{{2
341+
" Automatically unlock all sessions when Vim quits.
317342
let i = 0
318343
while i < len(s:lock_files)
319344
let lock_file = s:lock_files[i]
@@ -359,6 +384,7 @@ function! xolox#session#open_cmd(name, bang) abort " {{{2
359384
call s:lock_session(path)
360385
execute 'source' fnameescape(path)
361386
call s:last_session_persist(name)
387+
call s:flush_session()
362388
call xolox#misc#timer#stop("session.vim %s: Opened %s session in %s.", g:xolox#session#version, string(name), starttime)
363389
call xolox#misc#msg#info("session.vim %s: Opened %s session from %s.", g:xolox#session#version, string(name), fnamemodify(path, ':~'))
364390
endif
@@ -395,6 +421,7 @@ function! xolox#session#save_cmd(name, bang) abort " {{{2
395421
call xolox#misc#msg#warn(msg, g:xolox#session#version, string(name), friendly_path)
396422
else
397423
call s:last_session_persist(name)
424+
call s:flush_session()
398425
call xolox#misc#timer#stop("session.vim %s: Saved %s session in %s.", g:xolox#session#version, string(name), starttime)
399426
call xolox#misc#msg#info("session.vim %s: Saved %s session to %s.", g:xolox#session#version, string(name), friendly_path)
400427
let v:this_session = path
@@ -458,6 +485,7 @@ function! xolox#session#close_cmd(bang, silent, save_allowed) abort " {{{2
458485
execute s:oldcwd
459486
unlet s:oldcwd
460487
endif
488+
call s:flush_session()
461489
if v:this_session == ''
462490
if !a:silent
463491
let msg = "session.vim %s: Closed session."

doc/session.txt

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,13 @@ Contents ~
2020
4. The |g:session_extension| option
2121
5. The |g:session_autoload| option
2222
6. The |g:session_autosave| option
23-
7. The |g:session_verbose_messages| option
24-
8. The |g:session_default_to_last| option
25-
9. The |g:session_persist_globals| option
26-
10. The |g:session_restart_environment| option
27-
11. The |g:session_command_aliases| option
28-
12. The |g:loaded_session| option
23+
7. The |g:session_autosave_periodic| option
24+
8. The |g:session_verbose_messages| option
25+
9. The |g:session_default_to_last| option
26+
10. The |g:session_persist_globals| option
27+
11. The |g:session_restart_environment| option
28+
12. The |g:session_command_aliases| option
29+
13. The |g:loaded_session| option
2930
5. Compatibility with other plug-ins |session-compatibility-with-other-plug-ins|
3031
6. Known issues |session-known-issues|
3132
7. Contact |session-contact|
@@ -52,8 +53,10 @@ prompted whether you want to save the open session before quitting Vim:
5253

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

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

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

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

242+
-------------------------------------------------------------------------------
243+
The *g:session_autosave_periodic* option
244+
245+
This option sets the interval in minutes for automatic, periodic saving of
246+
active sessions. The default is zero which disables the feature.
247+
248+
Note that when the plug-in automatically saves a session (because you enabled
249+
this feature) the plug-in will not prompt for your permission.
250+
239251
-------------------------------------------------------------------------------
240252
The *g:session_verbose_messages* option
241253

plugin/session.vim

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
" Vim script
22
" Author: Peter Odding
3-
" Last Change: May 2, 2013
3+
" Last Change: May 5, 2013
44
" URL: http://peterodding.com/code/vim/session/
55

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

40+
" Periodically save the active session automatically? Set this to the
41+
" auto-save interval in minutes. The value zero disables the feature
42+
" (this is the default).
43+
if !exists('g:session_autosave_periodic')
44+
let g:session_autosave_periodic = 0
45+
endif
46+
4047
" Define the verbosity of messages.
4148
if !exists('g:session_verbose_messages')
4249
let g:session_verbose_messages = 1
@@ -92,6 +99,7 @@ unlet s:directory
9299
augroup PluginSession
93100
autocmd!
94101
au VimEnter * nested call xolox#session#auto_load()
102+
au CursorHold,CursorHoldI * call xolox#session#auto_save_periodic()
95103
au VimLeavePre * call xolox#session#auto_save()
96104
au VimLeavePre * call xolox#session#auto_unlock()
97105
augroup END

0 commit comments

Comments
 (0)