Commits

ZyX_I committed b738d36

Added unloadplugin function

Comments (0)

Files changed (3)

autoload/frawor.vim

                 \         '    return eval(a:expr)\n'.
                 \         "endfunction\"\n".
                 \"let s:g={}\n".
-                \"let s:F={}\n".
+                \"let s:F={'_functions': {}}\n".
                 \"let s:g._pluginloaded=".oneload."\n".
                 \((exists('*FraworRegister'))?
                 \   (""):
         latin letters and underscores). {fopts} is a dictionary with the 
         following keys (all are optional, but feature without any keys is 
         useless):
-        Key     Description ~
-        cons    Reference to a function that will take |frawor-t-plugdict| as 
-                its first argument and, possibly, some additional arguments. It 
-                is just a normal function that can do anything related to the 
-                plugin that uses frawor. When plugin is registered, function 
-                that is a wrapper to this function will be added to s:F._frawor 
-                dictionary.
-        load    Reference to a function that will take |frawor-t-plugdict| as 
-                its first argument. This function will be called only once for 
-                each plugin that depends on the plugin that registers this 
-                feature. Function will be called when dependent plugin is 
-                loaded. If value returned by this function is not 0, then it 
-                will be assigned to plugdict.features[{fid}]
-        unload  Reference to a function that will take |frawor-t-plugdict| as 
-                its first argument and plugdict.features[{fid}] as a second. 
-                This function will be called only once for each plugin that 
-                depends on the plugin that registers this feature. Function will 
-                be called when dependent plugin is unloaded.
-        init    Any value. Deep copy (|deepcopy()|) of it will be assigned to 
-                plugdict.features[{fid}]. Note that if `load' key is present, 
-                then this key will be ignored.
+        Key        Description ~
+        cons       Reference to a function that will take |frawor-t-plugdict| as 
+                   its first argument and, possibly, some additional arguments. 
+                   It is just a normal function that can do anything related to 
+                   the plugin that uses frawor. When plugin is registered, 
+                   function that is a wrapper to this function will be added to 
+                   s:F._frawor dictionary.
+        load       Reference to a function that will take |frawor-t-plugdict| as 
+                   its first argument. This function will be called only once 
+                   for each plugin that depends on the plugin that registers 
+                   this feature. Function will be called when dependent plugin 
+                   is loaded. If value returned by this function is not 0, then 
+                   it will be assigned to plugdict.features[{fid}]
+        unload     Reference to a function that will take |frawor-t-plugdict| as
+                   its first argument. This function will be called only once 
+                   for each plugin that depends on the plugin that registers 
+                   this feature. Function will be called when dependent plugin 
+                   is unloaded.
+        unloadpre  Just like unload, but function will be called when plugin is 
+                   queued for unloading, but nothing was yet unloaded.
+        init       Any value. Deep copy (|deepcopy()|) of it will be assigned to 
+                   plugdict.features[{fid}]. Note that if `load' key is present, 
+                   then this key will be ignored.
 
 ==============================================================================
 5. Type definitions                                             *frawor-types*

plugin/frawor.vim

                 \                'вы пытались создать ссылку на внутренную '.
                 \                'функцию дополнения без раскрытия «s:» в '.
                 \                '«<SNR>_{N}»?)',
+                \  'unotloaded': 'Невозможно выгрузить незагруженное '.
+                \                'дополнение %s',
             \}
 else
     let s:g._messages={
                 \                'callable (perhaps you tried to create '.
                 \                'a reference to a script-local function '.
                 \                'without replacing `s:'' with `<SNR>_{N})''?)',
+                \  'unotloaded': 'Unable to unload plugin %s that '.
+                \                'is not loaded',
             \}
 endif
 "▶1 s:Eval
     endif
     return 1
 endfunction
+"▶1 getdeps
+function s:F.getdeps(plugdict, hasdep)
+    let r=[a:plugdict]
+    for dplugdict in map(keys(a:plugdict.dependencies), 's:g.pls[v:val]')
+        if !has_key(a:hasdep, dplugdict.id)
+            let a:hasdep[dplugdict.id]=1
+            let r+=s:F.getdeps(dplugdict, a:hasdep)
+        endif
+    endfor
+    return r
+endfunction
+"▶1 depcomp
+function s:DepComp(plugdict1, plugdict2)
+    let depnum1=len(keys(get(s:g.dependents, a:plugdict1.id, {})))
+    let depnum2=len(keys(get(s:g.dependents, a:plugdict2.id, {})))
+    return ((depnum1>depnum2)?
+                \(1):
+                \((depnum1<depnum2)?
+                \   (-1):
+                \   (0)))
+endfunction
+let s:F._functions['s:DepComp']=function('s:DepComp')
+"▶1 getorderegdeps
+function s:F.getorderegdeps(plugdict)
+    let deps=sort(s:F.getdeps(a:plugdict, {}), s:F._functions['s:DepComp'])
+    let ordered=[]
+    let withdeps=[]
+    call map(deps, 'add(((empty(get(s:g.dependents, v:val.id, {})))?'.
+                \           '(ordered):'.
+                \           '(withdeps)), '.
+                \      'dplugdict)')
+    let orderedids=map(copy(ordered), 'v:val.id')
+    unlet deps
+    while !empty(withdeps)
+        let removedsmth=0
+        let i=0
+        while i<len(withdeps)
+            if empty(filter(keys(get(s:g.dependents, withdeps[i].id, {})),
+                        \   'index(orderedids, v:val)==-1'))
+                call add(ordered,    withdeps[i])
+                call add(orderedids, withdeps[i].id)
+                call remove(withdeps, i)
+                let removedsmth=1
+            else
+                let i+=1
+            endif
+        endwhile
+        if !removedsmth && !empty(withdeps)
+            call add(ordered,    withdeps[0])
+            call add(orderedids, withdeps[0].id)
+            call remove(withdeps, 0)
+        endif
+    endwhile
+    return ordered
+endfunction
+"▶1 runfeatures
+function s:F.runfeatures(plugdict, key)
+    for feature in values(s:g.features)
+        if has_key(feature, a:key) && has_key(a:plugdict.dependencies,
+                    \                         feature.plid)
+            call feature[a:key](a:plugdict)
+        endif
+    endfor
+    " XXX required in order not to copy list
+    return a:plugdict
+endfunction
+"▶1 unloadplugin
+function s:F.unloadplugin(plid)
+    let plugdict=s:g.pls[a:plid]
+    if plugdict.status!=0
+        let ordered=s:F.getorderegdeps(plugdict)
+        let tosource=map(reverse(copy(ordered)), 'v:val.file')
+        " XXX runfeature returns v:val
+        call map(ordered, 's:F.runfeatures(v:val, "unloadpre")')
+        for plugdict in ordered
+            call s:F.runfeatures(plugdict, "unload")
+            unlockvar plugdict.g
+            call map(keys(plugdict.g), 'remove(plugdict.g, v:val)')
+            unlockvar plugdict.F
+            call map(keys(plugdict.F), 'remove(plugdict.F, v:val)')
+            unlockvar plugdict.status
+            let plugdict.status=0
+            unlet s:g.pls[plugdict.id]
+        endfor
+        return tosource
+    else
+        return []
+    endif
+endfunction
 "▶1 FraworRegister
 function FraworRegister(...)
     return call(s:F.newplugin, a:000, {})
 endfunction
+let s:F._functions.FraworRegister=function('FraworRegister')
 "▶1 isfunc
 function s:F.isfunc(Func, msgpref, fid, plid)
     if type(a:Func)!=2
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.