Skip to content

Instantly share code, notes, and snippets.

@spacelis
Created November 15, 2012 21:11
Show Gist options
  • Save spacelis/4081300 to your computer and use it in GitHub Desktop.
Save spacelis/4081300 to your computer and use it in GitHub Desktop.
pymode-run
" DESC: Save file if it modified and run python code
fun! pymode#run#JumpToCurrentError() "{{{
let a:err = []
sil!substitute /File\s\"\(.*\)\"\zs/\=add(a:err, submatch(1))[1:0]/
sil!substitute /,\sline\s\(\d\+\)\zs/\=add(a:err,submatch(1))[1:0]/
wincmd k
if len(a:err) > 0
if filereadable(fnamemodify('.', ':p') . a:err[0])
exe "next " . fnamemodify('.', ':p') . a:err[0]
exe "normal " . a:err[1] . "gg"
elseif filereadable(a:err[0])
exe "next " . a:err[0]
exe "normal " . a:err[1] . "gg"
endif
endif
endfunction "}}}
fun! pymode#run#Run(line1, line2, ...) "{{{
if &modifiable && &modified | write | endif
py import subprocess
py import os
call pymode#WideMessage("Code running.")
let a:runtimeError = 0
try
py <<EOF
# Print stacktrace for exceptions at runtime
try:
try:
with open('project.pth') as projp:
paths = ':'.join([line.strip() for line in projp])
except:
paths = ''
paths += ':' + os.environ['PYTHONPATH']
pyrun_file = vim.eval('expand("%s:p")')
pyrun_cmd = ['/usr/bin/env', 'PYTHONPATH=' + paths, 'python', pyrun_file]
pyrun_callargs = vim.eval('join(a:000, " ")').split()
pyrun_cmd.extend(pyrun_callargs)
pyrun_out = subprocess.check_output(pyrun_cmd, stderr=subprocess.STDOUT, shell=False)
except subprocess.CalledProcessError as e:
pyrun_out = e.output + '\n ---RETURN---: ' + str(e.returncode)
vim.eval('throw "Runtime Error"')
EOF
catch /.*/
let a:runtimeError = 1
finally
call pymode#WideMessage("")
call pymode#TempBuffer()
py vim.current.buffer.append(pyrun_out.split('\n'), 0)
if a:runtimeError
echohl Error
echo "Run-time error."
echohl none
syntax clear
syntax match PymodeRuntimeError '^.*Error'
syntax match PymodeRuntimeErrorFile '\(File\s\)\@<=".*\.py"'
syntax match PymodeRuntimeErrorFunction '\(, in \)\@<=.*$'
map <silent> <buffer> <CR> :call pymode#run#JumpToCurrentError()<CR>
endif
wincmd p
endtry
endfunction "}}}
hi def link PymodeRuntimeError Constant
hi def link PymodeRuntimeErrorFile String
hi def link PymodeRuntimeErrorFunction Function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment