Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Several changes #23

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 74 additions & 20 deletions autoload/gundo.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import sys
import time
import vim
import tempfile


# Mercurial's graphlog code --------------------------------------------------------
Expand Down Expand Up @@ -77,7 +78,7 @@ def fix_long_right_edges(edges):
if end > start:
edges[i] = (start, end + 1)

def ascii(buf, state, type, char, text, coldata):
def ascii(buf, state, type, char, text, coldata, verbose):
"""prints an ASCII graph of the DAG

takes the following arguments (one call per node in the graph):
Expand All @@ -95,6 +96,8 @@ def ascii(buf, state, type, char, text, coldata):
in the next revision and the number of columns (ongoing edges)
in the current revision. That is: -1 means one column removed;
0 means no columns added or removed; 1 means one column added.
- Verbosity: if enabled then the graph prints an extra '|'
between each line of information.
"""

idx, edges, ncols, coldiff = coldata
Expand All @@ -116,7 +119,8 @@ def ascii(buf, state, type, char, text, coldata):
# o | | | / /
# o | |
add_padding_line = (len(text) > 2 and coldiff == -1 and
[x for (x, y) in edges if x + 1 < y])
[x for (x, y) in edges if x + 1 < y] and
verbose)

# fix_nodeline_tail says whether to rewrite
#
Expand Down Expand Up @@ -159,10 +163,14 @@ def ascii(buf, state, type, char, text, coldata):
lines.append(get_padding_line(idx, ncols, edges))
lines.append(shift_interline)

# TODO NEED TO store where these extra append('') sections are so that we
# can navigate down and up correctly.

# make sure that there are as many graph lines as there are
# log strings
while len(text) < len(lines):
text.append("")
if any("/" in s for s in lines) or verbose:
while len(text) < len(lines):
text.append('')
if len(lines) < len(text):
extra_interline = ["|", " "] * (ncols + coldiff)
while len(lines) < len(text):
Expand All @@ -178,7 +186,7 @@ def ascii(buf, state, type, char, text, coldata):
state[0] = coldiff
state[1] = idx

def generate(dag, edgefn, current):
def generate(dag, edgefn, current, verbose):
seen, state = [], [0, 0]
buf = Buffer()
for node, parents in list(dag):
Expand All @@ -189,9 +197,11 @@ def generate(dag, edgefn, current):
line = '[%s] %s' % (node.n, age_label)
if node.n == current:
char = '@'
elif node.saved:
char = 'w'
else:
char = 'o'
ascii(buf, state, 'C', char, [line], edgefn(seen, node, parents))
ascii(buf, state, 'C', char, [line], edgefn(seen, node, parents), verbose)
return buf.b


Expand Down Expand Up @@ -273,9 +283,13 @@ def _undo_to(n):

INLINE_HELP = '''\
" Gundo for %s (%d)
" j/k - move between undo states
" p - preview diff of selected and current states
" <cr> - revert to selected state
" j/k - Move between undo states.
" P - Play current state to selected undo.
" d - Vert diffpatch of selected undo and current state.
" p - Diff of selected undo and current state.
" r - Diff of selected undo and prior undo.
" q - Quit!
" <cr> - Revert to selected state.

'''

Expand All @@ -289,29 +303,32 @@ def write(self, s):
self.b += s

class Node(object):
def __init__(self, n, parent, time, curhead):
def __init__(self, n, parent, time, curhead, saved):
self.n = int(n)
self.parent = parent
self.children = []
self.curhead = curhead
self.saved = saved
self.time = time

def _make_nodes(alts, nodes, parent=None):
p = parent

for alt in alts:
curhead = 'curhead' in alt
node = Node(n=alt['seq'], parent=p, time=alt['time'], curhead=curhead)
nodes.append(node)
if alt.get('alt'):
_make_nodes(alt['alt'], nodes, p)
p = node
if alt:
curhead = 'curhead' in alt
saved = 'save' in alt
node = Node(n=alt['seq'], parent=p, time=alt['time'], curhead=curhead, saved=saved)
nodes.append(node)
if alt.get('alt'):
_make_nodes(alt['alt'], nodes, p)
p = node

def make_nodes():
ut = vim.eval('undotree()')
entries = ut['entries']

root = Node(0, None, False, 0)
root = Node(0, None, False, 0, 0)
nodes = []
_make_nodes(entries, nodes, root)
nodes.append(root)
Expand Down Expand Up @@ -420,7 +437,8 @@ def walk_nodes(nodes):
dag = sorted(nodes, key=lambda n: int(n.n), reverse=True)
current = changenr(nodes)

result = generate(walk_nodes(dag), asciiedges, current).rstrip().splitlines()
verbose = vim.eval('g:gundo_verbose_graph') == 1
result = generate(walk_nodes(dag), asciiedges, current, verbose).rstrip().splitlines()
result = [' ' + l for l in result]

target = (vim.eval('g:gundo_target_f'), int(vim.eval('g:gundo_target_n')))
Expand Down Expand Up @@ -473,17 +491,51 @@ def GundoRenderPreview():

_goto_window_for_buffer_name('__Gundo__')

def GundoRenderPatchdiff():
""" Call GundoRenderChangePreview and display a vert diffpatch with the
current file. """
if GundoRenderChangePreview():
# if there are no lines, do nothing (show a warning).
_goto_window_for_buffer_name('__Gundo_Preview__')
if vim.current.buffer[:] == ['']:
# restore the cursor position before exiting.
_goto_window_for_buffer_name('__Gundo__')
vim.command('unsilent echo "No difference between current file and undo number!"')
return False

# quit out of gundo main screen
_goto_window_for_buffer_name('__Gundo__')
vim.command('quit')

# save the __Gundo_Preview__ buffer to a temp file.
_goto_window_for_buffer_name('__Gundo_Preview__')
(handle,filename) = tempfile.mkstemp()
vim.command('silent! w %s' % (filename))
# exit the __Gundo_Preview__ window
vim.command('quit')
# diff the temp file
vim.command('silent! vert diffpatch %s' % (filename))

# TODO set the buftype to temp or nonwritable or...
# move out of the patch file and into the original file.
#vim.command('normal il')
return True
return False

def GundoRenderChangePreview():
""" Render the selected undo level with the current file.
Return True on success, False on failure. """

if not _check_sanity():
return
return False

target_state = vim.eval('s:GundoGetTargetState()')

# Check that there's an undo state. There may not be if we're talking about
# a buffer with no changes yet.
if target_state == None:
_goto_window_for_buffer_name('__Gundo__')
return
return False
else:
target_state = int(target_state)

Expand All @@ -500,6 +552,8 @@ def GundoRenderChangePreview():

_goto_window_for_buffer_name('__Gundo__')

return True


# Gundo undo/redo
def GundoRevert():
Expand Down
77 changes: 69 additions & 8 deletions autoload/gundo.vim
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ endif"}}}
if !exists("g:gundo_auto_preview")"{{{
let g:gundo_auto_preview = 1
endif"}}}
if !exists("g:gundo_verbose_graph")"{{{
let g:gundo_verbose_graph = 0
endif"}}}

let s:has_supported_python = 0
if g:gundo_prefer_python3 && has('python3')"{{{
Expand Down Expand Up @@ -94,7 +97,7 @@ endfunction"}}}

function! s:GundoInlineHelpLength()"{{{
if g:gundo_help
return 6
return 10
else
return 0
endif
Expand All @@ -114,6 +117,7 @@ function! s:GundoMapGraph()"{{{
nnoremap <script> <silent> <buffer> gg gg:call <sid>GundoMove(1)<CR>
nnoremap <script> <silent> <buffer> P :call <sid>GundoPlayTo()<CR>
nnoremap <script> <silent> <buffer> p :call <sid>GundoRenderChangePreview()<CR>
nnoremap <script> <silent> <buffer> d :call <sid>GundoRenderPatchdiff()<CR>
nnoremap <script> <silent> <buffer> r :call <sid>GundoRenderPreview()<CR>
nnoremap <script> <silent> <buffer> q :call <sid>GundoClose()<CR>
cabbrev <script> <silent> <buffer> q call <sid>GundoClose()
Expand Down Expand Up @@ -360,11 +364,23 @@ function! s:GundoMove(direction) range"{{{
else
let move_count = v:count1
endif
let distance = 2 * move_count
if g:gundo_verbose_graph
let distance = 2 * move_count

" If we're in between two nodes we move by one less to get back on track.
if stridx(start_line, '[') == -1
let distance = distance - 1
" If we're in between two nodes we move by one less to get back on track.
if stridx(start_line, '[') == -1
let distance = distance - 1
endif
else
let distance = move_count
let nextline = getline(line('.')+distance*a:direction)
let idx1 = stridx(nextline, '@')
let idx2 = stridx(nextline, 'o')
let idx3 = stridx(nextline, 'w')
" if the next line is not a revision - then go down one more.
if (idx1+idx2+idx3) == -3
let distance = distance + move_count
endif
endif

let target_n = line('.') + (distance * a:direction)
Expand All @@ -378,13 +394,21 @@ function! s:GundoMove(direction) range"{{{

let line = getline('.')

" Move to the node, whether it's an @ or an o
" Move to the node, whether it's an @, o, or w
let idx1 = stridx(line, '@')
let idx2 = stridx(line, 'o')
if idx1 != -1
let idx3 = stridx(line, 'w')
let idxs = []
if idx1 != -1 | let idxs += [idx1] | endif
if idx2 != -1 | let idxs += [idx2] | endif
if idx3 != -1 | let idxs += [idx3] | endif
let minidx = min(idxs)
if idx1 == minidx
call cursor(0, idx1 + 1)
else
elseif idx2 == minidx
call cursor(0, idx2 + 1)
else
call cursor(0, idx3 + 1)
endif

if g:gundo_auto_preview == 1
Expand All @@ -404,6 +428,14 @@ function! s:GundoRenderGraph()"{{{
endif
endfunction"}}}

function! s:GundoRenderPatchdiff()"{{{
if s:has_supported_python == 2 && g:gundo_prefer_python3
python3 GundoRenderPatchdiff()
else
python GundoRenderPatchdiff()
endif
endfunction"}}}

function! s:GundoRenderPreview()"{{{
if s:has_supported_python == 2 && g:gundo_prefer_python3
python3 GundoRenderPreview()
Expand Down Expand Up @@ -460,10 +492,39 @@ function! gundo#GundoRenderGraph()"{{{
call s:GundoRenderGraph()
endfunction"}}}

" automatically reload Gundo buffer if open
function! s:GundoRefresh()"{{{
" abort when there were no changes

" abort if our b:gundoChangedtick doens't exist
if !exists('b:gundoChangedtick')
return
endif

if b:gundoChangedtick == b:changedtick | return | endif
let b:gundoChangedtick = b:changedtick

let gundoWin = bufwinnr('__Gundo__')
let gundoPreWin = bufwinnr('__Gundo_Preview__')
let currentWin = bufwinnr('%')

" abort if Gundo is closed or is current window
if (gundoWin == -1) || (gundoPreWin == -1) || (gundoWin == currentWin) || (gundoPreWin == currentWin)
return
endif

:GundoRenderGraph

" switch back to previous window
execute currentWin . 'wincmd w'
endfunction"}}}

augroup GundoAug
autocmd!
autocmd BufNewFile __Gundo__ call s:GundoSettingsGraph()
autocmd BufNewFile __Gundo_Preview__ call s:GundoSettingsPreview()
autocmd CursorHold * call s:GundoRefresh()
autocmd BufEnter * let b:gundoChangedtick = 0
augroup END

"}}}
17 changes: 14 additions & 3 deletions doc/gundo.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ CONTENTS *Gundo-contents*
3.9 gundo_preview_statusline .. |gundo_preview_statusline|
gundo_tree_statusline ..... |gundo_tree_statusline|
3.10 gundo_auto_preview ........ |gundo_auto_preview|
3.11 gundo_verbose_graph ....... |gundo_verbose_graph|
4. License ......................... |GundoLicense|
5. Bugs ............................ |GundoBugs|
6. Contributing .................... |GundoContributing|
Expand Down Expand Up @@ -71,7 +72,7 @@ look something like this: >
| | | | |
| o | [3] 4 hours ago | |
| | | | |
| o | [2] 4 hours ago | |
| w | [2] 4 hours ago | |
| |/ | |
| o [1] 4 hours ago | |
| | | |
Expand All @@ -87,8 +88,9 @@ look something like this: >
+-----------------------------------+------------------------------------+
Preview pane

Your current position in the undo tree is marked with an '@' character. Other
nodes are marked with an 'o' character.
Your current position in the undo tree is marked with an '@' character. Undo
positions that were saved to disk are marked with a 'w'. Other nodes are marked
with an 'o' character.

When you toggle open the graph Gundo will put your cursor on your current
position in the tree. You can move up and down the graph with the j and
Expand Down Expand Up @@ -216,6 +218,15 @@ be useful on large files and undo trees to speed up Gundo.

Default: 1 (automatically preview diffs)

------------------------------------------------------------------------------
3.11 g:gundo_verbose_graph *gundo_verbose_graph*

Set this to 0 to create shorter graphs: the 'o' characters will only be used
when multiple branches exist, and extra lines of '|' are suppressed making for
a graph half as long.

Default: 1 (verbose graphs)

==============================================================================
4. License *GundoLicense*

Expand Down
2 changes: 1 addition & 1 deletion plugin/gundo.vim
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@


"{{{ Init
if !exists('g:gundo_debug') && (exists('g:gundo_disable') || exists('loaded_gundo') || &cp)"{{{
if !exists('g:gundo_debug') && (exists('g:gundo_disable') && g:gundo_disable == 1 || exists('loaded_gundo') || &cp)"{{{
finish
endif
let loaded_gundo = 1"}}}
Expand Down