Skip to content

Commit

Permalink
Fix an error when opening undotree in some special buffers.
Browse files Browse the repository at this point in the history
  • Loading branch information
mbbill committed Aug 29, 2012
1 parent c83d55e commit 4fa9844
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 45 deletions.
10 changes: 5 additions & 5 deletions README.md
@@ -1,7 +1,10 @@
### [Link to Vim.org](http://www.vim.org/scripts/script.php?script_id=4177)

### Screenshot
![](http://files.myopera.com/mbbill/files/undotree.png)

### Description
Vim 7.0 added a new feature named **Undo branches**. Basically it's a kind of ability to go back to the text after any change, even if they were undone. Vim stores undo history in a tree which you can browse and manipulate through a bunch of commands. But that was not enough straightforward and a bit hard to use. You may use `:help new-undo-branches` or `:help undo-tree` to get more detailed help.
Vim 7.0 added a new feature named **Undo branches**. Basically it's a kind of ability to go back to the text after any change, even if they were undone. Vim stores undo history in a tree which you can browse and manipulate through a bunch of commands. But that was not enough straightforward and a bit hard to use. You may use `:help new-undo-branches` or `:help undo-tree` to get more detailed help.
Now this plug-in will free you from those commands and bring back the power of undo tree.

### Features
Expand Down Expand Up @@ -45,10 +48,7 @@ Add the following line to your *vimrc* in order to make them stored together.
1. But if you still want to do some customization, there is also a couple of options provided.
* [Here](https://github.com/mbbill/undotree/blob/master/plugin/undotree.vim#L15) is a list of these options.

### Screenshot
![](http://files.myopera.com/mbbill/files/undotree.png)

### For any Issues
### Post any issue and feature request here:
https://github.com/mbbill/undotree/issues

### Debug
Expand Down
83 changes: 43 additions & 40 deletions plugin/undotree.vim
Expand Up @@ -180,7 +180,6 @@ endfunction

function! s:panel.SetFocus()
let winnr = bufwinnr(self.bufname)
call s:log("SetFocus() winnr:".winnr." bufname:".self.bufname)
" already focused.
if winnr == winnr()
return
Expand All @@ -189,6 +188,7 @@ function! s:panel.SetFocus()
echoerr "Fatal: window does not exist!"
return
endif
call s:log("SetFocus() winnr:".winnr." bufname:".self.bufname)
" wincmd would cause cursor outside window.
call s:exec("norm! ".winnr."\<c-w>\<c-w>")
endfunction
Expand Down Expand Up @@ -301,8 +301,11 @@ function! s:undotree.ActionInTarget(cmd)
if !self.SetTargetFocus()
return
endif
call s:exec(a:cmd)
call self.Update()
" Target should be a normal buffer.
if (&bt == '') && (&modifiable == 1) && (mode() == 'n')
call s:exec(a:cmd)
call self.Update()
endif
" Update not always set current focus.
call self.SetFocus()
endfunction
Expand Down Expand Up @@ -417,9 +420,7 @@ function! s:undotree.Show()
endif

" store info for the first update.
let self.targetBufnr = bufnr('%')
let self.rawtree = undotree()
let self.seq_last = self.rawtree.seq_last
let targetBufnr = bufnr('%')

" Create undotree window.
let cmd = g:undotree_SplitLocation . " vertical" .
Expand All @@ -442,64 +443,66 @@ function! s:undotree.Show()
call self.BindKey()
call self.BindAu()

let self.seq_cur = -1
let self.seq_curhead = -1
let self.seq_newhead = -1
call self.ConvertInput(1)
call self.Render()
call self.Draw()
call self.MarkSeqs()
if self.opendiff
call t:diffpanel.Show()
call self.UpdateDiff()
endif
call s:exec("norm! ".bufwinnr(targetBufnr)."\<c-w>\<c-w>")
let self.targetBufnr = -1 "force update
call self.Update()
if !g:undotree_SetFocusWhenToggle
call self.SetTargetFocus()
endif
endfunction

" called outside undotree window
function! s:undotree.Update()
if &bt != '' "it's nor a normal buffer, could be help, quickfix, etc.
return
endif
if &modifiable == 0 "no modifiable buffer.
return
endif
if mode() != 'n' "not in normal mode, return.
if !self.IsVisible()
return
endif
if !self.IsVisible()
let bufname = bufname('%')
if bufname == self.bufname || bufname == t:diffpanel.bufname
return
endif
"update undotree,set focus
if self.targetBufnr == bufnr('%')
let newrawtree = undotree()
if self.rawtree == newrawtree
if (&bt != '') || (&modifiable == 0) || (mode() != 'n')
if self.targetBufnr == bufnr('%')
return
endif
let emptybuf = 1 "This is not a valid buffer.
else
let emptybuf = 0
"update undotree,set focus
if self.targetBufnr == bufnr('%')
let newrawtree = undotree()
if self.rawtree == newrawtree
return
endif

" same buffer, but seq changed.
if newrawtree.seq_last == self.seq_last
call s:log("undotree.Update() update seqs")
let self.rawtree = newrawtree
call self.ConvertInput(0) "only update seqs.
if (self.seq_cur == self.seq_cur_bak) &&
\(self.seq_curhead == self.seq_curhead_bak)&&
\(self.seq_newhead == self.seq_newhead_bak)&&
\(self.save_last == self.save_last_bak)
" same buffer, but seq changed.
if newrawtree.seq_last == self.seq_last
call s:log("undotree.Update() update seqs")
let self.rawtree = newrawtree
call self.ConvertInput(0) "only update seqs.
if (self.seq_cur == self.seq_cur_bak) &&
\(self.seq_curhead == self.seq_curhead_bak)&&
\(self.seq_newhead == self.seq_newhead_bak)&&
\(self.save_last == self.save_last_bak)
return
endif
call self.SetFocus()
call self.MarkSeqs()
call self.UpdateDiff()
return
endif
call self.SetFocus()
call self.MarkSeqs()
call self.UpdateDiff()
return
endif
endif
call s:log("undotree.Update() update whole tree")

let self.targetBufnr = bufnr('%')
let self.rawtree = undotree()
if emptybuf " Show an empty undo tree instead of do nothing.
let self.rawtree = {'seq_last':0,'entries':[],'time_cur':0,'save_last':0,'synced':1,'save_cur':0,'seq_cur':0}
else
let self.rawtree = undotree()
endif
let self.seq_last = self.rawtree.seq_last
let self.seq_cur = -1
let self.seq_curhead = -1
Expand Down

0 comments on commit 4fa9844

Please sign in to comment.