diff --git a/ftplugin/python/ipy.vim b/ftplugin/python/ipy.vim index d9852d3..61d7498 100644 --- a/ftplugin/python/ipy.vim +++ b/ftplugin/python/ipy.vim @@ -168,6 +168,8 @@ noremap (IPython-ToggleSendOnSave) :call toggle_send_on_save() noremap (IPython-PlotClearCurrent) :Python2or3 run_command("plt.clf()") noremap (IPython-PlotCloseAll) :Python2or3 run_command("plt.close('all')") noremap (IPython-RunLineAsTopLevel) :Python2or3 dedent_run_this_line() +noremap (IPython-RunTextObj) :set opfunc=opfuncg@ +noremap (IPython-RunCell) :set opfunc=opfuncg@ap function! s:DoMappings() let b:did_ipython = 1 @@ -177,6 +179,7 @@ function! s:DoMappings() map g (IPython-ImportFile) endif " map (IPython-RunLine) + map (IPython-RunTextObj) map (IPython-RunLines) map ,d (IPython-OpenPyDoc) map (IPython-UpdateShell) @@ -434,3 +437,43 @@ function! GreedyCompleteIPython(findstart, base) return IPythonCmdComplete(a:base, a:base, len(a:base), 1) endif endfunction + +function! s:opfunc(type) + " Originally from tpope/vim-scriptease + let sel_save = &selection + let cb_save = &clipboard + let reg_save = @@ + let left_save = getpos("'<") + let right_save = getpos("'>") + let vimode_save = visualmode() + try + set selection=inclusive clipboard-=unnamed clipboard-=unnamedplus + if a:type =~ '^\d\+$' + silent exe 'normal! ^v'.a:type.'$hy' + elseif a:type =~# '^.$' + silent exe "normal! `<" . a:type . "`>y" + elseif a:type ==# 'line' + silent exe "normal! '[V']y" + elseif a:type ==# 'block' + silent exe "normal! `[\`]y" + elseif a:type ==# 'visual' + silent exe "normal! gvy" + else + silent exe "normal! `[v`]y" + endif + redraw + let l:cmd = @@ + finally + let @@ = reg_save + let &selection = sel_save + let &clipboard = cb_save + exe "normal! " . vimode_save . "\" + call setpos("'<", left_save) + call setpos("'>", right_save) + endtry +Python2or3 << EOF +import textwrap +import vim +run_command(textwrap.dedent(vim.eval('l:cmd'))) +EOF +endfunction