Commits

ZyX_I committed f028583

@/functions: Added on-load function creation

  • Participants
  • Parent commits a6a3be9

Comments (0)

Files changed (5)

File doc/frawor.txt

         plugin-local function definition). This feature will throw an error if 
         function is already defined. {funcdescr} is described in 
         |frawor-t-funcdescr|.
+        Note that if plugin is not loaded yet then instead of creating 
+        function it will define |FuncUndefined| event that will load the 
+        plugin and create requested function.
                                                            *frawor-f-wrapfunc*
 wrapfunc : function ({funcdescr})
          + unload

File plugin/frawor/functions.vim

 "▶1 Header
 scriptencoding utf-8
-execute frawor#Setup('0.0', {'@/decorators': '0.0'}, 1)
+execute frawor#Setup('0.0', {'@/decorators': '0.0',
+            \              '@/autocommands': '0.0',}, 1)
 "▶1 _messages
 if v:lang=~?'ru'
     let s:_messages={
         endif
     endfor
 endfunction
+"▶1 loadplugin      :: fdict → + fdict, …
+function s:F.loadplugin(plid, fdict)
+    call FraworLoad(a:plid)
+    call s:_f.augroup.del(a:fdict.augname)
+    for wfopts in a:fdict.delayed
+        let fundef=call(s:F.wrapfunc, wfopts, {})
+        let a:fdict.fundicts[fundef.id]=fundef
+    endfor
+    unlet a:fdict.augname a:fdict.delayed
+endfunction
 "▶1 addfunctions    :: {f}, functions::{fname: funopts} → + fdict, …
 let s:extfunctions={'nextid': 0}
+let s:lastdid=0
 function s:F.addfunctions(plugdict, fdict, functions)
     "▶2 Check a:functions
     if type(a:functions)!=type({})
         "▶2 Replace s: prefix with <SNR>{SID}_
         let fstr=s:F.rewritefname(a:plugdict.sid, fname)
         "▶2 Check function name
-        if fname!~#'^\%(\h:\w\+\|[A-Z_]\w*\)$'
+        if fname!~#'\v^%((h:|\<SNR\>\d+_)\w+|[A-Z_]\w*)$'
             call s:_f.throw('invfname', fname, a:plugdict.id)
         "▶2 Throw an error or redefine existing function
         elseif exists('*'.fname)
             endif
         endif
         "▲2
-        let fundef=s:F.wrapfunc(a:plugdict, d.funopts, a:fdict.fundictsname,
-                    \           fname)
-        let a:fdict.fundicts[fundef.id]=fundef
+        let wfopts=[a:plugdict, d.funopts, a:fdict.fundictsname, fname]
+        if a:plugdict.status==2
+            let fundef=call(s:F.wrapfunc, wfopts, {})
+            let a:fdict.fundicts[fundef.id]=fundef
+        else
+            if !has_key(a:fdict, 'delayed')
+                let a:fdict.delayed=[]
+                let a:fdict.augname=printf('FraworFunctionLoad_%x', s:lastdid)
+                let s:lastdid+=1
+            endif
+            let a:fdict.delayed+=[wfopts]
+            let fpattern=substitute(fname, '\c\m^<SNR>', '*', '')
+            call s:_f.augroup.add(a:fdict.augname,
+                        \         [['FuncUndefined', fpattern, 0,
+                        \           [s:F.loadplugin, a:plugdict.id, a:fdict]]])
+        endif
     endfor
 endfunction
 call s:_f.newfeature('addextfunctions', {'cons': s:F.addfunctions,
             \                     'init': {'fundictsname': 'wrappedfuncs',
             \                                  'fundicts': {}}})
 "▶1
-call frawor#Lockvar(s:, 'extfunctions,wrappedfuncs')
+call frawor#Lockvar(s:, 'extfunctions,wrappedfuncs,lastdid')
 " vim: fmr=▶,▲ sw=4 ts=4 sts=4 et tw=80

File test/rtp/plugin/wrapfunc-twoload.vim

+if !exists('s:_pluginloaded')
+    execute frawor#Setup('0.0', {'@/functions': '0.0'}, 0)
+    let s:rdict={}
+    call s:_f.addextfunctions({'Abc': s:rdict})
+    W: First load
+    finish
+elseif s:_pluginloaded
+    W: Third load
+    finish
+endif
+W: Second load
+function s:rdict.function()
+    W$ Abc
+endfunction

File test/wrapfunc-twoload.in

+:let &rtp.=",".escape($TESTDIR, ',\').'/rtp'
+:let g:testfile="plugin/".g:curtest.".vim"
+:source test.vim
+:call Abc()

File test/wrapfunc-twoload.ok

+: First load
+: Second load
+$ Abc