christoomey / vim-tmux-runner

Vim and tmux, sittin' in a tree...
MIT License
291 stars 37 forks source link

Cell Mode #96

Closed AlirezaMorsali closed 4 years ago

AlirezaMorsali commented 4 years ago

I have been trying a few plugins for cell-mode and so far the best option for me was to use vim-tmux-runner sending lines to ipython. The way I use it is to select multiple lines and then by VtrSendLinesToRunner send them to ipython I think it could be very interesting if you could add a cell mode to this plugin. I found these functions in jupyter-vim plugin which will make it very easy to add this functionality.

`def is_cell_separator(line): """ Determine whether a given line is a cell separator """

TODO allow users to define their own cell separators

cell_sep = ('##', '#%%', '# %%', '# <codecell>')
return line.startswith(cell_sep)

def run_cell(): """Run all the code between two cell separators""" cur_buf = vim.current.buffer (cur_line, cur_col) = vim.current.window.cursor cur_line -= 1

# Search upwards for cell separator
upper_bound = cur_line
while upper_bound > 0 and not is_cell_separator(cur_buf[upper_bound]):
    upper_bound -= 1

# Skip past the first cell separator if it exists
if is_cell_separator(cur_buf[upper_bound]):
    upper_bound += 1

# Search downwards for cell separator
lower_bound = min(upper_bound+1, len(cur_buf)-1)

while lower_bound < len(cur_buf)-1 and \
        not is_cell_separator(cur_buf[lower_bound]):
    lower_bound += 1

# Move before the last cell separator if it exists
if is_cell_separator(cur_buf[lower_bound]):
    lower_bound -= 1

# Make sure bounds are within buffer limits
upper_bound = max(0, min(upper_bound, len(cur_buf)-1))
lower_bound = max(0, min(lower_bound, len(cur_buf)-1))

# Make sure of proper ordering of bounds
lower_bound = max(upper_bound, lower_bound)`
christoomey commented 4 years ago

Hi @alirezamors, thanks for sharing! I think the best option for adding any sort of functionality like this would be captured in https://github.com/christoomey/vim-tmux-runner/issues/61 (Add operator for sending text). As much as possible I'd like to keep the plugin from having any special behavior directly implemented that can't be implemented outside the plugin, and I think the text object approach could potential solve this problem in a more general way.

With that in mind, I'm going to close this issue as I don't expect there is any work to be done here (beyond what's captured in the other issue).