@@ -4,6 +4,20 @@ let s:noswapfile = (2 == exists(':noswapfile')) ? 'noswapfile' : ''
44let s: noau = ' silent noautocmd keepjumps'
55let s: cb_map = {} " callback map
66
7+ " Debug:
8+ " echo '' > dirvish.log ; tail -F dirvish.log
9+ " nvim +"let g:dirvish_dbg=1" -- b1 b2
10+ " :bnext
11+ " -
12+ if get (g: , ' dirvish_dbg' )
13+ func ! s: log (msg, ... ) abort
14+ call writefile ([a: msg ], expand (' ~/dirvish.log' ), ' as' )
15+ endf
16+ else
17+ func ! s: log (msg, ... ) abort
18+ endf
19+ endif
20+
721func ! s: msg_error (msg) abort
822 redraw | echohl ErrorMsg | echomsg ' dirvish:' a: msg | echohl None
923endf
@@ -179,27 +193,28 @@ endf
179193
180194func ! s: save_state (d ) abort
181195 " Remember previous ('original') buffer.
182- let a: d .prevbuf = s: buf_isvalid (bufnr (' %' )) || ! exists (' w:dirvish' )
183- \ ? 0 + bufnr (' %' ) : w: dirvish .prevbuf
184- if ! s: buf_isvalid (a: d .prevbuf)
196+ let p = s: buf_valid (bufnr (' %' )) || ! exists (' w:dirvish' ) ? 0 + bufnr (' %' ) : w: dirvish .prevbuf
197+ if ! s: buf_valid (p )
185198 " If reached via :edit/:buffer/etc. we cannot get the (former) altbuf.
186- let a: d .prevbuf = exists (' b:dirvish' ) && s: buf_isvalid (b: dirvish .prevbuf)
187- \ ? b: dirvish .prevbuf : bufnr (' #' )
199+ let p = exists (' b:dirvish' ) && s: buf_valid (b: dirvish .prevbuf) ? b: dirvish .prevbuf : bufnr (' #' )
188200 endif
189201
190202 " Remember alternate buffer.
191- let a: d .altbuf = s: buf_isvalid (bufnr (' #' )) || ! exists (' w:dirvish' )
192- \ ? 0 + bufnr (' #' ) : w: dirvish .altbuf
193- if exists (' b:dirvish' ) && (a: d .altbuf == a: d .prevbuf || ! s: buf_isvalid (a: d .altbuf))
194- let a: d .altbuf = b: dirvish .altbuf
203+ let a = (p != bufnr (' #' ) && s: buf_valid (bufnr (' #' ))) || ! exists (' w:dirvish' ) ? 0 + bufnr (' #' ) : w: dirvish .altbuf
204+ if ! s: buf_valid (a ) || a == p
205+ let a = exists (' b:dirvish' ) && s: buf_valid (b: dirvish .altbuf) ? b: dirvish .altbuf : -1
195206 endif
196207
197208 " Save window-local settings.
209+ let a: d .altbuf = a
210+ let a: d .prevbuf = p
198211 let w: dirvish = extend (get (w: , ' dirvish' , {}), a: d , ' force' )
199212 let [w: dirvish ._w_wrap, w: dirvish ._w_cul] = [&l: wrap , &l: cul ]
200213 if has (' conceal' ) && ! exists (' b:dirvish' )
201214 let [w: dirvish ._w_cocu, w: dirvish ._w_cole] = [&l: concealcursor , &l: conceallevel ]
202215 endif
216+
217+ call s: log (printf (' save_state: bufnr=%d altbuf=%d prevbuf=%d' , bufnr (' ' ), a: d .altbuf, a: d .prevbuf))
203218endf
204219
205220func ! s: win_init () abort
@@ -222,6 +237,7 @@ func! s:buf_close() abort
222237 endif
223238
224239 let [altbuf, prevbuf] = [get (d , ' altbuf' , 0 ), get (d , ' prevbuf' , 0 )]
240+ call s: log (printf (' buf_close: bufnr=%d altbuf=%d prevbuf=%d' , bufnr (' ' ), altbuf, prevbuf))
225241 let found_alt = s: try_visit (altbuf, 0 )
226242 if ! s: try_visit (prevbuf, 0 ) && ! found_alt
227243 \ && (1 == bufnr (' %' ) || (prevbuf != bufnr (' %' ) && altbuf != bufnr (' %' )))
@@ -278,12 +294,8 @@ func! s:open_selected(splitcmd, bg, line1, line2) abort
278294 endif
279295endf
280296
281- func ! s: is_valid_altbuf (bnr) abort
282- return a: bnr != bufnr (' %' ) && bufexists (a: bnr ) && empty (getbufvar (a: bnr , ' dirvish' ))
283- endf
284-
285297func ! s: set_altbuf (bnr) abort
286- if ! s: is_valid_altbuf (a: bnr ) | return | endif
298+ if ! s: buf_valid (a: bnr ) | return | endif
287299
288300 if has (' patch-7.4.605' ) | let @# = a: bnr | return | endif
289301
@@ -296,7 +308,7 @@ func! s:set_altbuf(bnr) abort
296308endf
297309
298310func ! s: try_visit (bnr, noau) abort
299- if s: is_valid_altbuf (a: bnr )
311+ if s: buf_valid (a: bnr )
300312 " If _previous_ buffer is _not_ loaded (because of 'nohidden'), we must
301313 " allow autocmds (else no syntax highlighting; #13).
302314 let noau = a: noau && bufloaded (a: bnr ) ? ' noau' : ' '
@@ -468,7 +480,7 @@ func! s:should_reload() abort
468480 return ! s: buf_modified () || (empty (getline (1 )) && 1 == line (' $' ))
469481endf
470482
471- func ! s: buf_isvalid (bnr) abort
483+ func ! s: buf_valid (bnr) abort
472484 return bufexists (a: bnr ) && ! isdirectory (s: sl (bufname (a: bnr )))
473485endf
474486
0 commit comments