# HG changeset patch # User Steve Losh # Date 1288312923 14400 # Node ID 903d37063c3dc44e6ebe075701b787265aa24cb4 # Parent 2d3ac23e7413cba6f95d26531c807f77b4b29f87# Parent 731f1e6565c484c273312442fae1e1339c3702cf Merge. diff -r 731f1e6565c4 -r 903d37063c3d plugin/gundo.vim --- a/plugin/gundo.vim Wed Oct 27 19:09:02 2010 -0400 +++ b/plugin/gundo.vim Thu Oct 28 20:42:03 2010 -0400 @@ -385,18 +385,42 @@ normal = lambda s: vim.command('normal %s' % s) +MISSING_BUFFER = "Cannot find Gundo's target buffer (%s)" +MISSING_WINDOW = "Cannot find window (%s) for Gundo's target buffer (%s)" + +def _check_sanity(): + '''Check to make sure we're not crazy. + + Does the following things: + + * Make sure the target buffer still exists. + ''' + b = int(vim.eval('g:gundo_target_n')) + + n = vim.eval('bufname(%d)' % b) + if not n: + vim.command('echo "%s"' % (MISSING_BUFFER % b)) + return False + + w = int(vim.eval('bufwinnr(%d)' % b)) + if w == -1: + vim.command('echo "%s"' % (MISSING_WINDOW % (w, b))) + return False + + return True + def _goto_window_for_buffer(b): - w = vim.eval('bufwinnr(%d)' % int(b)) - vim.command('%dwincmd w' % int(w)) + w = int(vim.eval('bufwinnr(%d)' % int(b))) + vim.command('%dwincmd w' % w) def _goto_window_for_buffer_name(bn): b = vim.eval('bufnr("%s")' % bn) - _goto_window_for_buffer(b) + return _goto_window_for_buffer(b) def _undo_to(n): n = int(n) if n == 0: - vim.command('silent earlier 999999999d') + vim.command('silent earlier %s' % vim.eval('&undolevels')) else: vim.command('silent undo %d' % n) @@ -462,6 +486,24 @@ ENDPYTHON "}}} +"{{{ Gundo utility functions + +function! s:GundoGetTargetState()"{{{ + let target_line = matchstr(getline("."), '\v\[[0-9]+\]') + return matchstr(target_line, '\v[0-9]+') +endfunction"}}} + +function! s:GundoGoToWindowForBufferName(name)"{{{ + if bufwinnr(bufnr(a:name)) != -1 + exe bufwinnr(bufnr(a:name)) . "wincmd w" + return 1 + else + return 0 + endif +endfunction"}}} + +"}}} + "{{{ Gundo buffer settings function! s:GundoMapGraph()"{{{ @@ -508,6 +550,7 @@ setlocal norelativenumber setlocal nowrap setlocal foldlevel=20 + setlocal foldmethod=diff call s:GundoMapPreview() endfunction"}}} @@ -527,13 +570,13 @@ "}}} -"{{{ Buffer/window management +"{{{ Gundo buffer/window management function! s:GundoResizeBuffers(backto)"{{{ - exe bufwinnr(bufnr('__Gundo__')) . "wincmd w" + call s:GundoGoToWindowForBufferName('__Gundo__') exe "vertical resize " . g:gundo_width - exe bufwinnr(bufnr('__Gundo_Preview__')) . "wincmd w" + call s:GundoGoToWindowForBufferName('__Gundo_Preview__') exe "resize " . g:gundo_preview_height exe a:backto . "wincmd w" @@ -543,7 +586,7 @@ let existing_gundo_buffer = bufnr("__Gundo__") if existing_gundo_buffer == -1 - exe bufwinnr(bufnr('__Gundo_Preview__')) . "wincmd w" + call s:GundoGoToWindowForBufferName('__Gundo_Preview__') exe "new __Gundo__" if g:gundo_preview_bottom if g:gundo_right @@ -561,7 +604,7 @@ exe existing_gundo_window . "wincmd w" endif else - exe bufwinnr(bufnr('__Gundo_Preview__')) . "wincmd w" + call s:GundoGoToWindowForBufferName('__Gundo_Preview__') if g:gundo_preview_bottom if g:gundo_right exe "botright vsplit +buffer" . existing_gundo_buffer @@ -611,17 +654,15 @@ endfunction"}}} function! s:GundoClose()"{{{ - if bufwinnr(bufnr('__Gundo__')) != -1 - exe bufwinnr(bufnr('__Gundo__')) . "wincmd w" - quit - endif + if s:GundoGoToWindowForBufferName('__Gundo__') + quit + endif - if bufwinnr(bufnr('__Gundo_Preview__')) != -1 - exe bufwinnr(bufnr('__Gundo_Preview__')) . "wincmd w" - quit - endif + if s:GundoGoToWindowForBufferName('__Gundo_Preview__') + quit + endif - exe bufwinnr(g:gundo_target_n) . "wincmd w" + exe bufwinnr(g:gundo_target_n) . "wincmd w" endfunction"}}} function! s:GundoToggle()"{{{ @@ -647,15 +688,14 @@ GundoRender " TODO: Move these lines into RenderPreview - let target_line = matchstr(getline("."), '\v\[[0-9]+\]') - let target_num = matchstr(target_line, '\v[0-9]+') + let target_num = s:GundoGetTargetState() call s:GundoRenderPreview(target_num) endif endfunction"}}} "}}} -"{{{ Mouse handling +"{{{ Gundo mouse handling function! s:GundoMouseDoubleClick()"{{{ let start_line = getline('.') @@ -669,7 +709,7 @@ "}}} -"{{{ Movement +"{{{ Gundo movement function! s:GundoMove(direction)"{{{ let start_line = getline('.') @@ -701,18 +741,20 @@ call cursor(0, idx2 + 1) endif - let target_line = matchstr(getline("."), '\v\[[0-9]+\]') - let target_num = matchstr(target_line, '\v[0-9]+') + let target_num = s:GundoGetTargetState() call s:GundoRenderPreview(target_num) endfunction"}}} "}}} -"{{{ Rendering +"{{{ Gundo rendering function! s:GundoRenderGraph()"{{{ python << ENDPYTHON def GundoRenderGraph(): + if not _check_sanity(): + return + nodes, nmap = make_nodes() for node in nodes: @@ -810,6 +852,9 @@ before_time, after_time)) def GundoRenderPreview(): + if not _check_sanity(): + return + target_n = vim.eval('a:target') # Check that there's an undo state. There may not be if we're talking about @@ -838,33 +883,45 @@ "}}} -"{{{ Undo/redo commands +"{{{ Gundo undo/redo 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" python << ENDPYTHON -_undo_to(vim.eval('target_num')) +def GundoRevert(): + if not _check_sanity(): + return + + target_n = int(vim.eval('s:GundoGetTargetState()')) + back = vim.eval('g:gundo_target_n') + + _goto_window_for_buffer(back) + _undo_to(target_n) + + vim.command('GundoRenderGraph') + _goto_window_for_buffer(back) + +GundoRevert() ENDPYTHON - GundoRenderGraph - 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" - normal zR - python << ENDPYTHON def GundoPlayTo(): + if not _check_sanity(): + return + + target_n = int(vim.eval('s:GundoGetTargetState()')) + back = int(vim.eval('g:gundo_target_n')) + + vim.command('echo "%s"' % back) + + _goto_window_for_buffer(back) + normal('zR') + nodes, nmap = make_nodes() start = nmap[changenr(nodes)] - end = nmap[int(vim.eval('target_num'))] + end = nmap[target_n] def _walk_branch(origin, dest): rev = origin.n < dest.n @@ -896,7 +953,7 @@ _undo_to(node.n) vim.command('GundoRenderGraph') normal('zz') - vim.command('%dwincmd w' % int(vim.eval('back'))) + _goto_window_for_buffer(back) vim.command('redraw') vim.command('sleep 60m')