Commits

ZyX_I  committed 52005f9

@%aurum/vimdiff: Made _r.vimdiff.split() also setup autocommands for disabling vimdiff when closing other buffer
Ref #66 (not marking as fixed before adding tests)

  • Participants
  • Parent commits 5c12462

Comments (0)

Files changed (2)

File autoload/aurum/vimdiff.vim

             \ 'nodrev': 'Unsure what revision should be diffed with',
         \}
 let s:lastfullid=0
-"▶1 diffrestore
-let s:diffsaveopts=['diff', 'foldcolumn', 'foldenable', 'foldmethod',
-            \       'foldlevel', 'scrollbind', 'cursorbind', 'wrap']
-call filter(s:diffsaveopts, 'exists("+".v:val)')
-function s:F.diffrestore(buf, onenter)
-    if !bufexists(a:buf) || !exists('t:auvimdiff_origbufvar') ||
-                \t:auvimdiff_origbufvar.bufnr!=a:buf
-        return
+"▶1 restore
+function s:F.restore(dbvar)
+    augroup AuVimDiff
+        autocmd! BufEnter,BufWipeOut <buffer>
+    augroup END
+    unlet t:auvimdiff_diffbufvar
+    unlet t:auvimdiff_origbufvar
+    unlet t:auvimdiff_prevbuffers
+    let hastickchange=0
+    if !has_key(a:dbvar, 'changedtick') ||
+                \b:changedtick!=a:dbvar.changedtick
+        let hastickchange=1
     endif
-    let dbvar=t:auvimdiff_origbufvar
-    if a:onenter
-        if has_key(dbvar, 'diffbuf')
-            if bufexists(dbvar.diffbuf)
-                return
-            else
-                call s:F.diffrestore(a:buf, 0)
-            endif
-        elseif !has_key(dbvar, 'diffsaved')
-            return
-        endif
-        augroup AuVimDiff
-            autocmd! BufEnter <buffer>
-        augroup END
-        unlet t:auvimdiff_origbufvar
-        if b:changedtick!=dbvar.diffsaved.changedtick
-            return
-        endif
-        let curpos=getpos('.')
-        call winrestview(dbvar.diffsaved.winview)
+    let curpos=getpos('.')
+    if !hastickchange && has_key(a:dbvar, 'winview')
+        call winrestview(a:dbvar.winview)
+    endif
+    if has_key(a:dbvar, 'filetype')
+        let &l:filetype=a:dbvar.filetype
+    endif
+    if !hastickchange && has_key(a:dbvar, 'closedfolds')
         normal! zR
-        for line in dbvar.diffsaved.closedfolds
+        for line in a:dbvar.closedfolds
             try
                 execute line.'foldclose'
             catch /\VVim(foldclose):E490:/
                 break
             endtry
         endfor
-        call setpos('.', curpos)
-    else
-        for option in s:diffsaveopts
-            call setbufvar(a:buf, '&'.option, dbvar.diffsaved[option])
-        endfor
-        if has_key(dbvar, 'diffbuf')
-            if exists('t:auvimdiff_diffbufvar')
-                unlet t:auvimdiff_diffbufvar
-            endif
-            unlet dbvar.diffbuf
-        endif
-        if bufnr('%')==a:buf
-            call s:F.diffrestore(a:buf, 1)
-        endif
-        if exists('t:auvimdiff_prevbuffers')
-            unlet t:auvimdiff_prevbuffers
+    endif
+    call setpos('.', curpos)
+endfunction
+"▶1 diffrestore
+let s:F.diffrestore={}
+"▶2 diffrestore.onotherenter
+function s:F.diffrestore.onotherenter(buf, dbvar, ddbvar)
+    if has_key(a:dbvar, 'bufnr')
+        if bufexists(a:dbvar.bufnr)
+            return
+        else
+            call s:F.diffrestore.onwipe(a:buf, a:dbvar, a:ddbvar)
         endif
     endif
+    return s:F.restore(a:ddbvar)
+endfunction
+"▶2 diffrestore.onenter
+function s:F.diffrestore.onenter(buf, dbvar, ddbvar)
+    if has_key(a:dbvar, 'diffbuf')
+        if bufexists(a:dbvar.diffbuf)
+            return
+        else
+            call s:F.diffrestore.onotherwipe(a:buf, a:dbvar, a:ddbvar)
+        endif
+    endif
+    return s:F.restore(a:ddbvar)
+endfunction
+"▶2 diffrestore.onotherwipe
+function s:F.diffrestore.onotherwipe(buf, dbvar, ddbvar)
+    for [option, value] in items(a:dbvar.diffsaved)
+        call setbufvar(a:buf, '&'.option, value)
+    endfor
+    unlet a:dbvar.diffbuf
+    unlet a:ddbvar.bufnr
+    if bufnr('%')==a:buf
+        call s:F.diffrestore.onenter(a:buf, a:dbvar, a:ddbvar)
+    endif
+endfunction
+"▶2 diffrestore.onwipe
+function s:F.diffrestore.onwipe(buf, dbvar, ddbvar)
+    let dbuf=a:ddbvar.bufnr
+    unlet a:dbvar.bufnr
+    unlet a:ddbvar.srcbuf
+    if a:ddbvar.existed
+        for [option, value] in items(a:ddbvar.diffsaved)
+            call setbufvar(a:ddbvar.bufnr, '&'.option, value)
+        endfor
+    else
+        if bufexists(dbuf)
+            call feedkeys("\<C-\>\<C-n>:bwipeout ".dbuf."\n", 'n')
+        endif
+        return
+    endif
+    if bufnr('%')==a:ddbvar.bufnr
+        call s:F.diffrestore.onotherenter(a:buf, a:dbvar, a:ddbvar)
+    endif
+endfunction
+"▶2 diffrestore.call
+function s:F.diffrestore.call(buf, func)
+    if !bufexists(a:buf) || !exists('t:auvimdiff_origbufvar') ||
+                \t:auvimdiff_origbufvar.bufnr!=a:buf ||
+                \!exists('t:auvimdiff_diffbufvar')
+        return
+    endif
+    let dbvar=t:auvimdiff_origbufvar
+    let ddbvar=t:auvimdiff_diffbufvar
+    return s:F.diffrestore[a:func](a:buf, dbvar, ddbvar)
 endfunction
 "▶1 findwindow
 function s:F.findwindow()
     endfor
     return r
 endfunction
+"▶1 save
+let s:diffsaveopts=['diff', 'foldcolumn', 'foldenable', 'foldmethod',
+            \       'foldlevel', 'scrollbind', 'cursorbind', 'wrap']
+function s:F.save(buf, dbvar)
+    let a:dbvar.diffsaved={}
+    for option in s:diffsaveopts
+        let a:dbvar.diffsaved[option]=getbufvar(a:buf, '&'.option)
+    endfor
+    let a:dbvar.winview=winsaveview()
+    let a:dbvar.cursor=getpos('.')[1:]
+    let a:dbvar.changedtick=b:changedtick
+    let closedfolds=[]
+    let a:dbvar.closedfolds=closedfolds
+    for line in range(1, line('$'))
+        if foldclosed(line)!=-1
+            execute line.'foldopen'
+            call insert(closedfolds, line)
+        endif
+    endfor
+endfunction
 "▶1 diffsplit
 function s:F.diffsplit(difftarget, usewin)
     if !empty(filter(range(1, winnr('$')), 'getwinvar(v:val, "&diff")'))
     endif
     let buf=bufnr('%')
     let t:auvimdiff_origbufvar={'bufnr': buf}
+    let t:auvimdiff_diffbufvar={}
     let existed=bufexists(a:difftarget)
     let dbvar=t:auvimdiff_origbufvar
-    let diffsaved={}
+    let ddbvar=t:auvimdiff_diffbufvar
     let filetype=&filetype
-    let dbvar.diffsaved=diffsaved
-    for option in s:diffsaveopts
-        let diffsaved[option]=getbufvar(buf, '&'.option)
-    endfor
-    let diffsaved.winview=winsaveview()
-    let cursor=getpos('.')[1:]
-    let diffsaved.changedtick=b:changedtick
-    let closedfolds=[]
-    let diffsaved.closedfolds=closedfolds
-    for line in range(1, line('$'))
-        if foldclosed(line)!=-1
-            execute line.'foldopen'
-            call insert(closedfolds, line)
-        endif
-    endfor
+    call s:F.save(buf, dbvar)
     call s:_f.mapgroup.map('AuVimDiff', buf)
+    let vertical=(stridx(&diffopt, 'vertical')!=-1)
+    let splitcmd=((vertical)?('v'):('')).'split'
     "▶2 `usewin' option support
     " Uses left/right or upper/lower window if it has similar dimensions
     if (a:usewin==-1 ? s:_f.getoption('vimdiffusewin') : a:usewin)
         if s:F.findwindow()
             let prevbuf=s:_r.prevbuf()
             execute 'silent edit' fnameescape(a:difftarget)
-            diffthis
         else
-            execute 'silent diffsplit' fnameescape(a:difftarget)
+            execute 'silent' splitcmd fnameescape(a:difftarget)
         endif
     else
-        execute 'silent diffsplit' fnameescape(a:difftarget)
+        diffthis
+        execute 'silent' splitcmd fnameescape(a:difftarget)
     endif
     "▲2
-    if bufwinnr(buf)!=-1
-        execute bufwinnr(buf).'wincmd w'
-        call cursor(cursor)
-        wincmd p
+    if existed
+        call s:F.save(dbuf, ddbvar)
     endif
+    diffthis
+    execute bufwinnr(buf).'wincmd w'
+    call cursor(dbvar.cursor)
+    wincmd p
     if &filetype isnot# filetype
+        let ddbvar.filetype=&filetype
         let &filetype=filetype
     endif
     let dbuf=bufnr('%')
     let t:auvimdiff_prevbuffers={dbuf : 0}
-    let t:auvimdiff_diffbufvar={'bufnr': dbuf}
-    let ddbvar=t:auvimdiff_diffbufvar
-    let ddbvar.srcbuf=dbuf
+    let t:auvimdiff_diffbufvar.bufnr=dbuf
+    let ddbvar.srcbuf=buf
     let ddbvar.existed=existed
     if exists('prevbuf')
         let ddbvar.prevbuf=prevbuf
         let t:auvimdiff_prevbuffers[dbuf]=prevbuf
     endif
     let dbvar.diffbuf=dbuf
+    let dbvar.changedtick=b:changedtick
     call s:_f.mapgroup.map('AuVimDiff', dbuf)
     augroup AuVimDiff
+        execute 'autocmd BufEnter   <buffer> '.
+                    \':call s:F.diffrestore.call('.buf.', "onotherenter")'
         execute 'autocmd BufWipeOut <buffer> '.
-                    \':call s:F.diffrestore('.buf.', 0)'
+                    \':call s:F.diffrestore.call('.buf.', "onotherwipe")'
         execute 'autocmd BufEnter   <buffer='.buf.'> '.
-                    \':call s:F.diffrestore('.buf.', 1)'
+                    \':call s:F.diffrestore.call('.buf.', "onenter")'
+        execute 'autocmd BufWipeOut <buffer='.buf.'> '.
+                    \':call s:F.diffrestore.call('.buf.', "onwipe")'
     augroup END
 endfunction
 let s:_augroups+=['AuVimDiff']
 "▶1 exit
-function s:F.exit()
+function s:F.exit(...)
     let buf=bufnr('%')
     let cmd="\<C-\>\<C-n>"
     "▶2 AuV full was used
             endif
         endif
         let cmd.=':call <SNR>'.s:_sid."_Eval(".
-                    \               "'s:F.diffrestore(".buf.", 0)')\n"
+                    \               "'s:F.diffrestore.call(".buf.", ".
+                    \                                     "''onotherwipe'')')\n"
     "▶2 Opened buffer
     else
         if has_key(ddbvar, 'prevbuf') && bufexists(ddbvar.prevbuf)
                         \"endif\n"
         endif
         let cmd.=':call <SNR>'.s:_sid."_Eval(".
-                    \           "'s:F.diffrestore(".ddbvar.srcbuf.", 0)')\n"
+                    \               "'s:F.diffrestore.call(".ddbvar.srcbuf.", ".
+                    \                                     "''onotherwipe'')')\n"
     endif
     "▲2
     return cmd

File test/maps-annotate.in

 :0/\v^\s*2
 D:WW
 :bwipeout!
-:bwipeout!
 :W{{{1 C
 :AuAnnotate repo hgtestrepo file hgtestrepo/crepo.zsh rev 2 | wincmd p
 :0/\v^\s*2