]> git.lyx.org Git - lyx.git/blobdiff - src/text2.C
More fixes to insettabular/text (and some missing features added).
[lyx.git] / src / text2.C
index c119a12ebf8efc9129a225b268cbd8ab7ec46e9c..16fe687dc3057cc29c3c0af02da180ff8c9a6ce2 100644 (file)
@@ -22,6 +22,8 @@
 #include "insets/inseterror.h"
 #include "insets/insetbib.h"
 #include "insets/insetspecialchar.h"
+#include "insets/insettext.h"
+#include "insets/insetfloat.h"
 #include "layout.h"
 #include "LyXView.h"
 #include "support/textutils.h"
 #include "Painter.h"
 #include "font.h"
 #include "debug.h"
-
-//#define USE_OLD_CUT_AND_PASTE 1
+#include "lyxrc.h"
+#include "FloatList.h"
 
 using std::copy;
 using std::endl;
 using std::pair;
 
+
 LyXText::LyXText(BufferView * bv)
 {
        bv_owner = bv;
@@ -64,11 +67,14 @@ LyXText::LyXText(InsetText * inset)
 
 void LyXText::init()
 {
+       the_locking_inset = 0;
        firstrow = 0;
        lastrow = 0;
        number_of_rows = 0;
        refresh_y = 0;
        height = 0;
+       width = 0;
+       first = 0;
        status = LyXText::UNCHANGED;
        // set cursor at the very top position
        selection = true;           /* these setting is necessary 
@@ -129,9 +135,11 @@ void LyXText::init(BufferView * bview)
                par = par->Next();
        }
        SetCursorIntern(bview, firstrow->par(), 0);
-#if 1
+       sel_cursor = cursor;
+#if 0
        // Dump all rowinformation:
        Row * tmprow = firstrow;
+       lyxerr << "Width = " << width << endl;
        lyxerr << "Baseline Paragraph Pos Height Ascent Fill\n";
        while (tmprow) {
                lyxerr << tmprow->baseline() << '\t'
@@ -158,14 +166,6 @@ LyXText::~LyXText()
 }
 
 
-#if 0
-void LyXText::owner(BufferView * bv)
-{
-       if (bv_owner && bv) lyxerr << "LyXText::bv_owner already set!" << endl;
-       bv_owner = bv;
-}
-#endif
-
 // Gets the fully instantiated font at a given position in a paragraph
 // Basically the same routine as LyXParagraph::getFont() in paragraph.C.
 // The difference is that this one is used for displaying, and thus we
@@ -173,7 +173,7 @@ void LyXText::owner(BufferView * bv)
 // smaller. (Asger)
 // If position is -1, we get the layout font of the paragraph.
 // If position is -2, we get the font of the manual label of the paragraph.
-LyXFont LyXText::GetFont(Buffer const * buf, LyXParagraph * par,
+LyXFont const LyXText::GetFont(Buffer const * buf, LyXParagraph * par,
                         LyXParagraph::size_type pos) const
 {
        LyXLayout const & layout = 
@@ -181,17 +181,24 @@ LyXFont LyXText::GetFont(Buffer const * buf, LyXParagraph * par,
 
        char par_depth = par->GetDepth();
        // We specialize the 95% common case:
-       if (par->footnoteflag == LyXParagraph::NO_FOOTNOTE && !par_depth) {
+       if (
+#ifndef NEW_INSETS
+               par->footnoteflag == LyXParagraph::NO_FOOTNOTE &&
+#endif
+               !par_depth) {
                if (pos >= 0){
                        // 95% goes here
                        if (layout.labeltype == LABEL_MANUAL
                            && pos < BeginningOfMainBody(buf, par)) {
                                // 1% goes here
-                               return par->GetFontSettings(buf->params, pos).
-                                       realize(layout.reslabelfont);
-                       } else
-                               return par->GetFontSettings(buf->params, pos).
-                                       realize(layout.resfont);
+                               LyXFont f = par->GetFontSettings(buf->params,
+                                                                pos);
+                               return f.realize(layout.reslabelfont);
+                       } else {
+                               LyXFont f = par->GetFontSettings(buf->params, pos);
+                               return f.realize(layout.resfont);
+                       }
+                       
                } else {
                        // 5% goes here.
                        // process layoutfont for pos == -1 and labelfont for pos < -1
@@ -239,13 +246,14 @@ LyXFont LyXText::GetFont(Buffer const * buf, LyXParagraph * par,
 
        tmpfont.realize(textclasslist.TextClass(buf->params.textclass).defaultfont());
 
+#ifndef NEW_INSETS
        // Cosmetic improvement: If this is an open footnote, make the font 
        // smaller.
        if (par->footnoteflag == LyXParagraph::OPEN_FOOTNOTE
            && par->footnotekind == LyXParagraph::FOOTNOTE) {
                tmpfont.decSize();
        }
-
+#endif
        return tmpfont;
 }
 
@@ -287,11 +295,12 @@ void LyXText::SetCharFont(Buffer const * buf, LyXParagraph * par,
 
        layoutfont.realize(textclasslist.TextClass(buf->params.textclass).defaultfont());
 
+#ifndef NEW_INSETS
        if (par->footnoteflag == LyXParagraph::OPEN_FOOTNOTE
            && par->footnotekind == LyXParagraph::FOOTNOTE) {
                layoutfont.decSize();
        }
-
+#endif
        // Now, reduce font against full layout font
        font.reduce(layoutfont);
 
@@ -337,7 +346,7 @@ void LyXText::RemoveRow(Row * row) const
        /* this must not happen before the currentrow for clear reasons.
           so the trick is just to set the current row onto the previous
           row of this row */
-       long unused_y;
+       int unused_y;
        GetRow(row->par(), row->pos(), unused_y);
    
        if (row->next())
@@ -390,8 +399,9 @@ void LyXText::InsertParagraph(BufferView * bview, LyXParagraph * par,
                AppendParagraph(bview, row->next());
        }
 }
-    
 
+
+#ifndef NEW_INSETS
 void LyXText::ToggleFootnote(BufferView * bview)
 {
        LyXParagraph * par = cursor.par()->ParFromPos(cursor.pos());
@@ -404,14 +414,15 @@ void LyXText::ToggleFootnote(BufferView * bview)
                CloseFootnote(bview);
        }
 }
+#endif
 
 
+#ifndef NEW_INSETS
 void LyXText::OpenStuff(BufferView * bview)
 {
        if (cursor.pos() == 0 && cursor.par()->bibkey){
                cursor.par()->bibkey->Edit(bview, 0, 0, 0);
-       }
-       else if (cursor.pos() < cursor.par()->Last() 
+       } else if (cursor.pos() < cursor.par()->Last() 
                 && cursor.par()->GetChar(cursor.pos()) == LyXParagraph::META_INSET
                 && cursor.par()->GetInset(cursor.pos())->Editable()) {
                bview->owner()->getMiniBuffer()
@@ -419,12 +430,17 @@ void LyXText::OpenStuff(BufferView * bview)
                if (cursor.par()->GetInset(cursor.pos())->Editable() != Inset::HIGHLY_EDITABLE)
                        SetCursorParUndo(bview->buffer());
                cursor.par()->GetInset(cursor.pos())->Edit(bview, 0, 0, 0);
-       } else {
+       }
+#ifndef NEW_INSETS
+       else {
                ToggleFootnote(bview);
        }
+#endif
 }
+#endif
 
 
+#ifndef NEW_INSETS
 void LyXText::CloseFootnote(BufferView * bview)
 {
        LyXParagraph * tmppar;
@@ -494,11 +510,13 @@ void LyXText::CloseFootnote(BufferView * bview)
        if (cursor.row()->next())
                SetHeightOfRow(bview, cursor.row()->next());
 }
+#endif
 
 
 /* used in setlayout */
 // Asger is not sure we want to do this...
-void LyXText::MakeFontEntriesLayoutSpecific(Buffer const * buf, LyXParagraph * par)
+void LyXText::MakeFontEntriesLayoutSpecific(Buffer const * buf,
+                                           LyXParagraph * par)
 {
    
        LyXLayout const & layout =
@@ -518,17 +536,26 @@ void LyXText::MakeFontEntriesLayoutSpecific(Buffer const * buf, LyXParagraph * p
        }
 }
 
+
 LyXParagraph * LyXText::SetLayout(BufferView * bview,
                                  LyXCursor & cur, LyXCursor & sstart_cur,
                                  LyXCursor & send_cur,
                                  LyXTextClass::size_type layout)
 {
+#ifndef NEW_INSETS
        LyXParagraph * endpar = send_cur.par()->LastPhysicalPar()->Next();
+#else
+       LyXParagraph * endpar = send_cur.par()->Next();
+#endif
        LyXParagraph * undoendpar = endpar;
 
        if (endpar && endpar->GetDepth()) {
                while (endpar && endpar->GetDepth()) {
+#ifndef NEW_INSETS
                        endpar = endpar->LastPhysicalPar()->Next();
+#else
+                       endpar = endpar->Next();
+#endif
                        undoendpar = endpar;
                }
        } else if (endpar) {
@@ -536,7 +563,11 @@ LyXParagraph * LyXText::SetLayout(BufferView * bview,
        }
    
        SetUndo(bview->buffer(), Undo::EDIT,
-               sstart_cur.par()->ParFromPos(sstart_cur.pos())->previous, 
+#ifndef NEW_INSETS
+               sstart_cur.par()->ParFromPos(sstart_cur.pos())->previous,
+#else
+               sstart_cur.par()->previous,
+#endif
                undoendpar);
 
        /* ok we have a selection. This is always between sstart_cur
@@ -547,10 +578,16 @@ LyXParagraph * LyXText::SetLayout(BufferView * bview,
                textclasslist.Style(bview->buffer()->params.textclass, layout);
    
        while (cur.par() != send_cur.par()) {
+#ifndef NEW_INSETS
                if (cur.par()->footnoteflag == sstart_cur.par()->footnoteflag) {
+#endif
                        cur.par()->SetLayout(bview->buffer()->params, layout);
                        MakeFontEntriesLayoutSpecific(bview->buffer(), cur.par());
+#ifndef NEW_INSETS
                        LyXParagraph * fppar = cur.par()->FirstPhysicalPar();
+#else
+                       LyXParagraph * fppar = cur.par();
+#endif
                        fppar->added_space_top = lyxlayout.fill_top ?
                                VSpace(VSpace::VFILL) : VSpace(VSpace::NONE);
                        fppar->added_space_bottom = lyxlayout.fill_bottom ? 
@@ -562,13 +599,21 @@ LyXParagraph * LyXText::SetLayout(BufferView * bview,
                                delete fppar->bibkey;
                                fppar->bibkey = 0;
                        }
+#ifndef NEW_INSETS
                }
+#endif
                cur.par(cur.par()->Next());
        }
+#ifndef NEW_INSETS
        if (cur.par()->footnoteflag == sstart_cur.par()->footnoteflag) {
+#endif
                cur.par()->SetLayout(bview->buffer()->params, layout);
                MakeFontEntriesLayoutSpecific(bview->buffer(), cur.par());
+#ifndef NEW_INSETS
                LyXParagraph * fppar = cur.par()->FirstPhysicalPar();
+#else
+               LyXParagraph * fppar = cur.par();
+#endif
                fppar->added_space_top = lyxlayout.fill_top ?
                        VSpace(VSpace::VFILL) : VSpace(VSpace::NONE);
                fppar->added_space_bottom = lyxlayout.fill_bottom ? 
@@ -580,86 +625,17 @@ LyXParagraph * LyXText::SetLayout(BufferView * bview,
                        delete fppar->bibkey;
                        fppar->bibkey = 0;
                }
+#ifndef NEW_INSETS
        }
+#endif
        return endpar;
 }
 
 // set layout over selection and make a total rebreak of those paragraphs
 void LyXText::SetLayout(BufferView * bview, LyXTextClass::size_type layout)
 {
-       LyXCursor
-               tmpcursor = cursor;  /* store the current cursor  */
-
-#ifdef USE_OLD_SET_LAYOUT
-       // if there is no selection just set the layout
-       // of the current paragraph  */
-       if (!selection) {
-               sel_start_cursor = cursor;  // dummy selection
-               sel_end_cursor = cursor;
-       }
-
-       LyXParagraph * endpar = sel_end_cursor.par()->LastPhysicalPar()->Next();
-       LyXParagraph * undoendpar = endpar;
-
-       if (endpar && endpar->GetDepth()) {
-               while (endpar && endpar->GetDepth()) {
-                       endpar = endpar->LastPhysicalPar()->Next();
-                       undoendpar = endpar;
-               }
-       }
-       else if (endpar) {
-               endpar = endpar->Next(); // because of parindents etc.
-       }
-   
-       SetUndo(Undo::EDIT, 
-               sel_start_cursor.par()->ParFromPos(sel_start_cursor.pos())->previous, 
-               undoendpar);
+       LyXCursor tmpcursor = cursor;  /* store the current cursor  */
 
-       /* ok we have a selection. This is always between sel_start_cursor
-        * and sel_end cursor */ 
-       cursor = sel_start_cursor;
-   
-       LyXLayout const & lyxlayout =
-               textclasslist.Style(bview->buffer()->params.textclass, layout);
-   
-       while (cursor.par() != sel_end_cursor.par()) {
-               if (cursor.par()->footnoteflag ==
-                   sel_start_cursor.par()->footnoteflag) {
-                       cursor.par()->SetLayout(layout);
-                       MakeFontEntriesLayoutSpecific(cursor.par());
-                       LyXParagraph* fppar = cursor.par()->FirstPhysicalPar();
-                       fppar->added_space_top = lyxlayout.fill_top ?
-                               VSpace(VSpace::VFILL) : VSpace(VSpace::NONE);
-                       fppar->added_space_bottom = lyxlayout.fill_bottom ? 
-                               VSpace(VSpace::VFILL) : VSpace(VSpace::NONE); 
-                       if (lyxlayout.margintype == MARGIN_MANUAL)
-                               cursor.par()->SetLabelWidthString(lyxlayout.labelstring());
-                       if (lyxlayout.labeltype != LABEL_BIBLIO
-                           && fppar->bibkey) {
-                               delete fppar->bibkey;
-                               fppar->bibkey = 0;
-                       }
-               }
-               cursor.par() = cursor.par()->Next();
-       }
-       if (cursor.par()->footnoteflag ==
-           sel_start_cursor.par()->footnoteflag) {
-               cursor.par()->SetLayout(layout);
-               MakeFontEntriesLayoutSpecific(cursor.par());
-               LyXParagraph* fppar = cursor.par()->FirstPhysicalPar();
-               fppar->added_space_top = lyxlayout.fill_top ?
-                       VSpace(VSpace::VFILL) : VSpace(VSpace::NONE);
-               fppar->added_space_bottom = lyxlayout.fill_bottom ? 
-                       VSpace(VSpace::VFILL) : VSpace(VSpace::NONE); 
-               if (lyxlayout.margintype == MARGIN_MANUAL)
-                       cursor.par()->SetLabelWidthString(lyxlayout.labelstring());
-               if (lyxlayout.labeltype != LABEL_BIBLIO
-                   && fppar->bibkey) {
-                       delete fppar->bibkey;
-                       fppar->bibkey = 0;
-               }
-       }
-#else
        // if there is no selection just set the layout
        // of the current paragraph  */
        if (!selection) {
@@ -669,11 +645,10 @@ void LyXText::SetLayout(BufferView * bview, LyXTextClass::size_type layout)
        LyXParagraph *
                endpar = SetLayout(bview, cursor, sel_start_cursor,
                                   sel_end_cursor, layout);
-#endif
        RedoParagraphs(bview, sel_start_cursor, endpar);
    
        // we have to reset the selection, because the
-       // geometry could have changed */ 
+       // geometry could have changed
        SetCursor(bview, sel_start_cursor.par(),
                  sel_start_cursor.pos(), false);
        sel_cursor = cursor;
@@ -681,7 +656,7 @@ void LyXText::SetLayout(BufferView * bview, LyXTextClass::size_type layout)
                  false);
        UpdateCounters(bview, cursor.row());
        ClearSelection();
-       SetSelection(bview);
+       SetSelection();
        SetCursor(bview, tmpcursor.par(), tmpcursor.pos(), true);
 }
 
@@ -697,12 +672,21 @@ void  LyXText::IncDepth(BufferView * bview)
        }
 
        // We end at the next paragraph with depth 0
-       LyXParagraph * endpar = sel_end_cursor.par()->LastPhysicalPar()->Next();
+       LyXParagraph * endpar =
+#ifndef NEW_INSETS
+               sel_end_cursor.par()->LastPhysicalPar()->Next();
+#else
+               sel_end_cursor.par()->Next();
+#endif
        LyXParagraph * undoendpar = endpar;
 
        if (endpar && endpar->GetDepth()) {
                while (endpar && endpar->GetDepth()) {
+#ifndef NEW_INSETS
                        endpar = endpar->LastPhysicalPar()->Next();
+#else
+                       endpar = endpar->Next();
+#endif
                        undoendpar = endpar;
                }
        }
@@ -710,9 +694,13 @@ void  LyXText::IncDepth(BufferView * bview)
                endpar = endpar->Next(); // because of parindents etc.
        }
        
-       SetUndo(bview->buffer(), Undo::EDIT, 
+       SetUndo(bview->buffer(), Undo::EDIT,
+#ifndef NEW_INSETS
                sel_start_cursor
-               .par()->ParFromPos(sel_start_cursor.pos())->previous, 
+               .par()->ParFromPos(sel_start_cursor.pos())->previous,
+#else
+               sel_start_cursor.par()->previous,
+#endif
                undoendpar);
 
        LyXCursor tmpcursor = cursor; // store the current cursor
@@ -725,19 +713,30 @@ void  LyXText::IncDepth(BufferView * bview)
    
        while (true) {
                // NOTE: you can't change the depth of a bibliography entry
-               if (cursor.par()->footnoteflag ==
-                   sel_start_cursor.par()->footnoteflag
-                   && textclasslist.Style(bview->buffer()->params.textclass,
+               if (
+#ifndef NEW_INSETS
+                       cursor.par()->footnoteflag ==
+                   sel_start_cursor.par()->footnoteflag &&
+#endif
+                   textclasslist.Style(bview->buffer()->params.textclass,
                                      cursor.par()->GetLayout()
                                     ).labeltype != LABEL_BIBLIO) {
                        LyXParagraph * prev =
+#ifndef NEW_INSETS
                                cursor.par()->FirstPhysicalPar()->Previous();
+#else
+                               cursor.par()->Previous();
+#endif
                        if (prev 
                            && (prev->GetDepth() - cursor.par()->GetDepth() > 0
                                || (prev->GetDepth() == cursor.par()->GetDepth()
                                    && textclasslist.Style(bview->buffer()->params.textclass,
                                                      prev->GetLayout()).isEnvironment()))) {
+#ifndef NEW_INSETS
                                cursor.par()->FirstPhysicalPar()->depth++;
+#else
+                               cursor.par()->depth++;
+#endif
                                anything_changed = true;
                                }
                }
@@ -750,11 +749,19 @@ void  LyXText::IncDepth(BufferView * bview)
        if (!anything_changed) {
                cursor = sel_start_cursor;
                while (cursor.par() != sel_end_cursor.par()) {
+#ifndef NEW_INSETS
                        cursor.par()->FirstPhysicalPar()->depth = 0;
+#else
+                       cursor.par()->depth = 0;
+#endif
                        cursor.par(cursor.par()->Next());
                }
+#ifndef NEW_INSETS
                if (cursor.par()->footnoteflag == sel_start_cursor.par()->footnoteflag)
                        cursor.par()->FirstPhysicalPar()->depth = 0;
+#else
+                       cursor.par()->depth = 0;
+#endif
        }
    
        RedoParagraphs(bview, sel_start_cursor, endpar);
@@ -767,7 +774,7 @@ void  LyXText::IncDepth(BufferView * bview)
        SetCursor(bview, sel_end_cursor.par(), sel_end_cursor.pos());
        UpdateCounters(bview, cursor.row());
        ClearSelection();
-       SetSelection(bview);
+       SetSelection();
        SetCursor(bview, tmpcursor.par(), tmpcursor.pos());
 }
 
@@ -782,13 +789,20 @@ void  LyXText::DecDepth(BufferView * bview)
                sel_start_cursor = cursor; // dummy selection
                sel_end_cursor = cursor;
        }
-   
+#ifndef NEW_INSETS
        LyXParagraph * endpar = sel_end_cursor.par()->LastPhysicalPar()->Next();
+#else
+       LyXParagraph * endpar = sel_end_cursor.par()->Next();
+#endif
        LyXParagraph * undoendpar = endpar;
 
        if (endpar && endpar->GetDepth()) {
                while (endpar && endpar->GetDepth()) {
+#ifndef NEW_INSETS
                        endpar = endpar->LastPhysicalPar()->Next();
+#else
+                       endpar = endpar->Next();
+#endif
                        undoendpar = endpar;
                }
        }
@@ -796,9 +810,13 @@ void  LyXText::DecDepth(BufferView * bview)
                endpar = endpar->Next(); // because of parindents etc.
        }
    
-       SetUndo(bview->buffer(), Undo::EDIT, 
+       SetUndo(bview->buffer(), Undo::EDIT,
+#ifndef NEW_INSETS
                sel_start_cursor
-               .par()->ParFromPos(sel_start_cursor.pos())->previous, 
+               .par()->ParFromPos(sel_start_cursor.pos())->previous,
+#else
+               sel_start_cursor.par()->previous,
+#endif
                undoendpar);
 
        LyXCursor tmpcursor = cursor; // store the current cursor
@@ -808,11 +826,16 @@ void  LyXText::DecDepth(BufferView * bview)
        cursor = sel_start_cursor;
 
        while (true) {
+#ifndef NEW_INSETS
                if (cursor.par()->footnoteflag ==
                    sel_start_cursor.par()->footnoteflag) {
                        if (cursor.par()->FirstPhysicalPar()->depth)
                                cursor.par()->FirstPhysicalPar()->depth--;
                }
+#else
+                       if (cursor.par()->depth)
+                               cursor.par()->depth--;
+#endif
                if (cursor.par() == sel_end_cursor.par())
                        break;
                cursor.par(cursor.par()->Next());
@@ -828,7 +851,7 @@ void  LyXText::DecDepth(BufferView * bview)
        SetCursor(bview, sel_end_cursor.par(), sel_end_cursor.pos());
        UpdateCounters(bview, cursor.row());
        ClearSelection();
-       SetSelection(bview);
+       SetSelection();
        SetCursor(bview, tmpcursor.par(), tmpcursor.pos());
 }
 
@@ -847,7 +870,7 @@ void LyXText::SetFont(BufferView * bview, LyXFont const & font, bool toggleall)
                        layoutfont = GetFont(bview->buffer(), cursor.par(),-1);
                // Update current font
                real_current_font.update(font,
-                                        bview->buffer()->params.language_info,
+                                        bview->buffer()->params.language,
                                         toggleall);
 
                // Reduce to implicit settings
@@ -863,23 +886,35 @@ void LyXText::SetFont(BufferView * bview, LyXFont const & font, bool toggleall)
        // ok we have a selection. This is always between sel_start_cursor
        // and sel_end cursor
    
-       SetUndo(bview->buffer(), Undo::EDIT, 
+       SetUndo(bview->buffer(), Undo::EDIT,
+#ifndef NEW_INSETS
                sel_start_cursor.par()->ParFromPos(sel_start_cursor.pos())->previous, 
-               sel_end_cursor.par()->ParFromPos(sel_end_cursor.pos())->next); 
+               sel_end_cursor.par()->ParFromPos(sel_end_cursor.pos())->next
+#else
+               sel_start_cursor.par()->previous, 
+               sel_end_cursor.par()->next
+#endif
+               ); 
        cursor = sel_start_cursor;
        while (cursor.par() != sel_end_cursor.par() ||
-              (cursor.par()->footnoteflag == sel_start_cursor.par()->footnoteflag
-               && cursor.pos() < sel_end_cursor.pos())) 
+              (
+#ifndef NEW_INSETS
+                      cursor.par()->footnoteflag == sel_start_cursor.par()->footnoteflag &&
+#endif
+               cursor.pos() < sel_end_cursor.pos())) 
        {
                if (cursor.pos() < cursor.par()->Last()
+#ifndef NEW_INSETS
                    && cursor.par()->footnoteflag
-                   == sel_start_cursor.par()->footnoteflag) {
+                   == sel_start_cursor.par()->footnoteflag
+#endif
+                       ) {
                        // an open footnote should behave
                        // like a closed one
                        LyXFont newfont = GetFont(bview->buffer(), 
                                                  cursor.par(), cursor.pos());
                        newfont.update(font,
-                                      bview->buffer()->params.language_info,
+                                      bview->buffer()->params.language,
                                       toggleall);
                        SetCharFont(bview->buffer(),
                                    cursor.par(), cursor.pos(), newfont);
@@ -898,7 +933,7 @@ void LyXText::SetFont(BufferView * bview, LyXFont const & font, bool toggleall)
        sel_cursor = cursor;
        SetCursor(bview, sel_end_cursor.par(), sel_end_cursor.pos());
        ClearSelection();
-       SetSelection(bview);
+       SetSelection();
        SetCursor(bview, tmpcursor.par(), tmpcursor.pos(), true,
                  tmpcursor.boundary());
 }
@@ -907,10 +942,14 @@ void LyXText::SetFont(BufferView * bview, LyXFont const & font, bool toggleall)
 void LyXText::RedoHeightOfParagraph(BufferView * bview, LyXCursor const & cur)
 {
        Row * tmprow = cur.row();
-       long y = cur.y() - tmprow->baseline();
+       int y = cur.y() - tmprow->baseline();
 
        SetHeightOfRow(bview, tmprow);
+#ifndef NEW_INSETS
        LyXParagraph * first_phys_par = tmprow->par()->FirstPhysicalPar();
+#else
+       LyXParagraph * first_phys_par = tmprow->par();
+#endif
        // find the first row of the paragraph
        if (first_phys_par != tmprow->par())
                while (tmprow->previous()
@@ -937,9 +976,13 @@ void LyXText::RedoDrawingOfParagraph(BufferView * bview, LyXCursor const & cur)
 {
        Row * tmprow = cur.row();
    
-       long y = cur.y() - tmprow->baseline();
+       int y = cur.y() - tmprow->baseline();
        SetHeightOfRow(bview, tmprow);
+#ifndef NEW_INSETS
        LyXParagraph * first_phys_par = tmprow->par()->FirstPhysicalPar();
+#else
+       LyXParagraph * first_phys_par = tmprow->par();
+#endif
        // find the first row of the paragraph
        if (first_phys_par != tmprow->par())
                while (tmprow->previous() && tmprow->previous()->par() != first_phys_par)  {
@@ -972,12 +1015,16 @@ void LyXText::RedoParagraphs(BufferView * bview, LyXCursor const & cur,
    
        Row * tmprow = cur.row();
    
-       long y = cur.y() - tmprow->baseline();
+       int y = cur.y() - tmprow->baseline();
    
        if (!tmprow->previous()){
                first_phys_par = FirstParagraph();   // a trick/hack for UNDO
        } else {
+#ifndef NEW_INSETS
                first_phys_par = tmprow->par()->FirstPhysicalPar();
+#else
+               first_phys_par = tmprow->par();
+#endif
                // find the first row of the paragraph
                if (first_phys_par != tmprow->par())
                        while (tmprow->previous() &&
@@ -1047,6 +1094,10 @@ void LyXText::RedoParagraphs(BufferView * bview, LyXCursor const & cur,
 
 bool LyXText::FullRebreak(BufferView * bview)
 {
+       if (!firstrow) {
+               init(bview);
+               return true;
+       }
        if (need_break_row) {
                BreakAgain(bview, need_break_row);
                need_break_row = 0;
@@ -1064,7 +1115,7 @@ bool LyXText::FullRebreak(BufferView * bview)
  * They also delete the corresponding row */
    
 // need the selection cursor:
-void LyXText::SetSelection(BufferView * bview)
+void LyXText::SetSelection()
 {
        if (!selection) {
                last_sel_cursor = sel_cursor;
@@ -1111,20 +1162,10 @@ void LyXText::SetSelection(BufferView * bview)
        if (sel_start_cursor.par() == sel_end_cursor.par() && 
            sel_start_cursor.pos() == sel_end_cursor.pos())
                selection = false;
-
-       // Stuff what we got on the clipboard. Even if there is no selection.
-
-       // There is a problem with having the stuffing here in that the
-       // larger the selection the slower LyX will get. This can be
-       // solved by running the line below only when the selection has
-       // finished. The solution used currently just works, to make it
-       // faster we need to be more clever and probably also have more
-       // calls to stuffClipboard. (Lgb)
-       bview->stuffClipboard(selectionAsString(bview->buffer()));
 }
 
 
-string LyXText::selectionAsString(Buffer const * buffer) const
+string const LyXText::selectionAsString(Buffer const * buffer) const
 {
        if (!selection) return string();
        string result;
@@ -1162,6 +1203,8 @@ string LyXText::selectionAsString(Buffer const * buffer) const
 
 void LyXText::ClearSelection() const
 {
+       if (selection)
+               status = LyXText::NEED_MORE_REFRESH;
        selection = false;
        mark_set = false;
 }
@@ -1185,24 +1228,6 @@ void LyXText::CursorEnd(BufferView * bview) const
                else
                        SetCursor(bview,cursor.par(), RowLast(cursor.row()) + 1);
        }
-#ifndef NEW_TABULAR
-        if (cursor.par()->table) {
-                int cell = NumberOfCell(cursor.par(), cursor.pos());
-                if (cursor.par()->table->RowHasContRow(cell) &&
-                    cursor.par()->table->CellHasContRow(cell)<0) {
-                        if (!cursor.row()->next() || cursor.row()->next()->par() != cursor.row()->par())
-                                SetCursor(bview, cursor.par(), RowLast(cursor.row()) + 1);
-                        else {
-                                if (cursor.par()->Last() && 
-                                    (cursor.par()->GetChar(RowLast(cursor.row())) == ' '
-                                     || cursor.par()->IsNewline(RowLast(cursor.row()))))
-                                        SetCursor(bview, cursor.par(), RowLast(cursor.row()));
-                                else
-                                        SetCursor(bview, cursor.par(), RowLast(cursor.row()) + 1);
-                        }
-                }
-        }
-#endif
 }
 
 
@@ -1225,10 +1250,10 @@ void  LyXText::CursorBottom(BufferView * bview) const
 /* returns a pointer to the row near the specified y-coordinate
 * (relative to the whole text). y is set to the real beginning
 * of this row */
-Row * LyXText::GetRowNearY(long & y) const
+Row * LyXText::GetRowNearY(int & y) const
 {
        Row * tmprow = firstrow;
-       long tmpy = 0;
+       int tmpy = 0;
 
        while (tmprow->next() && tmpy + tmprow->height() <= y) {
                tmpy += tmprow->height();
@@ -1240,7 +1265,8 @@ Row * LyXText::GetRowNearY(long & y) const
 }
 
 
-void LyXText::ToggleFree(BufferView * bview, LyXFont const & font, bool toggleall)
+void LyXText::ToggleFree(BufferView * bview,
+                        LyXFont const & font, bool toggleall)
 {
        // If the mask is completely neutral, tell user
        if (font == LyXFont(LyXFont::ALL_IGNORE)) {
@@ -1255,7 +1281,8 @@ void LyXText::ToggleFree(BufferView * bview, LyXFont const & font, bool toggleal
        // If there is a change in the language the implicit word selection 
        // is disabled.
        LyXCursor resetCursor = cursor;
-       bool implicitSelection = (font.language() == ignore_language)
+       bool implicitSelection = (font.language() == ignore_language
+                                 && font.number() == LyXFont::IGNORE)
                ? SelectWordWhenUnderCursor(bview) : false;
 
        // Set font
@@ -1273,7 +1300,8 @@ void LyXText::ToggleFree(BufferView * bview, LyXFont const & font, bool toggleal
 
 
 LyXParagraph::size_type
-LyXText::BeginningOfMainBody(Buffer const * buf, LyXParagraph const * par) const
+LyXText::BeginningOfMainBody(Buffer const * buf,
+                            LyXParagraph const * par) const
 {
        if (textclasslist.Style(buf->params.textclass,
                                par->GetLayout()).labeltype != LABEL_MANUAL)
@@ -1283,6 +1311,7 @@ LyXText::BeginningOfMainBody(Buffer const * buf, LyXParagraph const * par) const
 }
 
 
+#ifndef NEW_INSETS
 /* if there is a selection, reset every environment you can find
 * in the selection, otherwise just the environment you are in */ 
 void LyXText::MeltFootnoteEnvironment(BufferView * bview)
@@ -1336,7 +1365,7 @@ void LyXText::MeltFootnoteEnvironment(BufferView * bview)
                tmppar->next->MakeSameLayout(cursor.par());
 
        // first the end
-       if ((!tmppar->GetLayout() && !tmppar->table)
+       if (!tmppar->GetLayout()
            || (tmppar->Next()
                && (!tmppar->Next()->Last()
                    || tmppar->Next()->HasSameLayout(tmppar)))) {
@@ -1353,7 +1382,7 @@ void LyXText::MeltFootnoteEnvironment(BufferView * bview)
        /* if there is no space between the text and the footnote, so we insert
         * a blank 
         * (only if the previous par and the footnotepar are not empty!) */
-       if ((!firsttmppar->next->GetLayout() && !firsttmppar->next->table)
+       if (!firsttmppar->next->GetLayout()
            || firsttmppar->HasSameLayout(firsttmppar->next)) {
                if (firsttmppar->size()
                    && !firsttmppar->IsSeparator(firsttmppar->size() - 1)
@@ -1377,6 +1406,7 @@ void LyXText::MeltFootnoteEnvironment(BufferView * bview)
    
        ClearSelection();
 }
+#endif
 
 
 /* the DTP switches for paragraphs. LyX will store them in the 
@@ -1401,12 +1431,20 @@ void LyXText::SetParagraph(BufferView * bview,
        }
 
        // make sure that the depth behind the selection are restored, too
+#ifndef NEW_INSETS
        LyXParagraph * endpar = sel_end_cursor.par()->LastPhysicalPar()->Next();
+#else
+       LyXParagraph * endpar = sel_end_cursor.par()->Next();
+#endif
        LyXParagraph * undoendpar = endpar;
 
        if (endpar && endpar->GetDepth()) {
                while (endpar && endpar->GetDepth()) {
+#ifndef NEW_INSETS
                        endpar = endpar->LastPhysicalPar()->Next();
+#else
+                       endpar = endpar->Next();
+#endif
                        undoendpar = endpar;
                }
        }
@@ -1414,20 +1452,31 @@ void LyXText::SetParagraph(BufferView * bview,
                endpar = endpar->Next(); // because of parindents etc.
        }
    
-       SetUndo(bview->buffer(), Undo::EDIT, 
+       SetUndo(bview->buffer(), Undo::EDIT,
+#ifndef NEW_INSETS
                sel_start_cursor
-               .par()->ParFromPos(sel_start_cursor.pos())->previous, 
+               .par()->ParFromPos(sel_start_cursor.pos())->previous,
+#else
+               sel_start_cursor.par()->previous,
+#endif
                undoendpar);
 
        
        LyXParagraph * tmppar = sel_end_cursor.par();
+#ifndef NEW_INSETS
        while (tmppar != sel_start_cursor.par()->FirstPhysicalPar()->Previous()) {
                SetCursor(bview, tmppar->FirstPhysicalPar(), 0);
+#else
+       while (tmppar != sel_start_cursor.par()->Previous()) {
+               SetCursor(bview, tmppar, 0);
+#endif
                status = LyXText::NEED_MORE_REFRESH;
                refresh_row = cursor.row();
                refresh_y = cursor.y() - cursor.row()->baseline();
+#ifndef NEW_INSETS
                if (cursor.par()->footnoteflag ==
                    sel_start_cursor.par()->footnoteflag) {
+#endif
                        cursor.par()->line_top = line_top;
                        cursor.par()->line_bottom = line_bottom;
                        cursor.par()->pagebreak_top = pagebreak_top;
@@ -1451,9 +1500,13 @@ void LyXText::SetParagraph(BufferView * bview,
                        }
                        cursor.par()->SetLabelWidthString(labelwidthstring);
                        cursor.par()->noindent = noindent;
+#ifndef NEW_INSETS
                }
                
                tmppar = cursor.par()->FirstPhysicalPar()->Previous();
+#else
+               tmppar = cursor.par()->Previous();
+#endif
        }
        
        RedoParagraphs(bview, sel_start_cursor, endpar);
@@ -1462,14 +1515,16 @@ void LyXText::SetParagraph(BufferView * bview,
        SetCursor(bview, sel_start_cursor.par(), sel_start_cursor.pos());
        sel_cursor = cursor;
        SetCursor(bview, sel_end_cursor.par(), sel_end_cursor.pos());
-       SetSelection(bview);
+       SetSelection();
        SetCursor(bview, tmpcursor.par(), tmpcursor.pos());
+       if (inset_owner)
+           bview->updateInset(inset_owner, true);
 }
 
 
 void LyXText::SetParagraphExtraOpt(BufferView * bview, int type,
-                                   char const * width,
-                                   char const * widthp,
+                                   string const & width,
+                                   string const & widthp,
                                    int alignment, bool hfill,
                                    bool start_minipage)
 {
@@ -1481,12 +1536,20 @@ void LyXText::SetParagraphExtraOpt(BufferView * bview, int type,
        }
 
        // make sure that the depth behind the selection are restored, too
+#ifndef NEW_INSETS
        LyXParagraph * endpar = sel_end_cursor.par()->LastPhysicalPar()->Next();
+#else
+       LyXParagraph * endpar = sel_end_cursor.par()->Next();
+#endif
        LyXParagraph * undoendpar = endpar;
 
        if (endpar && endpar->GetDepth()) {
                while (endpar && endpar->GetDepth()) {
+#ifndef NEW_INSETS
                        endpar = endpar->LastPhysicalPar()->Next();
+#else
+                       endpar = endpar->Next();
+#endif
                        undoendpar = endpar;
                }
        }
@@ -1494,19 +1557,30 @@ void LyXText::SetParagraphExtraOpt(BufferView * bview, int type,
                endpar = endpar->Next(); // because of parindents etc.
        }
    
-       SetUndo(bview->buffer(), Undo::EDIT, 
+       SetUndo(bview->buffer(), Undo::EDIT,
+#ifndef NEW_INSETS
                sel_start_cursor
-               .par()->ParFromPos(sel_start_cursor.pos())->previous, 
+               .par()->ParFromPos(sel_start_cursor.pos())->previous,
+#else
+               sel_start_cursor.par()->previous,
+#endif
                undoendpar);
        
        tmppar = sel_end_cursor.par();
+#ifndef NEW_INSETS
        while(tmppar != sel_start_cursor.par()->FirstPhysicalPar()->Previous()) {
                 SetCursor(bview, tmppar->FirstPhysicalPar(), 0);
+#else
+       while(tmppar != sel_start_cursor.par()->Previous()) {
+                SetCursor(bview, tmppar, 0);
+#endif
                 status = LyXText::NEED_MORE_REFRESH;
                 refresh_row = cursor.row();
                 refresh_y = cursor.y() - cursor.row()->baseline();
+#ifndef NEW_INSETS
                 if (cursor.par()->footnoteflag ==
                     sel_start_cursor.par()->footnoteflag) {
+#endif
                         if (type == LyXParagraph::PEXTRA_NONE) {
                                 if (cursor.par()->pextra_type != LyXParagraph::PEXTRA_NONE) {
                                         cursor.par()->UnsetPExtraType(bview->buffer()->params);
@@ -1519,15 +1593,19 @@ void LyXText::SetParagraphExtraOpt(BufferView * bview, int type,
                                 cursor.par()->pextra_start_minipage = start_minipage;
                                 cursor.par()->pextra_alignment = alignment;
                         }
-                }
+#ifndef NEW_INSETS
+               }
                 tmppar = cursor.par()->FirstPhysicalPar()->Previous();
+#else
+                tmppar = cursor.par()->Previous();
+#endif
         }
        RedoParagraphs(bview, sel_start_cursor, endpar);
        ClearSelection();
        SetCursor(bview, sel_start_cursor.par(), sel_start_cursor.pos());
        sel_cursor = cursor;
        SetCursor(bview, sel_end_cursor.par(), sel_end_cursor.pos());
-       SetSelection(bview);
+       SetSelection();
        SetCursor(bview, tmpcursor.par(), tmpcursor.pos());
 }
 
@@ -1540,6 +1618,8 @@ char loweralphaCounter(int n)
                return 'a' + n - 1;
 }
 
+
+static inline
 char alphaCounter(int n)
 {
        if (n < 1 || n > 26)
@@ -1548,6 +1628,8 @@ char alphaCounter(int n)
                return 'A' + n - 1;
 }
 
+
+static inline
 char hebrewCounter(int n)
 {
        static const char hebrew[22] = {
@@ -1561,7 +1643,9 @@ char hebrewCounter(int n)
                return hebrew[n-1];
 }
 
-static char const * romanCounter(int n)
+
+static inline
+string const romanCounter(int n)
 {
        static char const * roman[20] = {
                "i",   "ii",  "iii", "iv", "v",
@@ -1575,12 +1659,14 @@ static char const * romanCounter(int n)
                return roman[n-1];
 }
 
+
 // set the counter of a paragraph. This includes the labels
 void LyXText::SetCounter(Buffer const * buf, LyXParagraph * par) const
 {
+#ifndef NEW_INSETS
        // this is only relevant for the beginning of paragraph
        par = par->FirstPhysicalPar();
-
+#endif
        LyXLayout const & layout =
                textclasslist.Style(buf->params.textclass, 
                                    par->GetLayout());
@@ -1591,9 +1677,11 @@ void LyXText::SetCounter(Buffer const * buf, LyXParagraph * par) const
        /* copy the prev-counters to this one, unless this is the start of a 
           footnote or of a bibliography or the very first paragraph */
        if (par->Previous()
+#ifndef NEW_INSETS
            && !(par->Previous()->footnoteflag == LyXParagraph::NO_FOOTNOTE 
                    && par->footnoteflag == LyXParagraph::OPEN_FOOTNOTE
                    && par->footnotekind == LyXParagraph::FOOTNOTE)
+#endif
            && !(textclasslist.Style(buf->params.textclass,
                                par->Previous()->GetLayout()
                                ).labeltype != LABEL_BIBLIO
@@ -1601,17 +1689,25 @@ void LyXText::SetCounter(Buffer const * buf, LyXParagraph * par) const
                for (int i = 0; i < 10; ++i) {
                        par->setCounter(i, par->Previous()->GetFirstCounter(i));
                }
+#ifndef NEW_INSETS
                par->appendix = par->Previous()->FirstPhysicalPar()->appendix;
+#else
+               par->appendix = par->Previous()->appendix;
+#endif
                if (!par->appendix && par->start_of_appendix){
                  par->appendix = true;
                  for (int i = 0; i < 10; ++i) {
                    par->setCounter(i, 0);
                  }  
                }
+#ifndef NEW_INSETS
                par->enumdepth = par->Previous()->FirstPhysicalPar()->enumdepth;
                par->itemdepth = par->Previous()->FirstPhysicalPar()->itemdepth;
-       }
-       else {
+#else
+               par->enumdepth = par->Previous()->enumdepth;
+               par->itemdepth = par->Previous()->itemdepth;
+#endif
+       } else {
                for (int i = 0; i < 10; ++i) {
                        par->setCounter(i, 0);
                }  
@@ -1620,11 +1716,12 @@ void LyXText::SetCounter(Buffer const * buf, LyXParagraph * par) const
                par->itemdepth = 0;
        }
 
+#ifndef NEW_INSETS
         // if this is an open marginnote and this is the first
         // entry in the marginnote and the enclosing
         // environment is an enum/item then correct for the
         // LaTeX behaviour (ARRae)
-        if(par->footnoteflag == LyXParagraph::OPEN_FOOTNOTE
+        if (par->footnoteflag == LyXParagraph::OPEN_FOOTNOTE
           && par->footnotekind == LyXParagraph::MARGIN
            && par->Previous()
            && par->Previous()->footnoteflag != LyXParagraph::OPEN_FOOTNOTE
@@ -1641,7 +1738,7 @@ void LyXText::SetCounter(Buffer const * buf, LyXParagraph * par) const
                 par->enumdepth++;
                 par->itemdepth++;
         }
-
+#endif
        /* Maybe we have to increment the enumeration depth.
         * BUT, enumeration in a footnote is considered in isolation from its
         *      surrounding paragraph so don't increment if this is the
@@ -1655,9 +1752,11 @@ void LyXText::SetCounter(Buffer const * buf, LyXParagraph * par) const
                              par->Previous()->GetLayout()
                             ).labeltype == LABEL_COUNTER_ENUMI
            && par->enumdepth < 3
+#ifndef NEW_INSETS
            && !(par->Previous()->footnoteflag == LyXParagraph::NO_FOOTNOTE 
                    && par->footnoteflag == LyXParagraph::OPEN_FOOTNOTE
                    && par->footnotekind == LyXParagraph::FOOTNOTE)
+#endif
            && layout.labeltype != LABEL_BIBLIO) {
                par->enumdepth++;
        }
@@ -1665,9 +1764,11 @@ void LyXText::SetCounter(Buffer const * buf, LyXParagraph * par) const
        /* Maybe we have to decrement the enumeration depth, see note above */
        if (par->Previous()
            && par->Previous()->GetDepth() > par->GetDepth()
+#ifndef NEW_INSETS
            && !(par->Previous()->footnoteflag == LyXParagraph::NO_FOOTNOTE
                    && par->footnoteflag == LyXParagraph::OPEN_FOOTNOTE
                    && par->footnotekind == LyXParagraph::FOOTNOTE)
+#endif
            && layout.labeltype != LABEL_BIBLIO) {
                par->enumdepth = par->DepthHook(par->GetDepth())->enumdepth;
                par->setCounter(6 + par->enumdepth,
@@ -1692,9 +1793,9 @@ void LyXText::SetCounter(Buffer const * buf, LyXParagraph * par) const
        }
    
        /* is it a layout that has an automatic label ? */ 
-       if (layout.labeltype >=  LABEL_FIRST_COUNTER) {
+       if (layout.labeltype >=  LABEL_COUNTER_CHAPTER) {
       
-               int i = layout.labeltype - LABEL_FIRST_COUNTER;
+               int i = layout.labeltype - LABEL_COUNTER_CHAPTER;
                if (i >= 0 && i<= buf->params.secnumdepth) {
                        par->incCounter(i);     // increment the counter  
         
@@ -1711,13 +1812,10 @@ void LyXText::SetCounter(Buffer const * buf, LyXParagraph * par) const
                                        par->labelstring.erase();
                        }
 
-#ifdef HAVE_SSTREAM
                        std::ostringstream s;
-#else
-                       ostrstream s;
-#endif
+
                        if (!par->appendix) {
-                               switch (2 * LABEL_FIRST_COUNTER -
+                               switch (2 * LABEL_COUNTER_CHAPTER -
                                        textclass.maxcounter() + i) {
                                case LABEL_COUNTER_CHAPTER:
                                        s << par->getCounter(i);
@@ -1755,11 +1853,14 @@ void LyXText::SetCounter(Buffer const * buf, LyXParagraph * par) const
 
                                        break;
                                default:
+                                       // Can this ever be reached? And in the
+                                       // case it is, how can this be correct?
+                                       // (Lgb)
                                        s << par->getCounter(i) << '.';
                                         break;
                                }
                        } else { // appendix
-                               switch (2 * LABEL_FIRST_COUNTER - textclass.maxcounter() + i) {
+                               switch (2 * LABEL_COUNTER_CHAPTER - textclass.maxcounter() + i) {
                                case LABEL_COUNTER_CHAPTER:
                                        if (par->isRightToLeftPar(buf->params))
                                                s << hebrewCounter(par->getCounter(i));
@@ -1830,21 +1931,15 @@ void LyXText::SetCounter(Buffer const * buf, LyXParagraph * par) const
                                        // Can this ever be reached? And in the
                                        // case it is, how can this be correct?
                                        // (Lgb)
-                                       s << static_cast<unsigned char>(par->getCounter(i)) << '.';
+                                       s << par->getCounter(i) << '.';
                                        
                                        break;
                                }
                        }
-#ifdef HAVE_SSTREAM
+
                        par->labelstring += s.str().c_str();
                        // We really want to remove the c_str as soon as
                        // possible...
-#else
-                       s << '\0';
-                       char * tmps = s.str();
-                       par->labelstring += tmps;
-                       delete [] tmps;
-#endif
                        
                        for (i++; i < 10; ++i) {
                                // reset the following counters
@@ -1859,11 +1954,8 @@ void LyXText::SetCounter(Buffer const * buf, LyXParagraph * par) const
                        par->incCounter(i + par->enumdepth);
                        int number = par->getCounter(i + par->enumdepth);
 
-#ifdef HAVE_SSTREAM
                        std::ostringstream s;
-#else
-                       ostrstream s;
-#endif
+
                        switch (par->enumdepth) {
                        case 1:
                                if (par->isRightToLeftPar(buf->params))
@@ -1896,26 +1988,22 @@ void LyXText::SetCounter(Buffer const * buf, LyXParagraph * par) const
                                        s << number << '.';
                                break;
                        }
-#ifdef HAVE_SSTREAM
+
                        par->labelstring = s.str().c_str();
                        // we really want to get rid of that c_str()
-#else
-                       s << '\0';
-                       char * tmps = s.str();
-                       par->labelstring = tmps;
-                       delete [] tmps;
-#endif
 
                        for (i += par->enumdepth + 1; i < 10; ++i)
                                par->setCounter(i, 0);  /* reset the following counters  */
         
                } 
        } else if (layout.labeltype == LABEL_BIBLIO) {// ale970302
-               int i = LABEL_COUNTER_ENUMI - LABEL_FIRST_COUNTER + par->enumdepth;
+               int i = LABEL_COUNTER_ENUMI - LABEL_COUNTER_CHAPTER + par->enumdepth;
                par->incCounter(i);
                int number = par->getCounter(i);
-               if (!par->bibkey)
-                       par->bibkey = new InsetBibKey();
+               if (!par->bibkey) {
+                       InsetCommandParams p( "bibitem" );
+                       par->bibkey = new InsetBibKey(p);
+               }
                par->bibkey->setCounter(number);
                par->labelstring = layout.labelstring();
                
@@ -1924,23 +2012,33 @@ void LyXText::SetCounter(Buffer const * buf, LyXParagraph * par) const
                string s = layout.labelstring();
                
                // the caption hack:
-      
                if (layout.labeltype == LABEL_SENSITIVE) {
+                       bool isOK (par->InInset() && par->InInset()->owner() &&
+                                  (par->InInset()->owner()->LyxCode() == Inset::FLOAT_CODE));
+#ifndef NEW_INSETS
                        if (par->footnoteflag != LyXParagraph::NO_FOOTNOTE
                            && (par->footnotekind == LyXParagraph::FIG
-                               || par->footnotekind == LyXParagraph::WIDE_FIG))
+                               || par->footnotekind == LyXParagraph::WIDE_FIG)) {
                                s = (par->getParLanguage(buf->params)->lang() == "hebrew")
                                        ? ":øåéà" : "Figure:";
-                       else if (par->footnoteflag != LyXParagraph::NO_FOOTNOTE
+                       else if (par->footnoteflag != LyXParagraph::NO_FOOTNOTE
                                 && (par->footnotekind == LyXParagraph::TAB
-                                    || par->footnotekind == LyXParagraph::WIDE_TAB))
+                                    || par->footnotekind == LyXParagraph::WIDE_TAB)) {
                                s = (par->getParLanguage(buf->params)->lang() == "hebrew")
                                        ? ":äìáè" : "Table:";
-                       else if (par->footnoteflag != LyXParagraph::NO_FOOTNOTE
-                                && par->footnotekind == LyXParagraph::ALGORITHM)
+                       else if (par->footnoteflag != LyXParagraph::NO_FOOTNOTE
+                                  && par->footnotekind == LyXParagraph::ALGORITHM) {
                                s = (par->getParLanguage(buf->params)->lang() == "hebrew")
                                        ? ":íúéøåâìà" : "Algorithm:";
-                       else {
+                       } else
+#endif
+                       if (isOK) {
+                               InsetFloat * tmp = static_cast<InsetFloat*>(par->InInset()->owner());
+                               Floating const & fl
+                                       = floatList.getType(tmp->type());
+                               // We should get the correct number here too.
+                               s = fl.name() + " #:";
+                       } else {
                                /* par->SetLayout(0); 
                                   s = layout->labelstring;  */
                                s = (par->getParLanguage(buf->params)->lang() == "hebrew")
@@ -1965,11 +2063,17 @@ void LyXText::UpdateCounters(BufferView * bview, Row * row) const
        if (!row) {
                row = firstrow;
                par = row->par();
-       }
-       else {
+       } else {
                if (row->par()->next
-                   && row->par()->next->footnoteflag != LyXParagraph::OPEN_FOOTNOTE) {
+#ifndef NEW_INSETS
+                   && row->par()->next->footnoteflag != LyXParagraph::OPEN_FOOTNOTE
+#endif
+                       ) {
+#ifndef NEW_INSETS
                        par = row->par()->LastPhysicalPar()->Next();
+#else
+                       par = row->par()->Next();
+#endif
                } else {
                        par = row->par()->next;
                }
@@ -1983,8 +2087,11 @@ void LyXText::UpdateCounters(BufferView * bview, Row * row) const
                
                /* now  check for the headline layouts. remember that they
                 * have a dynamic left margin */ 
-               if (!par->IsDummy()
-                   && ( textclasslist.Style(bview->buffer()->params.textclass,
+               if (
+#ifndef NEW_INSETS
+                       !par->IsDummy() &&
+#endif
+                   ( textclasslist.Style(bview->buffer()->params.textclass,
                                             par->layout).margintype == MARGIN_DYNAMIC
                         || textclasslist.Style(bview->buffer()->params.textclass,
                                                par->layout).labeltype == LABEL_SENSITIVE)
@@ -1993,7 +2100,8 @@ void LyXText::UpdateCounters(BufferView * bview, Row * row) const
                        /* Rebreak the paragraph */ 
                        RemoveParagraph(row);
                        AppendParagraph(bview, row);
-       
+
+#ifndef NEW_INSETS
                        /* think about the damned open footnotes! */ 
                        while (par->Next() &&
                               (par->Next()->footnoteflag == LyXParagraph::OPEN_FOOTNOTE
@@ -2006,51 +2114,53 @@ void LyXText::UpdateCounters(BufferView * bview, Row * row) const
                                        AppendParagraph(bview, row);
                                }
                        }
+#endif
                }
-     
+#ifndef NEW_INSETS
                par = par->LastPhysicalPar()->Next();
+#else
+               par = par->Next();
+#endif
      
        }
 }
 
 
 /* insets an inset. */ 
-void LyXText::InsertInset(BufferView * bview, Inset *inset)
+void LyXText::InsertInset(BufferView * bview, Inset * inset)
 {
        if (!cursor.par()->InsertInsetAllowed(inset))
                return;
-       SetUndo(bview->buffer(), Undo::INSERT, 
+       SetUndo(bview->buffer(), Undo::INSERT,
+#ifndef NEW_INSETS
                cursor.par()->ParFromPos(cursor.pos())->previous, 
-               cursor.par()->ParFromPos(cursor.pos())->next);
-       cursor.par()->InsertChar(cursor.pos(), LyXParagraph::META_INSET);
+               cursor.par()->ParFromPos(cursor.pos())->next
+#else
+               cursor.par()->previous, 
+               cursor.par()->next
+#endif
+               );
        cursor.par()->InsertInset(cursor.pos(), inset);
        InsertChar(bview, LyXParagraph::META_INSET);  /* just to rebreak and refresh correctly.
                                      * The character will not be inserted a
                                      * second time */
+#if 1
+       // if we enter a text-inset the cursor should be to the left side
+       // of it! This couldn't happen before as Undo was not handled inside
+       // inset now after the Undo LyX tries to call inset->Edit(...) again
+       // and cannot do this as the cursor is behind the inset and GetInset
+       // does not return the inset!
+       if (inset->IsTextInset()) {
+               if (cursor.par()->isRightToLeftPar(bview->buffer()->params))
+                       CursorRight(bview);
+               else
+                       CursorLeft(bview);
+       }
+#endif
 }
 
 
-#ifdef USE_OLD_CUT_AND_PASTE
-// this is for the simple cut and paste mechanism
-static LyXParagraph * simple_cut_buffer = 0;
-static char simple_cut_buffer_textclass = 0;
-
-void DeleteSimpleCutBuffer()
-{
-       if (!simple_cut_buffer)
-               return;
-       LyXParagraph * tmppar;
-
-       while (simple_cut_buffer) {
-               tmppar =  simple_cut_buffer;
-               simple_cut_buffer = simple_cut_buffer()->next;
-               delete tmppar;
-       }
-       simple_cut_buffer = 0;
-}
-#endif
-
-void LyXText::copyEnvironmentType()
+void LyXText::copyEnvironmentType()
 {
        copylayouttype = cursor.par()->GetLayout();
 }
@@ -2061,21 +2171,30 @@ void LyXText::pasteEnvironmentType(BufferView * bview)
        SetLayout(bview, copylayouttype);
 }
 
-#ifdef USE_OLD_CUT_AND_PASTE
-void LyXText::CutSelection(Buffer const * buf, bool doclear)
+
+void LyXText::CutSelection(BufferView * bview, bool doclear)
 {
+       // Stuff what we got on the clipboard. Even if there is no selection.
+
+       // There is a problem with having the stuffing here in that the
+       // larger the selection the slower LyX will get. This can be
+       // solved by running the line below only when the selection has
+       // finished. The solution used currently just works, to make it
+       // faster we need to be more clever and probably also have more
+       // calls to stuffClipboard. (Lgb)
+       bview->stuffClipboard(selectionAsString(bview->buffer()));
+
        // This doesn't make sense, if there is no selection
        if (!selection)
                return;
    
        // OK, we have a selection. This is always between sel_start_cursor
-       // and sel_end cursor
-       LyXParagraph * tmppar;
-   
+       // and sel_end_cursor
+#ifndef NEW_INSETS
        // Check whether there are half footnotes in the selection
        if (sel_start_cursor.par()->footnoteflag != LyXParagraph::NO_FOOTNOTE
            || sel_end_cursor.par()->footnoteflag != LyXParagraph::NO_FOOTNOTE) {
-               tmppar = sel_start_cursor.par();
+       LyXParagraph * tmppar = sel_start_cursor.par();
                while (tmppar != sel_end_cursor.par()){
                        if (tmppar->footnoteflag != sel_end_cursor.par()->footnoteflag) {
                                WriteAlert(_("Impossible operation"),
@@ -2086,401 +2205,108 @@ void LyXText::CutSelection(Buffer const * buf, bool doclear)
                        tmppar = tmppar->Next();
                }
        }
-
-#ifndef NEW_TABULAR
-       /* table stuff -- begin */
-       if (sel_start_cursor.par()->table || sel_end_cursor.par()->table) {
-               if ( sel_start_cursor.par() != sel_end_cursor.par()) {
-                       WriteAlert(_("Impossible operation"),
-                                  _("Don't know what to do with half tables."),
-                                  _("sorry."));
-                       return;
-               }
-               sel_start_cursor.par()->table->Reinit();
-       }
-       /* table stuff -- end */
 #endif
-       
+
        // make sure that the depth behind the selection are restored, too
+#ifndef NEW_INSETS
        LyXParagraph * endpar = sel_end_cursor.par()->LastPhysicalPar()->Next();
+#else
+       LyXParagraph * endpar = sel_end_cursor.par()->Next();
+#endif
        LyXParagraph * undoendpar = endpar;
-
+    
        if (endpar && endpar->GetDepth()) {
                while (endpar && endpar->GetDepth()) {
+#ifndef NEW_INSETS
                        endpar = endpar->LastPhysicalPar()->Next();
+#else
+                       endpar = endpar->Next();
+#endif
                        undoendpar = endpar;
                }
        } else if (endpar) {
                endpar = endpar->Next(); // because of parindents etc.
        }
-   
-       SetUndo(bview->buffer(), Undo::DELETE, 
+    
+       SetUndo(bview->buffer(), Undo::DELETE,
+#ifndef NEW_INSETS
                sel_start_cursor
-               .par->ParFromPos(sel_start_cursor.pos())->previous, 
-               undoendpar);
-   
-       // clear the simple_cut_buffer
-       DeleteSimpleCutBuffer();
-   
-       // set the textclass
-       simple_cut_buffer_textclass = buf->params.textclass;
-
-#ifdef WITH_WARNINGS
-#warning Asger: Make cut more intelligent here.
+               .par()->ParFromPos(sel_start_cursor.pos())->previous,
+#else
+               sel_start_cursor.par()->previous,
 #endif
-       /* 
-          White paper for "intelligent" cutting:
-          
-          Example: "This is our text."
-          Using " our " as selection, cutting will give "This istext.".
-          Using "our" as selection, cutting will give "This is text.".
-          Using " our" as selection, cutting will give "This is text.".
-          Using "our " as selection, cutting will give "This is text.".
-          
-          All those four selections will (however) paste identically:
-          Pasting with the cursor right after the "is" will give the
-          original text with all four selections.
-          
-          The rationale is to be intelligent such that words are copied,
-          cut and pasted in a functional manner.
-          
-          This is not implemented yet. (Asger)
-
-          The changes below sees to do a lot of what you want. However
-          I have not verified that all cases work as they should:
-                    - cut in single row
-                    - cut in multiple row
-                    - cut with insets
-                    - cut across footnotes and paragraph
-          My simplistic tests show that the idea are basically sound but
-          there are some items to fix up...we only need to find them
-          first.
-
-          As do redo Asger's example above (with | beeing the cursor in the
-          result after cutting.):
-          
-          Example: "This is our text."
-          Using " our " as selection, cutting will give "This is|text.".
-          Using "our"   as selection, cutting will give "This is | text.".
-          Using " our"  as selection, cutting will give "This is| text.".
-          Using "our "  as selection, cutting will give "This is |text.".
-
-          (Lgb)
-       */
+               undoendpar);
+    
+       CutAndPaste cap;
 
        // there are two cases: cut only within one paragraph or
        // more than one paragraph
-   
+#ifndef NEW_INSETS
        if (sel_start_cursor.par()->ParFromPos(sel_start_cursor.pos()) 
-           == sel_end_cursor.par()->ParFromPos(sel_end_cursor.pos())) {
-               // only within one paragraph
-               simple_cut_buffer = new LyXParagraph;
-               LyXParagraph::size_type i =
-                       sel_start_cursor.pos();
-               for (; i < sel_end_cursor.pos(); ++i) {
-#ifndef NEW_TABULAR
-                       /* table stuff -- begin */
-                       if (sel_start_cursor.par()->table
-                           && sel_start_cursor.par()->IsNewline(sel_start_cursor.pos())) {
-                               sel_start_cursor.par()->CopyIntoMinibuffer(sel_start_cursor.pos());
-                               sel_start_cursor.pos()++;
-                       } else {
-                               /* table stuff -- end */
-#endif
-                               sel_start_cursor.par()->CopyIntoMinibuffer(sel_start_cursor.pos());
-                               sel_start_cursor.par()->Erase(sel_start_cursor.pos());
-#ifndef NEW_TABULAR
-                       }
+           == sel_end_cursor.par()->ParFromPos(sel_end_cursor.pos()))
+#else
+       if (sel_start_cursor.par() == sel_end_cursor.par())
 #endif
-                       simple_cut_buffer->InsertFromMinibuffer(simple_cut_buffer->Last());
-               }
-               endpar = sel_end_cursor.par()->Next();
+               {
+               // only within one paragraph
+               endpar = sel_start_cursor.par();
+               int pos = sel_end_cursor.pos();
+               cap.cutSelection(sel_start_cursor.par(), &endpar,
+                                sel_start_cursor.pos(), pos,
+                                bview->buffer()->params.textclass, doclear);
+               sel_end_cursor.pos(pos);
        } else {
-               // cut more than one paragraph
-   
-               sel_end_cursor.par()
-                       ->BreakParagraphConservative(sel_end_cursor.pos());
-               sel_end_cursor.par() = sel_end_cursor.par()->Next();
-               sel_end_cursor.pos() = 0;
-   
-               cursor = sel_end_cursor;
-
-               sel_start_cursor.par()
-                       ->BreakParagraphConservative(sel_start_cursor.pos());
-               // store the endparagraph for redoing later
-               endpar = sel_end_cursor.par()->Next(); /* needed because
-                                                       the sel_end_
-                                                       cursor.par()
-                                                       will be pasted! */
-   
-               // store the selection
-               simple_cut_buffer = sel_start_cursor.par()
-                       ->ParFromPos(sel_start_cursor.pos())->next;
-               simple_cut_buffer->previous = 0;
-               sel_end_cursor.par()->previous->next = 0;
-
-               // cut the selection
-               sel_start_cursor.par()->ParFromPos(sel_start_cursor.pos())->next 
-                       = sel_end_cursor.par();
-   
-               sel_end_cursor.par()->previous 
-                       = sel_start_cursor.par()->ParFromPos(sel_start_cursor.pos());
-
-               // care about footnotes
-               if (simple_cut_buffer->footnoteflag) {
-                       LyXParagraph * tmppar = simple_cut_buffer;
-                       while (tmppar){
-                               tmppar->footnoteflag = LyXParagraph::NO_FOOTNOTE;
-                               tmppar = tmppar->next;
-                       }
-               }
+               endpar = sel_end_cursor.par();
 
-               // the cut selection should begin with standard layout
-               simple_cut_buffer->Clear(); 
-   
-               // paste the paragraphs again, if possible
-               if (doclear)
-                       sel_start_cursor.par()->Next()->StripLeadingSpaces(simple_cut_buffer_textclass);
-               if (sel_start_cursor.par()->FirstPhysicalPar()->HasSameLayout(sel_start_cursor.par()->Next())
-                   || 
-                   !sel_start_cursor.par()->Next()->Last())
-                       sel_start_cursor.par()->ParFromPos(sel_start_cursor.pos())->PasteParagraph();
+               int pos = sel_end_cursor.pos();
+               cap.cutSelection(sel_start_cursor.par(), &endpar,
+                                sel_start_cursor.pos(), pos,
+                                bview->buffer()->params.textclass, doclear);
+               cursor.par(endpar);
+               sel_end_cursor.par(endpar);
+               sel_end_cursor.pos(pos);
+               cursor.pos(sel_end_cursor.pos());
        }
+       endpar = endpar->Next();
 
        // sometimes necessary
        if (doclear)
-               sel_start_cursor.par()->StripLeadingSpaces(simple_cut_buffer_textclass);
+               sel_start_cursor.par()->StripLeadingSpaces(bview->buffer()->params.textclass);
 
-       RedoParagraphs(sel_start_cursor, endpar);
+       RedoParagraphs(bview, sel_start_cursor, endpar);
    
        ClearSelection();
        cursor = sel_start_cursor;
-       SetCursor(cursor.par(), cursor.pos());
+       SetCursor(bview, cursor.par(), cursor.pos());
        sel_cursor = cursor;
-       UpdateCounters(cursor.row());
+       UpdateCounters(bview, cursor.row());
 }
 
-#else ///////////////////////////////////////////////////////////////////
 
-void LyXText::CutSelection(BufferView * bview, bool doclear)
+void LyXText::CopySelection(BufferView * bview)
 {
-    // This doesn't make sense, if there is no selection
-    if (!selection)
-       return;
-   
-    // OK, we have a selection. This is always between sel_start_cursor
-    // and sel_end cursor
-    LyXParagraph * tmppar;
-    
-    // Check whether there are half footnotes in the selection
-    if (sel_start_cursor.par()->footnoteflag != LyXParagraph::NO_FOOTNOTE
-       || sel_end_cursor.par()->footnoteflag != LyXParagraph::NO_FOOTNOTE) {
-       tmppar = sel_start_cursor.par();
-       while (tmppar != sel_end_cursor.par()){
-           if (tmppar->footnoteflag != sel_end_cursor.par()->footnoteflag) {
-               WriteAlert(_("Impossible operation"),
-                          _("Don't know what to do with half floats."),
-                          _("sorry."));
-               return;
-                       }
-           tmppar = tmppar->Next();
-       }
-    }
-
-#ifndef NEW_TABULAR
-    /* table stuff -- begin */
-    if (sel_start_cursor.par()->table || sel_end_cursor.par()->table) {
-       if ( sel_start_cursor.par() != sel_end_cursor.par()) {
-           WriteAlert(_("Impossible operation"),
-                      _("Don't know what to do with half tables."),
-                      _("sorry."));
-           return;
-       }
-       sel_start_cursor.par()->table->Reinit();
-    }
-    /* table stuff -- end */
-#endif
-    
-    // make sure that the depth behind the selection are restored, too
-    LyXParagraph * endpar = sel_end_cursor.par()->LastPhysicalPar()->Next();
-    LyXParagraph * undoendpar = endpar;
-    
-    if (endpar && endpar->GetDepth()) {
-       while (endpar && endpar->GetDepth()) {
-           endpar = endpar->LastPhysicalPar()->Next();
-           undoendpar = endpar;
-       }
-    } else if (endpar) {
-       endpar = endpar->Next(); // because of parindents etc.
-    }
-    
-    SetUndo(bview->buffer(), Undo::DELETE, sel_start_cursor
-           .par()->ParFromPos(sel_start_cursor.pos())->previous, undoendpar);
-    
-    CutAndPaste cap;
-
-    // there are two cases: cut only within one paragraph or
-    // more than one paragraph
-    if (sel_start_cursor.par()->ParFromPos(sel_start_cursor.pos()) 
-       == sel_end_cursor.par()->ParFromPos(sel_end_cursor.pos())) {
-       // only within one paragraph
-       endpar = sel_start_cursor.par();
-       int pos = sel_end_cursor.pos();
-       cap.cutSelection(sel_start_cursor.par(), &endpar,
-                        sel_start_cursor.pos(), pos,
-                        bview->buffer()->params.textclass, doclear);
-       sel_end_cursor.pos(pos);
-    } else {
-       endpar = sel_end_cursor.par();
-
-       int pos = sel_end_cursor.pos();
-       cap.cutSelection(sel_start_cursor.par(), &endpar,
-                        sel_start_cursor.pos(), pos,
-                        bview->buffer()->params.textclass, doclear);
-       cursor.par(endpar);
-       sel_end_cursor.par(endpar);
-       sel_end_cursor.pos(pos);
-       cursor.pos(sel_end_cursor.pos());
-    }
-    endpar = endpar->Next();
-
-    // sometimes necessary
-    if (doclear)
-       sel_start_cursor.par()->StripLeadingSpaces(bview->buffer()->params.textclass);
-
-    RedoParagraphs(bview, sel_start_cursor, endpar);
-   
-    ClearSelection();
-    cursor = sel_start_cursor;
-    SetCursor(bview, cursor.par(), cursor.pos());
-    sel_cursor = cursor;
-    UpdateCounters(bview, cursor.row());
-}
-#endif
-    
-#ifdef USE_OLD_CUT_AND_PASTE
-void LyXText::CopySelection(Buffer const * buf)
-{
-       // this doesnt make sense, if there is no selection
-       if (!selection)
-               return;
-
-       // ok we have a selection. This is always between sel_start_cursor
-       // and sel_end cursor
-       LyXParagraph * tmppar;
-   
-       /* check wether there are half footnotes in the selection */
-       if (sel_start_cursor.par()->footnoteflag != LyXParagraph::NO_FOOTNOTE
-           || sel_end_cursor.par()->footnoteflag != LyXParagraph::NO_FOOTNOTE) {
-               tmppar = sel_start_cursor.par();
-               while (tmppar != sel_end_cursor.par()) {
-                       if (tmppar->footnoteflag !=
-                           sel_end_cursor.par()->footnoteflag) {
-                               WriteAlert(_("Impossible operation"),
-                                          _("Don't know what to do"
-                                            " with half floats."),
-                                          _("sorry."));
-                               return;
-                       }
-                       tmppar = tmppar->Next();
-               }
-       }
-
-#ifndef NEW_TABULAR
-       /* table stuff -- begin */
-       if (sel_start_cursor.par()->table || sel_end_cursor.par()->table){
-               if ( sel_start_cursor.par() != sel_end_cursor.par()){
-                       WriteAlert(_("Impossible operation"),
-                                  _("Don't know what to do with half tables."),
-                                  _("sorry."));
-                       return;
-               }
-       }
-       /* table stuff -- end */
-#endif
-       
-       // delete the simple_cut_buffer
-       DeleteSimpleCutBuffer();
-
-       // set the textclass
-       simple_cut_buffer_textclass = buf->params.textclass;
-
-       // copy behind a space if there is one
-       while (sel_start_cursor.par()->Last() > sel_start_cursor.pos()
-              && sel_start_cursor.par()->IsLineSeparator(sel_start_cursor.pos())
-              && (sel_start_cursor.par() != sel_end_cursor.par()
-                  || sel_start_cursor.pos() < sel_end_cursor.pos()))
-               sel_start_cursor.pos()++; 
-
-       // there are two cases: copy only within one paragraph
-       // or more than one paragraph
-       if (sel_start_cursor.par()->ParFromPos(sel_start_cursor.pos()) 
-           == sel_end_cursor.par()->ParFromPos(sel_end_cursor.pos())) {
-               // only within one paragraph
-               simple_cut_buffer = new LyXParagraph;
-               LyXParagraph::size_type i = 0;
-               for (i = sel_start_cursor.pos(); i < sel_end_cursor.pos(); ++i){
-                       sel_start_cursor.par()->CopyIntoMinibuffer(i);
-                       simple_cut_buffer->InsertFromMinibuffer(i - sel_start_cursor.pos());
-               }
-       } else {
-               // copy more than one paragraph
-               // clone the paragraphs within the selection
-               tmppar =
-                       sel_start_cursor.par()->ParFromPos(sel_start_cursor.pos());
-               simple_cut_buffer = tmppar->Clone();
-               LyXParagraph *tmppar2 = simple_cut_buffer;
-     
-               while (tmppar != sel_end_cursor.par()->ParFromPos(sel_end_cursor.pos())
-                      && tmppar->next) {
-                       tmppar = tmppar->next;
-                       tmppar2->next = tmppar->Clone();
-                       tmppar2->next->previous = tmppar2;
-                       tmppar2 = tmppar2->next;
-               }
-               tmppar2->next = 0;
-
-               // care about footnotes
-               if (simple_cut_buffer->footnoteflag) {
-                       tmppar = simple_cut_buffer;
-                       while (tmppar){
-                               tmppar->footnoteflag =
-                                       LyXParagraph::NO_FOOTNOTE;
-                               tmppar = tmppar->next;
-                       }
-               }
-               
-               // the simple_cut_buffer paragraph is too big
-               LyXParagraph::size_type tmpi2 =
-                       sel_start_cursor.par()->PositionInParFromPos(sel_start_cursor.pos());
-               for (; tmpi2; --tmpi2)
-                       simple_cut_buffer->Erase(0);
-               
-               // now tmppar 2 is too big, delete all after sel_end_cursor.pos()
-     
-               tmpi2 = sel_end_cursor.par()->PositionInParFromPos(sel_end_cursor.pos());
-               while (tmppar2->size() > tmpi2) {
-                       tmppar2->Erase(tmppar2->size() - 1);
-               }
-       }
-}
+       // Stuff what we got on the clipboard. Even if there is no selection.
 
-#else //////////////////////////////////////////////////////////////////////
+       // There is a problem with having the stuffing here in that the
+       // larger the selection the slower LyX will get. This can be
+       // solved by running the line below only when the selection has
+       // finished. The solution used currently just works, to make it
+       // faster we need to be more clever and probably also have more
+       // calls to stuffClipboard. (Lgb)
+       bview->stuffClipboard(selectionAsString(bview->buffer()));
 
-void LyXText::CopySelection(Buffer const * buf)
-{
        // this doesnt make sense, if there is no selection
        if (!selection)
                return;
 
        // ok we have a selection. This is always between sel_start_cursor
        // and sel_end cursor
-       LyXParagraph * tmppar;
-   
+
+#ifndef NEW_INSETS
        /* check wether there are half footnotes in the selection */
        if (sel_start_cursor.par()->footnoteflag != LyXParagraph::NO_FOOTNOTE
            || sel_end_cursor.par()->footnoteflag != LyXParagraph::NO_FOOTNOTE) {
-               tmppar = sel_start_cursor.par();
+               LyXParagraph * tmppar = sel_start_cursor.par();
                while (tmppar != sel_end_cursor.par()) {
                        if (tmppar->footnoteflag !=
                            sel_end_cursor.par()->footnoteflag) {
@@ -2493,18 +2319,6 @@ void LyXText::CopySelection(Buffer const * buf)
                        tmppar = tmppar->Next();
                }
        }
-
-#ifndef NEW_TABULAR
-       /* table stuff -- begin */
-       if (sel_start_cursor.par()->table || sel_end_cursor.par()->table){
-               if ( sel_start_cursor.par() != sel_end_cursor.par()){
-                       WriteAlert(_("Impossible operation"),
-                                  _("Don't know what to do with half tables."),
-                                  _("sorry."));
-                       return;
-               }
-       }
-       /* table stuff -- end */
 #endif
    
        // copy behind a space if there is one
@@ -2518,222 +2332,9 @@ void LyXText::CopySelection(Buffer const * buf)
 
        cap.copySelection(sel_start_cursor.par(), sel_end_cursor.par(),
                          sel_start_cursor.pos(), sel_end_cursor.pos(),
-                         buf->params.textclass);
+                         bview->buffer()->params.textclass);
 }
-#endif          
-
-#ifdef USE_OLD_CUT_AND_PASTE
-void LyXText::PasteSelection(Buffer const * buf)
-{
-       // this does not make sense, if there is nothing to paste
-       if (!simple_cut_buffer)
-               return;
 
-       LyXParagraph * tmppar;
-       LyXParagraph * endpar;
-
-       LyXCursor tmpcursor;
-
-       // be carefull with footnotes in footnotes
-       if (cursor.par()->footnoteflag != LyXParagraph::NO_FOOTNOTE) {
-      
-               // check whether the cut_buffer includes a footnote
-               tmppar = simple_cut_buffer;
-               while (tmppar
-                      && tmppar->footnoteflag == LyXParagraph::NO_FOOTNOTE)
-                       tmppar = tmppar->next;
-      
-               if (tmppar) {
-                       WriteAlert(_("Impossible operation"),
-                                  _("Can't paste float into float!"),
-                                  _("Sorry."));
-                       return;
-               }
-       }
-
-#ifndef NEW_TABULAR
-       /* table stuff -- begin */
-       if (cursor.par()->table) {
-               if (simple_cut_buffer->next) {
-                       WriteAlert(_("Impossible operation"),
-                                  _("Table cell cannot include more than one paragraph!"),
-                                  _("Sorry."));
-                       return;
-               }
-       }
-       /* table stuff -- end */
-#endif
-   
-       SetUndo(bview->buffer(), Undo::INSERT, 
-               cursor.par()->ParFromPos(cursor.pos())->previous, 
-               cursor.par()->ParFromPos(cursor.pos())->next); 
-
-       tmpcursor = cursor;
-
-       // There are two cases: cutbuffer only one paragraph or many
-       if (!simple_cut_buffer->next) {
-               // only within a paragraph
-
-               tmppar = simple_cut_buffer->Clone();
-#ifndef NEW_TABULAR
-               /* table stuff -- begin */
-               bool table_too_small = false;
-               if (tmpcursor.par()->table) {
-                       while (simple_cut_buffer->size()
-                              && !table_too_small) {
-                               if (simple_cut_buffer->IsNewline(0)){
-                                       while(tmpcursor.pos() < tmpcursor.par()->Last() && !tmpcursor.par()->IsNewline(tmpcursor.pos()))
-                                               tmpcursor.pos()++;
-                                       simple_cut_buffer->Erase(0);
-                                       if (tmpcursor.pos() < tmpcursor.par()->Last())
-                                               tmpcursor.pos()++;
-                                       else
-                                               table_too_small = true;
-                               } else {
-                                       // This is an attempt to fix the
-                                       // "never insert a space at the
-                                       // beginning of a paragraph" problem.
-                                       if (tmpcursor.pos() == 0
-                                           && simple_cut_buffer->IsLineSeparator(0)) {
-                                               simple_cut_buffer->Erase(0);
-                                       } else {
-                                               simple_cut_buffer->CutIntoMinibuffer(0);
-                                               simple_cut_buffer->Erase(0);
-                                               tmpcursor.par()->InsertFromMinibuffer(tmpcursor.pos());
-                                               tmpcursor.pos()++;
-                                       }
-                               }
-                       }
-               } else {
-#endif
-                       /* table stuff -- end */
-                       // Some provisions should be done here for checking
-                       // if we are inserting at the beginning of a
-                       // paragraph. If there are a space at the beginning
-                       // of the text to insert and we are inserting at
-                       // the beginning of the paragraph the space should
-                       // be removed.
-                       while (simple_cut_buffer->size()) {
-                               // This is an attempt to fix the
-                               // "never insert a space at the
-                               // beginning of a paragraph" problem.
-                               if (tmpcursor.pos() == 0
-                                   && simple_cut_buffer->IsLineSeparator(0)) {
-                                       simple_cut_buffer->Erase(0);
-                               } else {
-                                       simple_cut_buffer->CutIntoMinibuffer(0);
-                                       simple_cut_buffer->Erase(0);
-                                       tmpcursor.par()->InsertFromMinibuffer(tmpcursor.pos());
-                                       tmpcursor.pos()++;
-                               }
-                       }
-#ifndef NEW_TABULAR
-               }
-#endif
-               delete simple_cut_buffer;
-               simple_cut_buffer = tmppar;
-               endpar = tmpcursor.par()->Next();
-       } else {
-               // many paragraphs
-               CutAndPaste cap;
-
-               // make a copy of the simple cut_buffer
-               tmppar = simple_cut_buffer;
-               LyXParagraph * simple_cut_clone = tmppar->Clone();
-               LyXParagraph * tmppar2 = simple_cut_clone;
-               if (cursor.par()->footnoteflag){
-                       tmppar->footnoteflag = cursor.par()->footnoteflag;
-                       tmppar->footnotekind = cursor.par()->footnotekind;
-               }
-               while (tmppar->next) {
-                       tmppar = tmppar->next;
-                       tmppar2->next = tmppar->Clone();
-                       tmppar2->next->previous = tmppar2;
-                       tmppar2 = tmppar2->next;
-                       if (cursor.par()->footnoteflag){
-                               tmppar->footnoteflag = cursor.par()->footnoteflag;
-                               tmppar->footnotekind = cursor.par()->footnotekind;
-                       }
-               }
-     
-               // make sure there is no class difference
-               cap.SwitchLayoutsBetweenClasses(simple_cut_buffer_textclass,
-                                               buf->params.textclass,
-                                               simple_cut_buffer);
-     
-               // make the simple_cut_buffer exactly the same layout than
-               // the cursor paragraph
-               simple_cut_buffer->MakeSameLayout(cursor.par());
-     
-               // find the end of the buffer
-               LyXParagraph * lastbuffer = simple_cut_buffer;
-               while (lastbuffer->Next())
-                       lastbuffer = lastbuffer->Next();
-     
-               bool paste_the_end = false;
-
-               // open the paragraph for inserting the simple_cut_buffer
-               // if necessary
-               if (cursor.par()->Last() > cursor.pos() || !cursor.par()->Next()){
-                       cursor.par()->BreakParagraphConservative(cursor.pos());
-                       paste_the_end = true;
-               }
-
-               // set the end for redoing later
-               endpar = cursor.par()->ParFromPos(cursor.pos())->next->Next();
-     
-               // paste it!
-               lastbuffer->ParFromPos(lastbuffer->Last())->next =
-                       cursor.par()->ParFromPos(cursor.pos())->next;
-               cursor.par()->ParFromPos(cursor.pos())->next->previous =
-                       lastbuffer->ParFromPos(lastbuffer->Last());
-     
-               cursor.par()->ParFromPos(cursor.pos())->next = simple_cut_buffer;
-               simple_cut_buffer->previous =
-                       cursor.par()->ParFromPos(cursor.pos());
-   
-               if (cursor.par()->ParFromPos(cursor.pos())->Next() == lastbuffer)
-                       lastbuffer = cursor.par();
-     
-               cursor.par()->ParFromPos(cursor.pos())->PasteParagraph();
-     
-               // store the new cursor position
-               tmpcursor.par() = lastbuffer;
-               tmpcursor.pos() = lastbuffer->Last();
-     
-               // maybe some pasting
-               if (lastbuffer->Next() && paste_the_end) {
-                       if (lastbuffer->Next()->HasSameLayout(lastbuffer)) {
-                               lastbuffer->ParFromPos(lastbuffer->Last())->PasteParagraph();
-        
-                       } else if (!lastbuffer->Next()->Last()) {
-                               lastbuffer->Next()->MakeSameLayout(lastbuffer);
-                               lastbuffer->ParFromPos(lastbuffer->Last())->PasteParagraph();
-        
-                       } else if (!lastbuffer->Last()) {
-                               lastbuffer->MakeSameLayout(lastbuffer->next);
-                               lastbuffer->ParFromPos(lastbuffer->Last())->PasteParagraph();
-        
-                       } else
-                               lastbuffer->Next()->StripLeadingSpaces(buffer->params.textclass);
-               }
-
-               // restore the simple cut buffer
-               simple_cut_buffer = simple_cut_clone;
-       }
-
-       RedoParagraphs(cursor, endpar);
-    
-       SetCursor(cursor.par(), cursor.pos());
-       ClearSelection();
-   
-       sel_cursor = cursor;
-       SetCursor(tmpcursor.par(), tmpcursor.pos());
-       SetSelection();
-       UpdateCounters(cursor.row());
-}
-
-#else ////////////////////////////////////////////////////////////////////
 
 void LyXText::PasteSelection(BufferView * bview)
 {
@@ -2743,9 +2344,15 @@ void LyXText::PasteSelection(BufferView * bview)
        if (!cap.checkPastePossible(cursor.par(), cursor.pos()))
                return;
 
-       SetUndo(bview->buffer(), Undo::INSERT, 
+       SetUndo(bview->buffer(), Undo::INSERT,
+#ifndef NEW_INSETS
                cursor.par()->ParFromPos(cursor.pos())->previous, 
-               cursor.par()->ParFromPos(cursor.pos())->next); 
+               cursor.par()->ParFromPos(cursor.pos())->next
+#else
+               cursor.par()->previous, 
+               cursor.par()->next
+#endif
+               ); 
 
        LyXParagraph * endpar;
        LyXParagraph * actpar = cursor.par();
@@ -2760,10 +2367,10 @@ void LyXText::PasteSelection(BufferView * bview)
    
        sel_cursor = cursor;
        SetCursor(bview, actpar, pos);
-       SetSelection(bview);
+       SetSelection();
        UpdateCounters(bview, cursor.row());
 }
-#endif   
+
 
 // returns a pointer to the very first LyXParagraph
 LyXParagraph * LyXText::FirstParagraph() const
@@ -2775,33 +2382,36 @@ LyXParagraph * LyXText::FirstParagraph() const
 // returns true if the specified string is at the specified position
 bool LyXText::IsStringInText(LyXParagraph * par,
                             LyXParagraph::size_type pos,
-                            char const * str) const
+                            string const & str) const
 {
-       if (par) {
-               int i = 0;
-               while (pos + i < par->Last() && str[i] && 
-                      str[i] == par->GetChar(pos + i)) {
-                       ++i;
-               }
-               if (!str[i])
-                       return true;
+       if (!par)
+               return false;
+
+       LyXParagraph::size_type i = 0;
+       while (pos + i < par->Last()
+              && string::size_type(i) < str.length()
+              && str[i] == par->GetChar(pos + i)) {
+               ++i;
        }
+       if (str.length() == string::size_type(i))
+               return true;
        return false;
 }
 
 
 // sets the selection over the number of characters of string, no check!!
-void LyXText::SetSelectionOverString(BufferView * bview, char const * string)
+void LyXText::SetSelectionOverString(BufferView * bview, string const & str)
 {
        sel_cursor = cursor;
-       for (int i = 0; string[i]; ++i)
+       for (int i = 0; str[i]; ++i)
                CursorRight(bview);
-       SetSelection(bview);
+       SetSelection();
 }
 
 
 // simple replacing. The font of the first selected character is used
-void LyXText::ReplaceSelectionWithString(BufferView * bview, char const * str)
+void LyXText::ReplaceSelectionWithString(BufferView * bview,
+                                        string const & str)
 {
        SetCursorParUndo(bview->buffer());
        FreezeUndo();
@@ -2813,16 +2423,16 @@ void LyXText::ReplaceSelectionWithString(BufferView * bview, char const * str)
 
        // Get font setting before we cut
        LyXParagraph::size_type pos = sel_end_cursor.pos();
-       LyXFont font = sel_start_cursor.par()->GetFontSettings(bview->buffer()->params,
-                                                            sel_start_cursor.pos());
+       LyXFont const font = sel_start_cursor.par()
+               ->GetFontSettings(bview->buffer()->params,
+                                 sel_start_cursor.pos());
 
        // Insert the new string
-       for (int i = 0; str[i]; ++i) {
-               sel_end_cursor.par()->InsertChar(pos, str[i]);
-               sel_end_cursor.par()->SetFont(pos, font);
+       for (string::const_iterator cit = str.begin(); cit != str.end(); ++cit) {
+               sel_end_cursor.par()->InsertChar(pos, (*cit), font);
                ++pos;
        }
-
+       
        // Cut the selection
        CutSelection(bview);
 
@@ -2832,7 +2442,7 @@ void LyXText::ReplaceSelectionWithString(BufferView * bview, char const * str)
 
 // if the string can be found: return true and set the cursor to
 // the new position
-bool LyXText::SearchForward(BufferView * bview, char const * str) const
+bool LyXText::SearchForward(BufferView * bview, string const & str) const
 {
        LyXParagraph * par = cursor.par();
        LyXParagraph::size_type pos = cursor.pos();
@@ -2853,7 +2463,7 @@ bool LyXText::SearchForward(BufferView * bview, char const * str) const
 }
 
 
-bool LyXText::SearchBackward(BufferView * bview, char const * string) const
+bool LyXText::SearchBackward(BufferView * bview, string const & str) const
 {
        LyXParagraph * par = cursor.par();
        int pos = cursor.pos();
@@ -2869,7 +2479,7 @@ bool LyXText::SearchBackward(BufferView * bview, char const * string) const
                                        pos = par->Last() - 1;
                        } while (par && pos < 0);
                }
-       } while (par && !IsStringInText(par, pos, string));
+       } while (par && !IsStringInText(par, pos, str));
   
        if (par) {
                SetCursor(bview, par, pos);
@@ -2885,7 +2495,6 @@ void LyXText::InsertStringA(BufferView * bview, string const & str)
        LyXParagraph * par = cursor.par();
        LyXParagraph::size_type pos = cursor.pos();
        LyXParagraph::size_type a = 0;
-        int cell = 0;
        LyXParagraph * endpar = cursor.par()->Next();
        
        SetCursorParUndo(bview->buffer());
@@ -2903,33 +2512,14 @@ void LyXText::InsertStringA(BufferView * bview, string const & str)
                        if (str[i] == ' ' 
                            && i + 1 < str.length() && str[i + 1] != ' '
                            && pos && par->GetChar(pos - 1)!= ' ') {
-                               par->InsertChar(pos,' ');
-                               par->SetFont(pos, current_font);
+                               par->InsertChar(pos, ' ', current_font);
                                ++pos;
-#ifndef NEW_TABLAR
-                       } else if (par->table) {
-                               if (str[i] == '\t') {
-                                       while((pos < par->size()) &&
-                                             (par->GetChar(pos) != LyXParagraph::META_NEWLINE))
-                                               ++pos;
-                                       if (pos < par->size())
-                                               ++pos;
-                                       else // no more fields to fill skip the rest
-                                               break;
-                               } else if ((str[i] != 13) &&
-                                          ((str[i] & 127) >= ' ')) {
-                                       par->InsertChar(pos, str[i]);
-                                       par->SetFont(pos, current_font);
-                                       ++pos;
-                               }
-#endif
                         } else if (str[i] == ' ') {
                                InsetSpecialChar * new_inset =
                                        new InsetSpecialChar(InsetSpecialChar::PROTECTED_SEPARATOR);
                                if (par->InsertInsetAllowed(new_inset)) {
-                                       par->InsertChar(pos, LyXParagraph::META_INSET);
-                                       par->SetFont(pos, current_font);
-                                       par->InsertInset(pos, new_inset);
+                                       par->InsertInset(pos, new_inset,
+                                                        current_font);
                                } else {
                                        delete new_inset;
                                }
@@ -2939,9 +2529,8 @@ void LyXText::InsertStringA(BufferView * bview, string const & str)
                                InsetSpecialChar * new_inset =
                                        new InsetSpecialChar(InsetSpecialChar::PROTECTED_SEPARATOR);
                                if (par->InsertInsetAllowed(new_inset)) {
-                                       par->InsertChar(pos, LyXParagraph::META_INSET);
-                                       par->SetFont(pos, current_font);
-                                       par->InsertInset(pos, new_inset);
+                                       par->InsertInset(pos, new_inset,
+                                                        current_font);
                                } else {
                                        delete new_inset;
                                }
@@ -2950,55 +2539,25 @@ void LyXText::InsertStringA(BufferView * bview, string const & str)
                        } else if (str[i] != 13 && 
                                   // Ignore unprintables
                                   (str[i] & 127) >= ' ') {
-                               par->InsertChar(pos, str[i]);
-                               par->SetFont(pos, current_font);
+                               par->InsertChar(pos, str[i], current_font);
                                ++pos;
                        }
                } else {
-#ifndef NEW_TABULAR
-                        if (par->table) {
-                                if ((i + 1) >= str.length()) {
-                                       if (pos < par->size())
-                                               ++pos;
-                                        break;
-                                }
-                                while((pos < par->size()) &&
-                                      (par->GetChar(pos) != LyXParagraph::META_NEWLINE))
-                                        ++pos;
-                                ++pos;
-                                cell = NumberOfCell(par, pos);
-                                while((pos < par->size()) &&
-                                      !(par->table->IsFirstCell(cell))) {
-
-                                        while((pos < par->size()) &&
-                                              (par->GetChar(pos) != LyXParagraph::META_NEWLINE))
-                                                ++pos;
-                                        ++pos;
-                                        cell = NumberOfCell(par, pos);
-                                }
-                                if (pos >= par->size())
-                                        // no more fields to fill skip the rest
-                                        break;
-                        } else {
-#endif
-                                if (!par->size()) { // par is empty
-                                       InsetSpecialChar * new_inset =
-                                               new InsetSpecialChar(InsetSpecialChar::PROTECTED_SEPARATOR);
-                                       if (par->InsertInsetAllowed(new_inset)) {
-                                               par->InsertChar(pos, LyXParagraph::META_INSET);
-                                               par->SetFont(pos, current_font);
-                                               par->InsertInset(pos, new_inset);
-                                       } else {
-                                               delete new_inset;
-                                       }
-                                        ++pos;
-                                }
-                                par->BreakParagraph(bview->buffer()->params, pos, flag);
-                                par = par->Next();
-                                pos = 0;
-#ifndef NEW_TABULAR
-                        }
-#endif
+                       if (!par->size()) { // par is empty
+                               InsetSpecialChar * new_inset =
+                                       new InsetSpecialChar(InsetSpecialChar::PROTECTED_SEPARATOR);
+                               if (par->InsertInsetAllowed(new_inset)) {
+                                       par->InsertInset(pos,
+                                                        new_inset,
+                                                        current_font);
+                               } else {
+                                       delete new_inset;
+                               }
+                               ++pos;
+                       }
+                       par->BreakParagraph(bview->buffer()->params, pos, flag);
+                       par = par->Next();
+                       pos = 0;
                }
                ++i;
        }
@@ -3007,7 +2566,7 @@ void LyXText::InsertStringA(BufferView * bview, string const & str)
        SetCursor(bview, cursor.par(), cursor.pos());
        sel_cursor = cursor;
        SetCursor(bview, par, pos);
-       SetSelection(bview);
+       SetSelection();
 }
 
 
@@ -3018,14 +2577,13 @@ void LyXText::InsertStringA(BufferView * bview, string const & str)
 void LyXText::InsertStringB(BufferView * bview, string const & s)
 {
        string str(s);
-       LyXParagraph * par = cursor.par();
        string::size_type i = 1;
        while (i < str.length()) {
-               if (str[i] == '\t' && !par->table)
+               if (str[i] == '\t')
                        str[i] = ' ';
                if (str[i] == ' ' && i + 1 < str.length() && str[i + 1] == ' ')
                        str[i] = 13;
-               if (str[i] == '\n' && i + 1 < str.length() && !par->table){
+               if (str[i] == '\n' && i + 1 < str.length(){
                        if (str[i + 1] != '\n') {
                                if (str[i - 1] != ' ')
                                        str[i] = ' ';
@@ -3098,115 +2656,112 @@ void LyXText::CheckParagraph(BufferView * bview, LyXParagraph * par,
 {
        LyXCursor tmpcursor;                    
 
-#ifndef NEW_TABULAR
-       /* table stuff -- begin*/
-   
-       if (par->table) {
-               CheckParagraphInTable(bview, par, pos);
-       }
-       else {
-#endif
-               /* table stuff -- end*/
-     
-               long y = 0;
-               LyXParagraph::size_type z;
-               Row * row = GetRow(par, pos, y);
-     
-               // is there a break one row above
-               if (row->previous() && row->previous()->par() == row->par()) {
-                       z = NextBreakPoint(bview, row->previous(), workWidth(bview));
-                       if ( z >= row->pos()) {
-                               // set the dimensions of the row above
-                               y -= row->previous()->height();
-                               refresh_y = y;
-                               refresh_row = row->previous();
-                               status = LyXText::NEED_MORE_REFRESH;
-       
-                               BreakAgain(bview, row->previous());
-
-                               // set the cursor again. Otherwise
-                               // dangling pointers are possible
-                               SetCursor(bview, cursor.par(), cursor.pos());
-                               sel_cursor = cursor;
-                               return;
-                       }
+       int y = 0;
+       LyXParagraph::size_type z;
+       Row * row = GetRow(par, pos, y);
+       
+       // is there a break one row above
+       if (row->previous() && row->previous()->par() == row->par()) {
+               z = NextBreakPoint(bview, row->previous(), workWidth(bview));
+               if (z >= row->pos()) {
+                       // set the dimensions of the row above
+                       y -= row->previous()->height();
+                       refresh_y = y;
+                       refresh_row = row->previous();
+                       status = LyXText::NEED_MORE_REFRESH;
+                       
+                       BreakAgain(bview, row->previous());
+                       
+                       // set the cursor again. Otherwise
+                       // dangling pointers are possible
+                       SetCursor(bview, cursor.par(), cursor.pos(),
+                                 false, cursor.boundary());
+                       sel_cursor = cursor;
+                       return;
                }
+       }
 
-               int tmpheight = row->height();
-               LyXParagraph::size_type tmplast = RowLast(row);
-               refresh_y = y;
-               refresh_row = row;
-
-               BreakAgain(bview, row);
-               if (row->height() == tmpheight && RowLast(row) == tmplast)
-                       status = LyXText::NEED_VERY_LITTLE_REFRESH;
-               else
-                       status = LyXText::NEED_MORE_REFRESH; 
-   
-               // check the special right address boxes
-               if (textclasslist.Style(bview->buffer()->params.textclass,
-                                       par->GetLayout()).margintype
-                   == MARGIN_RIGHT_ADDRESS_BOX) {
-                       tmpcursor.par(par);
-                       tmpcursor.row(row);
-                       tmpcursor.y(y);
-                       tmpcursor.x(0);
-                       tmpcursor.x_fix(0);
-                       tmpcursor.pos(pos);
-                       RedoDrawingOfParagraph(bview, tmpcursor); 
-               }
-#ifndef NEW_TABULAR
+       int const tmpheight = row->height();
+       LyXParagraph::size_type const tmplast = RowLast(row);
+       refresh_y = y;
+       refresh_row = row;
+       
+       BreakAgain(bview, row);
+       if (row->height() == tmpheight && RowLast(row) == tmplast)
+               status = LyXText::NEED_VERY_LITTLE_REFRESH;
+       else
+               status = LyXText::NEED_MORE_REFRESH; 
+       
+       // check the special right address boxes
+       if (textclasslist.Style(bview->buffer()->params.textclass,
+                               par->GetLayout()).margintype
+           == MARGIN_RIGHT_ADDRESS_BOX) {
+               tmpcursor.par(par);
+               tmpcursor.row(row);
+               tmpcursor.y(y);
+               tmpcursor.x(0);
+               tmpcursor.x_fix(0);
+               tmpcursor.pos(pos);
+               RedoDrawingOfParagraph(bview, tmpcursor); 
        }
-#endif
 
        // set the cursor again. Otherwise dangling pointers are possible
        // also set the selection
    
        if (selection) {
                tmpcursor = cursor;
-               SetCursorIntern(bview, sel_cursor.par(), sel_cursor.pos());
+               SetCursorIntern(bview, sel_cursor.par(), sel_cursor.pos(),
+                               false, sel_cursor.boundary());
                sel_cursor = cursor; 
                SetCursorIntern(bview, sel_start_cursor.par(),
-                               sel_start_cursor.pos());
+                               sel_start_cursor.pos(),
+                               false, sel_start_cursor.boundary());
                sel_start_cursor = cursor; 
                SetCursorIntern(bview, sel_end_cursor.par(),
-                               sel_end_cursor.pos());
+                               sel_end_cursor.pos(),
+                               false, sel_end_cursor.boundary());
                sel_end_cursor = cursor; 
                SetCursorIntern(bview, last_sel_cursor.par(),
-                               last_sel_cursor.pos());
+                               last_sel_cursor.pos(),
+                               false, last_sel_cursor.boundary());
                last_sel_cursor = cursor; 
                cursor = tmpcursor;
        }
-       SetCursorIntern(bview, cursor.par(), cursor.pos());
+       SetCursorIntern(bview, cursor.par(), cursor.pos(),
+                       false, cursor.boundary());
 }
 
 
-// returns 0 if inset wasn't found
-int LyXText::UpdateInset(BufferView * bview, Inset * inset)
+// returns false if inset wasn't found
+bool LyXText::UpdateInset(BufferView * bview, Inset * inset)
 {
        // first check the current paragraph
        int pos = cursor.par()->GetPositionOfInset(inset);
        if (pos != -1){
                CheckParagraph(bview, cursor.par(), pos);
-               return 1;
+               return true;
        }
   
        // check every paragraph
   
        LyXParagraph * par = FirstParagraph();
        do {
+#ifndef NEW_INSETS
                // make sure the paragraph is open
                if (par->footnoteflag != LyXParagraph::CLOSED_FOOTNOTE){
+#endif
                        pos = par->GetPositionOfInset(inset);
                        if (pos != -1){
                                CheckParagraph(bview, par, pos);
-                               return 1;
+                               return true;
                        }
+#ifndef NEW_INSETS
                }
+#endif
                par = par->Next();
        } while (par);
   
-       return 0;
+       return false;
 }
 
 
@@ -3223,6 +2778,7 @@ void LyXText::SetCursor(BufferView * bview, LyXParagraph * par,
 void LyXText::SetCursor(BufferView *bview, LyXCursor & cur, LyXParagraph * par,
                        LyXParagraph::size_type pos, bool boundary) const
 {
+#ifndef NEW_INSETS
        // correct the cursor position if impossible
        if (pos > par->Last()){
                LyXParagraph * tmppar = par->ParFromPos(pos);
@@ -3248,13 +2804,13 @@ void LyXText::SetCursor(BufferView *bview, LyXCursor & cur, LyXParagraph * par,
                }
                pos += par->size() + 1;
        }
-
+#endif
        cur.par(par);
        cur.pos(pos);
        cur.boundary(boundary);
 
        /* get the cursor y position in text  */
-       long y = 0;
+       int y = 0;
        Row * row = GetRow(par, pos, y);
        /* y is now the beginning of the cursor row */ 
        y += row->baseline();
@@ -3280,8 +2836,7 @@ void LyXText::SetCursor(BufferView *bview, LyXCursor & cur, LyXParagraph * par,
                cursor_vpos = (row->par()->isRightToLeftPar(bview->buffer()->params))
                        ? row->pos() : last + 1; 
        else if (pos > row->pos() &&
-                (pos > last || boundary || 
-                 (row->par()->table && row->par()->IsNewline(pos))))
+                (pos > last || boundary))
                /// Place cursor after char at (logical) position pos - 1
                cursor_vpos = (bidi_level(pos - 1) % 2 == 0)
                        ? log2vis(pos - 1) + 1 : log2vis(pos - 1);
@@ -3289,65 +2844,41 @@ void LyXText::SetCursor(BufferView *bview, LyXCursor & cur, LyXParagraph * par,
                /// Place cursor before char at (logical) position pos
                cursor_vpos = (bidi_level(pos) % 2 == 0)
                        ? log2vis(pos) : log2vis(pos) + 1;
-
-#ifndef NEW_TABULAR
-       /* table stuff -- begin*/
-       if (row->par()->table) {
-               int cell = NumberOfCell(row->par(), row->pos());
-               float x_old = x;
-               x += row->par()->table->GetBeginningOfTextInCell(cell);
-               for (LyXParagraph::size_type vpos = row->pos();
-                    vpos < cursor_vpos; ++vpos) {
-                       pos = vis2log(vpos);
-                       if (row->par()->IsNewline(pos)) {
-                               x = x_old + row->par()->table->WidthOfColumn(cell);
-                               x_old = x;
-                               ++cell;
-                               x += row->par()->table->GetBeginningOfTextInCell(cell);
-                       } else {
-                               x += SingleWidth(bview, row->par(), pos);
-                       }
-               }
-       } else {
-               /* table stuff -- end*/
-#endif
-               LyXParagraph::size_type main_body =
-                       BeginningOfMainBody(bview->buffer(), row->par());
-               if ((main_body > 0) &&
-                   ((main_body-1 > last) || 
-                    !row->par()->IsLineSeparator(main_body-1)))
-                       main_body = 0;
-
-               for (LyXParagraph::size_type vpos = row->pos();
-                    vpos < cursor_vpos; ++vpos) {
-                       pos = vis2log(vpos);
-                       if (main_body > 0 && pos == main_body-1) {
-                               x += fill_label_hfill +
-                                       lyxfont::width(textclasslist.Style(
-                                               bview->buffer()->params.textclass,
-                                               row->par()->GetLayout())
-                                                      .labelsep,
-                                                      GetFont(bview->buffer(), row->par(), -2));
-                               if (row->par()->IsLineSeparator(main_body-1))
-                                       x -= SingleWidth(bview, row->par(),main_body-1);
-                       }
-                       if (HfillExpansion(bview->buffer(), row, pos)) {
-                               x += SingleWidth(bview, row->par(), pos);
-                               if (pos >= main_body)
-                                       x += fill_hfill;
-                               else 
-                                       x += fill_label_hfill;
-                       } else if (row->par()->IsSeparator(pos)) {
-                               x += SingleWidth(bview, row->par(), pos);
-                               if (pos >= main_body)
-                                       x += fill_separator;
-                       } else
-                               x += SingleWidth(bview, row->par(), pos);
-               }
-#ifndef NEW_TABULAR
+       
+       LyXParagraph::size_type main_body =
+               BeginningOfMainBody(bview->buffer(), row->par());
+       if ((main_body > 0) &&
+           ((main_body-1 > last) || 
+            !row->par()->IsLineSeparator(main_body-1)))
+               main_body = 0;
+       
+       for (LyXParagraph::size_type vpos = row->pos();
+            vpos < cursor_vpos; ++vpos) {
+               pos = vis2log(vpos);
+               if (main_body > 0 && pos == main_body - 1) {
+                       x += fill_label_hfill +
+                               lyxfont::width(textclasslist.Style(
+                                       bview->buffer()->params.textclass,
+                                       row->par()->GetLayout())
+                                              .labelsep,
+                                              GetFont(bview->buffer(), row->par(), -2));
+                       if (row->par()->IsLineSeparator(main_body-1))
+                               x -= SingleWidth(bview, row->par(),main_body-1);
+               }
+               if (HfillExpansion(bview->buffer(), row, pos)) {
+                       x += SingleWidth(bview, row->par(), pos);
+                       if (pos >= main_body)
+                               x += fill_hfill;
+                       else 
+                               x += fill_label_hfill;
+               } else if (row->par()->IsSeparator(pos)) {
+                       x += SingleWidth(bview, row->par(), pos);
+                       if (pos >= main_body)
+                               x += fill_separator;
+               } else
+                       x += SingleWidth(bview, row->par(), pos);
        }
-#endif
-   
+       
        cur.x(int(x));
        cur.x_fix(cur.x());
        cur.row(row);
@@ -3359,131 +2890,11 @@ void LyXText::SetCursorIntern(BufferView * bview, LyXParagraph * par,
                              bool setfont, bool boundary) const
 {
        SetCursor(bview, cursor, par, pos, boundary);
-// #warning Remove this when verified working (Jug 20000413)
-#if 0
-       // correct the cursor position if impossible
-       if (pos > par->Last()){
-               LyXParagraph * tmppar = par->ParFromPos(pos);
-               pos = par->PositionInParFromPos(pos);
-               par = tmppar;
-       }
-       if (par->IsDummy() && par->previous &&
-           par->previous->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE) {
-               while (par->previous &&
-                      ((par->previous->IsDummy() && par->previous->previous->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE) ||
-                       (par->previous->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE))) {
-                       par = par->previous ;
-                       if (par->IsDummy() &&
-                           par->previous->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE)
-                               pos += par->size() + 1;
-               }
-               if (par->previous) {
-                       par = par->previous;
-               }
-               pos += par->size() + 1;
-       }
-
-       cursor.par() = par;
-       cursor.pos() = pos;
-
-       /* get the cursor y position in text  */
-       long y = 0;
-       Row * row = GetRow(par, pos, y);
-       /* y is now the beginning of the cursor row */ 
-       y += row->baseline();
-       /* y is now the cursor baseline */ 
-       cursor.y() = y;
-   
-       /* now get the cursors x position */
-       float x;
-       float fill_separator, fill_hfill, fill_label_hfill;
-       PrepareToPrint(row, x, fill_separator, fill_hfill, fill_label_hfill);
-       LyXParagraph::size_type cursor_vpos;
-       LyXParagraph::size_type last = RowLastPrintable(row);
-
-       if (pos > last + 1)   // This shouldn't happen.
-               pos = last+1;
-
-       if (last < row->pos())
-                cursor_vpos = 0;
-       else if (pos > last ||
-           (pos - 1 >= row->pos() &&
-            (row->par()->IsSeparator(pos) ||
-             (row->par()->table && row->par()->IsNewline(pos))
-             )))
-               /// Place cursor after char at (logical) position pos-1
-               cursor_vpos = (bidi_level(pos-1) % 2 == 0)
-                       ? log2vis(pos-1) + 1 : log2vis(pos-1);
-       else
-               /// Place cursor before char at (logical) position pos
-               cursor_vpos = (bidi_level(pos) % 2 == 0)
-                       ? log2vis(pos) : log2vis(pos) + 1;
-
-#ifndef NEW_TABULAR
-       /* table stuff -- begin*/
-       if (row->par()->table) {
-               int cell = NumberOfCell(row->par(), row->pos());
-               float x_old = x;
-               x += row->par()->table->GetBeginningOfTextInCell(cell);
-               for (LyXParagraph::size_type vpos = row->pos(); vpos < cursor_vpos; ++vpos)  {
-                       pos = vis2log(vpos);
-                       if (row->par()->IsNewline(pos)) {
-                               x = x_old + row->par()->table->WidthOfColumn(cell);
-                               x_old = x;
-                               ++cell;
-                               x += row->par()->table->GetBeginningOfTextInCell(cell);
-                       } else {
-                               x += SingleWidth(row->par(), pos);
-                       }
-               }
-       } else {
-               /* table stuff -- end*/
-#endif
-               LyXParagraph::size_type main_body =
-                       BeginningOfMainBody(row->par());
-               if (main_body > 0 &&
-                   (main_body-1 > last || 
-                    !row->par()->IsLineSeparator(main_body-1)))
-                       main_body = 0;
-
-               for (LyXParagraph::size_type vpos = row->pos(); vpos < cursor_vpos; ++vpos)  {
-                       pos = vis2log(vpos);
-                       if (main_body > 0 && pos == main_body-1) {
-                               x += fill_label_hfill +
-                                       lyxfont::width(textclasslist
-                                                      .Style(bview->buffer()->params.textclass,
-                                                             row->par()->GetLayout())
-                                                      .labelsep,
-                                                      GetFont(row->par(), -2));
-                               if (row->par()->IsLineSeparator(main_body-1))
-                                       x -= SingleWidth(row->par(), main_body-1);
-                       }
-                       if (HfillExpansion(row, pos)) {
-                               x += SingleWidth(row->par(), pos);
-                               if (pos >= main_body)
-                                       x += fill_hfill;
-                               else 
-                                       x += fill_label_hfill;
-                       }
-                       else if (row->par()->IsSeparator(pos)) {
-                               x += SingleWidth(row->par(), pos);
-                               if (pos >= main_body)
-                                       x += fill_separator;
-                       } else
-                               x += SingleWidth(row->par(), pos);
-               }
-#ifndef NEW_TABULAR
-       }
-#endif
-       cursor.x = int(x);
-   
-       cursor.x_fix = cursor.x;
-       cursor.row() = row;
-#endif
        if (setfont)
                SetCurrentFont(bview);
 }
 
+
 void LyXText::SetCurrentFont(BufferView * bview) const
 {
        LyXParagraph::size_type pos = cursor.pos();
@@ -3491,8 +2902,7 @@ void LyXText::SetCurrentFont(BufferView * bview) const
                --pos;
 
        if (pos > 0) {
-               if (pos == cursor.par()->Last() ||
-                   (cursor.par()->table && cursor.par()->IsNewline(pos)))
+               if (pos == cursor.par()->Last())
                        --pos;
                else if (cursor.par()->IsSeparator(pos)) {
                        if (pos > cursor.row()->pos() &&
@@ -3504,12 +2914,22 @@ void LyXText::SetCurrentFont(BufferView * bview) const
                }
        }
 
-       current_font = cursor.par()->GetFontSettings(bview->buffer()->params, pos);
+       current_font =
+               cursor.par()->GetFontSettings(bview->buffer()->params, pos);
        real_current_font = GetFont(bview->buffer(), cursor.par(), pos);
+
+       if (cursor.pos() == cursor.par()->Last() &&
+           IsBoundary(bview->buffer(), cursor.par(), cursor.pos()) &&
+           !cursor.boundary()) {
+               Language const * lang =
+                       cursor.par()->getParLanguage(bview->buffer()->params);
+               current_font.setLanguage(lang);
+               real_current_font.setLanguage(lang);
+       }
 }
 
 
-void LyXText::SetCursorFromCoordinates(BufferView * bview, int x, long y) const
+void LyXText::SetCursorFromCoordinates(BufferView * bview, int x, int y) const
 {
        LyXCursor old_cursor = cursor;
    
@@ -3531,7 +2951,7 @@ void LyXText::SetCursorFromCoordinates(BufferView * bview, int x, long y) const
 
 
 void LyXText::SetCursorFromCoordinates(BufferView * bview, LyXCursor & cur,
-                                      int x, long y) const
+                                      int x, int y) const
 {
        /* get the row first */ 
    
@@ -3549,21 +2969,6 @@ void LyXText::SetCursorFromCoordinates(BufferView * bview, LyXCursor & cur,
 
 
 void LyXText::CursorLeft(BufferView * bview, bool internal) const
-{
-       CursorLeftIntern(bview, internal);
-#ifndef NEW_TABULAR
-        if (cursor.par()->table) {
-                int cell = NumberOfCell(cursor.par(), cursor.pos());
-                if (cursor.par()->table->IsContRow(cell)
-                    && cursor.par()->table->CellHasContRow(cursor.par()->table->GetCellAbove(cell)) < 0) {
-                        CursorUp(bview);
-                }
-        }
-#endif
-}
-
-
-void LyXText::CursorLeftIntern(BufferView * bview, bool internal) const
 {
        if (cursor.pos() > 0) {
                bool boundary = cursor.boundary();
@@ -3573,40 +2978,21 @@ void LyXText::CursorLeftIntern(BufferView * bview, bool internal) const
                        SetCursor(bview, cursor.par(), cursor.pos() + 1, true, true);
        } else if (cursor.par()->Previous()) { // steps into the above paragraph.
                LyXParagraph * par = cursor.par()->Previous();
-               LyXParagraph::size_type pos = par->Last();
-               SetCursor(bview, par, pos);
-               if (IsBoundary(bview->buffer(), par, pos))
-                       SetCursor(bview, par, pos, false, true);
+               SetCursor(bview, par, par->Last());
        }
 }
 
 
 void LyXText::CursorRight(BufferView * bview, bool internal) const
 {
-       CursorRightIntern(bview, internal);
-#ifndef NEW_TABULAR
-        if (cursor.par()->table) {
-                int cell = NumberOfCell(cursor.par(), cursor.pos());
-                if (cursor.par()->table->IsContRow(cell) &&
-                    cursor.par()->table->CellHasContRow(cursor.par()->table->GetCellAbove(cell))<0) {
-                        CursorUp(bview);
-                }
-        }
-#endif
-}
-
-
-void LyXText::CursorRightIntern(BufferView * bview, bool internal) const
-{
-       if (cursor.pos() < cursor.par()->Last()) {
-               if (!internal && cursor.boundary() &&
-                   (!cursor.par()->table || !cursor.par()->IsNewline(cursor.pos())))
-                       SetCursor(bview, cursor.par(), cursor.pos(), true, false);
-               else {
-                       SetCursor(bview, cursor.par(), cursor.pos() + 1, true, false);
-                       if (!internal && IsBoundary(bview->buffer(), cursor.par(), cursor.pos()))
-                               SetCursor(bview, cursor.par(), cursor.pos(), true, true);
-               }
+       if (!internal && cursor.boundary() &&
+           !cursor.par()->IsNewline(cursor.pos()))
+               SetCursor(bview, cursor.par(), cursor.pos(), true, false);
+       else if (cursor.pos() < cursor.par()->Last()) {
+               SetCursor(bview, cursor.par(), cursor.pos() + 1, true, false);
+               if (!internal &&
+                   IsBoundary(bview->buffer(), cursor.par(), cursor.pos()))
+                       SetCursor(bview, cursor.par(), cursor.pos(), true, true);
        } else if (cursor.par()->Next())
                SetCursor(bview, cursor.par()->Next(), 0);
 }
@@ -3616,47 +3002,14 @@ void LyXText::CursorUp(BufferView * bview) const
 {
        SetCursorFromCoordinates(bview, cursor.x_fix(), 
                                 cursor.y() - cursor.row()->baseline() - 1);
-#ifndef NEW_TABULAR
-        if (cursor.par()->table) {
-                int cell = NumberOfCell(cursor.par(), cursor.pos());
-                if (cursor.par()->table->IsContRow(cell) &&
-                    cursor.par()->table->CellHasContRow(cursor.par()->table->GetCellAbove(cell))<0) {
-                        CursorUp(bview);
-                }
-        }
-#endif
 }
 
 
 void LyXText::CursorDown(BufferView * bview) const
 {
-#ifndef NEW_TABULAR
-        if (cursor.par()->table &&
-            cursor.par()->table->ShouldBeVeryLastRow(NumberOfCell(cursor.par(), cursor.pos())) &&
-            !cursor.par()->next)
-                return;
-#endif
-       
        SetCursorFromCoordinates(bview, cursor.x_fix(), 
                                 cursor.y() - cursor.row()->baseline()
                                 + cursor.row()->height() + 1);
-#ifndef NEW_TABULAR
-        if (cursor.par()->table) {
-                int cell = NumberOfCell(cursor.par(), cursor.pos());
-                int cell_above = cursor.par()->table->GetCellAbove(cell);
-                while(cursor.par()->table &&
-                      cursor.par()->table->IsContRow(cell) &&
-                      (cursor.par()->table->CellHasContRow(cell_above)<0)) {
-                    SetCursorFromCoordinates(bview, cursor.x_fix(), 
-                                             cursor.y() - cursor.row()->baseline()
-                                             + cursor.row()->height() + 1);
-                    if (cursor.par()->table) {
-                        cell = NumberOfCell(cursor.par(), cursor.pos());
-                        cell_above = cursor.par()->table->GetCellAbove(cell);
-                    }
-                }
-        }
-#endif
 }
 
 
@@ -3747,15 +3100,20 @@ void LyXText::DeleteEmptyParagraphMechanism(BufferView * bview,
        LyXCursor tmpcursor;
 
        if (old_cursor.par() != cursor.par()) {
-               if ( (old_cursor.par()->Last() == 0
+               if ((old_cursor.par()->Last() == 0
                      || (old_cursor.par()->Last() == 1
                          && old_cursor.par()->IsLineSeparator(0)))
+#ifndef NEW_INSETS
                     && old_cursor.par()->FirstPhysicalPar()
-                    == old_cursor.par()->LastPhysicalPar()) {
+                    == old_cursor.par()->LastPhysicalPar()
+#endif
+                       ) {
                        // ok, we will delete anything
                        
                        // make sure that you do not delete any environments
-                       if ((old_cursor.par()->footnoteflag != LyXParagraph::OPEN_FOOTNOTE &&
+#ifndef NEW_INSETS
+                       if ((
+                               old_cursor.par()->footnoteflag != LyXParagraph::OPEN_FOOTNOTE &&
                             !(old_cursor.row()->previous() 
                               && old_cursor.row()->previous()->par()->footnoteflag == LyXParagraph::OPEN_FOOTNOTE)
                             && !(old_cursor.row()->next() 
@@ -3766,6 +3124,7 @@ void LyXText::DeleteEmptyParagraphMechanism(BufferView * bview,
                                    || (old_cursor.row()->next()
                                        && old_cursor.row()->next()->par()->footnoteflag == LyXParagraph::OPEN_FOOTNOTE))
                                    )) {
+#endif
                                status = LyXText::NEED_MORE_REFRESH;
                                deleted = true;
                                
@@ -3777,7 +3136,11 @@ void LyXText::DeleteEmptyParagraphMechanism(BufferView * bview,
                                        LyXParagraph * endpar = old_cursor.par()->next;
                                        if (endpar && endpar->GetDepth()) {
                                                while (endpar && endpar->GetDepth()) {
+#ifndef NEW_INSETS
                                                        endpar = endpar->LastPhysicalPar()->Next();
+#else
+                                                       endpar = endpar->Next();
+#endif
                                                }
                                        }
                                        SetUndo(bview->buffer(), Undo::DELETE,
@@ -3812,7 +3175,11 @@ void LyXText::DeleteEmptyParagraphMechanism(BufferView * bview,
                                        LyXParagraph * endpar = old_cursor.par()->next;
                                        if (endpar && endpar->GetDepth()) {
                                                while (endpar && endpar->GetDepth()) {
+#ifndef NEW_INSETS
                                                        endpar = endpar->LastPhysicalPar()->Next();
+#else
+                                                       endpar = endpar->Next();
+#endif
                                                }
                                        }
                                        SetUndo(bview->buffer(), Undo::DELETE,
@@ -3849,7 +3216,9 @@ void LyXText::DeleteEmptyParagraphMechanism(BufferView * bview,
                                        // correct selection
                                        sel_cursor = cursor;
                                }
+#ifndef NEW_INSETS
                        }
+#endif
                }
                if (!deleted) {
                        if (old_cursor.par()->StripLeadingSpaces(bview->buffer()->params.textclass)) {
@@ -3875,6 +3244,8 @@ LyXParagraph * LyXText::GetParFromID(int id)
 // undo functions
 bool LyXText::TextUndo(BufferView * bview)
 {
+       if (inset_owner)
+               return false;
        // returns false if no undo possible
        Undo * undo = bview->buffer()->undostack.pop();
        if (undo) {
@@ -3891,6 +3262,8 @@ bool LyXText::TextUndo(BufferView * bview)
 
 bool LyXText::TextRedo(BufferView * bview)
 {
+       if (inset_owner)
+               return false;
        // returns false if no redo possible
        Undo * undo = bview->buffer()->redostack.pop();
        if (undo) {
@@ -3907,6 +3280,8 @@ bool LyXText::TextRedo(BufferView * bview)
 
 bool LyXText::TextHandleUndo(BufferView * bview, Undo * undo)
 {
+       if (inset_owner)
+               return false;
        // returns false if no undo possible
        bool result = false;
        if (undo) {
@@ -3961,8 +3336,7 @@ bool LyXText::TextHandleUndo(BufferView * bview, Undo * undo)
                        else
                                OwnerParagraph(tmppar3);
                        tmppar3->previous = before;
-               }
-               else {
+               } else {
                        if (!before)
                                OwnerParagraph(behind);
                }
@@ -3975,7 +3349,12 @@ bool LyXText::TextHandleUndo(BufferView * bview, Undo * undo)
     
                // Set the cursor for redoing
                if (before) {
+#ifndef NEW_INSETS
                        SetCursorIntern(bview, before->FirstSelfrowPar(), 0);
+#else
+                       SetCursorIntern(bview, before, 0);
+#endif
+#ifndef NEW_INSETS
                        // check wether before points to a closed float and open it if necessary
                        if (before && before->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE
                            && before->next && before->next->footnoteflag != LyXParagraph::NO_FOOTNOTE){
@@ -3988,8 +3367,10 @@ bool LyXText::TextHandleUndo(BufferView * bview, Undo * undo)
                                        tmppar4 = tmppar4->next;
                                }
                        }
+#endif
                }
-    
+
+#ifndef NEW_INSETS
                // open a cosed footnote at the end if necessary
                if (behind && behind->previous && 
                    behind->previous->footnoteflag != LyXParagraph::NO_FOOTNOTE &&
@@ -3999,13 +3380,18 @@ bool LyXText::TextHandleUndo(BufferView * bview, Undo * undo)
                                behind = behind->next;
                        }
                }
+#endif
     
                // calculate the endpar for redoing the paragraphs.
                if (behind) {
+#ifndef NEW_INSETS
                        if (behind->footnoteflag != LyXParagraph::CLOSED_FOOTNOTE)
                                endpar = behind->LastPhysicalPar()->Next();
                        else
                                endpar = behind->NextAfterFootnote()->LastPhysicalPar()->Next();
+#else
+                               endpar = behind->Next();
+#endif
                } else
                        endpar = behind;
     
@@ -4025,6 +3411,8 @@ bool LyXText::TextHandleUndo(BufferView * bview, Undo * undo)
 
 void LyXText::FinishUndo()
 {
+       if (inset_owner)
+               return;
        // makes sure the next operation will be stored
        undo_finished = true;
 }
@@ -4032,6 +3420,8 @@ void LyXText::FinishUndo()
 
 void LyXText::FreezeUndo()
 {
+       if (inset_owner)
+               return;
        // this is dangerous and for internal use only
        undo_frozen = true;
 }
@@ -4039,6 +3429,8 @@ void LyXText::FreezeUndo()
 
 void LyXText::UnFreezeUndo()
 {
+       if (inset_owner)
+               return;
        // this is dangerous and for internal use only
        undo_frozen = false;
 }
@@ -4048,6 +3440,8 @@ void LyXText::SetUndo(Buffer * buf, Undo::undo_kind kind,
                      LyXParagraph const * before,
                      LyXParagraph const * behind) const
 {
+       if (inset_owner)
+               return;
        if (!undo_frozen)
                buf->undostack.push(CreateUndo(buf, kind, before, behind));
        buf->redostack.clear();
@@ -4057,6 +3451,8 @@ void LyXText::SetUndo(Buffer * buf, Undo::undo_kind kind,
 void LyXText::SetRedo(Buffer * buf, Undo::undo_kind kind,
                      LyXParagraph const * before, LyXParagraph const * behind)
 {
+       if (inset_owner)
+               return;
        buf->redostack.push(CreateUndo(buf, kind, before, behind));
 }
 
@@ -4065,6 +3461,9 @@ Undo * LyXText::CreateUndo(Buffer * buf, Undo::undo_kind kind,
                           LyXParagraph const * before,
                           LyXParagraph const * behind) const
 {
+       if (inset_owner)
+               return 0;
+
        int before_number = -1;
        int behind_number = -1;
        if (before)
@@ -4078,8 +3477,7 @@ Undo * LyXText::CreateUndo(Buffer * buf, Undo::undo_kind kind,
        // appear one by one when undoing. 
        // EDIT is special since only layout information, not the
        // contents of a paragaph are stored.
-       if (!undo_finished && kind != Undo::EDIT && 
-           kind != Undo::FINISH){
+       if (!undo_finished && (kind != Undo::EDIT) && (kind != Undo::FINISH)){
                // check wether storing is needed
                if (!buf->undostack.empty() && 
                    buf->undostack.top()->kind == kind &&
@@ -4109,9 +3507,9 @@ Undo * LyXText::CreateUndo(Buffer * buf, Undo::undo_kind kind,
                        end = end->next;
        }
 
-       if (start && end
-           && start != end->next
-           && (before != behind || (!before && !behind))) {
+       if (start && end && (start != end->next) &&
+           ((before != behind) || (!before && !behind)))
+       {
                tmppar = start;
                tmppar2 = tmppar->Clone();
                tmppar2->id(tmppar->id());
@@ -4141,9 +3539,14 @@ Undo * LyXText::CreateUndo(Buffer * buf, Undo::undo_kind kind,
                tmppar2->next = 0;
        } else
                undopar = 0; // nothing to replace (undo of delete maybe)
-  
+
+#ifndef NEW_INSETS
        int cursor_par = cursor.par()->ParFromPos(cursor.pos())->id();
        int cursor_pos =  cursor.par()->PositionInParFromPos(cursor.pos());
+#else
+       int cursor_par = cursor.par()->id();
+       int cursor_pos =  cursor.pos();
+#endif
 
        Undo * undo = new Undo(kind, 
                               before_number, behind_number,  
@@ -4157,79 +3560,27 @@ Undo * LyXText::CreateUndo(Buffer * buf, Undo::undo_kind kind,
 
 void LyXText::SetCursorParUndo(Buffer * buf)
 {
-       SetUndo(buf, Undo::FINISH, 
+       if (inset_owner)
+               return;
+       SetUndo(buf, Undo::FINISH,
+#ifndef NEW_INSETS
                cursor.par()->ParFromPos(cursor.pos())->previous, 
-               cursor.par()->ParFromPos(cursor.pos())->next); 
-}
-
-
-#ifndef NEW_TABULAR
-void LyXText::RemoveTableRow(LyXCursor & cur) const
-{
-       int cell = -1;
-       int cell_org = 0;
-       int ocell = 0;
-    
-       // move to the previous row
-       int cell_act = NumberOfCell(cur.par(), cur.pos());
-       if (cell < 0)
-               cell = cell_act;
-       while (cur.pos() && !cur.par()->IsNewline(cur.pos() - 1))
-               cur.pos(cur.pos() - 1);
-       while (cur.pos() && 
-              !cur.par()->table->IsFirstCell(cell_act)) {
-               cur.pos(cur.pos() - 1);
-               while (cur.pos() && !cur.par()->IsNewline(cur.pos() - 1))
-                       cur.pos(cur.pos() - 1);
-               --cell;
-               --cell_act;
-       }
-       // now we have to pay attention if the actual table is the
-       //   main row of TableContRows and if yes to delete all of them
-       if (!cell_org)
-               cell_org = cell;
-       do {
-               ocell = cell;
-               // delete up to the next row
-               while (cur.pos() < cur.par()->Last() && 
-                      (cell_act == ocell
-                       || !cur.par()->table->IsFirstCell(cell_act))) {
-                       while (cur.pos() < cur.par()->Last() &&
-                              !cur.par()->IsNewline(cur.pos()))
-                               cur.par()->Erase(cur.pos());
-                       ++cell;
-                       ++cell_act;
-                       if (cur.pos() < cur.par()->Last())
-                               cur.par()->Erase(cur.pos());
-               }
-               if (cur.pos() && cur.pos() == cur.par()->Last()) {
-                       cur.pos(cur.pos() - 1);
-                       cur.par()->Erase(cur.pos()); // no newline at very end!
-               }
-       } while (((cell + 1) < cur.par()->table->GetNumberOfCells()) &&
-                !cur.par()->table->IsContRow(cell_org) &&
-                cur.par()->table->IsContRow(cell));
-       cur.par()->table->DeleteRow(cell_org);
-       return;
-}
+               cursor.par()->ParFromPos(cursor.pos())->next
+#else
+               cursor.par()->previous, 
+               cursor.par()->next
 #endif
-
-
-#ifndef NEW_TABULAR
-bool LyXText::IsEmptyTableCell() const
-{
-       LyXParagraph::size_type pos = cursor.pos() - 1;
-       while (pos >= 0 && pos < cursor.par()->Last()
-              && !cursor.par()->IsNewline(pos))
-               --pos;
-       return cursor.par()->IsNewline(pos + 1);
+               ); 
 }
-#endif
 
 
 void LyXText::toggleAppendix(BufferView * bview)
 {
+#ifndef NEW_INSETS
        LyXParagraph * par = cursor.par()->FirstPhysicalPar();
+#else
+       LyXParagraph * par = cursor.par();
+#endif
        bool start = !par->start_of_appendix;
 
        // ensure that we have only one start_of_appendix in this document
@@ -4246,6 +3597,7 @@ void LyXText::toggleAppendix(BufferView * bview)
        SetCursor(bview, cursor.par(), cursor.pos());
 }
 
+
 LyXParagraph * LyXText::OwnerParagraph() const
 {
        if (inset_owner)