Skip to content

Commit

Permalink
Auto-convert character case for functions and allow private functions.
Browse files Browse the repository at this point in the history
Private functions are available in the current script only: they use
Vim's <SID> prefix.

This fixes both issues #1 and #3.
  • Loading branch information
nvie committed Jul 27, 2010
1 parent 5ea0fe9 commit 54a5a96
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
26 changes: 26 additions & 0 deletions examples/example6.vim
@@ -0,0 +1,26 @@
python << eop
import os
import vim
from vim_bridge import bridged

@bridged
def public():
return "I am public."

@bridged
def _private():
return "I am private (available in the current script only)."

@bridged
def my_name_is_auto_converted():
return "In Python, I'm called my_name_is_auto_converted, but in Vim, I'm called MyNameIsAutoConverted :)"

@bridged
def _long_private_name():
return "I'm private, and my case is converted automatically."
eop

echo Public()
echo s:Private()
echo MyNameIsAutoConverted()
echo s:LongPrivateName()
11 changes: 10 additions & 1 deletion vim_bridge/__init__.py
Expand Up @@ -5,6 +5,13 @@
VERSION = (0, 3)
__version__ = ".".join(map(str, VERSION[0:2]))

def _convert_function_name(fname):
private = fname.startswith('_')
if private:
fname = fname[1:]
fname = "".join([part.capitalize() for part in fname.split('_')])
return (private, fname)

def _get_arguments(func):
return func.func_code.co_varnames[:func.func_code.co_argcount]

Expand All @@ -14,7 +21,9 @@ def bridged(fin):

func_args = _get_arguments(fin)

lines = ['fun! %s(%s)' % (fin.func_name, ", ".join(func_args))]
private, vimname = _convert_function_name(fin.func_name)
private = private and "s:" or ""
lines = ['fun! %s%s(%s)' % (private, vimname, ", ".join(func_args))]
lines.append('python << endp')
for arg in func_args:
lines.append('__vim_bridge_%s = vim.eval("a:%s")' % (arg, arg))
Expand Down

0 comments on commit 54a5a96

Please sign in to comment.