util.py 949 B

12345678910111213141516171819202122232425262728293031323334
  1. # import vim
  2. normal = lambda s: vim().command('normal %s' % s)
  3. normal_silent = lambda s: vim().command('silent! normal %s' % s)
  4. def vim():
  5. """ call Vim.
  6. This is wrapped so that it can easily be mocked.
  7. """
  8. import vim
  9. return vim
  10. def _goto_window_for_buffer(b):
  11. w = int(vim().eval('bufwinnr(%d)' % int(b)))
  12. vim().command('%dwincmd w' % int(w))
  13. def _goto_window_for_buffer_name(bn):
  14. b = vim().eval('bufnr("%s")' % bn)
  15. return _goto_window_for_buffer(b)
  16. # Rendering utility functions
  17. def _output_preview_text(lines):
  18. _goto_window_for_buffer_name('__Mundo_Preview__')
  19. vim().command('setlocal modifiable')
  20. vim().current.buffer[:] = [line.rstrip() for line in lines]
  21. vim().command('setlocal nomodifiable')
  22. def _undo_to(n):
  23. n = int(n)
  24. if n == 0:
  25. vim().command('silent earlier %s' % (int(vim().eval('&undolevels')) + 1))
  26. else:
  27. vim().command('silent undo %d' % int(n))