1. ZyX_I
  2. frawor

Commits

ZyX_I  committed 34d9bea

Added `redefine' fdef key

  • Participants
  • Parent commits b71d51f
  • Branches default

Comments (0)

Files changed (2)

File doc/frawor.txt

View file
  • Ignore whitespace
                    Each list in a list that is passed as `altervars' option is 
                    copied, but no |deepcopy()| is performed, so you may alter 
                    variable {value}s if they are |Lists| or |Dictionaries|.
+        redefine   If this key is present, then it will try to redefine 
+                   function if it exists, if it is not and function with given 
+                   name exists or |:delfunction| threw |E131| (function is in 
+                   use) error, then `fdef' exception is thrown.
 
 ==============================================================================
 5. Type definitions                                             *frawor-types*

File plugin/frawor/functions.vim

View file
  • Ignore whitespace
                 \           'v:val')
     call extend(s:g._messages, map({
                 \'foptsnotdict': 'описание функции не является словарём',
-                \        'fdef': 'функция уже определена в дополнении %s',
+                \        'fdef': 'функция уже определена',
                 \      'nofunc': 'описание функции не содержит '.
                 \                'ключа «functions»',
                 \        'nref': 'ключ «%s» не является ссылкой на функцию',
                 \           'v:val')
     call extend(s:g._messages, map({
                 \'foptsnotdict': 'function description must be a Dictionary',
-                \        'fdef': 'function was already defined by plugin %s',
+                \        'fdef': 'function was already defined',
                 \      'nofunc': 'function description lacks '.
                 \                '`function'' key',
                 \        'nref': 'key `%s'' is not a function reference',
     try
         execute 'delfunction '.fstr
     catch /^Vim(delfunction):E130:/
-        " Ignore «Unknown function» error
+        " «Unknown function» error
+        return 2
     catch /^Vim(delfunction):E131:/
         " Normally you should catch this error for FraworUnload function, so 
         " it has bang
+        return 0
     endtry
+    return 1
 endfunction
 "▶1 delfunctions
 function s:F.delfunctions(plugdict)
     elseif !has_key(a:fopts, 'function')
         call s:F._frawor.throw('nofunc', a:fname, a:plugdict.id)
     elseif type(a:fopts.function)!=2
+        call s:F._frawor.throw('nref', a:fname, a:plugdict.id, 'functions')
     endif
     "▲2
-    "▶2 Replace s: prefix with <SNR>{SID}_
     let fname=a:fname
-    if fname[:1]==#'s:'
-        let fname=printf(s:g.sidfpref, a:plugdict.sid).fname[2:]
-    endif
-    "▲2
     let fdicts=eval(a:fdictsname)
     let fdef  =   {"id":   fname,
                 \"plid": a:plugdict.id}
     return fdef
 endfunction
 "▶1 addextfunctions
-let s:g.functions={}
 function s:F.addextfunctions(plugdict, functions)
+    "▶2 Check a:function
     if type(a:function)!=type({})
         call s:F._frawor.throw('fnotdict', a:plugdict.id)
     endif
+    "▲2
     let d={}
     for [fname, d.fopts] in items(a:function)
+        "▶2 Replace s: prefix with <SNR>{SID}_
+        if fname[:1]==#'s:'
+            let fname=printf(s:g.sidfpref, a:plugdict.sid).fname[2:]
+        endif
         "▶2 Check function name
         if fname!~#'^\%(\h:\w\+\|[A-Z_]\w*\)$'
-            call s:F._frawor.throw('invfname', a:plugdict.id, fname)
-        elseif has_key(s:g.functions, fname)
-            call s:F._frawor.throw('fdef', fname, a:plugdict.id,
-                        \                  fdicts[fname].plid)
+            call s:F._frawor.throw('invfname', fname, a:plugdict.id)
+        "▶2 Throw an error or redefine existing function
+        elseif exists('*'.fname)
+            if !has_key(d.fopts, 'redefine')
+                call s:F._frawor.throw('fdef', fname, a:plugdict.id)
+            else
+                if !s:F.delfunction(a:plugdict.sid, function(fname))
+                    call s:F._frawor.throw('fdef', fname, a:plugdict.id)
+                endif
+            endif
         endif
         "▲2
         call s:F.wrapfunc(a:plugdict, d.fopts,