2.5: warning fixes
[blender.git] / source / blender / editors / mesh / editmesh_mods.c
index 294c46b03f3b19efa15a0dc9ce9cebb28901259d..44f63427e5f250f37f6a9eab02d9f0ad2ab91df2 100644 (file)
@@ -67,6 +67,7 @@ editmesh_mods.c, UI level access, no geometry changes
 #include "BKE_material.h"
 #include "BKE_texture.h"
 #include "BKE_utildefines.h"
+#include "BKE_report.h"
 
 #include "IMB_imbuf_types.h"
 #include "IMB_imbuf.h"
@@ -91,8 +92,7 @@ editmesh_mods.c, UI level access, no geometry changes
 #include "BLO_sys_types.h" // for intptr_t support
 
 /* XXX */
-static void waitcursor() {}
-static void error() {}
+static void waitcursor(int val) {}
 static int pupmenu() {return 0;}
 
 /* ****************************** MIRROR **************** */
@@ -247,10 +247,9 @@ int EM_mask_init_backbuf_border(ViewContext *vc, short mcords[][2], short tot, s
        }
        else if(vc->v3d->drawtype<OB_SOLID || (vc->v3d->flag & V3D_ZBUF_SELECT)==0) return 0;
 
-       if(em_vertoffs==0) return 0;
-       
        buf= view3d_read_backbuf(vc, xmin, ymin, xmax, ymax);
        if(buf==NULL) return 0;
+       if(em_vertoffs==0) return 0;
 
        dr = buf->rect;
 
@@ -260,6 +259,7 @@ int EM_mask_init_backbuf_border(ViewContext *vc, short mcords[][2], short tot, s
        glColor3ub(0, 0, 0);
        
        /* yah, opengl doesn't do concave... tsk! */
+       ED_region_pixelspace(vc->ar);
        draw_triangulated(mcords, tot); 
        
        glBegin(GL_LINE_LOOP);  /* for zero sized masks, lines */
@@ -301,11 +301,11 @@ int EM_init_backbuf_circle(ViewContext *vc, short xs, short ys, short rads)
                else return 0;
        }
        else if(vc->v3d->drawtype<OB_SOLID || (vc->v3d->flag & V3D_ZBUF_SELECT)==0) return 0;
-       if(em_vertoffs==0) return 0;
        
        xmin= xs-rads; xmax= xs+rads;
        ymin= ys-rads; ymax= ys+rads;
        buf= view3d_read_backbuf(vc, xmin, ymin, xmax, ymax);
+       if(em_vertoffs==0) return 0;
        if(buf==NULL) return 0;
 
        dr = buf->rect;
@@ -642,26 +642,25 @@ static int unified_findnearest(ViewContext *vc, EditVert **eve, EditEdge **eed,
 
 /* ****************  SIMILAR "group" SELECTS. FACE, EDGE AND VERTEX ************** */
 
-/* selects new faces/edges/verts based on the
- existing selection
+/* selects new faces/edges/verts based on the existing selection */
 
-FACES GROUP
- mode 1: same material
- mode 2: same image
- mode 3: same area
- mode 4: same perimeter
- mode 5: same normal
- mode 6: same co-planer
-*/
+/* FACES GROUP */
+
+#define SIMFACE_MATERIAL       201
+#define SIMFACE_IMAGE          202
+#define SIMFACE_AREA           203
+#define SIMFACE_PERIMETER      204
+#define SIMFACE_NORMAL         205
+#define SIMFACE_COPLANAR       206
 
 static EnumPropertyItem prop_simface_types[] = {
-       {1, "MATERIAL", "Material", ""},
-       {2, "IMAGE", "Image", ""},
-       {3, "AREA", "Area", ""},
-       {4, "PERIMETER", "Perimeter", ""},
-       {5, "NORMAL", "Normal", ""},
-       {6, "COPLANAR", "Co-planar", ""},
-       {0, NULL, NULL, NULL}
+       {SIMFACE_MATERIAL, "MATERIAL", 0, "Material", ""},
+       {SIMFACE_IMAGE, "IMAGE", 0, "Image", ""},
+       {SIMFACE_AREA, "AREA", 0, "Area", ""},
+       {SIMFACE_PERIMETER, "PERIMETER", 0, "Perimeter", ""},
+       {SIMFACE_NORMAL, "NORMAL", 0, "Normal", ""},
+       {SIMFACE_COPLANAR, "COPLANAR", 0, "Co-planar", ""},
+       {0, NULL, 0, NULL, NULL}
 };
 
 
@@ -695,12 +694,11 @@ static int similar_face_select__internal(Scene *scene, EditMesh *em, int mode)
        if (!ok || !deselcount) /* no data selected OR no more data to select */
                return 0;
        
-       /*if mode is 3 then record face areas, 4 record perimeter */
-       if (mode==3) {
+       if (mode==SIMFACE_AREA) {
                for(efa= em->faces.first; efa; efa= efa->next) {
                        efa->tmp.fp= EM_face_area(efa);
                }
-       } else if (mode==4) {
+       } else if (mode==SIMFACE_PERIMETER) {
                for(efa= em->faces.first; efa; efa= efa->next) {
                        efa->tmp.fp= EM_face_perimeter(efa);
                }
@@ -708,7 +706,7 @@ static int similar_face_select__internal(Scene *scene, EditMesh *em, int mode)
        
        for(base_efa= em->faces.first; base_efa; base_efa= base_efa->next) {
                if (base_efa->f1) { /* This was one of the faces originaly selected */
-                       if (mode==1) { /* same material */
+                       if (mode==SIMFACE_MATERIAL) { /* same material */
                                for(efa= em->faces.first; efa; efa= efa->next) {
                                        if (
                                                !(efa->f & SELECT) &&
@@ -722,7 +720,7 @@ static int similar_face_select__internal(Scene *scene, EditMesh *em, int mode)
                                                        return selcount;
                                        }
                                }
-                       } else if (mode==2) { /* same image */
+                       } else if (mode==SIMFACE_IMAGE) { /* same image */
                                MTFace *tf, *base_tf;
 
                                base_tf = (MTFace*)CustomData_em_get(&em->fdata, base_efa->data,
@@ -745,7 +743,7 @@ static int similar_face_select__internal(Scene *scene, EditMesh *em, int mode)
                                                }
                                        }
                                }
-                       } else if (mode==3 || mode==4) { /* same area OR same perimeter, both use the same temp var */
+                       } else if (mode==SIMFACE_AREA || mode==SIMFACE_PERIMETER) { /* same area OR same perimeter, both use the same temp var */
                                for(efa= em->faces.first; efa; efa= efa->next) {
                                        if (
                                                (!(efa->f & SELECT) && !efa->h) &&
@@ -758,7 +756,7 @@ static int similar_face_select__internal(Scene *scene, EditMesh *em, int mode)
                                                        return selcount;
                                        }
                                }
-                       } else if (mode==5) { /* same normal */
+                       } else if (mode==SIMFACE_NORMAL) {
                                float angle;
                                for(efa= em->faces.first; efa; efa= efa->next) {
                                        if (!(efa->f & SELECT) && !efa->h) {
@@ -772,7 +770,7 @@ static int similar_face_select__internal(Scene *scene, EditMesh *em, int mode)
                                                }
                                        }
                                }
-                       } else if (mode==6) { /* same planer */
+                       } else if (mode==SIMFACE_COPLANAR) { /* same planer */
                                float angle, base_dot, dot;
                                base_dot= Inpf(base_efa->cent, base_efa->n);
                                for(efa= em->faces.first; efa; efa= efa->next) {
@@ -801,7 +799,7 @@ static int similar_face_select_exec(bContext *C, wmOperator *op)
        Scene *scene= CTX_data_scene(C);
        Object *obedit= CTX_data_edit_object(C);
        Mesh *me= obedit->data;
-       EditMesh *em= me->edit_mesh
+       EditMesh *em= BKE_mesh_get_editmesh(me)
 
        int selcount = similar_face_select__internal(scene, em, RNA_int_get(op->ptr, "type"));
        
@@ -809,52 +807,35 @@ static int similar_face_select_exec(bContext *C, wmOperator *op)
                /* here was an edge-mode only select flush case, has to be generalized */
                EM_selectmode_flush(em);
                WM_event_add_notifier(C, NC_OBJECT|ND_GEOM_SELECT, obedit);
+               BKE_mesh_end_editmesh(me, em);
                return OPERATOR_FINISHED;
        }
        
+       BKE_mesh_end_editmesh(me, em);
        return OPERATOR_CANCELLED;
 }      
 
-void MESH_OT_similar_face_select(wmOperatorType *ot)
-{
-       /* identifiers */
-       ot->name= "Similar Face Select";
-       ot->idname= "MESH_OT_similar_face_select";
-       
-       /* api callbacks */
-       ot->invoke= WM_menu_invoke;
-       ot->exec= similar_face_select_exec;
-       ot->poll= ED_operator_editmesh;
-       
-       /* flags */
-       ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
-       
-       /* properties */
-       RNA_def_enum(ot->srna, "type", prop_simface_types, 0, "Type", "");
-}
-
 /* ***************************************************** */
 
-/*
-EDGE GROUP
- mode 1: same length
- mode 2: same direction
- mode 3: same number of face users
- mode 4: similar face angles.
- mode 5: similar crease
- mode 6: similar seam
- mode 7: similar sharp
-*/
+/* EDGE GROUP */
+
+#define SIMEDGE_LENGTH         101
+#define SIMEDGE_DIR                    102
+#define SIMEDGE_FACE           103
+#define SIMEDGE_FACE_ANGLE     104
+#define SIMEDGE_CREASE         105
+#define SIMEDGE_SEAM           106
+#define SIMEDGE_SHARP          107
 
 static EnumPropertyItem prop_simedge_types[] = {
-       {1, "LENGTH", "Length", ""},
-       {2, "DIR", "Direction", ""},
-       {3, "FACE", "Amount of Vertices in Face", ""},
-       {4, "FACE_ANGLE", "Face Angles", ""},
-       {5, "CREASE", "Crease", ""},
-       {6, "SEAM", "Seam", ""},
-       {7, "SHARP", "Sharpness", ""},
-       {0, NULL, NULL, NULL}
+       {SIMEDGE_LENGTH, "LENGTH", 0, "Length", ""},
+       {SIMEDGE_DIR, "DIR", 0, "Direction", ""},
+       {SIMEDGE_FACE, "FACE", 0, "Amount of Vertices in Face", ""},
+       {SIMEDGE_FACE_ANGLE, "FACE_ANGLE", 0, "Face Angles", ""},
+       {SIMEDGE_CREASE, "CREASE", 0, "Crease", ""},
+       {SIMEDGE_SEAM, "SEAM", 0, "Seam", ""},
+       {SIMEDGE_SHARP, "SHARP", 0, "Sharpness", ""},
+       {0, NULL, 0, NULL, NULL}
 };
 
 static int similar_edge_select__internal(Scene *scene, EditMesh *em, int mode)
@@ -881,19 +862,19 @@ static int similar_edge_select__internal(Scene *scene, EditMesh *em, int mode)
                        /* set all eed->tmp.l to 0 we use it later.
                        for counting face users*/
                        eed->tmp.l=0;
-                       eed->f2=0; /* only for mode 4, edge animations */
+                       eed->f2=0; /* only for mode SIMEDGE_FACE_ANGLE, edge animations */
                }
        }
        
        if (!ok || !deselcount) /* no data selected OR no more data to select*/
                return 0;
        
-       if (mode==1) { /*store length*/
+       if (mode==SIMEDGE_LENGTH) { /*store length*/
                for(eed= em->edges.first; eed; eed= eed->next) {
                        if (!eed->h) /* dont calc data for hidden edges*/
                                eed->tmp.fp= VecLenf(eed->v1->co, eed->v2->co);
                }
-       } else if (mode==3) { /*store face users*/
+       } else if (mode==SIMEDGE_FACE) { /*store face users*/
                EditFace *efa;
                /* cound how many faces each edge uses use tmp->l */
                for(efa= em->faces.first; efa; efa= efa->next) {
@@ -902,7 +883,7 @@ static int similar_edge_select__internal(Scene *scene, EditMesh *em, int mode)
                        efa->e3->tmp.l++;
                        if (efa->e4) efa->e4->tmp.l++;
                }
-       } else if (mode==4) { /*store edge angles */
+       } else if (mode==SIMEDGE_FACE_ANGLE) { /*store edge angles */
                EditFace *efa;
                int j;
                /* cound how many faces each edge uses use tmp.l */
@@ -944,7 +925,7 @@ static int similar_edge_select__internal(Scene *scene, EditMesh *em, int mode)
        
        for(base_eed= em->edges.first; base_eed; base_eed= base_eed->next) {
                if (base_eed->f1) {
-                       if (mode==1) { /* same length */
+                       if (mode==SIMEDGE_LENGTH) { /* same length */
                                for(eed= em->edges.first; eed; eed= eed->next) {
                                        if (
                                                !(eed->f & SELECT) &&
@@ -958,7 +939,7 @@ static int similar_edge_select__internal(Scene *scene, EditMesh *em, int mode)
                                                        return selcount;
                                        }
                                }
-                       } else if (mode==2) { /* same direction */
+                       } else if (mode==SIMEDGE_DIR) { /* same direction */
                                float base_dir[3], dir[3], angle;
                                VecSubf(base_dir, base_eed->v1->co, base_eed->v2->co);
                                for(eed= em->edges.first; eed; eed= eed->next) {
@@ -978,7 +959,7 @@ static int similar_edge_select__internal(Scene *scene, EditMesh *em, int mode)
                                                }
                                        }
                                }
-                       } else if (mode==3) { /* face users */                          
+                       } else if (mode==SIMEDGE_FACE) { /* face users */                               
                                for(eed= em->edges.first; eed; eed= eed->next) {
                                        if (
                                                !(eed->f & SELECT) &&
@@ -992,7 +973,7 @@ static int similar_edge_select__internal(Scene *scene, EditMesh *em, int mode)
                                                        return selcount;
                                        }
                                }
-                       } else if (mode==4 && base_eed->f2==2) { /* edge angles, f2==2 means the edge has an angle. */                          
+                       } else if (mode==SIMEDGE_FACE_ANGLE && base_eed->f2==2) { /* edge angles, f2==2 means the edge has an angle. */                         
                                for(eed= em->edges.first; eed; eed= eed->next) {
                                        if (
                                                !(eed->f & SELECT) &&
@@ -1007,7 +988,7 @@ static int similar_edge_select__internal(Scene *scene, EditMesh *em, int mode)
                                                        return selcount;
                                        }
                                }
-                       } else if (mode==5) { /* edge crease */
+                       } else if (mode==SIMEDGE_CREASE) { /* edge crease */
                                for(eed= em->edges.first; eed; eed= eed->next) {
                                        if (
                                                !(eed->f & SELECT) &&
@@ -1021,7 +1002,7 @@ static int similar_edge_select__internal(Scene *scene, EditMesh *em, int mode)
                                                        return selcount;
                                        }
                                }
-                       } else if (mode==6) { /* edge seam */
+                       } else if (mode==SIMEDGE_SEAM) { /* edge seam */
                                for(eed= em->edges.first; eed; eed= eed->next) {
                                        if (
                                                !(eed->f & SELECT) &&
@@ -1035,7 +1016,7 @@ static int similar_edge_select__internal(Scene *scene, EditMesh *em, int mode)
                                                        return selcount;
                                        }
                                }
-                       } else if (mode==7) { /* edge sharp */
+                       } else if (mode==SIMEDGE_SHARP) { /* edge sharp */
                                for(eed= em->edges.first; eed; eed= eed->next) {
                                        if (
                                                !(eed->f & SELECT) &&
@@ -1060,7 +1041,7 @@ static int similar_edge_select_exec(bContext *C, wmOperator *op)
        Scene *scene= CTX_data_scene(C);
        Object *obedit= CTX_data_edit_object(C);
        Mesh *me= obedit->data;
-       EditMesh *em= me->edit_mesh
+       EditMesh *em= BKE_mesh_get_editmesh(me)
 
        int selcount = similar_edge_select__internal(scene, em, RNA_int_get(op->ptr, "type"));
        
@@ -1068,30 +1049,14 @@ static int similar_edge_select_exec(bContext *C, wmOperator *op)
                /* here was an edge-mode only select flush case, has to be generalized */
                EM_selectmode_flush(em);
                WM_event_add_notifier(C, NC_OBJECT|ND_GEOM_SELECT, obedit);
+               BKE_mesh_end_editmesh(me, em);
                return OPERATOR_FINISHED;
        }
        
+       BKE_mesh_end_editmesh(me, em);
        return OPERATOR_CANCELLED;
 }
 
-void MESH_OT_similar_edge_select(wmOperatorType *ot)
-{
-       /* identifiers */
-       ot->name= "Similar Edge Select";
-       ot->idname= "MESH_OT_similar_edge_select";
-       
-       /* api callbacks */
-       ot->invoke= WM_menu_invoke;
-       ot->exec= similar_edge_select_exec;
-       ot->poll= ED_operator_editmesh;
-       
-       /* flags */
-       ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
-       
-       /* properties */
-       RNA_def_enum(ot->srna, "type", prop_simedge_types, 0, "Type", "");
-}
-
 /* ********************************* */
 
 /*
@@ -1100,11 +1065,16 @@ VERT GROUP
  mode 2: same number of face users
  mode 3: same vertex groups
 */
+
+#define SIMVERT_NORMAL 0
+#define SIMVERT_FACE   1
+#define SIMVERT_VGROUP 2
+
 static EnumPropertyItem prop_simvertex_types[] = {
-       {0, "NORMAL", "Normal", ""},
-       {1, "FACE", "Amount of Vertices in Face", ""},
-       {2, "VGROUP", "Vertex Groups", ""},
-       {0, NULL, NULL, NULL}
+       {SIMVERT_NORMAL, "NORMAL", 0, "Normal", ""},
+       {SIMVERT_FACE, "FACE", 0, "Amount of Vertices in Face", ""},
+       {SIMVERT_VGROUP, "VGROUP", 0, "Vertex Groups", ""},
+       {0, NULL, 0, NULL, NULL}
 };
 
 
@@ -1113,13 +1083,14 @@ static int similar_vert_select_exec(bContext *C, wmOperator *op)
        Scene *scene= CTX_data_scene(C);
        Object *obedit= CTX_data_edit_object(C);
        Mesh *me= obedit->data;
-       EditMesh *em= me->edit_mesh
+       EditMesh *em= BKE_mesh_get_editmesh(me)
        EditVert *eve, *base_eve=NULL;
        unsigned int selcount=0; /* count how many new edges we select*/
        
        /*count how many visible selected edges there are,
        so we can return when there are none left */
        unsigned int deselcount=0;
+       int mode= RNA_enum_get(op->ptr, "type");
        
        short ok=0;
        float thresh= scene->toolsettings->select_thresh;
@@ -1139,10 +1110,12 @@ static int similar_vert_select_exec(bContext *C, wmOperator *op)
                
        }
        
-       if (!ok || !deselcount) /* no data selected OR no more data to select*/
+       if (!ok || !deselcount) { /* no data selected OR no more data to select*/
+               BKE_mesh_end_editmesh(me, em);
                return 0;
+       }
        
-       if(RNA_enum_is_equal(op->ptr, "type", "FACE")) {
+       if(mode == SIMVERT_FACE) {
                /* store face users */
                EditFace *efa;
                
@@ -1159,7 +1132,7 @@ static int similar_vert_select_exec(bContext *C, wmOperator *op)
        for(base_eve= em->verts.first; base_eve; base_eve= base_eve->next) {
                if (base_eve->f1) {
                                
-                       if(RNA_enum_is_equal(op->ptr, "type", "NORMAL")) {
+                       if(mode == SIMVERT_NORMAL) {
                                float angle;
                                for(eve= em->verts.first; eve; eve= eve->next) {
                                        if (!(eve->f & SELECT) && !eve->h) {
@@ -1168,13 +1141,15 @@ static int similar_vert_select_exec(bContext *C, wmOperator *op)
                                                        eve->f |= SELECT;
                                                        selcount++;
                                                        deselcount--;
-                                                       if (!deselcount) /*have we selected all posible faces?, if so return*/
+                                                       if (!deselcount) {/*have we selected all posible faces?, if so return*/
+                                                               BKE_mesh_end_editmesh(me, em);
                                                                return selcount;
+                                                       }
                                                }
                                        }
                                }
                        }
-                       else if(RNA_enum_is_equal(op->ptr, "type", "FACE")) {
+                       else if(mode == SIMVERT_FACE) {
                                for(eve= em->verts.first; eve; eve= eve->next) {
                                        if (
                                                !(eve->f & SELECT) &&
@@ -1184,20 +1159,24 @@ static int similar_vert_select_exec(bContext *C, wmOperator *op)
                                                eve->f |= SELECT;
                                                selcount++;
                                                deselcount--;
-                                               if (!deselcount) /*have we selected all posible faces?, if so return*/
+                                               if (!deselcount) {/*have we selected all posible faces?, if so return*/
+                                                       BKE_mesh_end_editmesh(me, em);
                                                        return selcount;
+                                               }
                                        }
                                }
                        } 
-                       else if(RNA_enum_is_equal(op->ptr, "type", "VGROUP")) {
+                       else if(mode == SIMVERT_VGROUP) {
                                MDeformVert *dvert, *base_dvert;
                                short i, j; /* weight index */
 
                                base_dvert= CustomData_em_get(&em->vdata, base_eve->data,
                                        CD_MDEFORMVERT);
 
-                               if (!base_dvert || base_dvert->totweight == 0)
+                               if (!base_dvert || base_dvert->totweight == 0) {
+                                       BKE_mesh_end_editmesh(me, em);
                                        return selcount;
+                               }
                                
                                for(eve= em->verts.first; eve; eve= eve->next) {
                                        dvert= CustomData_em_get(&em->vdata, eve->data,
@@ -1212,8 +1191,10 @@ static int similar_vert_select_exec(bContext *C, wmOperator *op)
                                                                        eve->f |= SELECT;
                                                                        selcount++;
                                                                        deselcount--;
-                                                                       if (!deselcount) /*have we selected all posible faces?, if so return*/
+                                                                       if (!deselcount) { /*have we selected all posible faces?, if so return*/
+                                                                               BKE_mesh_end_editmesh(me, em);
                                                                                return selcount;
+                                                                       }
                                                                        break;
                                                                }
                                                        }
@@ -1226,27 +1207,83 @@ static int similar_vert_select_exec(bContext *C, wmOperator *op)
 
        if(selcount) {
                WM_event_add_notifier(C, NC_OBJECT|ND_GEOM_SELECT, obedit);
+               BKE_mesh_end_editmesh(me, em);
                return OPERATOR_FINISHED;
        }
+
+       BKE_mesh_end_editmesh(me, em);
        return OPERATOR_CANCELLED;
 }
 
-void MESH_OT_similar_vertex_select(wmOperatorType *ot)
+static int select_similar_exec(bContext *C, wmOperator *op)
+{
+       int type= RNA_enum_get(op->ptr, "type");
+
+       if(type < 100)
+               return similar_vert_select_exec(C, op);
+       else if(type < 200)
+               return similar_edge_select_exec(C, op);
+       else
+               return similar_face_select_exec(C, op);
+}
+
+static EnumPropertyItem *select_similar_type_itemf(bContext *C, PointerRNA *ptr, int *free)
+{
+       Object *obedit;
+       EnumPropertyItem *item= NULL;
+       int totitem= 0;
+       
+       if(C==NULL) {
+               /* needed for doc generation */
+               RNA_enum_items_add(&item, &totitem, prop_simvertex_types);
+               RNA_enum_items_add(&item, &totitem, prop_simedge_types);
+               RNA_enum_items_add(&item, &totitem, prop_simface_types);
+               RNA_enum_item_end(&item, &totitem);
+               *free= 1;
+               
+               return item;
+       }
+       
+       obedit= CTX_data_edit_object(C);
+       
+       if(obedit && obedit->type == OB_MESH) {
+               EditMesh *em= BKE_mesh_get_editmesh(obedit->data); 
+
+               if(em->selectmode & SCE_SELECT_VERTEX)
+                       RNA_enum_items_add(&item, &totitem, prop_simvertex_types);
+               else if(em->selectmode & SCE_SELECT_EDGE)
+                       RNA_enum_items_add(&item, &totitem, prop_simedge_types);
+               else if(em->selectmode & SCE_SELECT_FACE)
+                       RNA_enum_items_add(&item, &totitem, prop_simface_types);
+               RNA_enum_item_end(&item, &totitem);
+
+               *free= 1;
+
+               return item;
+       }
+       
+       return NULL;
+}
+
+void MESH_OT_select_similar(wmOperatorType *ot)
 {
+       PropertyRNA *prop;
+
        /* identifiers */
-       ot->name= "Similar Vertex Select";
-       ot->idname= "MESH_OT_similar_vertex_select";
+       ot->name= "Select Similar";
+       ot->idname= "MESH_OT_select_similar";
        
        /* api callbacks */
        ot->invoke= WM_menu_invoke;
-       ot->exec= similar_vert_select_exec;
+       ot->exec= select_similar_exec;
        ot->poll= ED_operator_editmesh;
        
        /* flags */
        ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
        
        /* properties */
-       RNA_def_enum(ot->srna, "type", prop_simvertex_types, 0, "Type", "");
+       prop= RNA_def_enum(ot->srna, "type", prop_simvertex_types, 0, "Type", "");
+       RNA_def_enum_funcs(prop, select_similar_type_itemf);
 }
 
 /* ******************************************* */
@@ -1379,7 +1416,7 @@ void EM_mesh_copy_edge(EditMesh *em, short type)
        }
 }
 
-void EM_mesh_copy_face(EditMesh *em, short type)
+void EM_mesh_copy_face(EditMesh *em, wmOperator *op, short type)
 {
        short change=0;
        
@@ -1405,7 +1442,7 @@ void EM_mesh_copy_face(EditMesh *em, short type)
                break;
        case 2: /* copy image */
                if (!tf_act) {
-                       error("mesh has no uv/image layers");
+                       BKE_report(op->reports, RPT_ERROR, "Mesh has no uv/image layers.");
                        return;
                }
                for(efa=em->faces.first; efa; efa=efa->next) {
@@ -1426,7 +1463,7 @@ void EM_mesh_copy_face(EditMesh *em, short type)
 
        case 3: /* copy UV's */
                if (!tf_act) {
-                       error("mesh has no uv/image layers");
+                       BKE_report(op->reports, RPT_ERROR, "Mesh has no uv/image layers.");
                        return;
                }
                for(efa=em->faces.first; efa; efa=efa->next) {
@@ -1439,7 +1476,7 @@ void EM_mesh_copy_face(EditMesh *em, short type)
                break;
        case 4: /* mode's */
                if (!tf_act) {
-                       error("mesh has no uv/image layers");
+                       BKE_report(op->reports, RPT_ERROR, "Mesh has no uv/image layers.");
                        return;
                }
                for(efa=em->faces.first; efa; efa=efa->next) {
@@ -1452,7 +1489,7 @@ void EM_mesh_copy_face(EditMesh *em, short type)
                break;
        case 5: /* copy transp's */
                if (!tf_act) {
-                       error("mesh has no uv/image layers");
+                       BKE_report(op->reports, RPT_ERROR, "Mesh has no uv/image layers.");
                        return;
                }
                for(efa=em->faces.first; efa; efa=efa->next) {
@@ -1466,7 +1503,7 @@ void EM_mesh_copy_face(EditMesh *em, short type)
 
        case 6: /* copy vcols's */
                if (!mcol_act) {
-                       error("mesh has no color layers");
+                       BKE_report(op->reports, RPT_ERROR, "Mesh has no color layers.");
                        return;
                } else {
                        /* guess the 4th color if needs be */
@@ -1508,7 +1545,7 @@ void EM_mesh_copy_face(EditMesh *em, short type)
 }
 
 
-void EM_mesh_copy_face_layer(EditMesh *em, short type) 
+void EM_mesh_copy_face_layer(EditMesh *em, wmOperator *op, short type) 
 {
        short change=0;
        
@@ -1523,7 +1560,7 @@ void EM_mesh_copy_face_layer(EditMesh *em, short type)
        case 8:
        case 9:
                if (CustomData_number_of_layers(&em->fdata, CD_MTFACE)<2) {
-                       error("mesh does not have multiple uv/image layers");
+                       BKE_report(op->reports, RPT_ERROR, "mesh does not have multiple uv/image layers");
                        return;
                } else {
                        int layer_orig_idx, layer_idx;
@@ -1550,7 +1587,7 @@ void EM_mesh_copy_face_layer(EditMesh *em, short type)
 
        case 10: /* select vcol layers - make sure this stays in sync with above code */
                if (CustomData_number_of_layers(&em->fdata, CD_MCOL)<2) {
-                       error("mesh does not have multiple color layers");
+                       BKE_report(op->reports, RPT_ERROR, "mesh does not have multiple color layers");
                        return;
                } else {
                        int layer_orig_idx, layer_idx;
@@ -1639,7 +1676,7 @@ void EM_mesh_copy_face_layer(EditMesh *em, short type)
 
 
 /* ctrl+c in mesh editmode */
-void mesh_copy_menu(EditMesh *em)
+void mesh_copy_menu(EditMesh *em, wmOperator *op)
 {
        EditSelection *ese;
        int ret;
@@ -1671,9 +1708,9 @@ void mesh_copy_menu(EditMesh *em)
                if (ret<1) return;
                
                if (ret<=6) {
-                       EM_mesh_copy_face(em, ret);
+                       EM_mesh_copy_face(em, op, ret);
                } else {
-                       EM_mesh_copy_face_layer(em, ret);
+                       EM_mesh_copy_face_layer(em, op, ret);
                }
        }
 }
@@ -1912,11 +1949,14 @@ static void edgering_select(EditMesh *em, EditEdge *startedge, int select)
        }
 }
 
-void loop_multiselect(EditMesh *em, int looptype)
+static int loop_multiselect(bContext *C, wmOperator *op)
 {
+       Object *obedit= CTX_data_edit_object(C);
+       EditMesh *em= BKE_mesh_get_editmesh(((Mesh *)obedit->data));
        EditEdge *eed;
        EditEdge **edarray;
        int edindex, edfirstcount;
+       int looptype= RNA_boolean_get(op->ptr, "ring");
        
        /* sets em->totedgesel */
        EM_nedges_selected(em);
@@ -1948,7 +1988,30 @@ void loop_multiselect(EditMesh *em, int looptype)
        }
        MEM_freeN(edarray);
 //     if (EM_texFaceCheck())
+       
+       WM_event_add_notifier(C, NC_OBJECT|ND_GEOM_SELECT, obedit);
+
+       BKE_mesh_end_editmesh(obedit->data, em);
+       return OPERATOR_FINISHED;       
+}
+
+void MESH_OT_loop_multi_select(wmOperatorType *ot)
+{
+       /* identifiers */
+       ot->name= "Multi Select Loops";
+       ot->idname= "MESH_OT_loop_multi_select";
+       
+       /* api callbacks */
+       ot->exec= loop_multiselect;
+       ot->poll= ED_operator_editmesh;
+       
+       /* flags */
+       ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
+       
+       /* properties */
+       RNA_def_boolean(ot->srna, "ring", 0, "Ring", "");
 }
+
                
 /* ***************** MAIN MOUSE SELECTION ************** */
 
@@ -1998,17 +2061,12 @@ static void mouse_mesh_loop(bContext *C, short mval[2], short extend, short ring
        }
 }
 
-static int mesh_loop_select_invoke(bContext *C, wmOperator *op, wmEvent *event)
+static int mesh_select_loop_invoke(bContext *C, wmOperator *op, wmEvent *event)
 {
-       ARegion *ar= CTX_wm_region(C);
-       short mval[2];  
-       
-       mval[0]= event->x - ar->winrct.xmin;
-       mval[1]= event->y - ar->winrct.ymin;
        
        view3d_operator_needs_opengl(C);
        
-       mouse_mesh_loop(C, mval, RNA_boolean_get(op->ptr, "extend"),
+       mouse_mesh_loop(C, event->mval, RNA_boolean_get(op->ptr, "extend"),
                                        RNA_boolean_get(op->ptr, "ring"));
        
        /* cannot do tweaks for as long this keymap is after transform map */
@@ -2022,7 +2080,7 @@ void MESH_OT_loop_select(wmOperatorType *ot)
        ot->idname= "MESH_OT_loop_select";
        
        /* api callbacks */
-       ot->invoke= mesh_loop_select_invoke;
+       ot->invoke= mesh_select_loop_invoke;
        ot->poll= ED_operator_editmesh;
        
        /* flags */
@@ -2106,24 +2164,19 @@ static void mouse_mesh_shortest_path(bContext *C, short mval[2])
 
 static int mesh_shortest_path_select_invoke(bContext *C, wmOperator *op, wmEvent *event)
 {
-       ARegion *ar= CTX_wm_region(C);
-       short mval[2];  
-       
-       mval[0]= event->x - ar->winrct.xmin;
-       mval[1]= event->y - ar->winrct.ymin;
        
        view3d_operator_needs_opengl(C);
 
-       mouse_mesh_shortest_path(C, mval);
+       mouse_mesh_shortest_path(C, event->mval);
        
        return OPERATOR_FINISHED;
 }
        
-void MESH_OT_shortest_path_select(wmOperatorType *ot)
+void MESH_OT_select_shortest_path(wmOperatorType *ot)
 {
        /* identifiers */
        ot->name= "Shortest Path Select";
-       ot->idname= "MESH_OT_shortest_path_select";
+       ot->idname= "MESH_OT_select_shortest_path";
        
        /* api callbacks */
        ot->invoke= mesh_shortest_path_select_invoke;
@@ -2207,80 +2260,122 @@ void mouse_mesh(bContext *C, short mval[2], short extend)
        
 }
 
-// XXX should we use CTX_scene(C)->selectmode & SCE_SELECT_FACE like it was in the past ? calls selectconnected_delimit_mesh_all if true
-void selectconnected_mesh_all(EditMesh *em)
+/* *********** select linked ************* */
+
+/* for use with selectconnected_delimit_mesh only! */
+#define is_edge_delimit_ok(eed) ((eed->tmp.l == 1) && (eed->seam==0))
+#define is_face_tag(efa) is_edge_delimit_ok(efa->e1) || is_edge_delimit_ok(efa->e2) || is_edge_delimit_ok(efa->e3) || (efa->v4 && is_edge_delimit_ok(efa->e4))
+
+#define face_tag(efa)\
+if(efa->v4)    efa->tmp.l=             efa->e1->tmp.l= efa->e2->tmp.l= efa->e3->tmp.l= efa->e4->tmp.l= 1;\
+else           efa->tmp.l=             efa->e1->tmp.l= efa->e2->tmp.l= efa->e3->tmp.l= 1;
+
+/* all - 1) use all faces for extending the selection  2) only use the mouse face
+* sel - 1) select  0) deselect 
+* */
+
+/* legacy warning, this function combines too much :) */
+static int select_linked_limited_invoke(ViewContext *vc, short all, short sel)
 {
-       EditVert *v1,*v2;
+       EditMesh *em= vc->em;
+       EditFace *efa;
        EditEdge *eed;
-       short done=1, toggle=0;
-
-       if(em->edges.first==0) return;
+       EditVert *eve;
+       short done=1, change=0;
+       
+       if(em->faces.first==0) return OPERATOR_CANCELLED;
+       
+       /* flag all edges+faces as off*/
+       for(eed= em->edges.first; eed; eed= eed->next)
+               eed->tmp.l=0;
+       
+       for(efa= em->faces.first; efa; efa= efa->next) {
+               efa->tmp.l = 0;
+       }
+       
+       if (all) {
+               // XXX verts?
+               for(eed= em->edges.first; eed; eed= eed->next) {
+                       if(eed->f & SELECT)
+                               eed->tmp.l= 1;
+               }
+               for(efa= em->faces.first; efa; efa= efa->next) {
+                       
+                       if (efa->f & SELECT) {
+                               face_tag(efa);
+                       } else {
+                               efa->tmp.l = 0;
+                       }
+               }
+       } 
+       else {
+               if( unified_findnearest(vc, &eve, &eed, &efa) ) {
+                       
+                       if(efa) {
+                               efa->tmp.l = 1;
+                               face_tag(efa);
+                       }
+                       else if(eed)
+                               eed->tmp.l= 1;
+                       else {
+                               for(eed= em->edges.first; eed; eed= eed->next)
+                                       if(eed->v1==eve || eed->v2==eve)
+                                               break;
+                               eed->tmp.l= 1;
+                       }
+               }
+               else
+                       return OPERATOR_FINISHED;
+       }
        
        while(done==1) {
                done= 0;
-               
-               toggle++;
-               if(toggle & 1) eed= em->edges.first;
-               else eed= em->edges.last;
-               
-               while(eed) {
-                       v1= eed->v1;
-                       v2= eed->v2;
-                       if(eed->h==0) {
-                               if(v1->f & SELECT) {
-                                       if( (v2->f & SELECT)==0 ) {
-                                               v2->f |= SELECT;
-                                               done= 1;
-                                       }
-                               }
-                               else if(v2->f & SELECT) {
-                                       if( (v1->f & SELECT)==0 ) {
-                                               v1->f |= SELECT;
-                                               done= 1;
-                                       }
+               /* simple algo - select all faces that have a selected edge
+               * this intern selects the edge, repeat until nothing is left to do */
+               for(efa= em->faces.first; efa; efa= efa->next) {
+                       if ((efa->tmp.l == 0) && (!efa->h)) {
+                               if (is_face_tag(efa)) {
+                                       face_tag(efa);
+                                       done= 1;
                                }
                        }
-                       if(toggle & 1) eed= eed->next;
-                       else eed= eed->prev;
                }
        }
-
-       /* now use vertex select flag to select rest */
-       EM_select_flush(em);
-       
-//     if (EM_texFaceCheck())
-}
-
-static int selectconnected_mesh_all_exec(bContext *C, wmOperator *op)
-{
-       Object *obedit= CTX_data_edit_object(C);
-       EditMesh *em= ((Mesh *)obedit->data)->edit_mesh;
        
-       selectconnected_mesh_all(em);
+       for(efa= em->faces.first; efa; efa= efa->next) {
+               if (efa->tmp.l) {
+                       if (sel) {
+                               if (!(efa->f & SELECT)) {
+                                       EM_select_face(efa, 1);
+                                       change = 1;
+                               }
+                       } else {
+                               if (efa->f & SELECT) {
+                                       EM_select_face(efa, 0);
+                                       change = 1;
+                               }
+                       }
+               }
+       }
        
-       WM_event_add_notifier(C, NC_OBJECT|ND_GEOM_SELECT, obedit);
-       return OPERATOR_FINISHED;       
-}
-
-void MESH_OT_selectconnected_mesh_all(wmOperatorType *ot)
-{
-       /* identifiers */
-       ot->name= "Select All of the Connected Mesh";
-       ot->idname= "MESH_OT_selectconnected_mesh_all";
+       if (!change)
+               return OPERATOR_CANCELLED;
        
-       /* api callbacks */
-       ot->exec= selectconnected_mesh_all_exec;
-       ot->poll= ED_operator_editmesh;
+       if (!sel) /* make sure de-selecting faces didnt de-select the verts/edges connected to selected faces, this is common with boundries */
+               for(efa= em->faces.first; efa; efa= efa->next)
+                       if (efa->f & SELECT)
+                               EM_select_face(efa, 1);
        
-       /* flags */
-       ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
+       //      if (EM_texFaceCheck())
        
+       return OPERATOR_FINISHED;
 }
 
-/* *********** select connected ************* */
+#undef is_edge_delimit_ok
+#undef is_face_tag
+#undef face_tag
 
-// XXX should we use CTX_scene(C)->selectmode & SCE_SELECT_FACE like it was in the past ? calls selectconnected_delimit_mesh if true
-static int selectconnected_mesh_invoke(bContext *C, wmOperator *op, wmEvent *event)
+static int select_linked_pick_invoke(bContext *C, wmOperator *op, wmEvent *event)
 {
        Object *obedit= CTX_data_edit_object(C);
        ViewContext vc;
@@ -2289,6 +2384,7 @@ static int selectconnected_mesh_invoke(bContext *C, wmOperator *op, wmEvent *eve
        EditFace *efa;
        short done=1, toggle=0;
        int sel= !RNA_boolean_get(op->ptr, "deselect");
+       int limit= RNA_boolean_get(op->ptr, "limit");
        
        /* unified_finednearest needs ogl */
        view3d_operator_needs_opengl(C);
@@ -2298,10 +2394,19 @@ static int selectconnected_mesh_invoke(bContext *C, wmOperator *op, wmEvent *eve
        
        if(vc.em->edges.first==0) return OPERATOR_CANCELLED;
        
-       vc.mval[0]= event->x - vc.ar->winrct.xmin;
-       vc.mval[1]= event->y - vc.ar->winrct.ymin;
+       vc.mval[0]= event->mval[0];
+       vc.mval[1]= event->mval[1];
+       
+       /* return warning! */
+       if(limit) {
+               int retval= select_linked_limited_invoke(&vc, 0, sel);
+               WM_event_add_notifier(C, NC_OBJECT|ND_GEOM_SELECT, obedit);
+               return retval;
+       }
        
        if( unified_findnearest(&vc, &eve, &eed, &efa)==0 ) {
+               WM_event_add_notifier(C, NC_OBJECT|ND_GEOM_SELECT, obedit);
+       
                return OPERATOR_CANCELLED;
        }
 
@@ -2358,131 +2463,109 @@ static int selectconnected_mesh_invoke(bContext *C, wmOperator *op, wmEvent *eve
        return OPERATOR_FINISHED;       
 }
 
-void MESH_OT_selectconnected_mesh(wmOperatorType *ot)
+void MESH_OT_select_linked_pick(wmOperatorType *ot)
 {
        /* identifiers */
-       ot->name= "Select Connected Mesh";
-       ot->idname= "MESH_OT_selectconnected_mesh";
+       ot->name= "Select Linked";
+       ot->idname= "MESH_OT_select_linked_pick";
        
        /* api callbacks */
-       ot->invoke= selectconnected_mesh_invoke;
+       ot->invoke= select_linked_pick_invoke;
        ot->poll= ED_operator_editmesh;
        
        /* flags */
        ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
        
        RNA_def_boolean(ot->srna, "deselect", 0, "Deselect", "");
+       RNA_def_boolean(ot->srna, "limit", 0, "Limit by Seams", "");
 }
 
-/* ************************* */
-
-/* for use with selectconnected_delimit_mesh only! */
-#define is_edge_delimit_ok(eed) ((eed->tmp.l == 1) && (eed->seam==0))
-#define is_face_tag(efa) is_edge_delimit_ok(efa->e1) || is_edge_delimit_ok(efa->e2) || is_edge_delimit_ok(efa->e3) || (efa->v4 && is_edge_delimit_ok(efa->e4))
 
-#define face_tag(efa)\
-       if(efa->v4)     efa->tmp.l=             efa->e1->tmp.l= efa->e2->tmp.l= efa->e3->tmp.l= efa->e4->tmp.l= 1;\
-       else            efa->tmp.l=             efa->e1->tmp.l= efa->e2->tmp.l= efa->e3->tmp.l= 1;
+/* ************************* */
 
-/* all - 1) use all faces for extending the selection  2) only use the mouse face
- * sel - 1) select  0) deselect 
- * */
-static void selectconnected_delimit_mesh__internal(ViewContext *vc, short all, short sel)
+void selectconnected_mesh_all(EditMesh *em)
 {
-       EditMesh *em= vc->em;
-       EditFace *efa;
+       EditVert *v1,*v2;
        EditEdge *eed;
-       short done=1, change=0;
-       int dist = 75;
-       
-       if(em->faces.first==0) return;
-       
-       /* flag all edges as off*/
-       for(eed= em->edges.first; eed; eed= eed->next)
-               eed->tmp.l=0;
+       short done=1, toggle=0;
        
-       if (all) {
-               for(efa= em->faces.first; efa; efa= efa->next) {
-                       if (efa->f & SELECT) {
-                               face_tag(efa);
-                       } else {
-                               efa->tmp.l = 0;
-                       }
-               }
-       } else {
-               EditFace *efa_mouse = findnearestface(vc, &dist);
-               
-               if( !efa_mouse ) {
-                       /* error("Nothing indicated "); */ /* this is mostly annoying, eps with occluded geometry */
-                       return;
-               }
-               
-               for(efa= em->faces.first; efa; efa= efa->next) {
-                       efa->tmp.l = 0;
-               }
-               efa_mouse->tmp.l = 1;
-               face_tag(efa_mouse);
-       }
+       if(em->edges.first==0) return;
        
        while(done==1) {
                done= 0;
-               /* simple algo - select all faces that have a selected edge
-                * this intern selects the edge, repeat until nothing is left to do */
-               for(efa= em->faces.first; efa; efa= efa->next) {
-                       if ((efa->tmp.l == 0) && (!efa->h)) {
-                               if (is_face_tag(efa)) {
-                                       face_tag(efa);
-                                       done= 1;
-                               }
-                       }
-               }
-       }
-       
-       for(efa= em->faces.first; efa; efa= efa->next) {
-               if (efa->tmp.l) {
-                       if (sel) {
-                               if (!(efa->f & SELECT)) {
-                                       EM_select_face(efa, 1);
-                                       change = 1;
+               
+               toggle++;
+               if(toggle & 1) eed= em->edges.first;
+               else eed= em->edges.last;
+               
+               while(eed) {
+                       v1= eed->v1;
+                       v2= eed->v2;
+                       if(eed->h==0) {
+                               if(v1->f & SELECT) {
+                                       if( (v2->f & SELECT)==0 ) {
+                                               v2->f |= SELECT;
+                                               done= 1;
+                                       }
                                }
-                       } else {
-                               if (efa->f & SELECT) {
-                                       EM_select_face(efa, 0);
-                                       change = 1;
+                               else if(v2->f & SELECT) {
+                                       if( (v1->f & SELECT)==0 ) {
+                                               v1->f |= SELECT;
+                                               done= 1;
+                                       }
                                }
                        }
+                       if(toggle & 1) eed= eed->next;
+                       else eed= eed->prev;
                }
        }
        
-       if (!change)
-               return;
-       
-       if (!sel) /* make sure de-selecting faces didnt de-select the verts/edges connected to selected faces, this is common with boundries */
-               for(efa= em->faces.first; efa; efa= efa->next)
-                       if (efa->f & SELECT)
-                               EM_select_face(efa, 1);
-       
-//     if (EM_texFaceCheck())
+       /* now use vertex select flag to select rest */
+       EM_select_flush(em);
        
+       //      if (EM_texFaceCheck())
 }
 
-#undef is_edge_delimit_ok
-#undef is_face_tag
-#undef face_tag
-
-void selectconnected_delimit_mesh(EditMesh *em)
+static int select_linked_exec(bContext *C, wmOperator *op)
 {
+       Object *obedit= CTX_data_edit_object(C);
+       EditMesh *em= BKE_mesh_get_editmesh(obedit->data);
        
-       // XXX selectconnected_delimit_mesh__internal(em, 0, ((G.qual & LR_SHIFTKEY)==0));
+       if( RNA_boolean_get(op->ptr, "limit") ) {
+               ViewContext vc;
+               em_setup_viewcontext(C, &vc);
+               select_linked_limited_invoke(&vc, 1, 1);
+       }
+       else
+               selectconnected_mesh_all(em);
+       
+       WM_event_add_notifier(C, NC_OBJECT|ND_GEOM_SELECT, obedit);
+
+       BKE_mesh_end_editmesh(obedit->data, em);
+       return OPERATOR_FINISHED;       
 }
-void selectconnected_delimit_mesh_all(ViewContext *vc)
+
+void MESH_OT_select_linked(wmOperatorType *ot)
 {
-       selectconnected_delimit_mesh__internal(vc, 1, 1);
-}      
+       /* identifiers */
+       ot->name= "Select Linked All";
+       ot->idname= "MESH_OT_select_linked";
        
+       /* api callbacks */
+       ot->exec= select_linked_exec;
+       ot->poll= ED_operator_editmesh;
        
+       /* flags */
+       ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
+       
+       RNA_def_boolean(ot->srna, "limit", 0, "Limit by Seams", "");
+}
+
+
+/* ************************* */
+
 /* swap is 0 or 1, if 1 it hides not selected */
-void hide_mesh(EditMesh *em, int swap)
+void EM_hide_mesh(EditMesh *em, int swap)
 {
        EditVert *eve;
        EditEdge *eed;
@@ -2587,23 +2670,21 @@ void hide_mesh(EditMesh *em, int swap)
 static int hide_mesh_exec(bContext *C, wmOperator *op)
 {
        Object *obedit= CTX_data_edit_object(C);
-       EditMesh *em= ((Mesh *)obedit->data)->edit_mesh;
+       EditMesh *em= BKE_mesh_get_editmesh(((Mesh *)obedit->data));
        
-       /* 'standard' behaviour - check if selected, then apply relevant selection */
-       if (RNA_boolean_get(op->ptr, "swap"))
-               hide_mesh(em,1);
-       else
-               hide_mesh(em,0);
+       EM_hide_mesh(em, RNA_boolean_get(op->ptr, "unselected"));
                
        WM_event_add_notifier(C, NC_OBJECT|ND_GEOM_SELECT, obedit);
+
+       BKE_mesh_end_editmesh(obedit->data, em);
        return OPERATOR_FINISHED;       
 }
 
-void MESH_OT_hide_mesh(wmOperatorType *ot)
+void MESH_OT_hide(wmOperatorType *ot)
 {
        /* identifiers */
-       ot->name= "Hide vertice or Hide All of the Mesh";
-       ot->idname= "MESH_OT_hide_mesh";
+       ot->name= "Hide Selection";
+       ot->idname= "MESH_OT_hide";
        
        /* api callbacks */
        ot->exec= hide_mesh_exec;
@@ -2613,10 +2694,10 @@ void MESH_OT_hide_mesh(wmOperatorType *ot)
        ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
        
        /* props */
-       RNA_def_boolean(ot->srna, "swap", 0, "Swap", "");
+       RNA_def_boolean(ot->srna, "unselected", 0, "Unselected", "Hide unselected rather than selected.");
 }
 
-void reveal_mesh(EditMesh *em)
+void EM_reveal_mesh(EditMesh *em)
 {
        EditVert *eve;
        EditEdge *eed;
@@ -2655,19 +2736,21 @@ void reveal_mesh(EditMesh *em)
 static int reveal_mesh_exec(bContext *C, wmOperator *op)
 {
        Object *obedit= CTX_data_edit_object(C);
-       EditMesh *em= ((Mesh *)obedit->data)->edit_mesh;
+       EditMesh *em= BKE_mesh_get_editmesh(((Mesh *)obedit->data));
        
-       reveal_mesh(em);
-               
+       EM_reveal_mesh(em);
+
        WM_event_add_notifier(C, NC_OBJECT|ND_GEOM_SELECT, obedit);
+
+       BKE_mesh_end_editmesh(obedit->data, em);
        return OPERATOR_FINISHED;       
 }
 
-void MESH_OT_reveal_mesh(wmOperatorType *ot)
+void MESH_OT_reveal(wmOperatorType *ot)
 {
        /* identifiers */
-       ot->name= "Reveal the Mesh";
-       ot->idname= "MESH_OT_reveal_mesh";
+       ot->name= "Reveal Hidden";
+       ot->idname= "MESH_OT_reveal";
        
        /* api callbacks */
        ot->exec= reveal_mesh_exec;
@@ -2677,242 +2760,24 @@ void MESH_OT_reveal_mesh(wmOperatorType *ot)
        ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
 }
 
-void hide_tface_uv(EditMesh *em, int swap)
-{
-#if 0
-       /* no space image here */
-       EditFace *efa;
-       MTFace *tface;
-       
-       if( is_uv_tface_editing_allowed()==0 ) return;
-
-       /* call the mesh function if we are in mesh sync sel */
-       if (G.sima->flag & SI_SYNC_UVSEL) {
-               hide_mesh(swap);
-               return;
-       }
-       
-       if(swap) {
-               for (efa= em->faces.first; efa; efa= efa->next) {
-                       if(efa->f & SELECT) {
-                               tface= CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
-                               if (G.sima->flag & SI_SELACTFACE) {
-                                       /* Pretend face mode */
-                                       if ((   (efa->v4==NULL && 
-                                                       (       tface->flag & (TF_SEL1|TF_SEL2|TF_SEL3)) ==                     (TF_SEL1|TF_SEL2|TF_SEL3) )                      ||
-                                                       (       tface->flag & (TF_SEL1|TF_SEL2|TF_SEL3|TF_SEL4)) ==     (TF_SEL1|TF_SEL2|TF_SEL3|TF_SEL4)       ) == 0) {
-                                               
-                                               if (em->selectmode == SCE_SELECT_FACE) {
-                                                       efa->f &= ~SELECT;
-                                                       /* must re-select after */
-                                                       efa->e1->f &= ~SELECT;
-                                                       efa->e2->f &= ~SELECT;
-                                                       efa->e3->f &= ~SELECT;
-                                                       if(efa->e4) efa->e4->f &= ~SELECT;
-                                               } else {
-                                                       EM_select_face(efa, 0);
-                                               }
-                                       }
-                                       tface->flag &= ~(TF_SEL1|TF_SEL2|TF_SEL3|TF_SEL4);
-                               } else if (em->selectmode == SCE_SELECT_FACE) {
-                                       if((tface->flag & (TF_SEL1|TF_SEL2|TF_SEL3))==0) {
-                                               if(!efa->v4)
-                                                       EM_select_face(efa, 0);
-                                               else if(!(tface->flag & TF_SEL4))
-                                                       EM_select_face(efa, 0);
-                                               tface->flag &= ~(TF_SEL1|TF_SEL2|TF_SEL3|TF_SEL4);
-                                       }
-                               } else {
-                                       /* EM_deselect_flush will deselect the face */
-                                       if((tface->flag & TF_SEL1)==0)                          efa->v1->f &= ~SELECT;
-                                       if((tface->flag & TF_SEL2)==0)                          efa->v2->f &= ~SELECT;
-                                       if((tface->flag & TF_SEL3)==0)                          efa->v3->f &= ~SELECT;
-                                       if((efa->v4) && (tface->flag & TF_SEL4)==0)     efa->v4->f &= ~SELECT;                  
-                                       tface->flag &= ~(TF_SEL1|TF_SEL2|TF_SEL3|TF_SEL4);
-                               }
-                       }
-               }
-       } else {
-               for (efa= em->faces.first; efa; efa= efa->next) {
-                       if(efa->f & SELECT) {
-                               tface= CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
-                               if (G.sima->flag & SI_SELACTFACE) {
-                                       if (    (efa->v4==NULL && 
-                                                       (       tface->flag & (TF_SEL1|TF_SEL2|TF_SEL3)) ==                     (TF_SEL1|TF_SEL2|TF_SEL3) )                      ||
-                                                       (       tface->flag & (TF_SEL1|TF_SEL2|TF_SEL3|TF_SEL4)) ==     (TF_SEL1|TF_SEL2|TF_SEL3|TF_SEL4)       ) {
-                                               
-                                               if (em->selectmode == SCE_SELECT_FACE) {
-                                                       efa->f &= ~SELECT;
-                                                       /* must re-select after */
-                                                       efa->e1->f &= ~SELECT;
-                                                       efa->e2->f &= ~SELECT;
-                                                       efa->e3->f &= ~SELECT;
-                                                       if(efa->e4) efa->e4->f &= ~SELECT;
-                                               } else {
-                                                       EM_select_face(efa, 0);
-                                               }
-                                       }
-                                       tface->flag &= ~(TF_SEL1|TF_SEL2|TF_SEL3|TF_SEL4);
-                               } else if (em->selectmode == SCE_SELECT_FACE) {
-                                       if(tface->flag & (TF_SEL1|TF_SEL2|TF_SEL3))
-                                               EM_select_face(efa, 0);
-                                       else if(efa->v4 && tface->flag & TF_SEL4)
-                                               EM_select_face(efa, 0);
-                                       tface->flag &= ~(TF_SEL1|TF_SEL2|TF_SEL3|TF_SEL4);
-                               } else {
-                                       /* EM_deselect_flush will deselect the face */
-                                       if(tface->flag & TF_SEL1)                               efa->v1->f &= ~SELECT;
-                                       if(tface->flag & TF_SEL2)                               efa->v2->f &= ~SELECT;
-                                       if(tface->flag & TF_SEL3)                               efa->v3->f &= ~SELECT;
-                                       if((efa->v4) && tface->flag & TF_SEL4)  efa->v4->f &= ~SELECT;
-                                       tface->flag &= ~(TF_SEL1|TF_SEL2|TF_SEL3|TF_SEL4);
-                               }
-                       }
-               }
-       }
-       
-       
-       /*deselects too many but ok for now*/
-       if(em->selectmode & (SCE_SELECT_EDGE|SCE_SELECT_VERTEX)) {
-               EM_deselect_flush(em);
-       }
-       
-       if (em->selectmode==SCE_SELECT_FACE) {
-               /* de-selected all edges from faces that were de-selected.
-                * now make sure all faces that are selected also have selected edges */
-               for (efa= em->faces.first; efa; efa= efa->next) {
-                       if (efa->f & SELECT) {
-                               EM_select_face(efa, 1);
-                       }
-               }
-       }
-       
-       EM_validate_selections();
-       
-// XXX object_tface_flags_changed(OBACT, 0);
-#endif
-}
-
-void reveal_tface_uv(EditMesh *em)
-{
-#if 0
-       /* function should move away? */
-       EditFace *efa;
-       MTFace *tface;
-
-       if( is_uv_tface_editing_allowed()==0 ) return;
-       
-       /* call the mesh function if we are in mesh sync sel */
-       if (G.sima->flag & SI_SYNC_UVSEL) {
-               reveal_mesh();
-               return;
-       }
-       
-       if (G.sima->flag & SI_SELACTFACE) {
-               if (em->selectmode == SCE_SELECT_FACE) {
-                       for (efa= em->faces.first; efa; efa= efa->next) {
-                               if (!(efa->h) && !(efa->f & SELECT)) {
-                                       tface= CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
-                                       EM_select_face(efa, 1);
-                                       tface->flag |= TF_SEL1|TF_SEL2|TF_SEL3|TF_SEL4;
-                               }
-                       }
-               } else {
-                       /* enable adjacent faces to have disconnected UV selections if sticky is disabled */
-                       if (G.sima->sticky == SI_STICKY_DISABLE) {
-                               for (efa= em->faces.first; efa; efa= efa->next) {
-                                       if (!(efa->h) && !(efa->f & SELECT)) {
-                                               /* All verts must be unselected for the face to be selected in the UV view */
-                                               if ((efa->v1->f&SELECT)==0 && (efa->v2->f&SELECT)==0 && (efa->v3->f&SELECT)==0 && (efa->v4==0 || (efa->v4->f&SELECT)==0)) {
-                                                       tface= CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
-                                                       tface->flag |= TF_SEL1|TF_SEL2|TF_SEL3|TF_SEL4;
-                                                       /* Cant use EM_select_face here because it unselects the verts
-                                                        * and we cant tell if the face was totally unselected or not */
-                                                       /*EM_select_face(efa, 1);
-                                                        * 
-                                                        * See Loop with EM_select_face() below... */
-                                                       efa->f |= SELECT;
-                                               }
-                                       }
-                               }
-                       } else {
-                               for (efa= em->faces.first; efa; efa= efa->next) {
-                                       if (!(efa->h) && !(efa->f & SELECT)) {
-                                               tface= CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
-                                               if ((efa->v1->f & SELECT)==0)                           {tface->flag |= TF_SEL1;}
-                                               if ((efa->v2->f & SELECT)==0)                           {tface->flag |= TF_SEL2;}
-                                               if ((efa->v3->f & SELECT)==0)                           {tface->flag |= TF_SEL3;}
-                                               if ((efa->v4 && (efa->v4->f & SELECT)==0))      {tface->flag |= TF_SEL4;}
-                                               efa->f |= SELECT;
-                                       }
-                               }
-                       }
-                       
-                       /* Select all edges and verts now */
-                       for (efa= em->faces.first; efa; efa= efa->next) {
-                               /* we only selected the face flags, and didnt changes edges or verts, fix this now */
-                               if (!(efa->h) && (efa->f & SELECT)) {
-                                       EM_select_face(efa, 1);
-                               }
-                       }
-                       EM_select_flush(em);
-               }
-       } else if (em->selectmode == SCE_SELECT_FACE) {
-               for (efa= em->faces.first; efa; efa= efa->next) {
-                       if (!(efa->h) && !(efa->f & SELECT)) {
-                               tface= CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
-                               efa->f |= SELECT;
-                               tface->flag |= TF_SEL1|TF_SEL2|TF_SEL3|TF_SEL4;
-                       }
-               }
-               
-               /* Select all edges and verts now */
-               for (efa= em->faces.first; efa; efa= efa->next) {
-                       /* we only selected the face flags, and didnt changes edges or verts, fix this now */
-                       if (!(efa->h) && (efa->f & SELECT)) {
-                               EM_select_face(efa, 1);
-                       }
-               }
-               
-       } else {
-               for (efa= em->faces.first; efa; efa= efa->next) {
-                       if (!(efa->h) && !(efa->f & SELECT)) {
-                               tface= CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
-                               if ((efa->v1->f & SELECT)==0)                           {tface->flag |= TF_SEL1;}
-                               if ((efa->v2->f & SELECT)==0)                           {tface->flag |= TF_SEL2;}
-                               if ((efa->v3->f & SELECT)==0)                           {tface->flag |= TF_SEL3;}
-                               if ((efa->v4 && (efa->v4->f & SELECT)==0))      {tface->flag |= TF_SEL4;}
-                               efa->f |= SELECT;
-                       }
-               }
-               
-               /* Select all edges and verts now */
-               for (efa= em->faces.first; efa; efa= efa->next) {
-                       /* we only selected the face flags, and didnt changes edges or verts, fix this now */
-                       if (!(efa->h) && (efa->f & SELECT)) {
-                               EM_select_face(efa, 1);
-                       }
-               }
-       }
-       
-// XXX object_tface_flags_changed(OBACT, 0);
-#endif
-}
-
-void select_faces_by_numverts(EditMesh *em, int numverts)
+int select_by_number_vertices_exec(bContext *C, wmOperator *op)
 {
+       Object *obedit= CTX_data_edit_object(C);
+       EditMesh *em= BKE_mesh_get_editmesh(((Mesh *)obedit->data));
        EditFace *efa;
+       int numverts= RNA_enum_get(op->ptr, "type");
 
        /* Selects trias/qiads or isolated verts, and edges that do not have 2 neighboring
         * faces
         */
 
        /* for loose vertices/edges, we first select all, loop below will deselect */
-       if(numverts==5)
+       if(numverts==5) {
                EM_set_flag_all(em, SELECT);
+       }
        else if(em->selectmode!=SCE_SELECT_FACE) {
-               error("Only works in face selection mode");
-               return;
+               BKE_report(op->reports, RPT_ERROR, "Only works in face selection mode");
+               return OPERATOR_CANCELLED;
        }
        
        for(efa= em->faces.first; efa; efa= efa->next) {
@@ -2924,8 +2789,32 @@ void select_faces_by_numverts(EditMesh *em, int numverts)
                }
        }
 
-//     if (EM_texFaceCheck())
+       WM_event_add_notifier(C, NC_OBJECT|ND_GEOM_SELECT, obedit);
        
+       return OPERATOR_FINISHED;
+}
+
+void MESH_OT_select_by_number_vertices(wmOperatorType *ot)
+{
+       static const EnumPropertyItem type_items[]= {
+               {3, "TRIANGLES", 0, "Triangles", NULL},
+               {4, "QUADS", 0, "Triangles", NULL},
+               {5, "OTHER", 0, "Other", NULL},
+               {0, NULL, 0, NULL, NULL}};
+
+       /* identifiers */
+       ot->name= "Select by Number of Vertices";
+       ot->idname= "MESH_OT_select_by_number_vertices";
+       
+       /* api callbacks */
+       ot->exec= select_by_number_vertices_exec;
+       ot->poll= ED_operator_editmesh;
+       
+       /* flags */
+       ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
+       
+       /* props */
+       RNA_def_enum(ot->srna, "type", type_items, 3, "Type", "Type of elements to select.");
 }
 
 static int select_sharp_edges_exec(bContext *C, wmOperator *op)
@@ -2935,7 +2824,7 @@ static int select_sharp_edges_exec(bContext *C, wmOperator *op)
        * small enough, select the edge
        */
        Object *obedit= CTX_data_edit_object(C);
-       EditMesh *em= ((Mesh *)obedit->data)->edit_mesh;
+       EditMesh *em= BKE_mesh_get_editmesh(((Mesh *)obedit->data));
        EditEdge *eed;
        EditFace *efa;
        EditFace **efa1;
@@ -2946,7 +2835,8 @@ static int select_sharp_edges_exec(bContext *C, wmOperator *op)
        /* 'standard' behaviour - check if selected, then apply relevant selection */
        
        if(em->selectmode==SCE_SELECT_FACE) {
-               error("Doesn't work in face selection mode");
+               BKE_report(op->reports, RPT_ERROR, "Doesn't work in face selection mode");
+               BKE_mesh_end_editmesh(obedit->data, em);
                return OPERATOR_CANCELLED;
        }
 
@@ -3025,14 +2915,16 @@ static int select_sharp_edges_exec(bContext *C, wmOperator *op)
 //     if (EM_texFaceCheck())
        
        WM_event_add_notifier(C, NC_OBJECT|ND_GEOM_SELECT, obedit); //TODO is this needed ?
+
+       BKE_mesh_end_editmesh(obedit->data, em);
        return OPERATOR_FINISHED;       
 }
 
-void MESH_OT_select_sharp_edges(wmOperatorType *ot)
+void MESH_OT_edges_select_sharp(wmOperatorType *ot)
 {
        /* identifiers */
        ot->name= "Select Sharp Edges";
-       ot->idname= "MESH_OT_select_sharp_edges";
+       ot->idname= "MESH_OT_edges_select_sharp";
        
        /* api callbacks */
        ot->exec= select_sharp_edges_exec;
@@ -3046,7 +2938,7 @@ void MESH_OT_select_sharp_edges(wmOperatorType *ot)
 }
 
 
-static void select_linked_flat_faces(EditMesh *em, float sharpness)
+static void select_linked_flat_faces(EditMesh *em, wmOperator *op, float sharpness)
 {
        /* Find faces that are linked to selected faces that are 
         * relatively flat (angle between faces is higher than
@@ -3060,7 +2952,7 @@ static void select_linked_flat_faces(EditMesh *em, float sharpness)
        float fsharpness;
        
        if(em->selectmode!=SCE_SELECT_FACE) {
-               error("Only works in face selection mode");
+               BKE_report(op->reports, RPT_ERROR, "Only works in face selection mode");
                return;
        }
 
@@ -3185,19 +3077,21 @@ static void select_linked_flat_faces(EditMesh *em, float sharpness)
 static int select_linked_flat_faces_exec(bContext *C, wmOperator *op)
 {
        Object *obedit= CTX_data_edit_object(C);
-       EditMesh *em= ((Mesh *)obedit->data)->edit_mesh;
+       EditMesh *em= BKE_mesh_get_editmesh(((Mesh *)obedit->data));
        
-       select_linked_flat_faces(em, RNA_float_get(op->ptr, "sharpness"));
+       select_linked_flat_faces(em, op, RNA_float_get(op->ptr, "sharpness"));
        
        WM_event_add_notifier(C, NC_OBJECT|ND_GEOM_SELECT, obedit);
+
+       BKE_mesh_end_editmesh(obedit->data, em);
        return OPERATOR_FINISHED;       
 }
 
-void MESH_OT_select_linked_flat_faces(wmOperatorType *ot)
+void MESH_OT_faces_select_linked_flat(wmOperatorType *ot)
 {
        /* identifiers */
        ot->name= "Select Linked Flat Faces";
-       ot->idname= "MESH_OT_select_linked_flat_faces";
+       ot->idname= "MESH_OT_faces_select_linked_flat";
        
        /* api callbacks */
        ot->exec= select_linked_flat_faces_exec;
@@ -3210,7 +3104,7 @@ void MESH_OT_select_linked_flat_faces(wmOperatorType *ot)
        RNA_def_float(ot->srna, "sharpness", 0.0f, 0.0f, FLT_MAX, "sharpness", "", 0.0f, 180.0f);
 }
 
-void select_non_manifold(EditMesh *em)
+void select_non_manifold(EditMesh *em, wmOperator *op )
 {
        EditVert *eve;
        EditEdge *eed;
@@ -3221,7 +3115,7 @@ void select_non_manifold(EditMesh *em)
         */
        
        if(em->selectmode==SCE_SELECT_FACE) {
-               error("Doesn't work in face selection mode");
+               BKE_report(op->reports, RPT_ERROR, "Doesn't work in face selection mode");
                return;
        }
 
@@ -3283,11 +3177,13 @@ void select_non_manifold(EditMesh *em)
 static int select_non_manifold_exec(bContext *C, wmOperator *op)
 {
        Object *obedit= CTX_data_edit_object(C);
-       EditMesh *em= ((Mesh *)obedit->data)->edit_mesh;
+       EditMesh *em= BKE_mesh_get_editmesh(((Mesh *)obedit->data));
        
-       select_non_manifold(em);
+       select_non_manifold(em, op);
        
        WM_event_add_notifier(C, NC_OBJECT|ND_GEOM_SELECT, obedit);
+
+       BKE_mesh_end_editmesh(obedit->data, em);
        return OPERATOR_FINISHED;       
 }
 
@@ -3305,7 +3201,7 @@ void MESH_OT_select_non_manifold(wmOperatorType *ot)
        ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
 }
 
-void selectswap_mesh(EditMesh *em) /* UI level */
+void EM_select_swap(EditMesh *em) /* exported for UV */
 {
        EditVert *eve;
        EditEdge *eed;
@@ -3341,25 +3237,27 @@ void selectswap_mesh(EditMesh *em) /* UI level */
 
 }
 
-static int selectswap_mesh_exec(bContext *C, wmOperator *op)
+static int select_inverse_mesh_exec(bContext *C, wmOperator *op)
 {
        Object *obedit= CTX_data_edit_object(C);
-       EditMesh *em= ((Mesh *)obedit->data)->edit_mesh;
+       EditMesh *em= BKE_mesh_get_editmesh(((Mesh *)obedit->data));
        
-       selectswap_mesh(em);
+       EM_select_swap(em);
        
        WM_event_add_notifier(C, NC_OBJECT|ND_GEOM_SELECT, obedit);
+
+       BKE_mesh_end_editmesh(obedit->data, em);
        return OPERATOR_FINISHED;       
 }
 
-void MESH_OT_selectswap_mesh(wmOperatorType *ot)
+void MESH_OT_select_inverse(wmOperatorType *ot)
 {
        /* identifiers */
-       ot->name= "Select Swap";
-       ot->idname= "MESH_OT_selectswap_mesh";
+       ot->name= "Select Inverse";
+       ot->idname= "MESH_OT_select_inverse";
        
        /* api callbacks */
-       ot->exec= selectswap_mesh_exec;
+       ot->exec= select_inverse_mesh_exec;
        ot->poll= ED_operator_editmesh;
        
        /* flags */
@@ -3368,29 +3266,32 @@ void MESH_OT_selectswap_mesh(wmOperatorType *ot)
        
 /* ******************** (de)select all operator **************** */
 
+void EM_toggle_select_all(EditMesh *em) /* exported for UV */
+{
+       if(EM_nvertices_selected(em))
+               EM_clear_flag_all(em, SELECT);
+       else 
+               EM_set_flag_all(em, SELECT);
+}
+
 static int toggle_select_all_exec(bContext *C, wmOperator *op)
 {
        Object *obedit= CTX_data_edit_object(C);
-       EditMesh *em= ((Mesh *)obedit->data)->edit_mesh;
+       EditMesh *em= BKE_mesh_get_editmesh(((Mesh *)obedit->data));
        
-       if( EM_nvertices_selected(em) ) {
-               EM_clear_flag_all(em, SELECT);
-       }
-       else  {
-               EM_set_flag_all(em, SELECT);
-       }
-               
-//             if (EM_texFaceCheck())
+       EM_toggle_select_all(em);
        
-       WM_event_add_notifier(C, NC_OBJECT|ND_GEOM_SELECT, obedit);
+       WM_event_add_notifier(C, NC_OBJECT|ND_GEOM_SELECT, obedit);     
+       BKE_mesh_end_editmesh(obedit->data, em);
+
        return OPERATOR_FINISHED;
 }
 
-void MESH_OT_de_select_all(wmOperatorType *ot)
+void MESH_OT_select_all_toggle(wmOperatorType *ot)
 {
        /* identifiers */
-       ot->name= "Select or Deselect All";
-       ot->idname= "MESH_OT_de_select_all";
+       ot->name= "Select/Deselect All";
+       ot->idname= "MESH_OT_select_all_toggle";
        
        /* api callbacks */
        ot->exec= toggle_select_all_exec;
@@ -3444,13 +3345,15 @@ void EM_select_more(EditMesh *em)
 static int select_more(bContext *C, wmOperator *op)
 {
        Object *obedit= CTX_data_edit_object(C);
-       EditMesh *em= ((Mesh *)obedit->data)->edit_mesh;
+       EditMesh *em= BKE_mesh_get_editmesh(((Mesh *)obedit->data)) ;
 
        EM_select_more(em);
 
 //     if (EM_texFaceCheck(em))
 
        WM_event_add_notifier(C, NC_OBJECT|ND_GEOM_SELECT, obedit);
+
+       BKE_mesh_end_editmesh(obedit->data, em);
        return OPERATOR_FINISHED;
 }
 
@@ -3532,12 +3435,14 @@ void EM_select_less(EditMesh *em)
 static int select_less(bContext *C, wmOperator *op)
 {
        Object *obedit= CTX_data_edit_object(C);
-       EditMesh *em= ((Mesh *)obedit->data)->edit_mesh;
+       EditMesh *em= BKE_mesh_get_editmesh(((Mesh *)obedit->data));
 
        EM_select_less(em);
 
 //     if (EM_texFaceCheck(em))
        WM_event_add_notifier(C, NC_OBJECT|ND_GEOM_SELECT, obedit);
+
+       BKE_mesh_end_editmesh(obedit->data, em);
        return OPERATOR_FINISHED;
 }
 
@@ -3555,12 +3460,12 @@ void MESH_OT_select_less(wmOperatorType *ot)
        ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
 }
 
-void selectrandom_mesh(EditMesh *em, int randfac) /* randomly selects a user-set % of vertices/edges/faces */
+static void selectrandom_mesh(EditMesh *em, float perc) /* randomly selects a user-set % of vertices/edges/faces */
 {
        EditVert *eve;
        EditEdge *eed;
        EditFace *efa;
-
+       float randfac= perc;
        /* Get the percentage of vertices to randomly select as 'randfac' */
 // XXX if(button(&randfac,0, 100,"Percentage:")==0) return;
 
@@ -3569,7 +3474,7 @@ void selectrandom_mesh(EditMesh *em, int randfac) /* randomly selects a user-set
        if(em->selectmode & SCE_SELECT_VERTEX) {
                for(eve= em->verts.first; eve; eve= eve->next) {
                        if(eve->h==0) {
-                               if ( (BLI_frand() * 100) < randfac) 
+                               if (BLI_frand() < randfac) 
                                        eve->f |= SELECT;
                        }
                }
@@ -3578,7 +3483,7 @@ void selectrandom_mesh(EditMesh *em, int randfac) /* randomly selects a user-set
        else if(em->selectmode & SCE_SELECT_EDGE) {
                for(eed= em->edges.first; eed; eed= eed->next) {
                        if(eed->h==0) {
-                               if ( (BLI_frand() * 100) < randfac) 
+                               if (BLI_frand() < randfac) 
                                        EM_select_edge(eed, 1);
                        }
                }
@@ -3587,7 +3492,7 @@ void selectrandom_mesh(EditMesh *em, int randfac) /* randomly selects a user-set
        else {
                for(efa= em->faces.first; efa; efa= efa->next) {
                        if(efa->h==0) {
-                               if ( (BLI_frand() * 100) < randfac) 
+                               if (BLI_frand() < randfac) 
                                        EM_select_face(efa, 1);
                        }
                }
@@ -3597,38 +3502,38 @@ void selectrandom_mesh(EditMesh *em, int randfac) /* randomly selects a user-set
 //     if (EM_texFaceCheck())
 }
 
-static int selectrandom_mesh_exec(bContext *C, wmOperator *op)
+static int mesh_select_random_exec(bContext *C, wmOperator *op)
 {
        Object *obedit= CTX_data_edit_object(C);
-       EditMesh *em= ((Mesh *)obedit->data)->edit_mesh;
+       EditMesh *em= BKE_mesh_get_editmesh(((Mesh *)obedit->data));
        
-       selectrandom_mesh(em, RNA_int_get(op->ptr,"percentage"));
+       selectrandom_mesh(em, RNA_float_get(op->ptr,"percent"));
                
        WM_event_add_notifier(C, NC_OBJECT|ND_GEOM_SELECT, obedit);
        
+       BKE_mesh_end_editmesh(obedit->data, em);
        return OPERATOR_FINISHED;       
 }
 
-void MESH_OT_selectrandom_mesh(wmOperatorType *ot)
+void MESH_OT_select_random(wmOperatorType *ot)
 {
        /* identifiers */
-       ot->name= "Select random mesh";
-       ot->idname= "MESH_OT_selectrandom_mesh";
-       
+       ot->name= "Select Random";
+       ot->idname= "MESH_OT_select_random";
+
        /* api callbacks */
-       ot->exec= selectrandom_mesh_exec;
+       ot->exec= mesh_select_random_exec;
+       ot->invoke= WM_operator_props_popup;
        ot->poll= ED_operator_editmesh;
 
        /* flags */
-       ot->flag= OPTYPE_REGISTER/*|OPTYPE_UNDO*/;
+       ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
        
        /* props */
-
-       
-       RNA_def_int(ot->srna, "percentage", 50, 0, 100, "Percentage", "", 0, 100);
+       RNA_def_float_percentage(ot->srna, "percent", 0.5f, 0.0f, 1.0f, "Percent", "Percentage of vertices to select randomly.", 0.0001f, 1.0f);
 }
 
-void editmesh_select_by_material(EditMesh *em, int index) 
+void EM_select_by_material(EditMesh *em, int index) 
 {
        EditFace *efa;
        
@@ -3641,7 +3546,7 @@ void editmesh_select_by_material(EditMesh *em, int index)
        EM_selectmode_flush(em);
 }
 
-void editmesh_deselect_by_material(EditMesh *em, int index) 
+void EM_deselect_by_material(EditMesh *em, int index) 
 {
        EditFace *efa;
        
@@ -3654,44 +3559,82 @@ void editmesh_deselect_by_material(EditMesh *em, int index)
        EM_selectmode_flush(em);
 }
 
-void EM_selectmode_menu(EditMesh *em)
+static void mesh_selection_type(Scene *scene, EditMesh *em, int val)
 {
-       int val, ctrl= 0; // XXX
-       
-//     if(em->selectmode & SCE_SELECT_VERTEX) pupmenu_set_active(1);
-//     else if(em->selectmode & SCE_SELECT_EDGE) pupmenu_set_active(2);
-//     else pupmenu_set_active(3);
-       
-//     val= pupmenu("Select Mode%t|Vertices|Edges|Faces");
-       
-       
        if(val>0) {
-               if(val==1){ 
+               if(val==1) 
                        em->selectmode= SCE_SELECT_VERTEX;
                        EM_selectmode_set(em);
-                       }
-               else if(val==2){
-                       if(ctrl) EM_convertsel(em, em->selectmode, SCE_SELECT_EDGE);
+               }
+               else if(val==2) {
+                       //if(ctrl) EM_convertsel(em, em->selectmode, SCE_SELECT_EDGE);
                        em->selectmode= SCE_SELECT_EDGE;
                        EM_selectmode_set(em);
                }
                
                else{
-                       if((ctrl)) EM_convertsel(em, em->selectmode, SCE_SELECT_FACE);
+                       //if((ctrl)) EM_convertsel(em, em->selectmode, SCE_SELECT_FACE);
                        em->selectmode= SCE_SELECT_FACE;
                        EM_selectmode_set(em);
                }
                
+               /* note, em stores selectmode to be able to pass it on everywhere without scene,
+                  this is only until all select modes and toolsettings are settled more */
+               scene->toolsettings->selectmode= em->selectmode;
 //             if (EM_texFaceCheck())
        }
 }
 
+static EnumPropertyItem prop_mesh_edit_types[] = {
+       {1, "VERT", 0, "Vertices", ""},
+       {2, "EDGE", 0, "Edges", ""},
+       {3, "FACE", 0, "Faces", ""},
+       {0, NULL, 0, NULL, NULL}
+};
+
+static int mesh_selection_type_exec(bContext *C, wmOperator *op)
+{              
+       
+       Object *obedit= CTX_data_edit_object(C);
+       EditMesh *em= BKE_mesh_get_editmesh(((Mesh *)obedit->data));
+
+       mesh_selection_type(CTX_data_scene(C), em, RNA_enum_get(op->ptr,"type"));
+
+       WM_event_add_notifier(C, NC_OBJECT|ND_GEOM_SELECT, obedit);
+       
+       BKE_mesh_end_editmesh(obedit->data, em);
+       return OPERATOR_FINISHED;
+}
+
+void MESH_OT_selection_type(wmOperatorType *ot)
+{
+       /* identifiers */
+       ot->name= "Selection Mode";
+       ot->idname= "MESH_OT_selection_type";
+       
+       /* api callbacks */
+       ot->invoke= WM_menu_invoke;
+       ot->exec= mesh_selection_type_exec;
+       
+       ot->poll= ED_operator_editmesh;
+       
+       /* flags */
+       ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
+       
+       /* props */
+       RNA_def_enum(ot->srna, "type", prop_mesh_edit_types, 0, "Type", "Set the mesh selection type");
+       
+}
 /* ************************* SEAMS AND EDGES **************** */
 
-void editmesh_mark_seam(EditMesh *em, int clear)
+static int editmesh_mark_seam(bContext *C, wmOperator *op)
 {
-       Mesh *me= NULL; // XXX
+       Scene *scene = CTX_data_scene(C);
+       Object *obedit= CTX_data_edit_object(C);
+       EditMesh *em= BKE_mesh_get_editmesh(((Mesh *)obedit->data));
+       Mesh *me= ((Mesh *)obedit->data);
        EditEdge *eed;
+       int clear = RNA_boolean_get(op->ptr, "clear");
        
        /* auto-enable seams drawing */
        if(clear==0) {
@@ -3717,19 +3660,45 @@ void editmesh_mark_seam(EditMesh *em, int clear)
                }
        }
 
+       BKE_mesh_end_editmesh(obedit->data, em);
+
+       DAG_object_flush_update(scene, obedit, OB_RECALC_DATA);
+       WM_event_add_notifier(C, NC_OBJECT|ND_GEOM_SELECT, obedit);
+
+       return OPERATOR_FINISHED;
 }
 
-void editmesh_mark_sharp(EditMesh *em, int set)
+void MESH_OT_mark_seam(wmOperatorType *ot)
 {
-       Mesh *me= NULL;
+       /* identifiers */
+       ot->name= "Mark Seam";
+       ot->idname= "MESH_OT_mark_seam";
+       
+       /* api callbacks */
+       ot->exec= editmesh_mark_seam;
+       ot->poll= ED_operator_editmesh;
+       
+       /* flags */
+       ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
+       
+       RNA_def_boolean(ot->srna, "clear", 0, "Clear", "");
+}
+
+static int editmesh_mark_sharp(bContext *C, wmOperator *op)
+{
+       Scene *scene = CTX_data_scene(C);
+       Object *obedit= CTX_data_edit_object(C);
+       EditMesh *em= BKE_mesh_get_editmesh(((Mesh *)obedit->data));
+       Mesh *me= ((Mesh *)obedit->data);
+       int clear = RNA_boolean_get(op->ptr, "clear");
        EditEdge *eed;
 
        /* auto-enable sharp edge drawing */
-       if(set) {
+       if(clear == 0) {
                me->drawflag |= ME_DRAWSHARP;
        }
 
-       if(set) {
+       if(!clear) {
                eed= em->edges.first;
                while(eed) {
                        if(!eed->h && (eed->f & SELECT)) eed->sharp = 1;
@@ -3743,166 +3712,30 @@ void editmesh_mark_sharp(EditMesh *em, int set)
                }
        }
 
-}
-
-void BME_Menu()        {
-       short ret;
-       ret= pupmenu("BME modeller%t|Select Edges of Vert%x1");
-       
-       switch(ret)
-       {
-               case 1:
-               //BME_edges_of_vert();
-               break;
-       }
-}
-
-
-
-void Vertex_Menu(EditMesh *em) 
-{
-       short ret;
-       ret= pupmenu("Vertex Specials%t|Remove Doubles%x1|Merge%x2|Smooth %x3|Select Vertex Path%x4|Blend From Shape%x5|Propagate To All Shapes%x6");
-
-       switch(ret)
-       {
-               case 1:
-// XXX                 notice("Removed %d Vertices", removedoublesflag(1, 0, scene->toolsettings->doublimit));
-                       break;
-               case 2: 
-// XXX                 mergemenu(em);
-                       break;
-               case 3:
-// XXX                 vertexsmooth(em);
-                       break;
-               case 4:
-// XXX                 pathselect(em);
-                       break;
-               case 5: 
-// XXX                 shape_copy_select_from(em);
-                       break;
-               case 6: 
-// XXX                 shape_propagate(em);
-                       break;
-       }
-       /* some items crashed because this is in the original W menu but not here. should really manage this better */
-//     DAG_object_flush_update(scene, obedit, OB_RECALC_DATA);
-}
-
-
-void Edge_Menu(EditMesh *em) 
-{
-       short ret;
+       BKE_mesh_end_editmesh(obedit->data, em);
 
-       ret= pupmenu("Edge Specials%t|Mark Seam %x1|Clear Seam %x2|Rotate Edge CW%x3|Rotate Edge CCW%x4|Loopcut%x6|Edge Slide%x5|Edge Loop Select%x7|Edge Ring Select%x8|Loop to Region%x9|Region to Loop%x10|Mark Sharp%x11|Clear Sharp%x12");
+       DAG_object_flush_update(scene, obedit, OB_RECALC_DATA);
+       WM_event_add_notifier(C, NC_OBJECT|ND_GEOM_SELECT, obedit);
 
-       switch(ret)
-       {
-       case 1:
-               editmesh_mark_seam(em, 0);
-               break;
-       case 2:
-               editmesh_mark_seam(em, 1);
-               break;
-       case 3:
-//             edge_rotate_selected(em, 2);
-               break;
-       case 4:
-//             edge_rotate_selected(em, 1);
-               break;
-       case 5:
-//             EdgeSlide(em, 0,0.0);
-               break;
-       case 6:
-//             CutEdgeloop(em, 1);
-               break;
-       case 7:
-//             loop_multiselect(em, 0);
-               break;
-       case 8:
-//             loop_multiselect(em, 1);
-               break;
-       case 9:
-//             loop_to_region(em);
-               break;
-       case 10:
-//             region_to_loop(em);
-               break;
-       case 11:
-//             editmesh_mark_sharp(em, 1);
-//             DAG_object_flush_update(scene, obedit, OB_RECALC_DATA);
-               break;
-       case 12: 
-//             editmesh_mark_sharp(em, 0);
-//             DAG_object_flush_update(scene, obedit, OB_RECALC_DATA);
-               break;
-       }
-       /* some items crashed because this is in the original W menu but not here. should really manage this better */
-//     DAG_object_flush_update(scene, obedit, OB_RECALC_DATA);
+       return OPERATOR_FINISHED;
 }
 
-void Face_Menu(EditMesh *em) 
+void MESH_OT_mark_sharp(wmOperatorType *ot)
 {
-       short ret;
-       ret= pupmenu(
-               "Face Specials%t|Flip Normals%x1|Bevel%x2|Shade Smooth%x3|Shade Flat%x4|"
-               "Triangulate (Ctrl T)%x5|Quads from Triangles (Alt J)%x6|Flip Triangle Edges (Ctrl Shift F)%x7|%l|"
-               "Face Mode Set%x8|Face Mode Clear%x9|%l|"
-               "UV Rotate (Shift - CCW)%x10|UV Mirror (Shift - Switch Axis)%x11|"
-               "Color Rotate (Shift - CCW)%x12|Color Mirror (Shift - Switch Axis)%x13");
-
-       switch(ret)
-       {
-               case 1:
-//                     flip_editnormals(em);
-//                     DAG_object_flush_update(scene, obedit, OB_RECALC_DATA);
-                       break;
-               case 2:
-//                     bevel_menu(em);
-                       break;
-               case 3:
-//                     mesh_set_smooth_faces(em, 1);
-                       break;
-               case 4:
-//                     mesh_set_smooth_faces(em, 0);
-                       break;
-                       
-               case 5: /* Quads to Tris */
-//                     convert_to_triface(em, 0);
-//                     DAG_object_flush_update(scene, obedit, OB_RECALC_DATA);
-                       break;
-               case 6: /* Tris to Quads */
-//                     join_triangles(em);
-                       break;
-               case 7: /* Flip triangle edges */
-//                     edge_flip(em);
-                       break;
-               case 8:
-//                     mesh_set_face_flags(em, 1);
-                       break;
-               case 9:
-//                     mesh_set_face_flags(em, 0);
-                       break;
-                       
-               /* uv texface options */
-               case 10:
-//                     mesh_rotate_uvs(em);
-                       break;
-               case 11:
-//                     mesh_mirror_uvs(em);
-                       break;
-               case 12:
-//                     mesh_rotate_colors(em);
-                       break;
-               case 13:
-//                     mesh_mirror_colors(em);
-                       break;
-       }
-       /* some items crashed because this is in the original W menu but not here. should really manage this better */
-//     DAG_object_flush_update(scene, obedit, OB_RECALC_DATA);
+       /* identifiers */
+       ot->name= "Mark Sharp";
+       ot->idname= "MESH_OT_mark_sharp";
+       
+       /* api callbacks */
+       ot->exec= editmesh_mark_sharp;
+       ot->poll= ED_operator_editmesh;
+       
+       /* flags */
+       ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
+       
+       RNA_def_boolean(ot->srna, "clear", 0, "Clear", "");
 }
 
-
 /* **************** NORMALS ************** */
 
 /* XXX value of select is messed up, it means two things */
@@ -4119,23 +3952,28 @@ void righthandfaces(EditMesh *em, int select)   /* makes faces righthand turning *
 
 static int righthandfaces_exec(bContext *C, wmOperator *op)
 {
+       Scene *scene = CTX_data_scene(C);
        Object *obedit= CTX_data_edit_object(C);
-       EditMesh *em= ((Mesh *)obedit->data)->edit_mesh;
+       EditMesh *em= BKE_mesh_get_editmesh(((Mesh *)obedit->data));
        
        /* 'standard' behaviour - check if selected, then apply relevant selection */
        
        // XXX  need other args
        righthandfaces(em, RNA_boolean_get(op->ptr, "inside"));
        
+       BKE_mesh_end_editmesh(obedit->data, em);
+
+       DAG_object_flush_update(scene, obedit, OB_RECALC_DATA);
        WM_event_add_notifier(C, NC_OBJECT|ND_GEOM_SELECT, obedit); //TODO is this needed ?
+
        return OPERATOR_FINISHED;       
 }
 
-void MESH_OT_consistant_normals(wmOperatorType *ot)
+void MESH_OT_normals_make_consistent(wmOperatorType *ot)
 {
        /* identifiers */
-       ot->name= "Make Normals consistant";
-       ot->idname= "MESH_OT_consistant_normals";
+       ot->name= "Make Normals Consistent";
+       ot->idname= "MESH_OT_normals_make_consistent";
        
        /* api callbacks */
        ot->exec= righthandfaces_exec;
@@ -4183,7 +4021,7 @@ static int mface_is_selected(MFace *mf)
         * which would use same as vertices method), then added
         * to interface! Hoera! - zr
         */
-void faceselect_align_view_to_selected(View3D *v3d, RegionView3D *rv3d, Mesh *me, int axis)
+void faceselect_align_view_to_selected(View3D *v3d, RegionView3D *rv3d, Mesh *me, wmOperator *op,  int axis)
 {
        float norm[3];
        int i, totselected = 0;
@@ -4214,7 +4052,7 @@ void faceselect_align_view_to_selected(View3D *v3d, RegionView3D *rv3d, Mesh *me
        }
 
        if (totselected == 0)
-               error("No faces selected.");
+               BKE_report(op->reports, RPT_ERROR, "No faces selected.");
        else
                view3d_align_axis_to_vector(v3d, rv3d, axis, norm);
 }
@@ -4240,13 +4078,13 @@ static void face_getnormal_obspace(Object *obedit, EditFace *efa, float *fno)
 }
 
 
-void editmesh_align_view_to_selected(Object *obedit, EditMesh *em, View3D *v3d, RegionView3D *rv3d, int axis)
+void editmesh_align_view_to_selected(Object *obedit, EditMesh *em, wmOperator *op, View3D *v3d, RegionView3D *rv3d, int axis)
 {
        int nselverts= EM_nvertices_selected(em);
        float norm[3]={0.0, 0.0, 0.0}; /* used for storing the mesh normal */
        
        if (nselverts==0) {
-               error("No faces or vertices selected.");
+               BKE_report(op->reports, RPT_ERROR, "No faces or vertices selected.");
        } 
        else if (EM_nfaces_selected(em)) {
                EditFace *efa;
@@ -4323,17 +4161,17 @@ void editmesh_align_view_to_selected(Object *obedit, EditMesh *em, View3D *v3d,
 
 /* **************** VERTEX DEFORMS *************** */
 
-void vertexsmooth(Object *obedit, EditMesh *em)
+static int smooth_vertex(bContext *C, wmOperator *op)
 {
-       Scene *scene= NULL; // XXX
+       Scene *scene= CTX_data_scene(C);
+       Object *obedit= CTX_data_edit_object(C);
+       EditMesh *em= BKE_mesh_get_editmesh(((Mesh *)obedit->data));
        EditVert *eve, *eve_mir = NULL;
        EditEdge *eed;
        float *adror, *adr, fac;
        float fvec[3];
        int teller=0;
-       ModifierData *md= obedit->modifiers.first;
-
-       if(em==NULL) return;
+       ModifierData *md;
 
        /* count */
        eve= em->verts.first;
@@ -4341,7 +4179,10 @@ void vertexsmooth(Object *obedit, EditMesh *em)
                if(eve->f & SELECT) teller++;
                eve= eve->next;
        }
-       if(teller==0) return;
+       if(teller==0) {
+               BKE_mesh_end_editmesh(obedit->data, em);
+               return OPERATOR_CANCELLED;
+       }
        
        adr=adror= (float *)MEM_callocN(3*sizeof(float *)*teller, "vertsmooth");
        eve= em->verts.first;
@@ -4358,8 +4199,8 @@ void vertexsmooth(Object *obedit, EditMesh *em)
        /* if there is a mirror modifier with clipping, flag the verts that
         * are within tolerance of the plane(s) of reflection 
         */
-       for (; md; md=md->next) {
-               if (md->type==eModifierType_Mirror) {
+       for(md=obedit->modifiers.first; md; md=md->next) {
+               if(md->type==eModifierType_Mirror) {
                        MirrorModifierData *mmd = (MirrorModifierData*) md;     
                
                        if(mmd->flag & MOD_MIR_CLIPPING) {
@@ -4450,8 +4291,42 @@ void vertexsmooth(Object *obedit, EditMesh *em)
 
        recalc_editnormals(em);
 
-//     DAG_object_flush_update(scene, obedit, OB_RECALC_DATA);
+       BKE_mesh_end_editmesh(obedit->data, em);
 
+       DAG_object_flush_update(scene, obedit, OB_RECALC_DATA);
+       WM_event_add_notifier(C, NC_OBJECT|ND_GEOM_SELECT, obedit);
+
+       return OPERATOR_FINISHED;
+}
+
+static int smooth_vertex_exec(bContext *C, wmOperator *op)
+{
+       int repeat = RNA_int_get(op->ptr, "repeat");
+       int i;
+
+       if (!repeat) repeat = 1;
+
+       for (i=0; i<repeat; i++) {
+               smooth_vertex(C, op);
+       }
+
+       return OPERATOR_FINISHED;
+}
+
+void MESH_OT_vertices_smooth(wmOperatorType *ot)
+{
+       /* identifiers */
+       ot->name= "Smooth Vertex";
+       ot->idname= "MESH_OT_vertices_smooth";
+       
+       /* api callbacks */
+       ot->exec= smooth_vertex_exec;
+       ot->poll= ED_operator_editmesh;
+       
+       /* flags */
+       ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
+
+       RNA_def_int(ot->srna, "repeat", 1, 1, 100, "Number of times to smooth the mesh", "", 1, INT_MAX);
 }
 
 void vertexnoise(Object *obedit, EditMesh *em)
@@ -4499,7 +4374,7 @@ void vertexnoise(Object *obedit, EditMesh *em)
 
 }
 
-void vertices_to_sphere(Scene *scene, View3D *v3d, Object *obedit, EditMesh *em, int perc)
+static void vertices_to_sphere(Scene *scene, View3D *v3d, Object *obedit, EditMesh *em, float perc)
 {
        EditVert *eve;
        float *curs, len, vec[3], cent[3], fac, facm, imat[3][3], bmat[3][3];
@@ -4507,8 +4382,8 @@ void vertices_to_sphere(Scene *scene, View3D *v3d, Object *obedit, EditMesh *em,
        
 // XXX if(button(&perc, 1, 100, "Percentage:")==0) return;
        
-       fac= perc/100.0;
-       facm= 1.0-fac;
+       fac= perc/100.0f;
+       facm= 1.0f-fac;
        
        Mat3CpyMat4(bmat, obedit->obmat);
        Mat3Inv(imat, bmat);
@@ -4561,20 +4436,23 @@ static int vertices_to_sphere_exec(bContext *C, wmOperator *op)
        Object *obedit= CTX_data_edit_object(C);
        View3D *v3d = CTX_wm_view3d(C);
        Scene *scene = CTX_data_scene(C);
-       EditMesh *em= ((Mesh *)obedit->data)->edit_mesh;
+       EditMesh *em= BKE_mesh_get_editmesh(((Mesh *)obedit->data));
        
-       vertices_to_sphere(scene, v3d, obedit, em, RNA_int_get(op->ptr,"percentage"));
+       vertices_to_sphere(scene, v3d, obedit, em, RNA_float_get(op->ptr,"percent"));
                
+       BKE_mesh_end_editmesh(obedit->data, em);
+
+       DAG_object_flush_update(scene, obedit, OB_RECALC_DATA);
        WM_event_add_notifier(C, NC_OBJECT|ND_GEOM_SELECT, obedit);
        
        return OPERATOR_FINISHED;       
 }
 
-void MESH_OT_vertices_to_sphere(wmOperatorType *ot)
+void MESH_OT_vertices_transform_to_sphere(wmOperatorType *ot)
 {
        /* identifiers */
        ot->name= "Vertices to Sphere";
-       ot->idname= "MESH_OT_vertices_to_sphere";
+       ot->idname= "MESH_OT_vertices_transform_to_sphere";
        
        /* api callbacks */
        ot->exec= vertices_to_sphere_exec;
@@ -4584,7 +4462,66 @@ void MESH_OT_vertices_to_sphere(wmOperatorType *ot)
        ot->flag= OPTYPE_REGISTER/*|OPTYPE_UNDO*/;
        
        /* props */
+       RNA_def_float(ot->srna, "percent", 100.0f, 0.0f, 100.0f, "Percent", "DOC_BROKEN", 0.01f, 100.0f);
+}
+
+void flipface(EditMesh *em, EditFace *efa)
+{
+       if(efa->v4) {
+               SWAP(EditVert *, efa->v2, efa->v4);
+               SWAP(EditEdge *, efa->e1, efa->e4);
+               SWAP(EditEdge *, efa->e2, efa->e3);
+               EM_data_interp_from_faces(em, efa, NULL, efa, 0, 3, 2, 1);
+       }
+       else {
+               SWAP(EditVert *, efa->v2, efa->v3);
+               SWAP(EditEdge *, efa->e1, efa->e3);
+               efa->e2->dir= 1-efa->e2->dir;
+               EM_data_interp_from_faces(em, efa, NULL, efa, 0, 2, 1, 3);
+       }
+
+       if(efa->v4) CalcNormFloat4(efa->v1->co, efa->v2->co, efa->v3->co, efa->v4->co, efa->n);
+       else CalcNormFloat(efa->v1->co, efa->v2->co, efa->v3->co, efa->n);
+}
+
+
+static int flip_normals(bContext *C, wmOperator *op)
+{
+       Scene *scene = CTX_data_scene(C);
+       Object *obedit= CTX_data_edit_object(C);
+       EditMesh *em= BKE_mesh_get_editmesh(((Mesh *)obedit->data));
+       EditFace *efa;
+       
+       efa= em->faces.first;
+       while(efa) {
+               if( efa->f & SELECT ){
+                       flipface(em, efa);
+               }
+               efa= efa->next;
+       }
+       
+       /* update vertex normals too */
+       recalc_editnormals(em);
+
+       BKE_mesh_end_editmesh(obedit->data, em);
+
+       DAG_object_flush_update(scene, obedit, OB_RECALC_DATA);
+       WM_event_add_notifier(C, NC_OBJECT|ND_GEOM_SELECT, obedit);
 
+       return OPERATOR_FINISHED;
+}
+
+void MESH_OT_flip_normals(wmOperatorType *ot)
+{
+       /* identifiers */
+       ot->name= "Flip Normals";
+       ot->idname= "MESH_OT_flip_normals";
        
-       RNA_def_int(ot->srna, "percentage", 100, 0, 100, "Percentage", "", 0, 100);
+       /* api callbacks */
+       ot->exec= flip_normals;
+       ot->poll= ED_operator_editmesh;
+       
+       /* flags */
+       ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
 }
+