fix for WM_keymap_remove_item() writing to freed memory.
[blender.git] / source / blender / windowmanager / intern / wm_keymap.c
index 9957d24c7ab03b536a84de342b62db38de30b06c..3739462ac2cc20eef2b26ff86b29d1135b5c6a55 100644 (file)
@@ -67,15 +67,15 @@ static wmKeyMapItem *wm_keymap_item_copy(wmKeyMapItem *kmi)
 {
        wmKeyMapItem *kmin = MEM_dupallocN(kmi);
 
-       kmin->prev= kmin->next= NULL;
+       kmin->prev = kmin->next = NULL;
        kmin->flag &= ~KMI_UPDATE;
 
-       if(kmin->properties) {
-               kmin->ptr= MEM_callocN(sizeof(PointerRNA), "UserKeyMapItemPtr");
+       if (kmin->properties) {
+               kmin->ptr = MEM_callocN(sizeof(PointerRNA), "UserKeyMapItemPtr");
                WM_operator_properties_create(kmin->ptr, kmin->idname);
 
-               kmin->properties= IDP_CopyProperty(kmin->properties);
-               kmin->ptr->data= kmin->properties;
+               kmin->properties = IDP_CopyProperty(kmin->properties);
+               kmin->ptr->data = kmin->properties;
        }
 
        return kmin;
@@ -84,7 +84,7 @@ static wmKeyMapItem *wm_keymap_item_copy(wmKeyMapItem *kmi)
 static void wm_keymap_item_free(wmKeyMapItem *kmi)
 {
        /* not kmi itself */
-       if(kmi->ptr) {
+       if (kmi->ptr) {
                WM_operator_properties_free(kmi->ptr);
                MEM_freeN(kmi->ptr);
        }
@@ -98,14 +98,13 @@ static void wm_keymap_item_properties_set(wmKeyMapItem *kmi)
 
 static int wm_keymap_item_equals_result(wmKeyMapItem *a, wmKeyMapItem *b)
 {
-       if(strcmp(a->idname, b->idname) != 0)
+       if (strcmp(a->idname, b->idname) != 0)
                return 0;
        
-       if(!((a->ptr==NULL && b->ptr==NULL) ||
-            (a->ptr && b->ptr && IDP_EqualsProperties(a->ptr->data, b->ptr->data))))
+       if (!RNA_struct_equals(a->ptr, b->ptr))
                return 0;
        
-       if((a->flag & KMI_INACTIVE) != (b->flag & KMI_INACTIVE))
+       if ((a->flag & KMI_INACTIVE) != (b->flag & KMI_INACTIVE))
                return 0;
        
        return (a->propvalue == b->propvalue);
@@ -144,9 +143,9 @@ static wmKeyMapDiffItem *wm_keymap_diff_item_copy(wmKeyMapDiffItem *kmdi)
        wmKeyMapDiffItem *kmdin = MEM_dupallocN(kmdi);
 
        kmdin->next = kmdin->prev = NULL;
-       if(kmdi->add_item)
+       if (kmdi->add_item)
                kmdin->add_item = wm_keymap_item_copy(kmdi->add_item);
-       if(kmdi->remove_item)
+       if (kmdi->remove_item)
                kmdin->remove_item = wm_keymap_item_copy(kmdi->remove_item);
        
        return kmdin;
@@ -154,11 +153,11 @@ static wmKeyMapDiffItem *wm_keymap_diff_item_copy(wmKeyMapDiffItem *kmdi)
 
 static void wm_keymap_diff_item_free(wmKeyMapDiffItem *kmdi)
 {
-       if(kmdi->remove_item) {
+       if (kmdi->remove_item) {
                wm_keymap_item_free(kmdi->remove_item);
                MEM_freeN(kmdi->remove_item);
        }
-       if(kmdi->add_item) {
+       if (kmdi->add_item) {
                wm_keymap_item_free(kmdi->add_item);
                MEM_freeN(kmdi->add_item);
        }
@@ -172,7 +171,7 @@ wmKeyConfig *WM_keyconfig_new(wmWindowManager *wm, const char *idname)
 {
        wmKeyConfig *keyconf;
        
-       keyconf= MEM_callocN(sizeof(wmKeyConfig), "wmKeyConfig");
+       keyconf = MEM_callocN(sizeof(wmKeyConfig), "wmKeyConfig");
        BLI_strncpy(keyconf->idname, idname, sizeof(keyconf->idname));
        BLI_addtail(&wm->keyconfigs, keyconf);
 
@@ -188,9 +187,9 @@ wmKeyConfig *WM_keyconfig_new_user(wmWindowManager *wm, const char *idname)
        return keyconf;
 }
 
-void WM_keyconfig_remove(wmWindowManager *wm, wmKeyConfig *keyconf)
+int WM_keyconfig_remove(wmWindowManager *wm, wmKeyConfig *keyconf)
 {
-       if (keyconf) {
+       if (BLI_findindex(&wm->keyconfigs, keyconf) != -1) {
                if (strncmp(U.keyconfigstr, keyconf->idname, sizeof(U.keyconfigstr)) == 0) {
                        BLI_strncpy(U.keyconfigstr, wm->defaultconf->idname, sizeof(U.keyconfigstr));
                        WM_keyconfig_update_tag(NULL, NULL);
@@ -198,6 +197,11 @@ void WM_keyconfig_remove(wmWindowManager *wm, wmKeyConfig *keyconf)
 
                BLI_remlink(&wm->keyconfigs, keyconf);
                WM_keyconfig_free(keyconf);
+
+               return TRUE;
+       }
+       else {
+               return FALSE;
        }
 }
 
@@ -205,7 +209,7 @@ void WM_keyconfig_free(wmKeyConfig *keyconf)
 {
        wmKeyMap *km;
 
-       while((km= keyconf->keymaps.first)) {
+       while ((km = keyconf->keymaps.first)) {
                WM_keymap_free(km);
                BLI_freelinkN(&keyconf->keymaps, km);
        }
@@ -217,8 +221,8 @@ static wmKeyConfig *wm_keyconfig_list_find(ListBase *lb, char *idname)
 {
        wmKeyConfig *kc;
 
-       for(kc= lb->first; kc; kc= kc->next)
-               if(0==strncmp(idname, kc->idname, KMAP_MAX_NAME))
+       for (kc = lb->first; kc; kc = kc->next)
+               if (0 == strncmp(idname, kc->idname, KMAP_MAX_NAME))
                        return kc;
        
        return NULL;
@@ -229,8 +233,8 @@ static wmKeyConfig *WM_keyconfig_active(wmWindowManager *wm)
        wmKeyConfig *keyconf;
 
        /* first try from preset */
-       keyconf= wm_keyconfig_list_find(&wm->keyconfigs, U.keyconfigstr);
-       if(keyconf)
+       keyconf = wm_keyconfig_list_find(&wm->keyconfigs, U.keyconfigstr);
+       if (keyconf)
                return keyconf;
        
        /* otherwise use default */
@@ -240,7 +244,7 @@ static wmKeyConfig *WM_keyconfig_active(wmWindowManager *wm)
 void WM_keyconfig_set_active(wmWindowManager *wm, const char *idname)
 {
        /* setting a different key configuration as active: we ensure all is
-          updated properly before and after making the change */
+        * updated properly before and after making the change */
 
        WM_keyconfig_update(wm);
 
@@ -255,11 +259,11 @@ void WM_keyconfig_set_active(wmWindowManager *wm, const char *idname)
 
 static wmKeyMap *wm_keymap_new(const char *idname, int spaceid, int regionid)
 {
-       wmKeyMap *km= MEM_callocN(sizeof(struct wmKeyMap), "keymap list");
+       wmKeyMap *km = MEM_callocN(sizeof(struct wmKeyMap), "keymap list");
 
        BLI_strncpy(km->idname, idname, KMAP_MAX_NAME);
-       km->spaceid= spaceid;
-       km->regionid= regionid;
+       km->spaceid = spaceid;
+       km->regionid = regionid;
 
        return km;
 }
@@ -270,18 +274,18 @@ static wmKeyMap *wm_keymap_copy(wmKeyMap *keymap)
        wmKeyMapItem *kmi, *kmin;
        wmKeyMapDiffItem *kmdi, *kmdin;
 
-       keymapn->modal_items= keymap->modal_items;
-       keymapn->poll= keymap->poll;
-       keymapn->items.first= keymapn->items.last= NULL;
-       keymapn->flag &= ~(KEYMAP_UPDATE|KEYMAP_EXPANDED);
+       keymapn->modal_items = keymap->modal_items;
+       keymapn->poll = keymap->poll;
+       keymapn->items.first = keymapn->items.last = NULL;
+       keymapn->flag &= ~(KEYMAP_UPDATE | KEYMAP_EXPANDED);
 
-       for(kmdi=keymap->diff_items.first; kmdi; kmdi=kmdi->next) {
-               kmdin= wm_keymap_diff_item_copy(kmdi);
+       for (kmdi = keymap->diff_items.first; kmdi; kmdi = kmdi->next) {
+               kmdin = wm_keymap_diff_item_copy(kmdi);
                BLI_addtail(&keymapn->items, kmdin);
        }
 
-       for(kmi=keymap->items.first; kmi; kmi=kmi->next) {
-               kmin= wm_keymap_item_copy(kmi);
+       for (kmi = keymap->items.first; kmi; kmi = kmi->next) {
+               kmin = wm_keymap_item_copy(kmi);
                BLI_addtail(&keymapn->items, kmin);
        }
 
@@ -293,10 +297,10 @@ void WM_keymap_free(wmKeyMap *keymap)
        wmKeyMapItem *kmi;
        wmKeyMapDiffItem *kmdi;
 
-       for(kmdi=keymap->diff_items.first; kmdi; kmdi=kmdi->next)
+       for (kmdi = keymap->diff_items.first; kmdi; kmdi = kmdi->next)
                wm_keymap_diff_item_free(kmdi);
 
-       for(kmi=keymap->items.first; kmi; kmi=kmi->next)
+       for (kmi = keymap->items.first; kmi; kmi = kmi->next)
                wm_keymap_item_free(kmi);
 
        BLI_freelistN(&keymap->diff_items);
@@ -305,18 +309,18 @@ void WM_keymap_free(wmKeyMap *keymap)
 
 static void keymap_event_set(wmKeyMapItem *kmi, short type, short val, int modifier, short keymodifier)
 {
-       kmi->type= type;
-       kmi->val= val;
-       kmi->keymodifier= keymodifier;
+       kmi->type = type;
+       kmi->val = val;
+       kmi->keymodifier = keymodifier;
 
-       if(modifier == KM_ANY) {
-               kmi->shift= kmi->ctrl= kmi->alt= kmi->oskey= KM_ANY;
+       if (modifier == KM_ANY) {
+               kmi->shift = kmi->ctrl = kmi->alt = kmi->oskey = KM_ANY;
        }
        else {
-               kmi->shift= (modifier & KM_SHIFT) ? KM_MOD_FIRST : ((modifier & KM_SHIFT2) ? KM_MOD_SECOND : FALSE);
-               kmi->ctrl=  (modifier & KM_CTRL)  ? KM_MOD_FIRST : ((modifier & KM_CTRL2)  ? KM_MOD_SECOND : FALSE);
-               kmi->alt=   (modifier & KM_ALT)   ? KM_MOD_FIRST : ((modifier & KM_ALT2)   ? KM_MOD_SECOND : FALSE);
-               kmi->oskey= (modifier & KM_OSKEY) ? KM_MOD_FIRST : ((modifier & KM_OSKEY2) ? KM_MOD_SECOND : FALSE);
+               kmi->shift = (modifier & KM_SHIFT) ? KM_MOD_FIRST : ((modifier & KM_SHIFT2) ? KM_MOD_SECOND : FALSE);
+               kmi->ctrl =  (modifier & KM_CTRL)  ? KM_MOD_FIRST : ((modifier & KM_CTRL2)  ? KM_MOD_SECOND : FALSE);
+               kmi->alt =   (modifier & KM_ALT)   ? KM_MOD_FIRST : ((modifier & KM_ALT2)   ? KM_MOD_SECOND : FALSE);
+               kmi->oskey = (modifier & KM_OSKEY) ? KM_MOD_FIRST : ((modifier & KM_OSKEY2) ? KM_MOD_SECOND : FALSE);
        }
 }
 
@@ -325,8 +329,9 @@ static void keymap_item_set_id(wmKeyMap *keymap, wmKeyMapItem *kmi)
        keymap->kmi_id++;
        if ((keymap->flag & KEYMAP_USER) == 0) {
                kmi->id = keymap->kmi_id;
-       } else {
-               kmi->id = -keymap->kmi_id; // User defined keymap entries have negative ids
+       }
+       else {
+               kmi->id = -keymap->kmi_id; /* User defined keymap entries have negative ids */
        }
 }
 
@@ -335,11 +340,11 @@ wmKeyMapItem *WM_keymap_verify_item(wmKeyMap *keymap, const char *idname, int ty
 {
        wmKeyMapItem *kmi;
        
-       for(kmi= keymap->items.first; kmi; kmi= kmi->next)
-               if(strncmp(kmi->idname, idname, OP_MAX_TYPENAME)==0)
+       for (kmi = keymap->items.first; kmi; kmi = kmi->next)
+               if (strncmp(kmi->idname, idname, OP_MAX_TYPENAME) == 0)
                        break;
-       if(kmi==NULL) {
-               kmi= MEM_callocN(sizeof(wmKeyMapItem), "keymap entry");
+       if (kmi == NULL) {
+               kmi = MEM_callocN(sizeof(wmKeyMapItem), "keymap entry");
                
                BLI_addtail(&keymap->items, kmi);
                BLI_strncpy(kmi->idname, idname, OP_MAX_TYPENAME);
@@ -355,7 +360,7 @@ wmKeyMapItem *WM_keymap_verify_item(wmKeyMap *keymap, const char *idname, int ty
 /* always add item */
 wmKeyMapItem *WM_keymap_add_item(wmKeyMap *keymap, const char *idname, int type, int val, int modifier, int keymodifier)
 {
-       wmKeyMapItem *kmi= MEM_callocN(sizeof(wmKeyMapItem), "keymap entry");
+       wmKeyMapItem *kmi = MEM_callocN(sizeof(wmKeyMapItem), "keymap entry");
        
        BLI_addtail(&keymap->items, kmi);
        BLI_strncpy(kmi->idname, idname, OP_MAX_TYPENAME);
@@ -373,21 +378,25 @@ wmKeyMapItem *WM_keymap_add_item(wmKeyMap *keymap, const char *idname, int type,
 /* menu wrapper for WM_keymap_add_item */
 wmKeyMapItem *WM_keymap_add_menu(wmKeyMap *keymap, const char *idname, int type, int val, int modifier, int keymodifier)
 {
-       wmKeyMapItem *kmi= WM_keymap_add_item(keymap, "WM_OT_call_menu", type, val, modifier, keymodifier);
+       wmKeyMapItem *kmi = WM_keymap_add_item(keymap, "WM_OT_call_menu", type, val, modifier, keymodifier);
        RNA_string_set(kmi->ptr, "name", idname);
        return kmi;
 }
 
-void WM_keymap_remove_item(wmKeyMap *keymap, wmKeyMapItem *kmi)
+int WM_keymap_remove_item(wmKeyMap *keymap, wmKeyMapItem *kmi)
 {
-       if(BLI_findindex(&keymap->items, kmi) != -1) {
-               if(kmi->ptr) {
+       if (BLI_findindex(&keymap->items, kmi) != -1) {
+               if (kmi->ptr) {
                        WM_operator_properties_free(kmi->ptr);
                        MEM_freeN(kmi->ptr);
                }
                BLI_freelinkN(&keymap->items, kmi);
 
-               WM_keyconfig_update_tag(keymap, kmi);
+               WM_keyconfig_update_tag(keymap, NULL);
+               return TRUE;
+       }
+       else {
+               return FALSE;
        }
 }
 
@@ -401,7 +410,7 @@ static void wm_keymap_addon_add(wmKeyMap *keymap, wmKeyMap *addonmap)
 {
        wmKeyMapItem *kmi, *kmin;
 
-       for(kmi=addonmap->items.first; kmi; kmi=kmi->next) {
+       for (kmi = addonmap->items.first; kmi; kmi = kmi->next) {
                kmin = wm_keymap_item_copy(kmi);
                keymap_item_set_id(keymap, kmin);
                BLI_addhead(&keymap->items, kmin);
@@ -412,8 +421,8 @@ static wmKeyMapItem *wm_keymap_find_item_equals(wmKeyMap *km, wmKeyMapItem *need
 {
        wmKeyMapItem *kmi;
 
-       for(kmi=km->items.first; kmi; kmi=kmi->next)
-               if(wm_keymap_item_equals(kmi, needle))
+       for (kmi = km->items.first; kmi; kmi = kmi->next)
+               if (wm_keymap_item_equals(kmi, needle))
                        return kmi;
        
        return NULL;
@@ -423,8 +432,8 @@ static wmKeyMapItem *wm_keymap_find_item_equals_result(wmKeyMap *km, wmKeyMapIte
 {
        wmKeyMapItem *kmi;
 
-       for(kmi=km->items.first; kmi; kmi=kmi->next)
-               if(wm_keymap_item_equals_result(kmi, needle))
+       for (kmi = km->items.first; kmi; kmi = kmi->next)
+               if (wm_keymap_item_equals_result(kmi, needle))
                        return kmi;
        
        return NULL;
@@ -435,16 +444,16 @@ static void wm_keymap_diff(wmKeyMap *diff_km, wmKeyMap *from_km, wmKeyMap *to_km
        wmKeyMapItem *kmi, *to_kmi, *orig_kmi;
        wmKeyMapDiffItem *kmdi;
 
-       for(kmi=from_km->items.first; kmi; kmi=kmi->next) {
+       for (kmi = from_km->items.first; kmi; kmi = kmi->next) {
                to_kmi = WM_keymap_item_find_id(to_km, kmi->id);
 
-               if(!to_kmi) {
+               if (!to_kmi) {
                        /* remove item */
                        kmdi = MEM_callocN(sizeof(wmKeyMapDiffItem), "wmKeyMapDiffItem");
                        kmdi->remove_item = wm_keymap_item_copy(kmi);
                        BLI_addtail(&diff_km->diff_items, kmdi);
                }
-               else if(to_kmi && !wm_keymap_item_equals(kmi, to_kmi)) {
+               else if (to_kmi && !wm_keymap_item_equals(kmi, to_kmi)) {
                        /* replace item */
                        kmdi = MEM_callocN(sizeof(wmKeyMapDiffItem), "wmKeyMapDiffItem");
                        kmdi->remove_item = wm_keymap_item_copy(kmi);
@@ -453,21 +462,21 @@ static void wm_keymap_diff(wmKeyMap *diff_km, wmKeyMap *from_km, wmKeyMap *to_km
                }
 
                /* sync expanded flag back to original so we don't loose it on repatch */
-               if(to_kmi) {
+               if (to_kmi) {
                        orig_kmi = WM_keymap_item_find_id(orig_km, kmi->id);
 
-                       if(!orig_kmi)
+                       if (!orig_kmi)
                                orig_kmi = wm_keymap_find_item_equals(addon_km, kmi);
 
-                       if(orig_kmi) {
+                       if (orig_kmi) {
                                orig_kmi->flag &= ~KMI_EXPANDED;
                                orig_kmi->flag |= (to_kmi->flag & KMI_EXPANDED);
                        }
                }
        }
 
-       for(kmi=to_km->items.first; kmi; kmi=kmi->next) {
-               if(kmi->id < 0) {
+       for (kmi = to_km->items.first; kmi; kmi = kmi->next) {
+               if (kmi->id < 0) {
                        /* add item */
                        kmdi = MEM_callocN(sizeof(wmKeyMapDiffItem), "wmKeyMapDiffItem");
                        kmdi->add_item = wm_keymap_item_copy(kmi);
@@ -481,23 +490,23 @@ static void wm_keymap_patch(wmKeyMap *km, wmKeyMap *diff_km)
        wmKeyMapDiffItem *kmdi;
        wmKeyMapItem *kmi_remove, *kmi_add;
 
-       for(kmdi=diff_km->diff_items.first; kmdi; kmdi=kmdi->next) {
+       for (kmdi = diff_km->diff_items.first; kmdi; kmdi = kmdi->next) {
                /* find item to remove */
                kmi_remove = NULL;
-               if(kmdi->remove_item) {
+               if (kmdi->remove_item) {
                        kmi_remove = wm_keymap_find_item_equals(km, kmdi->remove_item);
-                       if(!kmi_remove)
+                       if (!kmi_remove)
                                kmi_remove = wm_keymap_find_item_equals_result(km, kmdi->remove_item);
                }
 
                /* add item */
-               if(kmdi->add_item) {
+               if (kmdi->add_item) {
                        /* only if nothing to remove or item to remove found */
-                       if(!kmdi->remove_item || kmi_remove) {
+                       if (!kmdi->remove_item || kmi_remove) {
                                kmi_add = wm_keymap_item_copy(kmdi->add_item);
                                kmi_add->flag |= KMI_USER_MODIFIED;
 
-                               if(kmi_remove) {
+                               if (kmi_remove) {
                                        kmi_add->flag &= ~KMI_EXPANDED;
                                        kmi_add->flag |= (kmi_remove->flag & KMI_EXPANDED);
                                        kmi_add->id = kmi_remove->id;
@@ -511,7 +520,7 @@ static void wm_keymap_patch(wmKeyMap *km, wmKeyMap *diff_km)
                }
 
                /* remove item */
-               if(kmi_remove) {
+               if (kmi_remove) {
                        wm_keymap_item_free(kmi_remove);
                        BLI_freelinkN(&km->items, kmi_remove);
                }
@@ -525,27 +534,27 @@ static wmKeyMap *wm_keymap_patch_update(ListBase *lb, wmKeyMap *defaultmap, wmKe
 
        /* remove previous keymap in list, we will replace it */
        km = WM_keymap_list_find(lb, defaultmap->idname, defaultmap->spaceid, defaultmap->regionid);
-       if(km) {
-               expanded = (km->flag & (KEYMAP_EXPANDED|KEYMAP_CHILDREN_EXPANDED));
+       if (km) {
+               expanded = (km->flag & (KEYMAP_EXPANDED | KEYMAP_CHILDREN_EXPANDED));
                WM_keymap_free(km);
                BLI_freelinkN(lb, km);
        }
 
        /* copy new keymap from an existing one */
-       if(usermap && !(usermap->flag & KEYMAP_DIFF)) {
-               /* for compatibiltiy with old user preferences with non-diff
-                  keymaps we override the original entirely */
+       if (usermap && !(usermap->flag & KEYMAP_DIFF)) {
+               /* for compatibility with old user preferences with non-diff
+                * keymaps we override the original entirely */
                wmKeyMapItem *kmi, *orig_kmi;
 
                km = wm_keymap_copy(usermap);
 
                /* try to find corresponding id's for items */
-               for(kmi=km->items.first; kmi; kmi=kmi->next) {
+               for (kmi = km->items.first; kmi; kmi = kmi->next) {
                        orig_kmi = wm_keymap_find_item_equals(defaultmap, kmi);
-                       if(!orig_kmi)
+                       if (!orig_kmi)
                                orig_kmi = wm_keymap_find_item_equals_result(defaultmap, kmi);
 
-                       if(orig_kmi)
+                       if (orig_kmi)
                                kmi->id = orig_kmi->id;
                        else
                                kmi->id = -(km->kmi_id++);
@@ -557,16 +566,16 @@ static wmKeyMap *wm_keymap_patch_update(ListBase *lb, wmKeyMap *defaultmap, wmKe
                km = wm_keymap_copy(defaultmap);
 
        /* add addon keymap items */
-       if(addonmap)
+       if (addonmap)
                wm_keymap_addon_add(km, addonmap);
 
        /* tag as being user edited */
-       if(usermap)
+       if (usermap)
                km->flag |= KEYMAP_USER_MODIFIED;
-       km->flag |= KEYMAP_USER|expanded;
+       km->flag |= KEYMAP_USER | expanded;
 
        /* apply user changes of diff keymap */
-       if(usermap && (usermap->flag & KEYMAP_DIFF))
+       if (usermap && (usermap->flag & KEYMAP_DIFF))
                wm_keymap_patch(km, usermap);
 
        /* add to list */
@@ -582,25 +591,27 @@ static void wm_keymap_diff_update(ListBase *lb, wmKeyMap *defaultmap, wmKeyMap *
        /* create temporary default + addon keymap for diff */
        origmap = defaultmap;
 
-       if(addonmap) {
+       if (addonmap) {
                defaultmap = wm_keymap_copy(defaultmap);
                wm_keymap_addon_add(defaultmap, addonmap);
        }
 
        /* remove previous diff keymap in list, we will replace it */
        prevmap = WM_keymap_list_find(lb, km->idname, km->spaceid, km->regionid);
-       if(prevmap) {
+       if (prevmap) {
                WM_keymap_free(prevmap);
                BLI_freelinkN(lb, prevmap);
        }
 
        /* create diff keymap */
-       diffmap= wm_keymap_new(km->idname, km->spaceid, km->regionid);
+       diffmap = wm_keymap_new(km->idname, km->spaceid, km->regionid);
        diffmap->flag |= KEYMAP_DIFF;
+       if (defaultmap->flag & KEYMAP_MODAL)
+               diffmap->flag |= KEYMAP_MODAL;
        wm_keymap_diff(diffmap, defaultmap, km, origmap, addonmap);
 
        /* add to list if not empty */
-       if(diffmap->diff_items.first) {
+       if (diffmap->diff_items.first) {
                BLI_addtail(lb, diffmap);
        }
        else {
@@ -609,7 +620,7 @@ static void wm_keymap_diff_update(ListBase *lb, wmKeyMap *defaultmap, wmKeyMap *
        }
 
        /* free temporary default map */
-       if(addonmap) {
+       if (addonmap) {
                WM_keymap_free(defaultmap);
                MEM_freeN(defaultmap);
        }
@@ -618,16 +629,16 @@ static void wm_keymap_diff_update(ListBase *lb, wmKeyMap *defaultmap, wmKeyMap *
 /* ****************** storage in WM ************ */
 
 /* name id's are for storing general or multiple keymaps, 
  space/region ids are same as DNA_space_types.h */
-/* gets free'd in wm.c */
* space/region ids are same as DNA_space_types.h */
+/* gets freed in wm.c */
 
 wmKeyMap *WM_keymap_list_find(ListBase *lb, const char *idname, int spaceid, int regionid)
 {
        wmKeyMap *km;
 
-       for(km= lb->first; km; km= km->next)
-               if(km->spaceid==spaceid && km->regionid==regionid)
-                       if(0==strncmp(idname, km->idname, KMAP_MAX_NAME))
+       for (km = lb->first; km; km = km->next)
+               if (km->spaceid == spaceid && km->regionid == regionid)
+                       if (0 == strncmp(idname, km->idname, KMAP_MAX_NAME))
                                return km;
        
        return NULL;
@@ -635,10 +646,10 @@ wmKeyMap *WM_keymap_list_find(ListBase *lb, const char *idname, int spaceid, int
 
 wmKeyMap *WM_keymap_find(wmKeyConfig *keyconf, const char *idname, int spaceid, int regionid)
 {
-       wmKeyMap *km= WM_keymap_list_find(&keyconf->keymaps, idname, spaceid, regionid);
+       wmKeyMap *km = WM_keymap_list_find(&keyconf->keymaps, idname, spaceid, regionid);
        
-       if(km==NULL) {
-               km= wm_keymap_new(idname, spaceid, regionid);
+       if (km == NULL) {
+               km = wm_keymap_new(idname, spaceid, regionid);
                BLI_addtail(&keyconf->keymaps, km);
 
                WM_keyconfig_update_tag(km, NULL);
@@ -649,7 +660,7 @@ wmKeyMap *WM_keymap_find(wmKeyConfig *keyconf, const char *idname, int spaceid,
 
 wmKeyMap *WM_keymap_find_all(const bContext *C, const char *idname, int spaceid, int regionid)
 {
-       wmWindowManager *wm= CTX_wm_manager(C);
+       wmWindowManager *wm = CTX_wm_manager(C);
 
        return WM_keymap_list_find(&wm->userconf->keymaps, idname, spaceid, regionid);
 }
@@ -660,16 +671,16 @@ wmKeyMap *WM_keymap_find_all(const bContext *C, const char *idname, int spaceid,
 
 wmKeyMap *WM_modalkeymap_add(wmKeyConfig *keyconf, const char *idname, EnumPropertyItem *items)
 {
-       wmKeyMap *km= WM_keymap_find(keyconf, idname, 0, 0);
+       wmKeyMap *km = WM_keymap_find(keyconf, idname, 0, 0);
        km->flag |= KEYMAP_MODAL;
-       km->modal_items= items;
+       km->modal_items = items;
 
-       if(!items) {
+       if (!items) {
                /* init modal items from default config */
                wmWindowManager *wm = G.main->wm.first;
-               wmKeyMap *defaultkm= WM_keymap_list_find(&wm->defaultconf->keymaps, km->idname, 0, 0);
+               wmKeyMap *defaultkm = WM_keymap_list_find(&wm->defaultconf->keymaps, km->idname, 0, 0);
 
-               if(defaultkm) {
+               if (defaultkm) {
                        km->modal_items = defaultkm->modal_items;
                        km->poll = defaultkm->poll;
                }
@@ -682,9 +693,9 @@ wmKeyMap *WM_modalkeymap_get(wmKeyConfig *keyconf, const char *idname)
 {
        wmKeyMap *km;
        
-       for(km= keyconf->keymaps.first; km; km= km->next)
-               if(km->flag & KEYMAP_MODAL)
-                       if(0==strncmp(idname, km->idname, KMAP_MAX_NAME))
+       for (km = keyconf->keymaps.first; km; km = km->next)
+               if (km->flag & KEYMAP_MODAL)
+                       if (0 == strncmp(idname, km->idname, KMAP_MAX_NAME))
                                break;
        
        return km;
@@ -693,10 +704,10 @@ wmKeyMap *WM_modalkeymap_get(wmKeyConfig *keyconf, const char *idname)
 
 wmKeyMapItem *WM_modalkeymap_add_item(wmKeyMap *km, int type, int val, int modifier, int keymodifier, int value)
 {
-       wmKeyMapItem *kmi= MEM_callocN(sizeof(wmKeyMapItem), "keymap entry");
+       wmKeyMapItem *kmi = MEM_callocN(sizeof(wmKeyMapItem), "keymap entry");
        
        BLI_addtail(&km->items, kmi);
-       kmi->propvalue= value;
+       kmi->propvalue = value;
        
        keymap_event_set(kmi, type, val, modifier, keymodifier);
 
@@ -707,22 +718,65 @@ wmKeyMapItem *WM_modalkeymap_add_item(wmKeyMap *km, int type, int val, int modif
        return kmi;
 }
 
+wmKeyMapItem *WM_modalkeymap_add_item_str(wmKeyMap *km, int type, int val, int modifier, int keymodifier, const char *value)
+{
+       wmKeyMapItem *kmi = MEM_callocN(sizeof(wmKeyMapItem), "keymap entry");
+
+       BLI_addtail(&km->items, kmi);
+       BLI_strncpy(kmi->propvalue_str, value, sizeof(kmi->propvalue_str));
+
+       keymap_event_set(kmi, type, val, modifier, keymodifier);
+
+       keymap_item_set_id(km, kmi);
+
+       WM_keyconfig_update_tag(km, kmi);
+
+       return kmi;
+}
+
 void WM_modalkeymap_assign(wmKeyMap *km, const char *opname)
 {
-       wmOperatorType *ot= WM_operatortype_find(opname, 0);
+       wmOperatorType *ot = WM_operatortype_find(opname, 0);
        
-       if(ot)
-               ot->modalkeymap= km;
+       if (ot)
+               ot->modalkeymap = km;
        else
                printf("error: modalkeymap_assign, unknown operator %s\n", opname);
 }
 
+static void wm_user_modal_keymap_set_items(wmWindowManager *wm, wmKeyMap *km)
+{
+       /* here we convert propvalue string values delayed, due to python keymaps
+        * being created before the actual modal keymaps, so no modal_items */
+       wmKeyMap *defaultkm;
+       wmKeyMapItem *kmi;
+       int propvalue;
+
+       if (km && (km->flag & KEYMAP_MODAL) && !km->modal_items) {
+               defaultkm = WM_keymap_list_find(&wm->defaultconf->keymaps, km->idname, 0, 0);
+
+               if (!defaultkm)
+                       return;
+
+               km->modal_items = defaultkm->modal_items;
+               km->poll = defaultkm->poll;
+
+               for (kmi = km->items.first; kmi; kmi = kmi->next) {
+                       if (kmi->propvalue_str[0]) {
+                               if (RNA_enum_value_from_id(km->modal_items, kmi->propvalue_str, &propvalue))
+                                       kmi->propvalue = propvalue;
+                               kmi->propvalue_str[0] = '\0';
+                       }
+               }
+       }
+}
+
 /* ***************** get string from key events **************** */
 
 const char *WM_key_event_string(short type)
 {
-       const char *name= NULL;
-       if(RNA_enum_name(event_type_items, (int)type, &name))
+       const char *name = NULL;
+       if (RNA_enum_name(event_type_items, (int)type, &name))
                return name;
        
        return "";
@@ -732,29 +786,30 @@ char *WM_keymap_item_to_string(wmKeyMapItem *kmi, char *str, int len)
 {
        char buf[128];
 
-       buf[0]= 0;
+       buf[0] = 0;
 
        if (kmi->shift == KM_ANY &&
-               kmi->ctrl == KM_ANY &&
-               kmi->alt == KM_ANY &&
-               kmi->oskey == KM_ANY) {
-
+           kmi->ctrl == KM_ANY &&
+           kmi->alt == KM_ANY &&
+           kmi->oskey == KM_ANY)
+       {
                strcat(buf, "Any ");
-       } else {
-               if(kmi->shift)
+       }
+       else {
+               if (kmi->shift)
                        strcat(buf, "Shift ");
 
-               if(kmi->ctrl)
+               if (kmi->ctrl)
                        strcat(buf, "Ctrl ");
 
-               if(kmi->alt)
+               if (kmi->alt)
                        strcat(buf, "Alt ");
 
-               if(kmi->oskey)
+               if (kmi->oskey)
                        strcat(buf, "Cmd ");
        }
                
-       if(kmi->keymodifier) {
+       if (kmi->keymodifier) {
                strcat(buf, WM_key_event_string(kmi->keymodifier));
                strcat(buf, " ");
        }
@@ -765,33 +820,48 @@ char *WM_keymap_item_to_string(wmKeyMapItem *kmi, char *str, int len)
        return str;
 }
 
-static wmKeyMapItem *wm_keymap_item_find_handlers(const bContext *C, ListBase *handlers, const char *opname, int UNUSED(opcontext), IDProperty *properties, int compare_props, int hotkey, wmKeyMap **keymap_r)
+static wmKeyMapItem *wm_keymap_item_find_handlers(
+        const bContext *C, ListBase *handlers, const char *opname, int UNUSED(opcontext),
+        IDProperty *properties, int is_strict, int hotkey, wmKeyMap **keymap_r)
 {
-       wmWindowManager *wm= CTX_wm_manager(C);
+       wmWindowManager *wm = CTX_wm_manager(C);
        wmEventHandler *handler;
        wmKeyMap *keymap;
        wmKeyMapItem *kmi;
 
        /* find keymap item in handlers */
-       for(handler=handlers->first; handler; handler=handler->next) {
-               keymap= WM_keymap_active(wm, handler->keymap);
+       for (handler = handlers->first; handler; handler = handler->next) {
+               keymap = WM_keymap_active(wm, handler->keymap);
 
-               if(keymap && (!keymap->poll || keymap->poll((bContext*)C))) {
-                       for(kmi=keymap->items.first; kmi; kmi=kmi->next) {
+               if (keymap && (!keymap->poll || keymap->poll((bContext *)C))) {
+                       for (kmi = keymap->items.first; kmi; kmi = kmi->next) {
                                
-                               if(strcmp(kmi->idname, opname) == 0 && WM_key_event_string(kmi->type)[0]) {
+                               if (strcmp(kmi->idname, opname) == 0 && WM_key_event_string(kmi->type)[0]) {
                                        if (hotkey)
                                                if (!ISHOTKEY(kmi->type))
                                                        continue;
-                                       
-                                       if(compare_props) {
-                                               if(kmi->ptr && IDP_EqualsProperties(properties, kmi->ptr->data)) {
-                                                       if(keymap_r) *keymap_r= keymap;
+
+                                       if (properties) {
+
+                                               /* example of debugging keymaps */
+#if 0
+                                               if (kmi->ptr) {
+                                                       if (strcmp("MESH_OT_rip_move", opname) == 0) {
+                                                               printf("OPERATOR\n");
+                                                               IDP_spit(properties);
+                                                               printf("KEYMAP\n");
+                                                               IDP_spit(kmi->ptr->data);
+                                                       }
+                                               }
+#endif
+
+                                               if (kmi->ptr && IDP_EqualsProperties_ex(properties, kmi->ptr->data, is_strict)) {
+                                                       if (keymap_r) *keymap_r = keymap;
                                                        return kmi;
                                                }
                                        }
                                        else {
-                                               if(keymap_r) *keymap_r= keymap;
+                                               if (keymap_r) *keymap_r = keymap;
                                                return kmi;
                                        }
                                }
@@ -800,73 +870,94 @@ static wmKeyMapItem *wm_keymap_item_find_handlers(const bContext *C, ListBase *h
        }
        
        /* ensure un-initialized keymap is never used */
-       if(keymap_r) *keymap_r= NULL;
+       if (keymap_r) *keymap_r = NULL;
        return NULL;
 }
 
-static wmKeyMapItem *wm_keymap_item_find_props(const bContext *C, const char *opname, int opcontext, IDProperty *properties, int compare_props, int hotkey, wmKeyMap **keymap_r)
+static wmKeyMapItem *wm_keymap_item_find_props(
+        const bContext *C, const char *opname, int opcontext,
+        IDProperty *properties, int is_strict, int hotkey, wmKeyMap **keymap_r)
 {
-       wmWindow *win= CTX_wm_window(C);
-       ScrArea *sa= CTX_wm_area(C);
-       ARegion *ar= CTX_wm_region(C);
-       wmKeyMapItem *found= NULL;
+       wmWindow *win = CTX_wm_window(C);
+       ScrArea *sa = CTX_wm_area(C);
+       ARegion *ar = CTX_wm_region(C);
+       wmKeyMapItem *found = NULL;
 
        /* look into multiple handler lists to find the item */
-       if(win)
-               found= wm_keymap_item_find_handlers(C, &win->handlers, opname, opcontext, properties, compare_props, hotkey, keymap_r);
-       
+       if (win)
+               found = wm_keymap_item_find_handlers(C, &win->handlers, opname, opcontext, properties, is_strict, hotkey, keymap_r);
 
-       if(sa && found==NULL)
-               found= wm_keymap_item_find_handlers(C, &sa->handlers, opname, opcontext, properties, compare_props, hotkey, keymap_r);
+       if (sa && found == NULL)
+               found = wm_keymap_item_find_handlers(C, &sa->handlers, opname, opcontext, properties, is_strict, hotkey, keymap_r);
 
-       if(found==NULL) {
-               if(ELEM(opcontext, WM_OP_EXEC_REGION_WIN, WM_OP_INVOKE_REGION_WIN)) {
-                       if(sa) {
+       if (found == NULL) {
+               if (ELEM(opcontext, WM_OP_EXEC_REGION_WIN, WM_OP_INVOKE_REGION_WIN)) {
+                       if (sa) {
                                if (!(ar && ar->regiontype == RGN_TYPE_WINDOW))
-                                       ar= BKE_area_find_region_type(sa, RGN_TYPE_WINDOW);
+                                       ar = BKE_area_find_region_type(sa, RGN_TYPE_WINDOW);
                                
-                               if(ar)
-                                       found= wm_keymap_item_find_handlers(C, &ar->handlers, opname, opcontext, properties, compare_props, hotkey, keymap_r);
+                               if (ar)
+                                       found = wm_keymap_item_find_handlers(C, &ar->handlers, opname, opcontext, properties, is_strict, hotkey, keymap_r);
                        }
                }
-               else if(ELEM(opcontext, WM_OP_EXEC_REGION_CHANNELS, WM_OP_INVOKE_REGION_CHANNELS)) {
+               else if (ELEM(opcontext, WM_OP_EXEC_REGION_CHANNELS, WM_OP_INVOKE_REGION_CHANNELS)) {
                        if (!(ar && ar->regiontype == RGN_TYPE_CHANNELS))
-                               ar= BKE_area_find_region_type(sa, RGN_TYPE_CHANNELS);
+                               ar = BKE_area_find_region_type(sa, RGN_TYPE_CHANNELS);
 
-                       if(ar)
-                               found= wm_keymap_item_find_handlers(C, &ar->handlers, opname, opcontext, properties, compare_props, hotkey, keymap_r);
+                       if (ar)
+                               found = wm_keymap_item_find_handlers(C, &ar->handlers, opname, opcontext, properties, is_strict, hotkey, keymap_r);
                }
-               else if(ELEM(opcontext, WM_OP_EXEC_REGION_PREVIEW, WM_OP_INVOKE_REGION_PREVIEW)) {
+               else if (ELEM(opcontext, WM_OP_EXEC_REGION_PREVIEW, WM_OP_INVOKE_REGION_PREVIEW)) {
                        if (!(ar && ar->regiontype == RGN_TYPE_PREVIEW))
-                               ar= BKE_area_find_region_type(sa, RGN_TYPE_PREVIEW);
+                               ar = BKE_area_find_region_type(sa, RGN_TYPE_PREVIEW);
 
-                       if(ar)
-                               found= wm_keymap_item_find_handlers(C, &ar->handlers, opname, opcontext, properties, compare_props, hotkey, keymap_r);
+                       if (ar)
+                               found = wm_keymap_item_find_handlers(C, &ar->handlers, opname, opcontext, properties, is_strict, hotkey, keymap_r);
                }
                else {
-                       if(ar)
-                               found= wm_keymap_item_find_handlers(C, &ar->handlers, opname, opcontext, properties, compare_props, hotkey, keymap_r);
+                       if (ar)
+                               found = wm_keymap_item_find_handlers(C, &ar->handlers, opname, opcontext, properties, is_strict, hotkey, keymap_r);
                }
        }
-       
+
        return found;
 }
 
-static wmKeyMapItem *wm_keymap_item_find(const bContext *C, const char *opname, int opcontext, IDProperty *properties, int hotkey, wmKeyMap **keymap_r)
+static wmKeyMapItem *wm_keymap_item_find(
+        const bContext *C, const char *opname, int opcontext,
+        IDProperty *properties, const short hotkey, const short UNUSED(sloppy), wmKeyMap **keymap_r)
 {
-       wmKeyMapItem *found= wm_keymap_item_find_props(C, opname, opcontext, properties, 1, hotkey, keymap_r);
+       wmKeyMapItem *found = wm_keymap_item_find_props(C, opname, opcontext, properties, 1, hotkey, keymap_r);
+
+       if (!found && properties) {
+               wmOperatorType *ot = WM_operatortype_find(opname, TRUE);
+               if (ot) {
+                       /* make a copy of the properties and set any unset props
+                        * to their default values, so the ID property compare function succeeds */
+                       PointerRNA opptr;
+                       IDProperty *properties_default = IDP_CopyProperty(properties);
 
-       if(!found)
-               found= wm_keymap_item_find_props(C, opname, opcontext, NULL, 0, hotkey, keymap_r);
+                       RNA_pointer_create(NULL, ot->srna, properties_default, &opptr);
+
+                       if (WM_operator_properties_default(&opptr, TRUE)) {
+                               found = wm_keymap_item_find_props(C, opname, opcontext, properties_default, 0, hotkey, keymap_r);
+                       }
+
+                       IDP_FreeProperty(properties_default);
+                       MEM_freeN(properties_default);
+               }
+       }
 
        return found;
 }
 
-char *WM_key_event_operator_string(const bContext *C, const char *opname, int opcontext, IDProperty *properties, char *str, int len)
+char *WM_key_event_operator_string(
+        const bContext *C, const char *opname, int opcontext,
+        IDProperty *properties, const short sloppy, char *str, int len)
 {
-       wmKeyMapItem *kmi= wm_keymap_item_find(C, opname, opcontext, properties, 0, NULL);
+       wmKeyMapItem *kmi = wm_keymap_item_find(C, opname, opcontext, properties, 0, sloppy, NULL);
        
-       if(kmi) {
+       if (kmi) {
                WM_keymap_item_to_string(kmi, str, len);
                return str;
        }
@@ -874,17 +965,19 @@ char *WM_key_event_operator_string(const bContext *C, const char *opname, int op
        return NULL;
 }
 
-int WM_key_event_operator_id(const bContext *C, const char *opname, int opcontext, IDProperty *properties, int hotkey, wmKeyMap **keymap_r)
+int WM_key_event_operator_id(
+        const bContext *C, const char *opname, int opcontext,
+        IDProperty *properties, int hotkey, wmKeyMap **keymap_r)
 {
-       wmKeyMapItem *kmi= wm_keymap_item_find(C, opname, opcontext, properties, hotkey, keymap_r);
+       wmKeyMapItem *kmi = wm_keymap_item_find(C, opname, opcontext, properties, hotkey, TRUE, keymap_r);
        
-       if(kmi)
+       if (kmi)
                return kmi->id;
        else
                return 0;
 }
 
-int    WM_keymap_item_compare(wmKeyMapItem *k1, wmKeyMapItem *k2)
+int WM_keymap_item_compare(wmKeyMapItem *k1, wmKeyMapItem *k2)
 {
        int k1type, k2type;
 
@@ -895,10 +988,10 @@ int       WM_keymap_item_compare(wmKeyMapItem *k1, wmKeyMapItem *k2)
        k1type = WM_userdef_event_map(k1->type);
        k2type = WM_userdef_event_map(k2->type);
 
-       if(k1type != KM_ANY && k2type != KM_ANY && k1type != k2type)
+       if (k1type != KM_ANY && k2type != KM_ANY && k1type != k2type)
                return 0;
 
-       if(k1->val != KM_ANY && k2->val != KM_ANY) {
+       if (k1->val != KM_ANY && k2->val != KM_ANY) {
                /* take click, press, release conflict into account */
                if (k1->val == KM_CLICK && ELEM3(k2->val, KM_PRESS, KM_RELEASE, KM_CLICK) == 0)
                        return 0;
@@ -908,19 +1001,19 @@ int      WM_keymap_item_compare(wmKeyMapItem *k1, wmKeyMapItem *k2)
                        return 0;
        }
 
-       if(k1->shift != KM_ANY && k2->shift != KM_ANY && k1->shift != k2->shift)
+       if (k1->shift != KM_ANY && k2->shift != KM_ANY && k1->shift != k2->shift)
                return 0;
 
-       if(k1->ctrl != KM_ANY && k2->ctrl != KM_ANY && k1->ctrl != k2->ctrl)
+       if (k1->ctrl != KM_ANY && k2->ctrl != KM_ANY && k1->ctrl != k2->ctrl)
                return 0;
 
-       if(k1->alt != KM_ANY && k2->alt != KM_ANY && k1->alt != k2->alt)
+       if (k1->alt != KM_ANY && k2->alt != KM_ANY && k1->alt != k2->alt)
                return 0;
 
-       if(k1->oskey != KM_ANY && k2->oskey != KM_ANY && k1->oskey != k2->oskey)
+       if (k1->oskey != KM_ANY && k2->oskey != KM_ANY && k1->oskey != k2->oskey)
                return 0;
 
-       if(k1->keymodifier != k2->keymodifier)
+       if (k1->keymodifier != k2->keymodifier)
                return 0;
 
        return 1;
@@ -936,11 +1029,11 @@ static int WM_KEYMAP_UPDATE = 0;
 void WM_keyconfig_update_tag(wmKeyMap *km, wmKeyMapItem *kmi)
 {
        /* quick tag to do delayed keymap updates */
-       WM_KEYMAP_UPDATE= 1;
+       WM_KEYMAP_UPDATE = 1;
 
-       if(km)
+       if (km)
                km->flag |= KEYMAP_UPDATE;
-       if(kmi)
+       if (kmi)
                kmi->flag |= KMI_UPDATE;
 }
 
@@ -949,11 +1042,11 @@ static int wm_keymap_test_and_clear_update(wmKeyMap *km)
        wmKeyMapItem *kmi;
        int update;
        
-       update= (km->flag & KEYMAP_UPDATE);
+       update = (km->flag & KEYMAP_UPDATE);
        km->flag &= ~KEYMAP_UPDATE;
 
-       for(kmi=km->items.first; kmi; kmi=kmi->next) {
-               update= update || (kmi->flag & KMI_UPDATE);
+       for (kmi = km->items.first; kmi; kmi = kmi->next) {
+               update = update || (kmi->flag & KMI_UPDATE);
                kmi->flag &= ~KMI_UPDATE;
        }
        
@@ -962,12 +1055,12 @@ static int wm_keymap_test_and_clear_update(wmKeyMap *km)
 
 static wmKeyMap *wm_keymap_preset(wmWindowManager *wm, wmKeyMap *km)
 {
-       wmKeyConfig *keyconf= WM_keyconfig_active(wm);
+       wmKeyConfig *keyconf = WM_keyconfig_active(wm);
        wmKeyMap *keymap;
 
-       keymap= WM_keymap_list_find(&keyconf->keymaps, km->idname, km->spaceid, km->regionid);
-       if(!keymap)
-               keymap= WM_keymap_list_find(&wm->defaultconf->keymaps, km->idname, km->spaceid, km->regionid);
+       keymap = WM_keymap_list_find(&keyconf->keymaps, km->idname, km->spaceid, km->regionid);
+       if (!keymap)
+               keymap = WM_keymap_list_find(&wm->defaultconf->keymaps, km->idname, km->spaceid, km->regionid);
 
        return keymap;
 }
@@ -979,60 +1072,64 @@ void WM_keyconfig_update(wmWindowManager *wm)
        wmKeyMapDiffItem *kmdi;
        int compat_update = 0;
 
-       if(!WM_KEYMAP_UPDATE)
+       if (G.background)
+               return;
+       if (!WM_KEYMAP_UPDATE)
                return;
        
        /* update operator properties for non-modal user keymaps */
-       for(km=U.user_keymaps.first; km; km=km->next) {
-               if((km->flag & KEYMAP_MODAL) == 0) {
-                       for(kmdi=km->diff_items.first; kmdi; kmdi=kmdi->next) {
-                               if(kmdi->add_item)
+       for (km = U.user_keymaps.first; km; km = km->next) {
+               if ((km->flag & KEYMAP_MODAL) == 0) {
+                       for (kmdi = km->diff_items.first; kmdi; kmdi = kmdi->next) {
+                               if (kmdi->add_item)
                                        wm_keymap_item_properties_set(kmdi->add_item);
-                               if(kmdi->remove_item)
+                               if (kmdi->remove_item)
                                        wm_keymap_item_properties_set(kmdi->remove_item);
                        }
 
-                       for(kmi=km->items.first; kmi; kmi=kmi->next)
+                       for (kmi = km->items.first; kmi; kmi = kmi->next)
                                wm_keymap_item_properties_set(kmi);
                }
        }
 
        /* update U.user_keymaps with user key configuration changes */
-       for(km=wm->userconf->keymaps.first; km; km=km->next) {
+       for (km = wm->userconf->keymaps.first; km; km = km->next) {
                /* only diff if the user keymap was modified */
-               if(wm_keymap_test_and_clear_update(km)) {
+               if (wm_keymap_test_and_clear_update(km)) {
                        /* find keymaps */
-                       defaultmap= wm_keymap_preset(wm, km);
-                       addonmap= WM_keymap_list_find(&wm->addonconf->keymaps, km->idname, km->spaceid, km->regionid);
+                       defaultmap = wm_keymap_preset(wm, km);
+                       addonmap = WM_keymap_list_find(&wm->addonconf->keymaps, km->idname, km->spaceid, km->regionid);
 
                        /* diff */
-                       if(defaultmap)
+                       if (defaultmap)
                                wm_keymap_diff_update(&U.user_keymaps, defaultmap, addonmap, km);
                }
        }
 
        /* create user key configuration from preset + addon + user preferences */
-       for(km=wm->defaultconf->keymaps.first; km; km=km->next) {
+       for (km = wm->defaultconf->keymaps.first; km; km = km->next) {
                /* find keymaps */
-               defaultmap= wm_keymap_preset(wm, km);
-               addonmap= WM_keymap_list_find(&wm->addonconf->keymaps, km->idname, km->spaceid, km->regionid);
-               usermap= WM_keymap_list_find(&U.user_keymaps, km->idname, km->spaceid, km->regionid);
+               defaultmap = wm_keymap_preset(wm, km);
+               addonmap = WM_keymap_list_find(&wm->addonconf->keymaps, km->idname, km->spaceid, km->regionid);
+               usermap = WM_keymap_list_find(&U.user_keymaps, km->idname, km->spaceid, km->regionid);
+
+               wm_user_modal_keymap_set_items(wm, defaultmap);
 
                /* add */
-               kmn= wm_keymap_patch_update(&wm->userconf->keymaps, defaultmap, addonmap, usermap);
+               kmn = wm_keymap_patch_update(&wm->userconf->keymaps, defaultmap, addonmap, usermap);
 
-               if(kmn) {
-                       kmn->modal_items= km->modal_items;
-                       kmn->poll= km->poll;
+               if (kmn) {
+                       kmn->modal_items = km->modal_items;
+                       kmn->poll = km->poll;
                }
 
                /* in case of old non-diff keymaps, force extra update to create diffs */
                compat_update = compat_update || (usermap && !(usermap->flag & KEYMAP_DIFF));
        }
 
-       WM_KEYMAP_UPDATE= 0;
+       WM_KEYMAP_UPDATE = 0;
 
-       if(compat_update) {
+       if (compat_update) {
                WM_keyconfig_update_tag(NULL, NULL);
                WM_keyconfig_update(wm);
        }
@@ -1047,13 +1144,13 @@ wmKeyMap *WM_keymap_active(wmWindowManager *wm, wmKeyMap *keymap)
 {
        wmKeyMap *km;
 
-       if(!keymap)
+       if (!keymap)
                return NULL;
        
        /* first user defined keymaps */
-       km= WM_keymap_list_find(&wm->userconf->keymaps, keymap->idname, keymap->spaceid, keymap->regionid);
+       km = WM_keymap_list_find(&wm->userconf->keymaps, keymap->idname, keymap->spaceid, keymap->regionid);
 
-       if(km)
+       if (km)
                return km;
 
        return keymap;
@@ -1068,14 +1165,14 @@ void WM_keymap_restore_item_to_default(bContext *C, wmKeyMap *keymap, wmKeyMapIt
        wmKeyMap *defaultmap, *addonmap;
        wmKeyMapItem *orig;
 
-       if(!keymap)
+       if (!keymap)
                return;
 
        /* construct default keymap from preset + addons */
-       defaultmap= wm_keymap_preset(wm, keymap);
-       addonmap= WM_keymap_list_find(&wm->addonconf->keymaps, keymap->idname, keymap->spaceid, keymap->regionid);
+       defaultmap = wm_keymap_preset(wm, keymap);
+       addonmap = WM_keymap_list_find(&wm->addonconf->keymaps, keymap->idname, keymap->spaceid, keymap->regionid);
 
-       if(addonmap) {
+       if (addonmap) {
                defaultmap = wm_keymap_copy(defaultmap);
                wm_keymap_addon_add(defaultmap, addonmap);
        }
@@ -1083,22 +1180,22 @@ void WM_keymap_restore_item_to_default(bContext *C, wmKeyMap *keymap, wmKeyMapIt
        /* find original item */
        orig = WM_keymap_item_find_id(defaultmap, kmi->id);
 
-       if(orig) {
+       if (orig) {
                /* restore to original */
-               if(strcmp(orig->idname, kmi->idname) != 0) {
+               if (strcmp(orig->idname, kmi->idname) != 0) {
                        BLI_strncpy(kmi->idname, orig->idname, sizeof(kmi->idname));
                        WM_keymap_properties_reset(kmi, NULL);
                }
 
                if (orig->properties) {
-                       if(kmi->properties) {
+                       if (kmi->properties) {
                                IDP_FreeProperty(kmi->properties);
                                MEM_freeN(kmi->properties);
-                               kmi->properties= NULL;
+                               kmi->properties = NULL;
                        }
 
-                       kmi->properties= IDP_CopyProperty(orig->properties);
-                       kmi->ptr->data= kmi->properties;
+                       kmi->properties = IDP_CopyProperty(orig->properties);
+                       kmi->ptr->data = kmi->properties;
                }
 
                kmi->propvalue = orig->propvalue;
@@ -1115,7 +1212,7 @@ void WM_keymap_restore_item_to_default(bContext *C, wmKeyMap *keymap, wmKeyMapIt
        }
 
        /* free temporary keymap */
-       if(addonmap) {
+       if (addonmap) {
                WM_keymap_free(defaultmap);
                MEM_freeN(defaultmap);
        }
@@ -1127,9 +1224,9 @@ void WM_keymap_restore_to_default(wmKeyMap *keymap, bContext *C)
        wmKeyMap *usermap;
 
        /* remove keymap from U.user_keymaps and update */
-       usermap= WM_keymap_list_find(&U.user_keymaps, keymap->idname, keymap->spaceid, keymap->regionid);
+       usermap = WM_keymap_list_find(&U.user_keymaps, keymap->idname, keymap->spaceid, keymap->regionid);
 
-       if(usermap) {
+       if (usermap) {
                WM_keymap_free(usermap);
                BLI_freelinkN(&U.user_keymaps, usermap);
 
@@ -1142,7 +1239,7 @@ wmKeyMapItem *WM_keymap_item_find_id(wmKeyMap *keymap, int id)
 {
        wmKeyMapItem *kmi;
        
-       for (kmi=keymap->items.first; kmi; kmi=kmi->next) {
+       for (kmi = keymap->items.first; kmi; kmi = kmi->next) {
                if (kmi->id == id) {
                        return kmi;
                }
@@ -1155,7 +1252,7 @@ wmKeyMapItem *WM_keymap_item_find_id(wmKeyMap *keymap, int id)
 /* Needs to be kept up to date with Keymap and Operator naming */
 wmKeyMap *WM_keymap_guess_opname(const bContext *C, const char *opname)
 {
-       wmKeyMap *km=NULL;
+       wmKeyMap *km = NULL;
        SpaceLink *sl = CTX_wm_space_data(C);
        
        /* Window */
@@ -1174,6 +1271,10 @@ wmKeyMap *WM_keymap_guess_opname(const bContext *C, const char *opname)
        else if (strstr(opname, "MARKER_OT")) {
                km = WM_keymap_find_all(C, "Markers", 0, 0);
        }
+       /* Import/Export*/
+       else if (strstr(opname, "IMPORT_") || strstr(opname, "EXPORT_")) {
+               km = WM_keymap_find_all(C, "Window", 0, 0);
+       }
        
        
        /* 3D View */
@@ -1190,7 +1291,7 @@ wmKeyMap *WM_keymap_guess_opname(const bContext *C, const char *opname)
                km = WM_keymap_find_all(C, "Mesh", 0, 0);
                
                /* some mesh operators are active in object mode too, like add-prim */
-               if(km && km->poll && km->poll((bContext *)C)==0) {
+               if (km && km->poll && km->poll((bContext *)C) == 0) {
                        km = WM_keymap_find_all(C, "Object Mode", 0, 0);
                }
        }
@@ -1198,7 +1299,7 @@ wmKeyMap *WM_keymap_guess_opname(const bContext *C, const char *opname)
                km = WM_keymap_find_all(C, "Curve", 0, 0);
                
                /* some curve operators are active in object mode too, like add-prim */
-               if(km && km->poll && km->poll((bContext *)C)==0) {
+               if (km && km->poll && km->poll((bContext *)C) == 0) {
                        km = WM_keymap_find_all(C, "Object Mode", 0, 0);
                }
        }
@@ -1209,13 +1310,20 @@ wmKeyMap *WM_keymap_guess_opname(const bContext *C, const char *opname)
                km = WM_keymap_find_all(C, "Pose", 0, 0);
        }
        else if (strstr(opname, "SCULPT_OT")) {
-               km = WM_keymap_find_all(C, "Sculpt", 0, 0);
+               switch (CTX_data_mode_enum(C)) {
+                       case OB_MODE_SCULPT:
+                               km = WM_keymap_find_all(C, "Sculpt", 0, 0);
+                               break;
+                       case OB_MODE_EDIT:
+                               km = WM_keymap_find_all(C, "UV Sculpt", 0, 0);
+                               break;
+               }
        }
        else if (strstr(opname, "MBALL_OT")) {
                km = WM_keymap_find_all(C, "Metaball", 0, 0);
                
                /* some mball operators are active in object mode too, like add-prim */
-               if(km && km->poll && km->poll((bContext *)C)==0) {
+               if (km && km->poll && km->poll((bContext *)C) == 0) {
                        km = WM_keymap_find_all(C, "Object Mode", 0, 0);
                }
        }
@@ -1231,7 +1339,7 @@ wmKeyMap *WM_keymap_guess_opname(const bContext *C, const char *opname)
        else if (strstr(opname, "PAINT_OT")) {
                
                /* check for relevant mode */
-               switch(CTX_data_mode_enum(C)) {
+               switch (CTX_data_mode_enum(C)) {
                        case OB_MODE_WEIGHT_PAINT:
                                km = WM_keymap_find_all(C, "Weight Paint", 0, 0);
                                break;
@@ -1308,7 +1416,7 @@ wmKeyMap *WM_keymap_guess_opname(const bContext *C, const char *opname)
        else if (strstr(opname, "TRANSFORM_OT")) {
                
                /* check for relevant editor */
-               switch(sl->spacetype) {
+               switch (sl->spacetype) {
                        case SPACE_VIEW3D:
                                km = WM_keymap_find_all(C, "3D View", sl->spacetype, 0);
                                break;