1
0

gundo.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  1. # ============================================================================
  2. # File: gundo.py
  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.
  8. #
  9. # ============================================================================
  10. import difflib
  11. import itertools
  12. import sys
  13. import time
  14. import vim
  15. # Mercurial's graphlog code --------------------------------------------------------
  16. def asciiedges(seen, rev, parents):
  17. """adds edge info to changelog DAG walk suitable for ascii()"""
  18. if rev not in seen:
  19. seen.append(rev)
  20. nodeidx = seen.index(rev)
  21. knownparents = []
  22. newparents = []
  23. for parent in parents:
  24. if parent in seen:
  25. knownparents.append(parent)
  26. else:
  27. newparents.append(parent)
  28. ncols = len(seen)
  29. seen[nodeidx:nodeidx + 1] = newparents
  30. edges = [(nodeidx, seen.index(p)) for p in knownparents]
  31. if len(newparents) > 0:
  32. edges.append((nodeidx, nodeidx))
  33. if len(newparents) > 1:
  34. edges.append((nodeidx, nodeidx + 1))
  35. nmorecols = len(seen) - ncols
  36. return nodeidx, edges, ncols, nmorecols
  37. def get_nodeline_edges_tail(
  38. node_index, p_node_index, n_columns, n_columns_diff, p_diff, fix_tail):
  39. if fix_tail and n_columns_diff == p_diff and n_columns_diff != 0:
  40. # Still going in the same non-vertical direction.
  41. if n_columns_diff == -1:
  42. start = max(node_index + 1, p_node_index)
  43. tail = ["|", " "] * (start - node_index - 1)
  44. tail.extend(["/", " "] * (n_columns - start))
  45. return tail
  46. else:
  47. return ["\\", " "] * (n_columns - node_index - 1)
  48. else:
  49. return ["|", " "] * (n_columns - node_index - 1)
  50. def draw_edges(edges, nodeline, interline):
  51. for (start, end) in edges:
  52. if start == end + 1:
  53. interline[2 * end + 1] = "/"
  54. elif start == end - 1:
  55. interline[2 * start + 1] = "\\"
  56. elif start == end:
  57. interline[2 * start] = "|"
  58. else:
  59. nodeline[2 * end] = "+"
  60. if start > end:
  61. (start, end) = (end, start)
  62. for i in range(2 * start + 1, 2 * end):
  63. if nodeline[i] != "+":
  64. nodeline[i] = "-"
  65. def fix_long_right_edges(edges):
  66. for (i, (start, end)) in enumerate(edges):
  67. if end > start:
  68. edges[i] = (start, end + 1)
  69. def ascii(buf, state, type, char, text, coldata):
  70. """prints an ASCII graph of the DAG
  71. takes the following arguments (one call per node in the graph):
  72. - Somewhere to keep the needed state in (init to asciistate())
  73. - Column of the current node in the set of ongoing edges.
  74. - Type indicator of node data == ASCIIDATA.
  75. - Payload: (char, lines):
  76. - Character to use as node's symbol.
  77. - List of lines to display as the node's text.
  78. - Edges; a list of (col, next_col) indicating the edges between
  79. the current node and its parents.
  80. - Number of columns (ongoing edges) in the current revision.
  81. - The difference between the number of columns (ongoing edges)
  82. in the next revision and the number of columns (ongoing edges)
  83. in the current revision. That is: -1 means one column removed;
  84. 0 means no columns added or removed; 1 means one column added.
  85. """
  86. idx, edges, ncols, coldiff = coldata
  87. assert -2 < coldiff < 2
  88. if coldiff == -1:
  89. # Transform
  90. #
  91. # | | | | | |
  92. # o | | into o---+
  93. # |X / |/ /
  94. # | | | |
  95. fix_long_right_edges(edges)
  96. # add_padding_line says whether to rewrite
  97. #
  98. # | | | | | | | |
  99. # | o---+ into | o---+
  100. # | / / | | | # <--- padding line
  101. # o | | | / /
  102. # o | |
  103. add_padding_line = (len(text) > 2 and coldiff == -1 and
  104. [x for (x, y) in edges if x + 1 < y])
  105. # fix_nodeline_tail says whether to rewrite
  106. #
  107. # | | o | | | | o | |
  108. # | | |/ / | | |/ /
  109. # | o | | into | o / / # <--- fixed nodeline tail
  110. # | |/ / | |/ /
  111. # o | | o | |
  112. fix_nodeline_tail = len(text) <= 2 and not add_padding_line
  113. # nodeline is the line containing the node character (typically o)
  114. nodeline = ["|", " "] * idx
  115. nodeline.extend([char, " "])
  116. nodeline.extend(
  117. get_nodeline_edges_tail(idx, state[1], ncols, coldiff,
  118. state[0], fix_nodeline_tail))
  119. # shift_interline is the line containing the non-vertical
  120. # edges between this entry and the next
  121. shift_interline = ["|", " "] * idx
  122. if coldiff == -1:
  123. n_spaces = 1
  124. edge_ch = "/"
  125. elif coldiff == 0:
  126. n_spaces = 2
  127. edge_ch = "|"
  128. else:
  129. n_spaces = 3
  130. edge_ch = "\\"
  131. shift_interline.extend(n_spaces * [" "])
  132. shift_interline.extend([edge_ch, " "] * (ncols - idx - 1))
  133. # draw edges from the current node to its parents
  134. draw_edges(edges, nodeline, shift_interline)
  135. # lines is the list of all graph lines to print
  136. lines = [nodeline]
  137. if add_padding_line:
  138. lines.append(get_padding_line(idx, ncols, edges))
  139. lines.append(shift_interline)
  140. # make sure that there are as many graph lines as there are
  141. # log strings
  142. while len(text) < len(lines):
  143. text.append("")
  144. if len(lines) < len(text):
  145. extra_interline = ["|", " "] * (ncols + coldiff)
  146. while len(lines) < len(text):
  147. lines.append(extra_interline)
  148. # print lines
  149. indentation_level = max(ncols, ncols + coldiff)
  150. for (line, logstr) in zip(lines, text):
  151. ln = "%-*s %s" % (2 * indentation_level, "".join(line), logstr)
  152. buf.write(ln.rstrip() + '\n')
  153. # ... and start over
  154. state[0] = coldiff
  155. state[1] = idx
  156. def generate(dag, edgefn, current):
  157. seen, state = [], [0, 0]
  158. buf = Buffer()
  159. for node, parents in list(dag):
  160. if node.time:
  161. age_label = age(int(node.time))
  162. else:
  163. age_label = 'Original'
  164. line = '[%s] %s' % (node.n, age_label)
  165. if node.n == current:
  166. char = '@'
  167. else:
  168. char = 'o'
  169. ascii(buf, state, 'C', char, [line], edgefn(seen, node, parents))
  170. return buf.b
  171. # Mercurial age function -----------------------------------------------------------
  172. agescales = [("year", 3600 * 24 * 365),
  173. ("month", 3600 * 24 * 30),
  174. ("week", 3600 * 24 * 7),
  175. ("day", 3600 * 24),
  176. ("hour", 3600),
  177. ("minute", 60),
  178. ("second", 1)]
  179. def age(ts):
  180. '''turn a timestamp into an age string.'''
  181. def plural(t, c):
  182. if c == 1:
  183. return t
  184. return t + "s"
  185. def fmt(t, c):
  186. return "%d %s" % (c, plural(t, c))
  187. now = time.time()
  188. then = ts
  189. if then > now:
  190. return 'in the future'
  191. delta = max(1, int(now - then))
  192. if delta > agescales[0][1] * 2:
  193. return time.strftime('%Y-%m-%d', time.gmtime(float(ts)))
  194. for t, s in agescales:
  195. n = delta // s
  196. if n >= 2 or s == 1:
  197. return '%s ago' % fmt(t, n)
  198. # Python Vim utility functions -----------------------------------------------------
  199. normal = lambda s: vim.command('normal %s' % s)
  200. MISSING_BUFFER = "Cannot find Gundo's target buffer (%s)"
  201. MISSING_WINDOW = "Cannot find window (%s) for Gundo's target buffer (%s)"
  202. def _check_sanity():
  203. '''Check to make sure we're not crazy.
  204. Does the following things:
  205. * Make sure the target buffer still exists.
  206. '''
  207. b = int(vim.eval('g:gundo_target_n'))
  208. if not vim.eval('bufloaded(%d)' % b):
  209. vim.command('echo "%s"' % (MISSING_BUFFER % b))
  210. return False
  211. w = int(vim.eval('bufwinnr(%d)' % b))
  212. if w == -1:
  213. vim.command('echo "%s"' % (MISSING_WINDOW % (w, b)))
  214. return False
  215. return True
  216. def _goto_window_for_buffer(b):
  217. w = int(vim.eval('bufwinnr(%d)' % int(b)))
  218. vim.command('%dwincmd w' % w)
  219. def _goto_window_for_buffer_name(bn):
  220. b = vim.eval('bufnr("%s")' % bn)
  221. return _goto_window_for_buffer(b)
  222. def _undo_to(n):
  223. n = int(n)
  224. if n == 0:
  225. vim.command('silent earlier %s' % (int(vim.eval('&undolevels')) + 1))
  226. else:
  227. vim.command('silent undo %d' % n)
  228. INLINE_HELP = '''\
  229. " Gundo for %s (%d)
  230. " j/k - move between undo states
  231. " p - preview diff of selected and current states
  232. " <cr> - revert to selected state
  233. '''
  234. # Python undo tree data structures and functions -----------------------------------
  235. class Buffer(object):
  236. def __init__(self):
  237. self.b = ''
  238. def write(self, s):
  239. self.b += s
  240. class Node(object):
  241. def __init__(self, n, parent, time, curhead):
  242. self.n = int(n)
  243. self.parent = parent
  244. self.children = []
  245. self.curhead = curhead
  246. self.time = time
  247. def _make_nodes(alts, nodes, parent=None):
  248. p = parent
  249. for alt in alts:
  250. curhead = 'curhead' in alt
  251. node = Node(n=alt['seq'], parent=p, time=alt['time'], curhead=curhead)
  252. nodes.append(node)
  253. if alt.get('alt'):
  254. _make_nodes(alt['alt'], nodes, p)
  255. p = node
  256. def make_nodes():
  257. ut = vim.eval('undotree()')
  258. entries = ut['entries']
  259. root = Node(0, None, False, 0)
  260. nodes = []
  261. _make_nodes(entries, nodes, root)
  262. nodes.append(root)
  263. nmap = dict((node.n, node) for node in nodes)
  264. return nodes, nmap
  265. def changenr(nodes):
  266. _curhead_l = list(itertools.dropwhile(lambda n: not n.curhead, nodes))
  267. if _curhead_l:
  268. current = _curhead_l[0].parent.n
  269. else:
  270. current = int(vim.eval('changenr()'))
  271. return current
  272. # Gundo rendering ------------------------------------------------------------------
  273. # Rendering utility functions
  274. def _fmt_time(t):
  275. return time.strftime('%Y-%m-%d %I:%M:%S %p', time.localtime(float(t)))
  276. def _output_preview_text(lines):
  277. _goto_window_for_buffer_name('__Gundo_Preview__')
  278. vim.command('setlocal modifiable')
  279. vim.current.buffer[:] = lines
  280. vim.command('setlocal nomodifiable')
  281. def _generate_preview_diff(current, node_before, node_after):
  282. _goto_window_for_buffer(vim.eval('g:gundo_target_n'))
  283. if not node_after.n: # we're at the original file
  284. before_lines = []
  285. _undo_to(0)
  286. after_lines = vim.current.buffer[:]
  287. before_name = 'n/a'
  288. before_time = ''
  289. after_name = 'Original'
  290. after_time = ''
  291. elif not node_before.n: # we're at a pseudo-root state
  292. _undo_to(0)
  293. before_lines = vim.current.buffer[:]
  294. _undo_to(node_after.n)
  295. after_lines = vim.current.buffer[:]
  296. before_name = 'Original'
  297. before_time = ''
  298. after_name = node_after.n
  299. after_time = _fmt_time(node_after.time)
  300. else:
  301. _undo_to(node_before.n)
  302. before_lines = vim.current.buffer[:]
  303. _undo_to(node_after.n)
  304. after_lines = vim.current.buffer[:]
  305. before_name = node_before.n
  306. before_time = _fmt_time(node_before.time)
  307. after_name = node_after.n
  308. after_time = _fmt_time(node_after.time)
  309. _undo_to(current)
  310. return list(difflib.unified_diff(before_lines, after_lines,
  311. before_name, after_name,
  312. before_time, after_time))
  313. def _generate_change_preview_diff(current, node_before, node_after):
  314. _goto_window_for_buffer(vim.eval('g:gundo_target_n'))
  315. _undo_to(node_before.n)
  316. before_lines = vim.current.buffer[:]
  317. _undo_to(node_after.n)
  318. after_lines = vim.current.buffer[:]
  319. before_name = node_before.n or 'Original'
  320. before_time = node_before.time and _fmt_time(node_before.time) or ''
  321. after_name = node_after.n or 'Original'
  322. after_time = node_after.time and _fmt_time(node_after.time) or ''
  323. _undo_to(current)
  324. return list(difflib.unified_diff(before_lines, after_lines,
  325. before_name, after_name,
  326. before_time, after_time))
  327. def GundoRenderGraph():
  328. if not _check_sanity():
  329. return
  330. nodes, nmap = make_nodes()
  331. for node in nodes:
  332. node.children = [n for n in nodes if n.parent == node]
  333. def walk_nodes(nodes):
  334. for node in nodes:
  335. if node.parent:
  336. yield (node, [node.parent])
  337. else:
  338. yield (node, [])
  339. dag = sorted(nodes, key=lambda n: int(n.n), reverse=True)
  340. current = changenr(nodes)
  341. result = generate(walk_nodes(dag), asciiedges, current).rstrip().splitlines()
  342. result = [' ' + l for l in result]
  343. target = (vim.eval('g:gundo_target_f'), int(vim.eval('g:gundo_target_n')))
  344. if int(vim.eval('g:gundo_help')):
  345. header = (INLINE_HELP % target).splitlines()
  346. else:
  347. header = []
  348. vim.command('call s:GundoOpenGraph()')
  349. vim.command('setlocal modifiable')
  350. vim.current.buffer[:] = (header + result)
  351. vim.command('setlocal nomodifiable')
  352. i = 1
  353. for line in result:
  354. try:
  355. line.split('[')[0].index('@')
  356. i += 1
  357. break
  358. except ValueError:
  359. pass
  360. i += 1
  361. vim.command('%d' % (i+len(header)-1))
  362. def GundoRenderPreview():
  363. if not _check_sanity():
  364. return
  365. target_state = vim.eval('s:GundoGetTargetState()')
  366. # Check that there's an undo state. There may not be if we're talking about
  367. # a buffer with no changes yet.
  368. if target_state == None:
  369. _goto_window_for_buffer_name('__Gundo__')
  370. return
  371. else:
  372. target_state = int(target_state)
  373. _goto_window_for_buffer(vim.eval('g:gundo_target_n'))
  374. nodes, nmap = make_nodes()
  375. current = changenr(nodes)
  376. node_after = nmap[target_state]
  377. node_before = node_after.parent
  378. vim.command('call s:GundoOpenPreview()')
  379. _output_preview_text(_generate_preview_diff(current, node_before, node_after))
  380. _goto_window_for_buffer_name('__Gundo__')
  381. def GundoRenderChangePreview():
  382. if not _check_sanity():
  383. return
  384. target_state = vim.eval('s:GundoGetTargetState()')
  385. # Check that there's an undo state. There may not be if we're talking about
  386. # a buffer with no changes yet.
  387. if target_state == None:
  388. _goto_window_for_buffer_name('__Gundo__')
  389. return
  390. else:
  391. target_state = int(target_state)
  392. _goto_window_for_buffer(vim.eval('g:gundo_target_n'))
  393. nodes, nmap = make_nodes()
  394. current = changenr(nodes)
  395. node_after = nmap[target_state]
  396. node_before = nmap[current]
  397. vim.command('call s:GundoOpenPreview()')
  398. _output_preview_text(_generate_change_preview_diff(current, node_before, node_after))
  399. _goto_window_for_buffer_name('__Gundo__')
  400. # Gundo undo/redo
  401. def GundoRevert():
  402. if not _check_sanity():
  403. return
  404. target_n = int(vim.eval('s:GundoGetTargetState()'))
  405. back = vim.eval('g:gundo_target_n')
  406. _goto_window_for_buffer(back)
  407. _undo_to(target_n)
  408. vim.command('GundoRenderGraph')
  409. _goto_window_for_buffer(back)
  410. if int(vim.eval('g:gundo_close_on_revert')):
  411. vim.command('GundoToggle')
  412. def GundoPlayTo():
  413. if not _check_sanity():
  414. return
  415. target_n = int(vim.eval('s:GundoGetTargetState()'))
  416. back = int(vim.eval('g:gundo_target_n'))
  417. vim.command('echo "%s"' % back)
  418. _goto_window_for_buffer(back)
  419. normal('zR')
  420. nodes, nmap = make_nodes()
  421. start = nmap[changenr(nodes)]
  422. end = nmap[target_n]
  423. def _walk_branch(origin, dest):
  424. rev = origin.n < dest.n
  425. nodes = []
  426. if origin.n > dest.n:
  427. current, final = origin, dest
  428. else:
  429. current, final = dest, origin
  430. while current.n >= final.n:
  431. if current.n == final.n:
  432. break
  433. nodes.append(current)
  434. current = current.parent
  435. else:
  436. return None
  437. nodes.append(current)
  438. if rev:
  439. return reversed(nodes)
  440. else:
  441. return nodes
  442. branch = _walk_branch(start, end)
  443. if not branch:
  444. vim.command('unsilent echo "No path to that node from here!"')
  445. return
  446. for node in branch:
  447. _undo_to(node.n)
  448. vim.command('GundoRenderGraph')
  449. normal('zz')
  450. _goto_window_for_buffer(back)
  451. vim.command('redraw')
  452. vim.command('sleep 60m')
  453. def initPythonModule():
  454. if sys.version_info[:2] < (2, 4):
  455. vim.command('let s:has_supported_python = 0')