Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Version 2.2: Stable version.

  • Loading branch information...
commit 2d6e95c73c6e7f92078ae8c518cbe8b816369af4 1 parent d1f0391
@mbbill mbbill authored committed
Showing with 69 additions and 25 deletions.
  1. +66 −24 plugin/undotree.vim
  2. +3 −1 syntax/undotree.vim
View
90 plugin/undotree.vim
@@ -28,13 +28,18 @@ endif
" Golbal buf counter.
let s:undobufNr = 0
+" Help text
+let s:helpmore = ['" Undotree quick help',
+ \'" -------------------']
+let s:helpless = ['" Press ? for help.']
+
" Keymap
let s:keymap = []
" action, key, help.
-let s:keymap += [['Enter','<cr>','Revert to current node']]
-let s:keymap += [['Enter','<2-LeftMouse>','Revert to current node']]
-let s:keymap += [['Godown','J','Revert to previous node']]
-let s:keymap += [['Goup','K','Revert to next node']]
+let s:keymap += [['Enter','<cr>','Revert to current state']]
+let s:keymap += [['Enter','<2-LeftMouse>','Revert to current state']]
+let s:keymap += [['Godown','J','Revert to previous state']]
+let s:keymap += [['Goup','K','Revert to next state']]
let s:keymap += [['Undo','u','Undo']]
let s:keymap += [['Redo','<c-r>','Redo']]
let s:keymap += [['Help','?','Toggle quick help']]
@@ -60,12 +65,30 @@ endfunction
" Exec without autocommands
function! s:exec(cmd)
+ call s:log("s:exec() ".a:cmd)
let ei_bak= &eventignore
set eventignore=all
silent exe a:cmd
let &eventignore = ei_bak
endfunction
+let s:debug = 0
+let s:debugfile = '~/undotree_debug.log'
+function! s:debugon()
+ let s:debug = 1
+ exec 'redir >> '. s:debugfile
+ silent echo "=======================================\n"
+ redir END
+endfunction
+
+function! s:log(msg)
+ if s:debug
+ exec 'redir >> ' . s:debugfile
+ silent echon strftime('%H:%M:%S') . ': ' . a:msg . "\n"
+ redir END
+ endif
+endfunction
+
"=================================================
" Template object, like a class.
let s:undotree = {}
@@ -90,11 +113,12 @@ endfunction
function! s:undotree.BindKey()
for i in s:keymap
- silent exec 'nnoremap <silent> <buffer> '.i[1].' :UndotreeAction '.i[0].'<cr>'
+ silent exec 'nnoremap <silent> <buffer> '.i[1].' :call UndotreeAction("'.i[0].'")<cr>'
endfor
endfunction
function! s:undotree.Action(action)
+ call s:log("Action() ".a:action)
if !self.IsVisible() || bufname("%") != self.bufname
"echoerr "Fatal: window does not exists."
return
@@ -167,6 +191,7 @@ endfunction
function! s:undotree.SetFocus()
let winnr = bufwinnr(self.bufname)
+ call s:log("SetFocus() winnr:".winnr." bufname:".self.bufname)
if winnr == -1
echoerr "Fatal: undotree window does not exist!"
return
@@ -180,6 +205,7 @@ endfunction
" May fail due to target window closed.
function! s:undotree.SetTargetFocus()
let winnr = bufwinnr(self.targetBufname)
+ call s:log("SetTargetFocus() winnr:".winnr." targetBufname:".self.targetBufname)
if winnr == -1
return 0
else
@@ -190,12 +216,14 @@ endfunction
function! s:undotree.RestoreFocus()
let previousWinnr = winnr("#")
+ call s:log("RestoreFocus() previousWinnr:".previousWinnr)
if previousWinnr > 0
call s:exec("norm! ".previousWinnr."\<c-w>\<c-w>")
endif
endfunction
function! s:undotree.Show()
+ call s:log("Show()")
if self.IsVisible()
return
endif
@@ -221,20 +249,23 @@ function! s:undotree.Show()
setlocal nomodifiable
setfiletype undotree
call s:undotree.BindKey()
+ " why refresh twice here?
call self.Update()
call self.RestoreFocus()
endfunction
function! s:undotree.Hide()
+ call s:log("Hide()")
if !self.IsVisible()
return
endif
call self.SetFocus()
- quit
+ call s:exec("quit")
" quit this window will restore focus to the previous window automatically.
endfunction
function! s:undotree.Toggle()
+ call s:log("Toggle()")
if self.IsVisible()
call self.Hide()
else
@@ -243,6 +274,7 @@ function! s:undotree.Toggle()
endfunction
function! s:undotree.Update()
+ call s:log("Update()")
if !self.IsVisible()
return
endif
@@ -264,12 +296,11 @@ function! s:undotree.AppendHelp()
call append(0,'') "empty line
if self.showHelp
for i in s:keymap
- call append(0,'" '.i[1].': '.i[2])
+ call append(0,'" '.i[1].' : '.i[2])
endfor
- call append(0,'" ------------------')
- call append(0,'" undotree quickhelp')
+ call append(0,s:helpmore)
else
- call append(0,'" Press ? for help.')
+ call append(0,s:helpless)
endif
endfunction
@@ -277,9 +308,10 @@ function! s:undotree.Index2Screen(index)
" calculate line number according to the help text.
" index starts from zero and lineNr starts from 1.
if self.showHelp
- let lineNr = a:index + len(s:keymap) + 3 + 1
+ " 2 means 1 empty line + 1 index padding (index starts from zero)
+ let lineNr = a:index + len(s:keymap) + len(s:helpmore) + 2
else
- let lineNr = a:index + 2 + 1
+ let lineNr = a:index + len(s:helpless) + 2
endif
return lineNr
endfunction
@@ -287,9 +319,9 @@ endfunction
" <0 if index is invalid. e.g. current line is in help text.
function! s:undotree.Screen2Index(line)
if self.showHelp
- let index = a:line - len(s:keymap) - 3 - 1
+ let index = a:line - len(s:keymap) - len(s:helpmore) - 2
else
- let index = a:line - 2 - 1
+ let index = a:line - len(s:helpless) - 2
endif
return index
endfunction
@@ -298,7 +330,7 @@ endfunction
function! s:undotree.Draw()
" remember the current cursor position.
let linePos = line('.') "Line number of cursor
- normal! H
+ call s:exec('normal! H')
let topPos = line('.') "Line number of the first line in screen.
setlocal modifiable
@@ -499,7 +531,7 @@ function! s:undotree.Render()
endif
endfor
" append meta info.
- let padding = ' '
+ let padding = ''
let newline = newline . padding
if node.curpos
let newline = newline.'>'.(node.seq).'< '.
@@ -574,7 +606,8 @@ endfunction
"=================================================
" It will set the target of undotree window to the current editing buffer.
-function! s:undotreeUpdate()
+function! UndotreeUpdate()
+ call s:log(">>>>>>> UndotreeUpdate()")
if type(gettabvar(tabpagenr(),'undotree')) != type(s:undotree)
return
endif
@@ -584,20 +617,28 @@ function! s:undotreeUpdate()
if mode() != 'n' "not in normal mode, return.
return
endif
+ if !t:undotree.IsVisible()
+ return
+ endif
+ call s:log(">>> UndotreeUpdate()")
call t:undotree.UpdateTarget()
call t:undotree.SetFocus()
call t:undotree.Update()
call t:undotree.RestoreFocus()
+ call s:log("<<< UndotreeUpdate() leave")
endfunction
-function! s:undotreeToggle()
+function! UndotreeToggle()
+ call s:log(">>> UndotreeToggle()")
if type(gettabvar(tabpagenr(),'undotree')) != type(s:undotree)
let t:undotree = s:new(s:undotree)
endif
call t:undotree.Toggle()
+ call s:log("<<< UndotreeToggle() leave")
endfunction
-function! s:undotreeAction(action)
+function! UndotreeAction(action)
+ call s:log("UndotreeAction()")
if type(gettabvar(tabpagenr(),'undotree')) != type(s:undotree)
echoerr "Fatal: t:undotree does not exists!"
return
@@ -605,14 +646,15 @@ function! s:undotreeAction(action)
call t:undotree.Action(a:action)
endfunction
-" Internal commands, args:linenr, action
-command! -n=1 -bar UndotreeAction :call s:undotreeAction(<f-args>)
-command! -n=0 -bar UndotreeUpdate :call s:undotreeUpdate()
+function! UndotreeDebugon()
+ call s:debugon()
+endfunction
+autocmd InsertEnter,InsertLeave,WinEnter,WinLeave,CursorMoved * call UndotreeUpdate()
+"=================================================
" User commands.
-command! -n=0 -bar UndotreeToggle :call s:undotreeToggle()
+command! -n=0 -bar UndotreeToggle :call UndotreeToggle()
-autocmd InsertEnter,InsertLeave,WinEnter,WinLeave,CursorMoved * call s:undotreeUpdate()
" vim: set et fdm=marker sts=4 sw=4:
View
4 syntax/undotree.vim
@@ -17,7 +17,8 @@ syn match UndotreeSeq ' \zs\d\+\ze '
syn match UndotreeCurrent '>\d\+<'
syn match UndotreeNext '{\d\+}'
syn match UndotreeHead '\[\d\+]'
-syn match UndotreeHelp '^".*$'
+syn match UndotreeHelp '^".*$' contains=UndotreeHelpKey
+syn match UndotreeHelpKey '^" \zs.\{-}\ze:' contained
hi link UndotreeNode Question
hi link UndotreeNodeCurrent CursorLineNr
@@ -29,6 +30,7 @@ hi link UndotreeCurrent CursorLineNr
hi link UndotreeNext Type
hi link UndotreeHead Identifier
hi link UndotreeHelp Comment
+hi link UndotreeHelpKey Type
let b:undotree_syntax = 'undotree'
Please sign in to comment.
Something went wrong with that request. Please try again.