From 73f00ea79151907af6b28c4ad285949fbce6f4ac Mon Sep 17 00:00:00 2001 From: Dan Stahlke Date: Sat, 28 Jan 2012 21:29:17 -0500 Subject: [PATCH 1/2] mappings; execute with motion --- ftplugin/python/ipy.vim | 55 ++++++++++++++++++++++++++++++++++++----- 1 file changed, 49 insertions(+), 6 deletions(-) diff --git a/ftplugin/python/ipy.vim b/ftplugin/python/ipy.vim index 27d918c..b414fb8 100644 --- a/ftplugin/python/ipy.vim +++ b/ftplugin/python/ipy.vim @@ -419,10 +419,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 +439,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 +459,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 +535,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 From 99811abc0d01cc4de179c305c12f2647790794b6 Mon Sep 17 00:00:00 2001 From: Dan Stahlke Date: Sun, 29 Jan 2012 13:31:23 -0500 Subject: [PATCH 2/2] set syntax for doc window --- ftplugin/python/ipy.vim | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/ftplugin/python/ipy.vim b/ftplugin/python/ipy.vim index b414fb8..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()