Commits

ZyX_I  committed 996366c

Fixed a typo

  • Participants
  • Parent commits d30c585

Comments (0)

Files changed (2)

File autoload/frawor/python.vim

 "▶1 Header
 scriptencoding utf-8
-execute frawor#Setup('1.0', {'@/os': '0.0',
-            \         '@/resources': '0.0',})
+execute frawor#Setup('1.1', {'@/os': '0.0',
+            \         '@/resources': '0.0',
+            \            '@/python': '1.0',})
 "▶1 Define messages
 if v:lang=~?'ru'
     let s:_messages={
 "▶2 register feature
 call s:_f.newfeature('addpythonpath', {'register': s:F.addpydir,
             \                         'unloadpre': s:F.delpydir,})
+"▶1 FraworTypedCall
+for s:p in [''] " ['', '3']
+    " XXX Must go after newfeature call
+    execute 'python'.s:p 'import frawor'
+    execute 'python'.s:p 'frawor._ftc_name="<SNR>'.s:_sid.'_FraworTypedCall"'
+endfor
+unlet s:p
+function s:F.gettypes(v)
+    if type(a:v)==type({}) || type(a:v)==type([])
+        let r=map(copy(a:v), 's:F.gettypes(v:val)')
+        call map(a:v, 'type(v:val)==2 ? string(v:val)[10:-3] : v:val')
+        return r
+    else
+        return type(a:v)
+    endif
+endfunction
+function s:FraworTypedCall(...)
+    let r=deepcopy(call('call', a:000))
+    return [s:F.gettypes(r), r]
+endfunction
+let s:_functions+=['s:FraworTypedCall']
 "▶1
 call frawor#Lockvar(s:, 'addedpaths')
 " vim: fmr=▶,▲ sw=4 ts=4 sts=4 et tw=80

File python/frawor/__init__.py

+import vim
+import json
+
+if False and hasattr(vim, 'bindeval'):
+    def vim_get_func(f, rettype=None):
+        '''Return a vim function binding.'''
+        return vim.bindeval('function('+json.dumps(f)+')')
+else:
+    class VimFunc(object):
+        __slots__ = ('f',)
+
+        def __init__(self, f):
+            self.f = vim_dumps(f)
+
+        def __call__(self, *args, **kwargs):
+            vim_args = [self.f, vim_dumps(args)]
+            if 'self' in kwargs:
+                vim_args.append(vim_dumps(kwargs['self']))
+            types, r = vim.eval(_ftc_name+'('+','.join(vim_args)+')')
+            return _type_returned_value(types, r)
+
+        def __repr__(self):
+            return self.__class__.__name__+'('+self.f+')'
+
+    vim_get_func = VimFunc
+
+    TYPES = {
+        '0': int,
+        '2': VimFunc,
+        '5': float,
+    }
+    def _type_returned_value(types, r):
+        if type(types) is dict:
+            for k in types:
+                r[k] = _type_returned_value(types[k], r[k])
+        elif type(types) is list:
+            for i in range(len(types)):
+                r[i] = _type_returned_value(types[i], r[i])
+        elif types in TYPES:
+            r=TYPES[types](r)
+        return r
+
+    def nonutf_dumps(obj):
+        todump=[('dump', obj)]
+        r=''
+        while todump:
+            t, obj = todump.pop(0)
+            if t == 'inject':
+                r+=obj
+            else:
+                tobj=type(obj)
+                if tobj is int:
+                    r+=str(obj)
+                elif tobj is float:
+                    r += "%1.1e" % obj
+                elif tobj is list or tobj is tuple:
+                    r+='['
+                    todump.insert(0, ('inject', ']'))
+                    for value in reversed(obj):
+                        todump[:0]=[('dump', value), ('inject', ',')]
+                elif tobj is dict:
+                    r+='{'
+                    todump.insert(0, ('inject', '}'))
+                    for key, value in obj.items():
+                        todump[:0]=[('dump', key),
+                                    ('inject', ':'),
+                                    ('dump', value),
+                                    ('inject', ',')]
+                elif tobj is VimFunc:
+                    r+='function('+obj.f+')'
+                elif tobj is unicode:
+                    todump.insert(0, ('dump', tobj.encode(vim.eval('&encoding'))))
+                elif tobj is str  or  tobj is bytes:
+                    r+='"'+obj.replace('\\', '\\\\').replace('"', '\\"').replace('\n', '\\n')+'"'
+                else:
+                    raise TypeError('nonutf_dumps cannot serialize `'+tobj.__name__+'\'')
+        return r
+
+    utf_dumps = json.dumps
+
+    def vim_dumps(obj):
+        try:
+            return utf_dumps(obj)
+        except (UnicodeDecodeError, TypeError):
+            return _nonutf_dumps(obj)