Work around differing bind targets (due to addons etc)

Amber Yust [04-28-14 - 05:26]
Work around differing bind targets (due to addons etc)
Filename
Binder.lua
diff --git a/Binder.lua b/Binder.lua
index f675cb2..d5b0fef 100644
--- a/Binder.lua
+++ b/Binder.lua
@@ -96,21 +96,28 @@ end

 function Binder.RestoreBindingsFromTable()
     local bindCount = 0
+    local attemptedBindCount = 0
     local skippedBindCount = 0
     local maxBindings = GetMaxBindingsPerAction()
     for actionName, actionBindings in pairs(Binder.bindings) do
         local layerIndex, categoryIndex, actionIndex = GetActionIndicesFromName(actionName)
-        CallSecureProtected("UnbindAllKeysFromAction", layerIndex, categoryIndex, actionIndex)
-        for bindingIndex, bind in ipairs(actionBindings) do
-            if bindingIndex <= maxBindings then
-                CallSecureProtected("BindKeyToAction", layerIndex, categoryIndex, actionIndex, bindingIndex, bind["keyCode"], bind["mod1"], bind["mod2"], bind["mod3"], bind["mod4"])
-                bindCount = bindCount + 1
-            else
-                skippedBindCount = skippedBindCount + 1
+        if layerIndex and categoryIndex and actionIndex then
+            CallSecureProtected("UnbindAllKeysFromAction", layerIndex, categoryIndex, actionIndex)
+            for bindingIndex, bind in ipairs(actionBindings) do
+                if bindingIndex <= maxBindings then
+                    attemptedBindCount = attemptedBindCount + 1
+                    CallSecureProtected("BindKeyToAction", layerIndex, categoryIndex, actionIndex, bindingIndex, bind["keyCode"], bind["mod1"], bind["mod2"], bind["mod3"], bind["mod4"])
+                    bindCount = bindCount + 1
+                else
+                    skippedBindCount = skippedBindCount + 1
+                end
             end
+        else
+            skippedBindCount = skippedBindCount + 1
         end
     end
     Binder.debug.bindCount = bindCount
+    Binder.debug.attemptedBindCount = attemptedBindCount
     Binder.debug.skippedBindCount = skippedBindCount
     Binder.debug.maxBindingsPerAction = maxBindings
 end
@@ -122,7 +129,14 @@ function Binder.SaveBindings(bindSetName, isSilent)
     end
     Binder.debug.savingSet = bindSetName
     Binder.BuildBindingsTable()
-    Binder.savedVariables.bindings[bindSetName] = Binder.bindings
+
+    -- Update any existing bind set as a set union, or create new
+    local bindSet = Binder.savedVariables.bindings[bindSetName] or {}
+    for bindName, binding in pairs(Binder.bindings) do
+        bindSet[bindName] = binding
+    end
+    Binder.savedVariables.bindings[bindSetName] = bindSet
+
     if not isSilent then
         print("Saved ", Binder.bindCount, " bindings to bind set '", bindSetName, "'.")
     end