1 # ============================================================================
3 # Description: vim global plugin to visualize your undo tree
4 # Maintainer: Steve Losh <steve@stevelosh.com>
5 # License: GPLv2+ -- look it up.
6 # Notes: Much of this code was thiefed from Mercurial, and the rest was
7 # heavily inspired by scratch.vim and histwin.vim.
9 # ============================================================================
18 # Mercurial's graphlog code --------------------------------------------------------
19 def asciiedges(seen, rev, parents):
20 """adds edge info to changelog DAG walk suitable for ascii()"""
23 nodeidx = seen.index(rev)
27 for parent in parents:
29 knownparents.append(parent)
31 newparents.append(parent)
34 seen[nodeidx:nodeidx + 1] = newparents
35 edges = [(nodeidx, seen.index(p)) for p in knownparents]
37 if len(newparents) > 0:
38 edges.append((nodeidx, nodeidx))
39 if len(newparents) > 1:
40 edges.append((nodeidx, nodeidx + 1))
42 nmorecols = len(seen) - ncols
43 return nodeidx, edges, ncols, nmorecols
45 def get_nodeline_edges_tail(
46 node_index, p_node_index, n_columns, n_columns_diff, p_diff, fix_tail):
47 if fix_tail and n_columns_diff == p_diff and n_columns_diff != 0:
48 # Still going in the same non-vertical direction.
49 if n_columns_diff == -1:
50 start = max(node_index + 1, p_node_index)
51 tail = ["|", " "] * (start - node_index - 1)
52 tail.extend(["/", " "] * (n_columns - start))
55 return ["\\", " "] * (n_columns - node_index - 1)
57 return ["|", " "] * (n_columns - node_index - 1)
59 def draw_edges(edges, nodeline, interline):
60 for (start, end) in edges:
62 interline[2 * end + 1] = "/"
63 elif start == end - 1:
64 interline[2 * start + 1] = "\\"
66 interline[2 * start] = "|"
68 nodeline[2 * end] = "+"
70 (start, end) = (end, start)
71 for i in range(2 * start + 1, 2 * end):
72 if nodeline[i] != "+":
75 def fix_long_right_edges(edges):
76 for (i, (start, end)) in enumerate(edges):
78 edges[i] = (start, end + 1)
80 def ascii(buf, state, type, char, text, coldata):
81 """prints an ASCII graph of the DAG
83 takes the following arguments (one call per node in the graph):
85 - Somewhere to keep the needed state in (init to asciistate())
86 - Column of the current node in the set of ongoing edges.
87 - Type indicator of node data == ASCIIDATA.
88 - Payload: (char, lines):
89 - Character to use as node's symbol.
90 - List of lines to display as the node's text.
91 - Edges; a list of (col, next_col) indicating the edges between
92 the current node and its parents.
93 - Number of columns (ongoing edges) in the current revision.
94 - The difference between the number of columns (ongoing edges)
95 in the next revision and the number of columns (ongoing edges)
96 in the current revision. That is: -1 means one column removed;
97 0 means no columns added or removed; 1 means one column added.
100 idx, edges, ncols, coldiff = coldata
101 assert -2 < coldiff < 2
109 fix_long_right_edges(edges)
111 # add_padding_line says whether to rewrite
114 # | o---+ into | o---+
115 # | / / | | | # <--- padding line
118 add_padding_line = (len(text) > 2 and coldiff == -1 and
119 [x for (x, y) in edges if x + 1 < y])
121 # fix_nodeline_tail says whether to rewrite
123 # | | o | | | | o | |
125 # | o | | into | o / / # <--- fixed nodeline tail
128 fix_nodeline_tail = len(text) <= 2 and not add_padding_line
130 # nodeline is the line containing the node character (typically o)
131 nodeline = ["|", " "] * idx
132 nodeline.extend([char, " "])
135 get_nodeline_edges_tail(idx, state[1], ncols, coldiff,
136 state[0], fix_nodeline_tail))
138 # shift_interline is the line containing the non-vertical
139 # edges between this entry and the next
140 shift_interline = ["|", " "] * idx
150 shift_interline.extend(n_spaces * [" "])
151 shift_interline.extend([edge_ch, " "] * (ncols - idx - 1))
153 # draw edges from the current node to its parents
154 draw_edges(edges, nodeline, shift_interline)
156 # lines is the list of all graph lines to print
159 lines.append(get_padding_line(idx, ncols, edges))
160 lines.append(shift_interline)
162 # make sure that there are as many graph lines as there are
164 while len(text) < len(lines):
166 if len(lines) < len(text):
167 extra_interline = ["|", " "] * (ncols + coldiff)
168 while len(lines) < len(text):
169 lines.append(extra_interline)
172 indentation_level = max(ncols, ncols + coldiff)
173 for (line, logstr) in zip(lines, text):
174 ln = "%-*s %s" % (2 * indentation_level, "".join(line), logstr)
175 buf.write(ln.rstrip() + '\n')
181 def generate(dag, edgefn, current):
182 seen, state = [], [0, 0]
184 for node, parents in list(dag):
186 age_label = age(int(node.time))
188 age_label = 'Original'
189 line = '[%s] %s' % (node.n, age_label)
190 if node.n == current:
194 ascii(buf, state, 'C', char, [line], edgefn(seen, node, parents))
198 # Mercurial age function -----------------------------------------------------------
199 agescales = [("year", 3600 * 24 * 365),
200 ("month", 3600 * 24 * 30),
201 ("week", 3600 * 24 * 7),
208 '''turn a timestamp into an age string.'''
215 return "%d %s" % (c, plural(t, c))
220 return 'in the future'
222 delta = max(1, int(now - then))
223 if delta > agescales[0][1] * 2:
224 return time.strftime('%Y-%m-%d', time.gmtime(float(ts)))
226 for t, s in agescales:
229 return '%s ago' % fmt(t, n)
232 # Python Vim utility functions -----------------------------------------------------
233 normal = lambda s: vim.command('normal %s' % s)
235 MISSING_BUFFER = "Cannot find Gundo's target buffer (%s)"
236 MISSING_WINDOW = "Cannot find window (%s) for Gundo's target buffer (%s)"
239 '''Check to make sure we're not crazy.
241 Does the following things:
243 * Make sure the target buffer still exists.
245 b = int(vim.eval('g:gundo_target_n'))
247 if not vim.eval('bufloaded(%d)' % b):
248 vim.command('echo "%s"' % (MISSING_BUFFER % b))
251 w = int(vim.eval('bufwinnr(%d)' % b))
253 vim.command('echo "%s"' % (MISSING_WINDOW % (w, b)))
258 def _goto_window_for_buffer(b):
259 w = int(vim.eval('bufwinnr(%d)' % int(b)))
260 vim.command('%dwincmd w' % w)
262 def _goto_window_for_buffer_name(bn):
263 b = vim.eval('bufnr("%s")' % bn)
264 return _goto_window_for_buffer(b)
269 vim.command('silent earlier %s' % (int(vim.eval('&undolevels')) + 1))
271 vim.command('silent undo %d' % n)
276 " j/k - move between undo states
277 " p - preview diff of selected and current states
278 " <cr> - revert to selected state
283 # Python undo tree data structures and functions -----------------------------------
284 class Buffer(object):
292 def __init__(self, n, parent, time, curhead):
296 self.curhead = curhead
299 def _make_nodes(alts, nodes, parent=None):
303 curhead = 'curhead' in alt
304 node = Node(n=alt['seq'], parent=p, time=alt['time'], curhead=curhead)
307 _make_nodes(alt['alt'], nodes, p)
311 ut = vim.eval('undotree()')
312 entries = ut['entries']
314 root = Node(0, None, False, 0)
316 _make_nodes(entries, nodes, root)
318 nmap = dict((node.n, node) for node in nodes)
322 _curhead_l = list(itertools.dropwhile(lambda n: not n.curhead, nodes))
324 current = _curhead_l[0].parent.n
326 current = int(vim.eval('changenr()'))
330 # Gundo rendering ------------------------------------------------------------------
332 # Rendering utility functions
334 return time.strftime('%Y-%m-%d %I:%M:%S %p', time.localtime(float(t)))
336 def _output_preview_text(lines):
337 _goto_window_for_buffer_name('__Gundo_Preview__')
338 vim.command('setlocal modifiable')
339 vim.current.buffer[:] = lines
340 vim.command('setlocal nomodifiable')
342 def _generate_preview_diff(current, node_before, node_after):
343 _goto_window_for_buffer(vim.eval('g:gundo_target_n'))
345 if not node_after.n: # we're at the original file
349 after_lines = vim.current.buffer[:]
353 after_name = 'Original'
355 elif not node_before.n: # we're at a pseudo-root state
357 before_lines = vim.current.buffer[:]
359 _undo_to(node_after.n)
360 after_lines = vim.current.buffer[:]
362 before_name = 'Original'
364 after_name = node_after.n
365 after_time = _fmt_time(node_after.time)
367 _undo_to(node_before.n)
368 before_lines = vim.current.buffer[:]
370 _undo_to(node_after.n)
371 after_lines = vim.current.buffer[:]
373 before_name = node_before.n
374 before_time = _fmt_time(node_before.time)
375 after_name = node_after.n
376 after_time = _fmt_time(node_after.time)
380 return list(difflib.unified_diff(before_lines, after_lines,
381 before_name, after_name,
382 before_time, after_time))
384 def _generate_change_preview_diff(current, node_before, node_after):
385 _goto_window_for_buffer(vim.eval('g:gundo_target_n'))
387 _undo_to(node_before.n)
388 before_lines = vim.current.buffer[:]
390 _undo_to(node_after.n)
391 after_lines = vim.current.buffer[:]
393 before_name = node_before.n or 'Original'
394 before_time = node_before.time and _fmt_time(node_before.time) or ''
395 after_name = node_after.n or 'Original'
396 after_time = node_after.time and _fmt_time(node_after.time) or ''
400 return list(difflib.unified_diff(before_lines, after_lines,
401 before_name, after_name,
402 before_time, after_time))
404 def GundoRenderGraph():
405 if not _check_sanity():
408 nodes, nmap = make_nodes()
411 node.children = [n for n in nodes if n.parent == node]
413 def walk_nodes(nodes):
416 yield (node, [node.parent])
420 dag = sorted(nodes, key=lambda n: int(n.n), reverse=True)
421 current = changenr(nodes)
423 result = generate(walk_nodes(dag), asciiedges, current).rstrip().splitlines()
424 result = [' ' + l for l in result]
426 target = (vim.eval('g:gundo_target_f'), int(vim.eval('g:gundo_target_n')))
428 if int(vim.eval('g:gundo_help')):
429 header = (INLINE_HELP % target).splitlines()
433 vim.command('call s:GundoOpenGraph()')
434 vim.command('setlocal modifiable')
435 vim.current.buffer[:] = (header + result)
436 vim.command('setlocal nomodifiable')
441 line.split('[')[0].index('@')
447 vim.command('%d' % (i+len(header)-1))
449 def GundoRenderPreview():
450 if not _check_sanity():
453 target_state = vim.eval('s:GundoGetTargetState()')
455 # Check that there's an undo state. There may not be if we're talking about
456 # a buffer with no changes yet.
457 if target_state == None:
458 _goto_window_for_buffer_name('__Gundo__')
461 target_state = int(target_state)
463 _goto_window_for_buffer(vim.eval('g:gundo_target_n'))
465 nodes, nmap = make_nodes()
466 current = changenr(nodes)
468 node_after = nmap[target_state]
469 node_before = node_after.parent
471 vim.command('call s:GundoOpenPreview()')
472 _output_preview_text(_generate_preview_diff(current, node_before, node_after))
474 _goto_window_for_buffer_name('__Gundo__')
476 def GundoRenderChangePreview():
477 if not _check_sanity():
480 target_state = vim.eval('s:GundoGetTargetState()')
482 # Check that there's an undo state. There may not be if we're talking about
483 # a buffer with no changes yet.
484 if target_state == None:
485 _goto_window_for_buffer_name('__Gundo__')
488 target_state = int(target_state)
490 _goto_window_for_buffer(vim.eval('g:gundo_target_n'))
492 nodes, nmap = make_nodes()
493 current = changenr(nodes)
495 node_after = nmap[target_state]
496 node_before = nmap[current]
498 vim.command('call s:GundoOpenPreview()')
499 _output_preview_text(_generate_change_preview_diff(current, node_before, node_after))
501 _goto_window_for_buffer_name('__Gundo__')
506 if not _check_sanity():
509 target_n = int(vim.eval('s:GundoGetTargetState()'))
510 back = vim.eval('g:gundo_target_n')
512 _goto_window_for_buffer(back)
515 vim.command('GundoRenderGraph')
516 _goto_window_for_buffer(back)
518 if int(vim.eval('g:gundo_close_on_revert')):
519 vim.command('GundoToggle')
522 if not _check_sanity():
525 target_n = int(vim.eval('s:GundoGetTargetState()'))
526 back = int(vim.eval('g:gundo_target_n'))
528 vim.command('echo "%s"' % back)
530 _goto_window_for_buffer(back)
533 nodes, nmap = make_nodes()
535 start = nmap[changenr(nodes)]
538 def _walk_branch(origin, dest):
539 rev = origin.n < dest.n
542 if origin.n > dest.n:
543 current, final = origin, dest
545 current, final = dest, origin
547 while current.n >= final.n:
548 if current.n == final.n:
550 nodes.append(current)
551 current = current.parent
554 nodes.append(current)
557 return reversed(nodes)
561 branch = _walk_branch(start, end)
564 vim.command('unsilent echo "No path to that node from here!"')
569 vim.command('GundoRenderGraph')
571 _goto_window_for_buffer(back)
572 vim.command('redraw')
573 vim.command('sleep 60m')
575 def initPythonModule():
576 if sys.version_info[:2] < (2, 4):
577 vim.command('let s:has_supported_python = 0')