@@ -109,9 +109,8 @@ function! xolox#session#save_state(commands) " {{{2
109109 endtry
110110endfunction
111111
112- " Integration between :mksession, :NERDTree and :Project. {{{3
113-
114- function ! xolox#session#save_special_windows (session)
112+ function ! xolox#session#save_special_windows (session) " {{{2
113+ " Integration between :mksession, :NERDTree and :Project.
115114 let tabpage = tabpagenr ()
116115 let window = winnr ()
117116 try
@@ -174,6 +173,15 @@ function! s:jump_to_window(session, tabpage, window)
174173 call add (a: session , a: window . ' wincmd w' )
175174endfunction
176175
176+ function ! s: nerdtree_persist ()
177+ " Remember current working directory and whether NERDTree is loaded.
178+ if exists (' b:NERDTreeRoot' )
179+ return ' NERDTree ' . fnameescape (b: NERDTreeRoot .path .str ()) . ' | only'
180+ else
181+ return ' cd ' . fnameescape (getcwd ())
182+ endif
183+ endfunction
184+
177185" Automatic commands to manage the default session. {{{1
178186
179187function ! xolox#session#auto_load () " {{{2
@@ -277,7 +285,7 @@ function! xolox#session#open_cmd(name, bang) abort " {{{2
277285 let msg = " %s: The %s session at %s doesn't exist!"
278286 call xolox#misc#msg#warn (msg, s: script , string (name), fnamemodify (path , ' :~' ))
279287 elseif a: bang == ' !' || ! s: session_is_locked (path , ' OpenSession' )
280- let oldcwd = getcwd ()
288+ let oldcwd = s: nerdtree_persist ()
281289 call xolox#session#close_cmd (a: bang , 1 )
282290 let s: oldcwd = oldcwd
283291 call s: lock_session (path )
@@ -375,9 +383,9 @@ function! xolox#session#close_cmd(bang, silent) abort " {{{2
375383 execute ' silent bdelete' bufnr
376384 endif
377385 endfor
378- " Restore working directory from before :OpenSession.
386+ " Restore working directory (and NERDTree?) from before :OpenSession.
379387 if exists (' s:oldcwd' )
380- execute ' cd ' fnameescape ( s: oldcwd)
388+ execute s: oldcwd
381389 unlet s: oldcwd
382390 endif
383391 unlet ! s: session_is_dirty
0 commit comments