changeset 29:1ce59d5aa5e9

Merged changes.
author Ludovic Chabant <ludovic@chabant.com>
date Tue, 20 Dec 2011 16:16:52 -0800
parents 0cdfdab43907 (diff) 8ad46beaa94e (current diff)
children 35d097b9513c 3a0f7bb6ea64
files
diffstat 1 files changed, 111 insertions(+), 28 deletions(-) [+]
line wrap: on
line diff
--- a/plugin/lawrencium.vim	Wed Dec 14 23:02:39 2011 -0800
+++ b/plugin/lawrencium.vim	Tue Dec 20 16:16:52 2011 -0800
@@ -314,7 +314,7 @@
         return s:ListRepoFiles(a:ArgLead, a:CmdLine, a:CursorPos)
 endfunction
 
-call s:AddMainCommand("-bang -complete=customlist,s:CompleteHg -nargs=* Hg :execute s:Hg(<bang>0, <f-args>)")
+call s:AddMainCommand("-bang -complete=customlist,s:CompleteHg -nargs=* Hg :call s:Hg(<bang>0, <f-args>)")
 
 " }}}
 
@@ -354,11 +354,11 @@
     " Add some handy mappings.
     nnoremap <buffer> <silent> <C-N> :call search('^[MARC\!\?I ]\s.', 'We')<cr>
     nnoremap <buffer> <silent> <C-P> :call search('^[MARC\!\?I ]\s.','Wbe')<cr>
-    nnoremap <buffer> <silent> <cr>  :execute <SID>HgStatus_FileEdit()<cr>
-    nnoremap <buffer> <silent> <C-D> :execute <SID>HgStatus_FileDiff(0)<cr>
-    nnoremap <buffer> <silent> <C-V> :execute <SID>HgStatus_FileDiff(1)<cr>
-    nnoremap <buffer> <silent> <C-A> :execute <SID>HgStatus_FileAdd()<cr>
-    nnoremap <buffer> <silent> <C-R> :execute <SID>HgStatus_Refresh()<cr>
+    nnoremap <buffer> <silent> <cr>  :call <SID>HgStatus_FileEdit()<cr>
+    nnoremap <buffer> <silent> <C-D> :call <SID>HgStatus_FileDiff(0)<cr>
+    nnoremap <buffer> <silent> <C-V> :call <SID>HgStatus_FileDiff(1)<cr>
+    nnoremap <buffer> <silent> <C-A> :call <SID>HgStatus_FileAdd()<cr>
+    nnoremap <buffer> <silent> <C-R> :call <SID>HgStatus_Refresh()<cr>
     nnoremap <buffer> <silent> q     :bdelete<cr>
 
     " Make sure the file is deleted with the buffer.
@@ -390,15 +390,19 @@
 function! s:HgStatus_FileEdit() abort
     " Get the path of the file the cursor is on.
     let l:filename = s:HgStatus_GetSelectedPath()
-    
-    " Go back to the previous window and open the file there, or open an
-    " existing buffer.
+   
+    " If the file is already open in a window, jump to that window.
+    " Otherwise, jump to the previous window and open it there.
+    for nr in range(1, winnr('$'))
+        let l:br = winbufnr(nr)
+        let l:bpath = fnamemodify(bufname(l:br), ':p')
+        if l:bpath ==# l:filename
+            execute nr . 'wincmd w'
+            return
+        endif
+    endfor
     wincmd p
-    if bufexists(l:filename)
-        execute 'buffer ' . l:filename
-    else
-        execute 'edit ' . l:filename
-    endif
+    execute 'edit ' . l:filename
 endfunction
 
 function! s:HgStatus_FileAdd() abort
@@ -418,12 +422,9 @@
 endfunction
 
 function! s:HgStatus_FileDiff(vertical) abort
-    " Get the path of the file the cursor is on.
-    let l:filename = s:HgStatus_GetSelectedPath()
-    
-    " Go back to the previous window and call HgDiff.
-    wincmd p
-    call s:HgDiff(l:filename, a:vertical)
+    " Open the file and run `Hgdiff` on it.
+    call s:HgStatus_FileEdit()
+    call s:HgDiff('%:p', a:vertical)
 endfunction
 
 function! s:HgStatus_GetSelectedPath() abort
@@ -441,7 +442,7 @@
     return matchstr(l:line, '\v^[MARC\!\?I ]')
 endfunction
 
-call s:AddMainCommand("Hgstatus :execute s:HgStatus()")
+call s:AddMainCommand("Hgstatus :call s:HgStatus()")
 
 " }}}
 
@@ -454,7 +455,16 @@
 
 " Hgedit {{{
 
-call s:AddMainCommand("-bang -nargs=? -complete=customlist,s:ListRepoFiles Hgedit :edit<bang> `=s:hg_repo().GetFullPath(<q-args>)`")
+function! s:HgEdit(bang, filename) abort
+    let l:full_path = s:hg_repo().GetFullPath(a:filename)
+    if a:bang
+        execute "edit! " . l:full_path
+    else
+        execute "edit " . l:full_path
+    endif
+endfunction
+
+call s:AddMainCommand("-bang -nargs=? -complete=customlist,s:ListRepoFiles Hgedit :call s:HgEdit(<bang>0, <f-args>)")
 
 " }}}
 
@@ -491,13 +501,13 @@
             execute 'edit ' . fnameescape(l:path)
         endif
         " Make it part of the diff group.
-        diffthis
+        call s:HgDiff_DiffThis()
     else
         let l:temp_file = tempname()
         call l:repo.RunCommand('cat', '-r', '"'.l:rev1.'"', '-o', l:temp_file, l:path)
         execute 'edit ' . fnameescape(l:temp_file)
         " Make it part of the diff group.
-        diffthis
+        call s:HgDiff_DiffThis()
         " Remember the repo it belongs to.
         let b:mercurial_dir = l:repo.root_dir
         " Make sure it's deleted when we move away from it.
@@ -522,8 +532,80 @@
     endif
 endfunction
 
-call s:AddMainCommand("-nargs=* -complete=customlist,s:ListRepoFiles Hgdiff :execute s:HgDiff('%:p', 0, <f-args>)")
-call s:AddMainCommand("-nargs=* -complete=customlist,s:ListRepoFiles Hgvdiff :execute s:HgDiff('%:p', 1, <f-args>)")
+function! s:HgDiff_DiffThis() abort
+    " Store some commands to run when we exit diff mode.
+    " It's needed because `diffoff` reverts those settings to their default
+    " values, instead of their previous ones.
+    if !&diff
+        call s:trace('Enabling diff mode on ' . bufname('%'))
+        let w:lawrencium_diffoff = {}
+        let w:lawrencium_diffoff['&diff'] = 0
+        let w:lawrencium_diffoff['&wrap'] = &l:wrap
+        let w:lawrencium_diffoff['&scrollopt'] = &l:scrollopt
+        let w:lawrencium_diffoff['&scrollbind'] = &l:scrollbind
+        let w:lawrencium_diffoff['&cursorbind'] = &l:cursorbind
+        let w:lawrencium_diffoff['&foldmethod'] = &l:foldmethod
+        let w:lawrencium_diffoff['&foldcolumn'] = &l:foldcolumn
+        diffthis
+    endif
+endfunction
+
+function! s:HgDiff_DiffOff(...) abort
+    " Get the window name (given as a paramter, or current window).
+    let l:nr = a:0 ? a:1 : winnr()
+
+    " Run the commands we saved in `HgDiff_DiffThis`, or just run `diffoff`.
+    let l:backup = getwinvar(l:nr, 'lawrencium_diffoff')
+    if type(l:backup) == type({}) && len(l:backup) > 0
+        call s:trace('Disabling diff mode on ' . l:nr)
+        for key in keys(l:backup)
+            call setwinvar(l:nr, key, l:backup[key])
+        endfor
+        call setwinvar(l:nr, 'lawrencium_diffoff', {})
+    else
+        call s:trace('Disabling diff mode on ' . l:nr . ' (but no true restore)')
+        diffoff
+    endif
+endfunction
+
+function! s:HgDiff_GetDiffWindows() abort
+    let l:result = []
+    for nr in range(1, winnr('$'))
+        if getwinvar(nr, '&diff')
+            call add(l:result, nr)
+        endif
+    endfor
+    return l:result
+endfunction
+
+function! s:HgDiff_CleanUp() abort
+    " If we're not leaving a diff window, do nothing.
+    if !&diff
+        return
+    endif
+
+    " If there will be only one diff window left (plus the one we're leaving),
+    " turn off diff everywhere.
+    let l:nrs = s:HgDiff_GetDiffWindows()
+    if len(l:nrs) <= 2
+        call s:trace('Disabling diff mode in ' . len(l:nrs) . ' windows.')
+        for nr in l:nrs
+            if getwinvar(nr, '&diff')
+                call s:HgDiff_DiffOff(nr)
+            endif
+        endfor
+    else
+        call s:trace('Still ' . len(l:nrs) . ' diff windows open.')
+    endif
+endfunction
+
+augroup lawrencium_diff
+  autocmd!
+  autocmd BufWinLeave * call s:HgDiff_CleanUp()
+augroup end
+
+call s:AddMainCommand("-nargs=* -complete=customlist,s:ListRepoFiles Hgdiff :call s:HgDiff('%:p', 0, <f-args>)")
+call s:AddMainCommand("-nargs=* -complete=customlist,s:ListRepoFiles Hgvdiff :call s:HgDiff('%:p', 1, <f-args>)")
 
 " }}}
 
@@ -539,6 +621,7 @@
     execute l:split . ' ' . l:commit_path
     call append(0, ['', ''])
     call append(2, split(s:HgCommit_GenerateMessage(l:repo), '\n'))
+    call cursor(1, 1)
 
     " Setup the auto-command that will actually commit on write/exit,
     " and make the buffer delete itself on exit.
@@ -615,8 +698,8 @@
     endif
 endfunction
 
-call s:AddMainCommand("-bang Hgcommit :execute s:HgCommit(<bang>0, 0)")
-call s:AddMainCommand("-bang Hgvcommit :execute s:HgCommit(<bang>0, 1)")
+call s:AddMainCommand("-bang Hgcommit :call s:HgCommit(<bang>0, 0)")
+call s:AddMainCommand("-bang Hgvcommit :call s:HgCommit(<bang>0, 1)")
 
 " }}}