diff --git a/ftplugin/python/ipy.vim b/ftplugin/python/ipy.vim index 27d918c..b9b9631 100644 --- a/ftplugin/python/ipy.vim +++ b/ftplugin/python/ipy.vim @@ -188,6 +188,10 @@ def get_doc_buffer(level=0): #vim.command('pcl') #vim.command('pedit doc') #vim.command('normal ') # go to previous window + # "rest" is the syntax of python documentation. Stock vim doesn't have + # a syntax file for rest, but if the user has installed one then this will + # cause the docs to be colorized. + vim.command('setlocal syntax=rest') def update_subchannel_msgs(debug=False): msgs = km.sub_channel.get_msgs() @@ -419,10 +423,10 @@ if !exists('g:ipy_perform_mappings') let g:ipy_perform_mappings = 1 endif if g:ipy_perform_mappings != 0 - map :python run_this_file() - map :python run_this_line() + map IPyRunFile + map IPyRunLine map :python run_these_lines() - map d :py get_doc_buffer() + map d IPyDocWord map s :py update_subchannel_msgs(); echo("vim-ipython shell updated",'Operator') map :python toggle_reselect() "map :python send('%pdb') @@ -439,10 +443,17 @@ if g:ipy_perform_mappings != 0 "" Example of how to quickly close all figures with a keystroke "map :python run_command("plt.close('all')") + " p runs command from a visual selection or a motion. + " For example, piw to execute a word. + vmap p IPyRunSel + nmap p IPyRunSel + " pp runs one line (analogous to e.g. "dd") + nmap pp IPyRunLine + "pi custom - map :python run_this_file() - map :python run_this_line() - imap :python run_this_line() + map IpyRunFile + map IPyRunLine + imap IPyRunLine map :python dedent_run_this_line() vmap :python run_these_lines() vmap :python dedent_run_these_lines() @@ -452,6 +463,15 @@ if g:ipy_perform_mappings != 0 vmap :s/^\([ \t]*\)#/\1/ endif +nnoremap IPyRunFile :py run_this_file() +noremap IPyRunLine :python run_this_line() +inoremap IPyRunLine :python run_this_line() +" See: http://learnvimscriptthehardway.stevelosh.com/chapters/34.html +" And: :help map +vnoremap IPyRunSel :call IPythonRunOp(visualmode(), 1) +nnoremap IPyRunSel :set operatorfunc=IPythonRunOpg@ +nnoremap IPyDocWord :py get_doc_buffer() + command! -nargs=* IPython :py km_from_string("") command! -nargs=0 IPythonClipboard :py km_from_string(vim.eval('@+')) command! -nargs=0 IPythonXSelection :py km_from_string(vim.eval('@*')) @@ -519,3 +539,30 @@ endpython endif endfun set completefunc=CompleteIPython + +" Only create function if it doesn't already exist. It isn't sufficient to +" just use "function!" since the function may already be referenced by +" operatorfunc, and vim doesn't allow redefining the function in that case. +if !exists('*s:IPythonRunOp') +function s:IPythonRunOp(type, ...) + let saved_unnamed_register = @@ + + if a:0 " Invoked from Visual mode, use '< and '> marks. + exe "silent normal! `<" . a:type . "`>y" + elseif a:type ==# 'char' + silent normal! `[v`]y + elseif a:type == 'line' + silent normal! '[V']y + elseif a:type == 'block' + silent normal! `[\`]y + else + echo "error: type=".a:type." a:0=".a:0 + return + endif + + "echo "0: ".a:0." t: ".a:type." @: ".@@ + python run_command(vim.eval('@@')) + + let @@ = saved_unnamed_register +endfunction +endif