Skip to content

Commit

Permalink
Mark session dirty when buffer list changes? (issue #8)
Browse files Browse the repository at this point in the history
  • Loading branch information
xolox committed Sep 28, 2011
1 parent 199f6e8 commit 1ed64d2
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 9 deletions.
43 changes: 36 additions & 7 deletions autoload/xolox/session.vim
@@ -1,9 +1,9 @@
" Vim script
" Author: Peter Odding
" Last Change: September 26, 2011
" Last Change: September 28, 2011
" URL: http://peterodding.com/code/vim/session/

let g:xolox#session#version = '1.4.18'
let g:xolox#session#version = '1.4.19'

" Public API for session persistence. {{{1

Expand Down Expand Up @@ -259,9 +259,10 @@ function! xolox#session#auto_unlock() " {{{2
endfunction

function! xolox#session#auto_dirty_check() " {{{2
" This function is called each time a WinEnter event fires to detect when
" the current tab page is changed in some way. This enables the plug-in to
" not bother with the auto-save dialog when the session hasn't changed.
" This function is called each time a BufEnter event fires to detect when
" the current tab page (or the buffer list) is changed in some way. This
" enables the plug-in to not bother with the auto-save dialog when the
" session hasn't changed.
if v:this_session == ''
" Don't waste CPU time when no session is loaded.
return
Expand All @@ -272,6 +273,13 @@ function! xolox#session#auto_dirty_check() " {{{2
let last_tabpage = tabpagenr('$')
call filter(s:cached_layouts, 'v:key <= last_tabpage')
endif
" Check the buffer list.
let all_buffers = s:serialize_buffer_list()
if all_buffers != get(s:cached_layouts, 0, '')
let s:session_is_dirty = 1
endif
let s:cached_layouts[0] = all_buffers
" Check the layout of the current tab page.
let tabpagenr = tabpagenr()
let keys = ['tabpage:' . tabpagenr]
let buflist = tabpagebuflist()
Expand All @@ -281,13 +289,34 @@ function! xolox#session#auto_dirty_check() " {{{2
\ winwidth(winnr), winheight(winnr), buflist[winnr - 1]))
endfor
let layout = join(keys, "\n")
let cached_layout = get(s:cached_layouts, tabpagenr, '')
if cached_layout != '' && cached_layout != layout
if layout != get(s:cached_layouts, tabpagenr, '')
let s:session_is_dirty = 1
endif
let s:cached_layouts[tabpagenr] = layout
endfunction

function! s:serialize_buffer_list()
if &sessionoptions =~ '\<buffers\>'
return join(map(range(1, bufnr('$')), 's:serialize_buffer_state(v:val)'), "\n")
endif
return ''
endfunction

function! s:serialize_buffer_state(bufnr)
" TODO ssop =~ '\<blank\>' ?
let bufname = bufname(a:bufnr)
if bufname =~ '^NERD_tree_\d\+$'
" TODO I thought this would work, but somehow it doesn't?!
let root = getbufvar(a:bufnr, 'b:NERDTreeRoot')
if !empty(root)
let bufname = root.path.str() . '/' . bufname
endif
elseif bufname != ''
let bufname = fnamemodify(bufname, ':p')
endif
return a:bufnr . ':' . bufname
endfunction

function! s:prompt(msg, var) " {{{2
let value = eval(a:var)
if value == 'yes' || (type(value) != type('') && value)
Expand Down
4 changes: 2 additions & 2 deletions plugin/session.vim
@@ -1,6 +1,6 @@
" Vim script
" Author: Peter Odding
" Last Change: August 31, 2011
" Last Change: September 28, 2011
" URL: http://peterodding.com/code/vim/session/

" Support for automatic update using the GLVS plug-in.
Expand Down Expand Up @@ -62,7 +62,7 @@ augroup PluginSession
au VimEnter * nested call xolox#session#auto_load()
au VimLeavePre * call xolox#session#auto_save()
au VimLeavePre * call xolox#session#auto_unlock()
au TabEnter,WinEnter * call xolox#session#auto_dirty_check()
au BufEnter * call xolox#session#auto_dirty_check()
augroup END

" Define commands that enable users to manage multiple named sessions.
Expand Down

0 comments on commit 1ed64d2

Please sign in to comment.