Commits

ZyX_I committed 5e70bf7

@%aurum/commit: Added diff window autoclosing for automatically opened diffs
Fixes #52

Comments (0)

Files changed (3)

autoload/aurum/commit.vim

             \'commitautoopendiff':  {'default': 0, 'filter': 'bool'},
             \'commitinfowincmd':    {
             \   'default': 'largest_adjacent',
-            \   'checker': 'match /\v^%([jkhlwWtbpcvs]|'.
+            \   'checker': 'match /\v^%([jkhlwWtbpvs]|'.
             \                          'largest%(_%(vertical|'.
             \                                      'horizontal|'.
             \                                      'adjacent))?)$/'},
                 \ fnameescape(s:_r.os.path.join(a:bvar.repo.path, a:file))
     return s:_r.vimdiff.split([['file', a:bvar.repo, a:hex, a:file]], 0)
 endfunction
-"▶1 findwindow
+"▶1 findwindow :: () -> winexisted::Bool
 function s:F.winsize(wnr)
     let width=winwidth(a:wnr)
     return (width>80 ? 80 : width)*winheight(a:wnr)
     else
         execute 'silent' (max([winheight(0)-5, winheight(0)/2]))  'split'
     endif
+    return 0
 endfunction
 function s:F.findwindow()
     let wcommand=s:_f.getoption('commitinfowincmd')
             execute 'wincmd' wcommand
             if              exists('w:aurum_commit_mark')
                         \&& w:aurum_commit_mark is# commit_mark
-                call s:F.winsplit()
+                return s:F.winsplit()
             endif
+            return (stridx('sv', wcommand)==-1)
         finally
             call remove(wscope, 'aurum_commit_mark')
         endtry
         endfor
         if mwnr
             execute mwnr.'wincmd w'
+            return 1
         else
-            call s:F.winsplit()
+            return s:F.winsplit()
         endif
     else
         let maxsize=0
             endif
         endfor
         if mwcmd is 0
-            call s:F.winsplit()
+            return s:F.winsplit()
         else
             execute 'wincmd' mwcmd
+            return 1
         endif
     endif
 endfunction
         startinsert!
         "▶2 Open diff
         if s:_f.getoption('commitautoopendiff')
-            call bvar.findwindow()
-            call s:_r.mrun('silent edit', 'diff', bvar.repo, 0, 0,
-                        \                         keys(revstatus), {})
+            let winexisted=bvar.findwindow()
+            if winexisted
+                let prevbuf=bufnr('%')
+            else
+                let prevbuf=0
+            endif
+            let existed=s:_r.mrun('silent edit', 'diff', bvar.repo, 0, 0,
+                        \                                keys(revstatus), {})
+            if !existed
+                setlocal bufhidden=wipe
+            endif
+            let dbuf=bufnr('%')
+            let dtabpagenr=tabpagenr()
             silent! %foldopen!
             wincmd p
+            augroup AuCommitAutoCloseDiff
+                execute 'autocmd BufWipeOut <buffer> '.
+                            \':call s:F.closediffbuf('.dbuf.', '.
+                            \                          dtabpagenr.', '.
+                            \                          prevbuf.', '
+                            \                          existed.', '.
+                            \                          winexisted.')'
+            augroup END
         endif
         "▲2
         return 0
         return 1
     endif
 endfunction
+let s:_augroups+=['AuCommitAutoCloseDiff']
+"▶1 closediffbuf
+function s:F.closediffbuf(dbuf, dtabpagenr, prevbuf, existed, winexisted)
+    if bufexists(a:dbuf) && count(tabpagebuflist(a:dtabpagenr), a:dbuf)==1
+        let cmds=[]
+        let switchcmds=[]
+        if (a:prevbuf && bufexists(a:prevbuf)) || a:winexisted
+            let curtab=tabpagenr()
+            if curtab!=a:dtabpagenr
+                let cmds+=['tabnext '.a:dtabpagenr]
+                call insert(switchcmds, 'tabnext '.curtab)
+            endif
+            let cmds+=['execute bufwinnr('.a:dbuf.') "wincmd w"']
+            call insert(switchcmds, 'wincmd p')
+            if a:prevbuf && bufexists(a:prevbuf)
+                let cmds+=['buffer '.a:prevbuf]
+            else
+                let cmds+=['bnext']
+            endif
+        endif
+        if !a:existed
+            let cmds+=['if bufexists('.a:dbuf.')', 'bwipeout '.a:dbuf, 'endif']
+        endif
+        let cmds+=switchcmds
+        return feedkeys("\<C-\>\<C-n>:".join(cmds, '|')."\n\<C-l>", 'n')
+    endif
+endfunction
 "▶1 savemsg :: message, bvar → + g:
 function s:F.savemsg(message, bvar)
     if a:message!~#"[^[:blank:]\n]"

autoload/aurum/record.vim

 function s:F.commitfindwindow()
     let [lwnr, rwnr, swnr]=s:F.getwnrs()
     execute lwnr.'wincmd w'
+    return 1
 endfunction
 "▶1 write
 function s:F.write(bvar)
 commitautoopendiff                                *g:aurum_commitautoopendiff*
     Bool. Determines whether diff should be opened when commit window is 
     created. Uses |g:aurum_commitinfowincmd| to determine where diff should be 
-    opened.
+    opened. Opened diff will be automatically closed when commit buffer is 
+    closed.
     Default: 0.
 
 remembermsg                                              *g:aurum_remembermsg*