ivanov / vim-ipython

A two-way integration between Vim and IPython 0.11+
http://github.com/ivanov/vim-ipython
1.04k stars 163 forks source link

dedent_run_these_lines() should handle indentation less than shiftwidth (e.g., restructured text) #48

Open ariddell opened 11 years ago

ariddell commented 11 years ago

This is for the wishlist. It would be nice to be able to run code directly from restructured text documents where the initial indentation is 2 or 3. For example

This is some sphinx documentation with examples:

.. ipython:: python
   import os
   import matplotlib.pyplot as plt
   import numpy as np
   import scipy.stats

dedent_run_these_lines() will not work on this -- because the indentation is only three spaces. This is a quick hack I have to get it to work, if count == 0, then set count = 1 so it tries to indent. I'm sure there's a better way..

def dedent_run_these_lines():
    r = vim.current.range
    shiftwidth = vim.eval('&shiftwidth')
    count = int(vim.eval('indent(%d+1)/%s' % (r.start,shiftwidth)))
    if count == 0:
       count = 1
    if count > 0:
       vim.command("'<,'>" + "<"*count)
    run_these_lines()
    if count > 0:
       vim.command("silent undo")

Thanks!