Skip to content

Instantly share code, notes, and snippets.

@somian
Last active January 2, 2016 17:19
Show Gist options
  • Save somian/8335596 to your computer and use it in GitHub Desktop.
Save somian/8335596 to your computer and use it in GitHub Desktop.
A simple but usable skeleton example of a (conservative, error-checking) "local python module code file loader" in Vim Script (vimL)
"^-^" vim: set filetype=vim et sw=4
" This file contains free software: you can redistribute it and/or modify
" it under the terms of the GNU General Public License as published by
" the Free Software Foundation, either version 3 of the License, or
" (at your option) any later version.
"
" The code herein is published informally in the hope that it will be useful,
" but WITHOUT ANY WARRANTY; without even the implied warranty of
" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
" GNU General Public License for more details.
"
" You should have received a copy of the GNU General Public License
" along with this file if this file is part of a published or released
" software distribution. If not, see <http://www.gnu.org/licenses/>.
let s:scriptdir = expand('<sfile>:p:h')
let s:scriptf = expand('<sfile>:t')
if has('python')
funct! PyGrab(pycodefile)
let l:pycodefile = a:pycodefile
:python << .endscript
import vim
import os
import sys
import runpy
this_script_dir = vim.eval(r's:scriptdir')
this_script_file = vim.eval(r's:scriptf')
def vecho(stri) :
vim.command('echohl SpellBad')
vim.command('echo "In py code, in [%s], in vimf %s: %s"' % (__name__, this_script_file, stri))
vim.command('echohl None')
return None
pyc = vim.eval('l:pycodefile')
if sys.path[0] == "" : pass
elif sys.path[0] == this_script_dir : pass
else : sys.path.insert(0, this_script_dir)
if not os.access(pyscriptdir + '/' + pyc , os.R_OK) :
vecho('Cannot find or do not have read permissions for %s.' % pyc)
raise IOError
try :
pcdict = runpy.run_path(this_script_dir + '/' + pyc)
except ImportError :
vecho("Import of Python code in %s failed. Terminating execution." % pyc)
raise
# put your code here. this is a contrived, trivial example.
for ui in pcdict :
print str(ui)
.endscript
endfun
endif " has('python')
" Last modified: Thu 09 Jan 2014 10:04:39 AM EST
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment