ef0fbc6e5e62

Add a PlayTo function for the lols.
[view raw] [browse files]
author Steve Losh <steve@dwaiter.com>
date Tue, 12 Oct 2010 20:20:37 -0400
parents 072ecb446535
children 6506376b04a1
branches/tags (none)
files plugin/gundo.vim

Changes

--- a/plugin/gundo.vim	Tue Oct 12 19:24:16 2010 -0400
+++ b/plugin/gundo.vim	Tue Oct 12 20:20:37 2010 -0400
@@ -82,6 +82,7 @@
         nnoremap <script> <silent> <buffer> k     :call <sid>GundoMove(-1)<CR>
         nnoremap <script> <silent> <buffer> gg    gg:call <sid>GundoMove(1)<CR>
         nnoremap <script> <silent> <buffer> G     G:call <sid>GundoMove(-1)<CR>
+        nnoremap <script> <silent> <buffer> P     :call <sid>GundoPlayTo()<CR>
     else
         let existing_gundo_window = bufwinnr(existing_gundo_buffer)
 
@@ -188,18 +189,6 @@
 endfunction
 "}}}
 
-"{{{ Undo/Redo Commands
-function! s:GundoRevert()
-    let target_line = matchstr(getline("."), '\v\[[0-9]+\]')
-    let target_num = matchstr(target_line, '\v[0-9]+')
-    let back = bufwinnr(g:gundo_target_n)
-    exe back . "wincmd w"
-    exe "undo " . target_num
-    GundoRender
-    exe back . "wincmd w"
-endfunction
-"}}}
-
 "{{{ Mercurial's Graphlog Code
 python << ENDPYTHON
 def asciiedges(seen, rev, parents):
@@ -569,6 +558,7 @@
         p = node
 
 def make_nodes():
+    # TODO: We need a node mapping to get rid of nasty loops.
     ut = vim.eval('undotree()')
     entries = ut['entries']
 
@@ -578,6 +568,7 @@
     return (root, nodes)
 
 def changenr(nodes):
+    # TODO: This seems to sometimes be wrong right after you open a file...
     _curhead_l = list(itertools.dropwhile(lambda n: not n.curhead, nodes))
     if _curhead_l:
         current = _curhead_l[0].parent.n
@@ -675,6 +666,72 @@
 endfunction
 "}}}
 
+"{{{ Undo/Redo Commands
+function! s:GundoRevert()
+    let target_line = matchstr(getline("."), '\v\[[0-9]+\]')
+    let target_num = matchstr(target_line, '\v[0-9]+')
+    let back = bufwinnr(g:gundo_target_n)
+    exe back . "wincmd w"
+    exe "undo " . target_num
+    GundoRender
+    exe back . "wincmd w"
+endfunction
+
+function! s:GundoPlayTo()
+    let target_line = matchstr(getline("."), '\v\[[0-9]+\]')
+    let target_num = matchstr(target_line, '\v[0-9]+')
+    let back = bufwinnr(g:gundo_target_n)
+    exe back . "wincmd w"
+
+python << ENDPYTHON
+def GundoPlayTo():
+    root, nodes = make_nodes()
+
+    def _find_node(nodes, n):
+        for node in nodes:
+            if node.n == n:
+                return node
+
+    start = _find_node(nodes, changenr(nodes))
+    end = _find_node(nodes, int(vim.eval('target_num')))
+
+    def _walk_branch(origin, dest):
+        rev = origin.n < dest.n
+
+        nodes = []
+        current = origin if origin.n > dest.n else dest
+        final = dest if origin.n > dest.n else origin
+
+        while current.n >= final.n:
+            if current.n == final.n:
+                break
+            nodes.append(current)
+            current = current.parent
+        else:
+            return None
+        nodes.append(current)
+
+        return reversed(nodes) if rev else nodes
+
+    branch = _walk_branch(start, end)
+
+    if not branch:
+        vim.command('unsilent echo "No path to that node from here!"')
+        return
+
+    for node in branch:
+        vim.command('silent undo %d' % node.n)
+        vim.command('GundoRender')
+        normal('zz')
+        vim.command('%dwincmd w' % int(vim.eval('back')))
+        vim.command('redraw')
+        vim.command('sleep 60m')
+
+GundoPlayTo()
+ENDPYTHON
+endfunction
+"}}}
+
 "{{{ Misc
 command! -nargs=0 GundoOpenBuffer call s:GundoOpenBuffer()
 command! -nargs=0 GundoToggle call s:GundoToggle()