Skip to content

Commit 57aaea5

Browse files
committed
Add pymode#Execute function to wrap calls to python interpreter. Extract setting up sys.path responsibility into a separate autoload module. Modify run.vim to support pymode#Execute.
1 parent 5eb0022 commit 57aaea5

File tree

5 files changed

+39
-25
lines changed

5 files changed

+39
-25
lines changed

autoload/pymode.vim

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@ fun! pymode#Default(name, default) "{{{
1212
endfunction "}}}
1313

1414

15+
fun! pymode#Execute(expression) "{{{
16+
" DESC: Execute an expression in the default python interpreter
17+
"
18+
execute 'python '.a:expression
19+
endfunction "}}}
20+
21+
1522
fun! pymode#Option(name) "{{{
1623

1724
let name = 'b:pymode_' . a:name

autoload/pymode/path.vim

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
fun! pymode#path#Activate(plugin_root) "{{{
2+
3+
python << EOF
4+
import sys, vim, os
5+
6+
curpath = vim.eval("getcwd()")
7+
libpath = os.path.join(vim.eval("a:plugin_root"), 'pylibs')
8+
9+
sys.path = [libpath, curpath] + vim.eval("g:pymode_paths") + sys.path
10+
EOF
11+
12+
endfunction "}}}

autoload/pymode/run.vim

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,24 @@ fun! pymode#run#Run(line1, line2) "{{{
88
return 0
99
endtry
1010
endif
11-
py import StringIO
12-
py sys.stdout, stdout_ = StringIO.StringIO(), sys.stdout
13-
py sys.stderr, stderr_ = StringIO.StringIO(), sys.stderr
14-
py enc = vim.eval('&enc')
11+
call pymode#Execute("import StringIO")
12+
call pymode#Execute("sys.stdout, stdout_ = StringIO.StringIO(), sys.stdout")
13+
call pymode#Execute("sys.stderr, stderr_ = StringIO.StringIO(), sys.stderr")
14+
call pymode#Execute("enc = vim.eval('&enc')")
1515
call setqflist([])
1616
call pymode#WideMessage("Code running.")
1717
try
18-
py context = globals()
19-
py context['raw_input'] = context['input'] = lambda s: vim.eval('input("{0}")'.format(s))
20-
py execfile(vim.eval('expand("%:p")'), context)
21-
py out, err = sys.stdout.getvalue().strip(), sys.stderr.getvalue()
22-
py sys.stdout, sys.stderr = stdout_, stderr_
18+
call pymode#Execute("context = globals()")
19+
call pymode#Execute("context['raw_input'] = context['input'] = lambda s: vim.eval('input(\"{0}\")'.format(s))")
20+
call pymode#Execute("execfile(vim.eval('expand(\"%:p\")'), context)")
21+
call pymode#Execute("out, err = sys.stdout.getvalue().strip(), sys.stderr.getvalue()")
22+
call pymode#Execute("sys.stdout, sys.stderr = stdout_, stderr_")
2323

2424
cexpr ""
25-
py for x in err.strip().split('\n'): vim.command('caddexpr "' + x.replace('"', r'\"') + '"')
25+
python << EOF
26+
for x in err.strip().split('\n'):
27+
vim.command('caddexpr "' + x.replace('"', r'\"') + '"')
28+
EOF
2629
let l:oldefm = &efm
2730
set efm=%C\ %.%#,%A\ \ File\ \"%f\"\\,\ line\ %l%.%#,%Z%[%^\ ]%\\@=%m
2831
call pymode#QuickfixOpen(0, g:pymode_lint_hold, g:pymode_lint_maxheight, g:pymode_lint_minheight, 0)

ftplugin/python/init-pymode.vim

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,7 @@ endif
4242
if !pymode#Default('g:pymode_path', 1) || g:pymode_path
4343

4444
call pymode#Default('g:pymode_paths', [])
45-
46-
python << EOF
47-
import sys, vim, os
48-
49-
curpath = vim.eval("getcwd()")
50-
libpath = os.path.join(vim.eval("expand('<sfile>:p:h:h:h')"), 'pylibs')
51-
52-
sys.path = [libpath, curpath] + vim.eval("g:pymode_paths") + sys.path
53-
EOF
45+
call pymode#path#Activate(expand("<sfile>:p:h:h:h"))
5446

5547
endif " }}}
5648

@@ -130,9 +122,9 @@ if !pymode#Default("g:pymode_lint", 1) || g:pymode_lint
130122
let g:pymode_lint_config = expand("<sfile>:p:h:h:h") . "/pylint.ini"
131123
endif
132124

133-
py from pymode import queue
125+
call pymode#Execute("from pymode import queue")
134126

135-
au VimLeavePre * py queue.stop_queue()
127+
au VimLeavePre * call pymode#Execute("queue.stop_queue()")
136128

137129
endif
138130

@@ -252,7 +244,7 @@ if !pymode#Default("g:pymode_rope", 1) || g:pymode_rope
252244
call pymode#Default("g:pymode_rope_always_show_complete_menu", 0)
253245

254246
" DESC: Init Rope
255-
py import ropevim
247+
call pymode#Execute("import ropevim")
256248

257249
fun! RopeCodeAssistInsertMode() "{{{
258250
call RopeCodeAssist()
@@ -263,7 +255,7 @@ if !pymode#Default("g:pymode_rope", 1) || g:pymode_rope
263255
if isdirectory(getcwd() . '/.ropeproject')
264256
" In order to pass it the quiet kwarg I need to open the project
265257
" using python and not vim, which should be no major issue
266-
py ropevim._interface.open_project(quiet=True)
258+
call pymode#Execute("ropevim._interface.open_project(quiet=True)")
267259
return ""
268260
endif
269261
endfunction "}}}
@@ -275,7 +267,7 @@ if !pymode#Default("g:pymode_rope", 1) || g:pymode_rope
275267

276268
fun! RopeOmni(findstart, base) "{{{
277269
if a:findstart
278-
py ropevim._interface._find_start()
270+
call pymode#Execute("ropevim._interface._find_start()")
279271
return g:pymode_offset
280272
else
281273
call RopeOmniComplete()

ftplugin/python/pymode.vim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ if pymode#Option('lint')
7676
" DESC: Run queue
7777
let &l:updatetime = g:pymode_updatetime
7878
au CursorHold <buffer> call pymode#queue#Poll()
79-
au BufLeave <buffer> py queue.stop_queue()
79+
au BufLeave <buffer> call pymode#Execute("queue.stop_queue()")
8080

8181
endif
8282

0 commit comments

Comments
 (0)