christoomey / vim-tmux-runner

Vim and tmux, sittin' in a tree...

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Cell Mode

AlirezaMorsali opened this issue · comments

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 = ('##', '#%%', '# %%', '# ')
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)`

Hi @alirezamors, thanks for sharing! I think the best option for adding any sort of functionality like this would be captured in #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).