]> git.lyx.org Git - lyx.git/blobdiff - src/text2.C
Tiny things found while peering at chset code
[lyx.git] / src / text2.C
index b1bc3e4af01a706996990d967bc2442c9b080f07..7de8328f1535db77d188deaff1f5f76e847d6cbe 100644 (file)
@@ -6,16 +6,16 @@
  *           Copyright 1995 Matthias Ettrich
  *           Copyright 1995-1999 The LyX Team.
  *
- * ======================================================*/
+ * ====================================================== */
 
 #include <config.h>
 
 #include <cctype>
 #include FORMS_H_LOCATION
 
+
 #ifdef __GNUG__
 #pragma implementation "lyxtext.h"
-#pragma implementation "undo.h"
 #endif
 
 #include "LString.h"
 #include "lyxtext.h"
 #include "gettext.h"
 
-extern MiniBuffer *minibuffer;
+using std::copy;
+
+extern MiniBuffer * minibuffer;
 
 // Constructor
-LyXText::LyXText(int pw, Buffer *p)
+LyXText::LyXText(int pw, Buffer * p)
 {
        firstrow = 0;
        lastrow = 0;
@@ -46,9 +48,9 @@ LyXText::LyXText(int pw, Buffer *p)
        parameters = &p->params;
        params = p;
        number_of_rows = 0;
-       refresh_y= 0;
+       refresh_y = 0;
        status = LyXText::UNCHANGED;
-       LyXParagraph *par = p->paragraph;
+       LyXParagraph * par = p->paragraph;
        current_font = GetFont(par, 0);
    
        height = 0;
@@ -82,7 +84,7 @@ LyXText::LyXText(int pw, Buffer *p)
 LyXText::~LyXText()
 {
        // Delete all rows, this does not touch the paragraphs!
-       Row *tmprow = firstrow;
+       Row * tmprow = firstrow;
        while (firstrow) {
                tmprow = firstrow->next;
                delete firstrow;
@@ -98,31 +100,32 @@ LyXText::~LyXText()
 // 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(LyXParagraph* par, int pos)
+LyXFont LyXText::GetFont(LyXParagraph * par,
+                        LyXParagraph::size_type pos)
 {
-       LyXLayout *layout = 
-               lyxstyle.Style(parameters->textclass, par->GetLayout());
+       LyXLayout const & layout = 
+               textclasslist.Style(parameters->textclass, par->GetLayout());
 
        char par_depth = par->GetDepth();
        // We specialize the 95% common case:
        if (par->footnoteflag == LyXParagraph::NO_FOOTNOTE && !par_depth) {
                if (pos >= 0){
                        // 95% goes here
-                       if (layout->labeltype == LABEL_MANUAL
+                       if (layout.labeltype == LABEL_MANUAL
                            && pos < BeginningOfMainBody(par)) {
                                // 1% goes here
                                return par->GetFontSettings(pos).
-                                               realize(layout->reslabelfont);
+                                               realize(layout.reslabelfont);
                        } else
                                return par->GetFontSettings(pos).
-                                               realize(layout->resfont);
+                                               realize(layout.resfont);
                } else {
                        // 5% goes here.
                        // process layoutfont for pos == -1 and labelfont for pos < -1
                        if (pos == -1)
-                               return layout->resfont;
+                               return layout.resfont;
                        else
-                               return layout->reslabelfont;
+                               return layout.reslabelfont;
                }
        }
 
@@ -134,36 +137,34 @@ LyXFont LyXText::GetFont(LyXParagraph* par, int pos)
                // 95% goes here
                if (pos < BeginningOfMainBody(par)) {
                        // 1% goes here
-                       layoutfont = layout->labelfont;
+                       layoutfont = layout.labelfont;
                } else {
                        // 99% goes here
-                       layoutfont = layout->font;
+                       layoutfont = layout.font;
                }
                tmpfont = par->GetFontSettings(pos);
                tmpfont.realize(layoutfont);
-       } else{
+       } else {
                // 5% goes here.
                // process layoutfont for pos == -1 and labelfont for pos < -1
                if (pos == -1)
-                       tmpfont = layout->font;
+                       tmpfont = layout.font;
                else
-                       tmpfont = layout->labelfont;
+                       tmpfont = layout.labelfont;
        }
 
        // Resolve against environment font information
-       //if (par->GetDepth()){ // already in while condition
-               while (par && par_depth && !tmpfont.resolved()) {
-                       par = par->DepthHook(par_depth - 1);
-                       if (par) {
-                               tmpfont.realize(lyxstyle.
-                                               Style(parameters->textclass,
-                                                     par->GetLayout())->font);
-                               par_depth = par->GetDepth();
-                       }
+       while (par && par_depth && !tmpfont.resolved()) {
+               par = par->DepthHook(par_depth - 1);
+               if (par) {
+                       tmpfont.realize(textclasslist.
+                                       Style(parameters->textclass,
+                                             par->GetLayout()).font);
+                       par_depth = par->GetDepth();
                }
-       //}
+       }
 
-       tmpfont.realize(lyxstyle.TextClass(parameters->textclass)->defaultfont);
+       tmpfont.realize(textclasslist.TextClass(parameters->textclass).defaultfont());
 
        // Cosmetic improvement: If this is an open footnote, make the font 
        // smaller.
@@ -176,24 +177,26 @@ LyXFont LyXText::GetFont(LyXParagraph* par, int pos)
 }
 
 
-void LyXText::SetCharFont(LyXParagraph *par, int pos, LyXFont font)
+void LyXText::SetCharFont(LyXParagraph * par,
+                         LyXParagraph::size_type pos,
+                         LyXFont font)
 {
        /* let the insets convert their font */ 
-       if (par->GetChar(pos) == LYX_META_INSET) {
+       if (par->GetChar(pos) == LyXParagraph::META_INSET) {
                if (par->GetInset(pos))
                        font = par->GetInset(pos)->ConvertFont(font);
        }
 
-       LyXLayout *layout = lyxstyle.Style(parameters->textclass,
+       LyXLayout const & layout = textclasslist.Style(parameters->textclass,
                                           par->GetLayout());
 
        // Get concrete layout font to reduce against
        LyXFont layoutfont;
 
        if (pos < BeginningOfMainBody(par))
-               layoutfont = layout->labelfont;
+               layoutfont = layout.labelfont;
        else
-               layoutfont = layout->font;
+               layoutfont = layout.font;
 
        // Realize against environment font information
        if (par->GetDepth()){
@@ -201,13 +204,13 @@ void LyXText::SetCharFont(LyXParagraph *par, int pos, LyXFont font)
                while (!layoutfont.resolved() && tp && tp->GetDepth()) {
                        tp = tp->DepthHook(tp->GetDepth()-1);
                        if (tp)
-                               layoutfont.realize(lyxstyle.
+                               layoutfont.realize(textclasslist.
                                                Style(parameters->textclass,
-                                                     tp->GetLayout())->font);
+                                                     tp->GetLayout()).font);
                }
        }
 
-       layoutfont.realize(lyxstyle.TextClass(parameters->textclass)->defaultfont);
+       layoutfont.realize(textclasslist.TextClass(parameters->textclass).defaultfont());
 
        if (par->footnoteflag == LyXParagraph::OPEN_FOOTNOTE
            && par->footnotekind == LyXParagraph::FOOTNOTE) {
@@ -223,9 +226,10 @@ void LyXText::SetCharFont(LyXParagraph *par, int pos, LyXFont font)
 
 /* inserts a new row behind the specified row, increments
  * the touched counters */
-void LyXText::InsertRow(Row *row, LyXParagraph *par, int pos)
+void LyXText::InsertRow(Row * row, LyXParagraph * par,
+                       LyXParagraph::size_type pos)
 {
-       Row *tmprow = new Row;
+       Row * tmprow = new Row;
        if (!row) {
                tmprow->previous = 0;
                tmprow->next = firstrow;
@@ -254,7 +258,7 @@ void LyXText::InsertRow(Row *row, LyXParagraph *par, int pos)
 
 
 /* removes the row and reset the touched counters */
-void LyXText::RemoveRow(Row *row)
+void LyXText::RemoveRow(Row * row)
 {
        /* this must not happen before the currentrow for clear reasons.
           so the trick is just to set the current row onto the previous
@@ -281,16 +285,16 @@ void LyXText::RemoveRow(Row *row)
        height -= row->height;         /* the text becomes smaller  */
    
        delete row;
-       number_of_rows--;                              /* one row less  */
+       --number_of_rows;                              /* one row less  */
 }
 
+
 /* remove all following rows of the paragraph of the specified row. */
-void LyXText::RemoveParagraph(Row *row)
+void LyXText::RemoveParagraph(Row * row)
 {
-       LyXParagraph *tmppar;
-       Row *tmprow;
+       Row * tmprow;
 
-       tmppar = row->par;
+       LyXParagraph * tmppar = row->par;
        row = row->next;
     
        while (row && row->par == tmppar) {
@@ -302,7 +306,7 @@ void LyXText::RemoveParagraph(Row *row)
    
   
 /* insert the specified paragraph behind the specified row */
-void LyXText::InsertParagraph(LyXParagraph *par, Row *row)
+void LyXText::InsertParagraph(LyXParagraph * par, Row * row)
 {
        InsertRow(row, par, 0);        /* insert a new row, starting 
                                        * at postition 0 */
@@ -323,9 +327,7 @@ void LyXText::InsertParagraph(LyXParagraph *par, Row *row)
 
 void LyXText::ToggleFootnote()
 {
-       LyXParagraph *par;
-   
-       par = cursor.par->ParFromPos(cursor.pos);
+       LyXParagraph * par = cursor.par->ParFromPos(cursor.pos);
        if (par->next && par->next->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE){
                OpenFootnote();
                minibuffer->Set(_("Opened float"));
@@ -340,15 +342,15 @@ void LyXText::ToggleFootnote()
 void LyXText::OpenStuff()
 {
        if (cursor.pos == 0 && cursor.par->bibkey){
-               cursor.par->bibkey->Edit(0,0);
+               cursor.par->bibkey->Edit(0, 0);
        }
        else if (cursor.pos < cursor.par->Last() 
-                && cursor.par->GetChar(cursor.pos) == LYX_META_INSET
+                && cursor.par->GetChar(cursor.pos) == LyXParagraph::META_INSET
                 && cursor.par->GetInset(cursor.pos)->Editable()) {
                minibuffer->Set(cursor.par->GetInset(cursor.pos)->EditMessage());
                if (cursor.par->GetInset(cursor.pos)->Editable() != 2)
                        SetCursorParUndo();
-               cursor.par->GetInset(cursor.pos)->Edit(0,0);
+               cursor.par->GetInset(cursor.pos)->Edit(0, 0);
        }
        else {
                ToggleFootnote();
@@ -358,10 +360,10 @@ void LyXText::OpenStuff()
 
 void LyXText::CloseFootnote()
 {
-       LyXParagraph *par, *endpar,*tmppar;
-       Row *row;
+       LyXParagraph * endpar, * tmppar;
+       Row * row;
    
-       par = cursor.par->ParFromPos(cursor.pos);
+       LyXParagraph * par = cursor.par->ParFromPos(cursor.pos);
    
        /* if the cursor is not in an open footnote, or 
         * there is no open footnote in this paragraph, just return. */ 
@@ -376,16 +378,14 @@ void LyXText::CloseFootnote()
                /* ok, move the cursor right before the footnote */ 
  
                /* just a little faster than using CursorRight() */
-               for (cursor.pos=0; cursor.par->ParFromPos(cursor.pos)!=par; cursor.pos++);
+               for (cursor.pos = 0; cursor.par->ParFromPos(cursor.pos)!= par; cursor.pos++);
                /* now the cursor is at the beginning of the physical par */
-               SetCursor(cursor.par, cursor.pos + cursor.par->ParFromPos(cursor.pos)->last);
+               SetCursor(cursor.par,
+                         cursor.pos +
+                         cursor.par->ParFromPos(cursor.pos)->text.size());
        }
        else  {
                /* we are in a footnote, so let us move at the beginning */ 
-               /*      while (cursor.par->footnoteflag == LyXParagraph::OPEN_FOOTNOTE)
-                       cursor.par = cursor.par->Previous();
-              
-                       SetCursor(cursor.par, cursor.par->Last()); */
                /* this is just faster than using just CursorLeft() */ 
        
                tmppar = cursor.par;
@@ -409,10 +409,6 @@ void LyXText::CloseFootnote()
    
        tmppar->CloseFootnote(cursor.pos);
 
-       /* set the dimensions of the cursor row */
-       /* row->fill = Fill(row, paperwidth);
-          SetHeightOfRow(row);  */ 
-   
        while (tmppar != endpar) {
                RemoveRow(row->next);
                if (row->next)
@@ -434,19 +430,18 @@ void LyXText::CloseFootnote()
 
 /* used in setlayout */
 // Asger is not sure we want to do this...
-void LyXText::MakeFontEntriesLayoutSpecific(LyXParagraph *par)
+void LyXText::MakeFontEntriesLayoutSpecific(LyXParagraph * par)
 {
        LyXFont layoutfont, tmpfont;
    
-       int pos;
-   
-       LyXLayout* layout = lyxstyle.Style(parameters->textclass, par->GetLayout());
-   
-       for (pos = 0; pos < par->Last(); pos++) {
+       LyXLayout const & layout = textclasslist.Style(parameters->textclass, par->GetLayout());
+
+       for (LyXParagraph::size_type pos = 0;
+            pos < par->Last(); ++pos) {
                if (pos < BeginningOfMainBody(par))
-                       layoutfont = layout->labelfont;
+                       layoutfont = layout.labelfont;
                else
-                       layoutfont = layout->font;
+                       layoutfont = layout.font;
       
                tmpfont = par->GetFontSettings(pos);
                tmpfont.reduce(layoutfont);
@@ -466,12 +461,11 @@ void  LyXText::SetLayout(char layout)
                sel_end_cursor = cursor;
        }
 
-       LyXParagraph *endpar = sel_end_cursor.par->LastPhysicalPar()->Next();
-       LyXParagraph *undoendpar = endpar;
+       LyXParagraph * endpar = sel_end_cursor.par->LastPhysicalPar()->Next();
+       LyXParagraph * undoendpar = endpar;
 
        if (endpar && endpar->GetDepth()) {
                while (endpar && endpar->GetDepth()) {
-                       //endpar->depth = 0;
                        endpar = endpar->LastPhysicalPar()->Next();
                        undoendpar = endpar;
                }
@@ -490,7 +484,7 @@ void  LyXText::SetLayout(char layout)
         * and sel_end cursor */ 
        cursor = sel_start_cursor;
    
-       LyXLayout * lyxlayout = lyxstyle.Style(parameters->textclass, layout);
+       LyXLayout const & lyxlayout = textclasslist.Style(parameters->textclass, layout);
    
        while (cursor.par != sel_end_cursor.par) {
                if (cursor.par->footnoteflag ==
@@ -498,13 +492,13 @@ void  LyXText::SetLayout(char layout)
                        cursor.par->SetLayout(layout);
                        MakeFontEntriesLayoutSpecific(cursor.par);
                        LyXParagraph* fppar = cursor.par->FirstPhysicalPar();
-                       fppar->added_space_top = lyxlayout->fill_top ?
+                       fppar->added_space_top = lyxlayout.fill_top ?
                                VSpace(VSpace::VFILL) : VSpace(VSpace::NONE);
-                       fppar->added_space_bottom = lyxlayout->fill_bottom ? 
+                       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
+                       if (lyxlayout.margintype == MARGIN_MANUAL)
+                               cursor.par->SetLabelWidthString(lyxlayout.labelstring());
+                       if (lyxlayout.labeltype != LABEL_BIBLIO
                            && fppar->bibkey) {
                                delete fppar->bibkey;
                                fppar->bibkey = 0;
@@ -517,13 +511,13 @@ void  LyXText::SetLayout(char layout)
                cursor.par->SetLayout(layout);
                MakeFontEntriesLayoutSpecific(cursor.par);
                LyXParagraph* fppar = cursor.par->FirstPhysicalPar();
-               fppar->added_space_top = lyxlayout->fill_top ?
+               fppar->added_space_top = lyxlayout.fill_top ?
                        VSpace(VSpace::VFILL) : VSpace(VSpace::NONE);
-               fppar->added_space_bottom = lyxlayout->fill_bottom ? 
+               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
+               if (lyxlayout.margintype == MARGIN_MANUAL)
+                       cursor.par->SetLabelWidthString(lyxlayout.labelstring());
+               if (lyxlayout.labeltype != LABEL_BIBLIO
                    && fppar->bibkey) {
                        delete fppar->bibkey;
                        fppar->bibkey = 0;
@@ -555,8 +549,8 @@ void  LyXText::IncDepth()
        }
 
        // We end at the next paragraph with depth 0
-       LyXParagraph *endpar = sel_end_cursor.par->LastPhysicalPar()->Next();
-       LyXParagraph *undoendpar = endpar;
+       LyXParagraph * endpar = sel_end_cursor.par->LastPhysicalPar()->Next();
+       LyXParagraph * undoendpar = endpar;
 
        if (endpar && endpar->GetDepth()) {
                while (endpar && endpar->GetDepth()) {
@@ -584,15 +578,15 @@ void  LyXText::IncDepth()
                // NOTE: you can't change the depth of a bibliography entry
                if (cursor.par->footnoteflag ==
                    sel_start_cursor.par->footnoteflag
-                   && lyxstyle.Style(parameters->textclass,
+                   && textclasslist.Style(parameters->textclass,
                                      cursor.par->GetLayout()
-                                    )->labeltype != LABEL_BIBLIO) {
+                                    ).labeltype != LABEL_BIBLIO) {
                        LyXParagraph * prev = cursor.par->FirstPhysicalPar()->Previous();
                        if (prev 
                            && (prev->GetDepth() - cursor.par->GetDepth() > 0
                                || (prev->GetDepth() == cursor.par->GetDepth()
-                                   && lyxstyle.Style(parameters->textclass,
-                                                     prev->GetLayout())->isEnvironment()))) {
+                                   && textclasslist.Style(parameters->textclass,
+                                                     prev->GetLayout()).isEnvironment()))) {
                                cursor.par->FirstPhysicalPar()->depth++;
                                anything_changed = true;
                                }
@@ -637,8 +631,8 @@ void  LyXText::DecDepth()
                sel_end_cursor = cursor;
        }
    
-       LyXParagraph *endpar = sel_end_cursor.par->LastPhysicalPar()->Next();
-       LyXParagraph *undoendpar = endpar;
+       LyXParagraph * endpar = sel_end_cursor.par->LastPhysicalPar()->Next();
+       LyXParagraph * undoendpar = endpar;
 
        if (endpar && endpar->GetDepth()) {
                while (endpar && endpar->GetDepth()) {
@@ -696,9 +690,8 @@ void  LyXText::SetFont(LyXFont font, bool toggleall)
                        layoutfont = GetFont(cursor.par, -2);
                else
                        layoutfont = GetFont(cursor.par, -1);
-
                // Update current font
-               real_current_font.update(font,toggleall);
+               real_current_font.update(font, toggleall);
 
                // Reduce to implicit settings
                current_font = real_current_font;
@@ -726,8 +719,8 @@ void  LyXText::SetFont(LyXFont font, bool toggleall)
                    == sel_start_cursor.par->footnoteflag) {   /* an open footnote
                                                                * should behave
                                                                * like a closed */
-                       LyXFont newfont = GetFont(cursor.par,cursor.pos);
-                       newfont.update(font,toggleall);
+                       LyXFont newfont = GetFont(cursor.par, cursor.pos);
+                       newfont.update(font, toggleall);
                        SetCharFont(cursor.par, cursor.pos, newfont);
                        cursor.pos++;
                } else {
@@ -751,15 +744,11 @@ void  LyXText::SetFont(LyXFont font, bool toggleall)
 
 void LyXText::RedoHeightOfParagraph(LyXCursor cursor)
 {
-       Row *tmprow;
-       LyXParagraph *first_phys_par;
-       long y;
-   
-       tmprow = cursor.row;
-   
-       y = cursor.y - tmprow->baseline;
+       Row * tmprow = cursor.row;
+       long y = cursor.y - tmprow->baseline;
+
        SetHeightOfRow(tmprow);
-       first_phys_par = tmprow->par->FirstPhysicalPar();
+       LyXParagraph * first_phys_par = tmprow->par->FirstPhysicalPar();
        /* find the first row of the paragraph */
        if (first_phys_par != tmprow->par)
                while (tmprow->previous && tmprow->previous->par != first_phys_par)  {
@@ -783,15 +772,11 @@ void LyXText::RedoHeightOfParagraph(LyXCursor cursor)
 
 void LyXText::RedoDrawingOfParagraph(LyXCursor cursor)
 {
-       Row *tmprow;
-       LyXParagraph *first_phys_par;
-       long y;
+       Row * tmprow = cursor.row;
    
-       tmprow = cursor.row;
-   
-       y = cursor.y - tmprow->baseline;
+       long y = cursor.y - tmprow->baseline;
        SetHeightOfRow(tmprow);
-       first_phys_par = tmprow->par->FirstPhysicalPar();
+       LyXParagraph * first_phys_par = tmprow->par->FirstPhysicalPar();
        /* find the first row of the paragraph */
        if (first_phys_par != tmprow->par)
                while (tmprow->previous && tmprow->previous->par != first_phys_par)  {
@@ -817,15 +802,14 @@ void LyXText::RedoDrawingOfParagraph(LyXCursor cursor)
 /* deletes and inserts again all paragaphs between the cursor
 * and the specified par 
 * This function is needed after SetLayout and SetFont etc. */
-void LyXText::RedoParagraphs(LyXCursor cursor, LyXParagraph *endpar)
+void LyXText::RedoParagraphs(LyXCursor cursor, LyXParagraph * endpar)
 {
-       Row *tmprow, *tmprow2;
-       LyXParagraph *tmppar, *first_phys_par;
-       long y;
+       Row * tmprow2;
+       LyXParagraph * tmppar, * first_phys_par;
    
-       tmprow = cursor.row;
+       Row * tmprow = cursor.row;
    
-       y = cursor.y - tmprow->baseline;
+       long y = cursor.y - tmprow->baseline;
    
        if (!tmprow->previous){
                first_phys_par = FirstParagraph();   // a trick/hack for UNDO
@@ -897,12 +881,6 @@ void LyXText::RedoParagraphs(LyXCursor cursor, LyXParagraph *endpar)
    
        if (tmprow && tmprow->next)
                SetHeightOfRow(tmprow->next);
-   
-       /* restore the correct refresh row  */
-/*    if (refresh_row)
-      refresh_row = refresh_row->next;
-      else
-      refresh_row = firstrow;*/ 
 }
 
 
@@ -1030,9 +1008,9 @@ void  LyXText::CursorBottom()
 /* 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)
+Row * LyXText::GetRowNearY(long & y)
 {
-       Row* tmprow;
+       Row * tmprow;
        long tmpy;
    
        if (currentrow){
@@ -1044,7 +1022,7 @@ Row* LyXText::GetRowNearY(long& y)
                tmpy = 0;
        }
 
-       if (tmpy<=y)
+       if (tmpy<= y)
                while (tmprow->next && tmpy + tmprow->height <= y) {
                        tmpy += tmprow->height;
                        tmprow = tmprow->next;
@@ -1078,7 +1056,7 @@ void LyXText::ToggleFree(LyXFont font, bool toggleall)
        int implicitSelection = SelectWordWhenUnderCursor();
 
        // Set font
-       SetFont(font,toggleall);
+       SetFont(font, toggleall);
        //minibuffer->Set(_("Font style changed"));
 
        /* Implicit selections are cleared afterwards and cursor is set to the
@@ -1092,9 +1070,9 @@ void LyXText::ToggleFree(LyXFont font, bool toggleall)
 }
 
 
-int LyXText::BeginningOfMainBody(LyXParagraph *par)
+LyXParagraph::size_type LyXText::BeginningOfMainBody(LyXParagraph * par) const
 {
-       if (lyxstyle.Style(parameters->textclass, par->GetLayout())->labeltype != LABEL_MANUAL)
+       if (textclasslist.Style(parameters->textclass, par->GetLayout()).labeltype != LABEL_MANUAL)
                return 0;
        else
                return par->BeginningOfMainBody();
@@ -1105,7 +1083,7 @@ int LyXText::BeginningOfMainBody(LyXParagraph *par)
 * in the selection, otherwise just the environment you are in */ 
 void LyXText::MeltFootnoteEnvironment()
 {
-       LyXParagraph *tmppar, *firsttmppar;
+       LyXParagraph * tmppar, * firsttmppar;
    
        ClearSelection();
    
@@ -1128,8 +1106,8 @@ void LyXText::MeltFootnoteEnvironment()
        firsttmppar = cursor.par->ParFromPos(cursor.pos);
        tmppar = firsttmppar;
        /* tmppar is now the paragraph right before the footnote */
-   
-       char first_footnote_par_is_not_empty = tmppar->next->last;
+
+       char first_footnote_par_is_not_empty = tmppar->next->text.size();
    
        while (tmppar->next && tmppar->next->footnoteflag == LyXParagraph::OPEN_FOOTNOTE) {
                tmppar = tmppar->next;         /* I use next instead of Next(),
@@ -1139,8 +1117,8 @@ void LyXText::MeltFootnoteEnvironment()
                tmppar->footnoteflag = LyXParagraph::NO_FOOTNOTE;
       
                /* remember the captions and empty paragraphs */
-               if ((lyxstyle.Style(parameters->textclass,
-                                   tmppar->GetLayout())->labeltype == LABEL_SENSITIVE)
+               if ((textclasslist.Style(parameters->textclass,
+                                   tmppar->GetLayout()).labeltype == LABEL_SENSITIVE)
                    || !tmppar->Last())
                        tmppar->SetLayout(0);
        }
@@ -1168,8 +1146,8 @@ void LyXText::MeltFootnoteEnvironment()
         * (only if the previous par and the footnotepar are not empty!) */
        if ((!firsttmppar->next->GetLayout() && !firsttmppar->next->table)
            || firsttmppar->HasSameLayout(firsttmppar->next)) {
-               if (firsttmppar->last
-                   && !firsttmppar->IsSeparator(firsttmppar->last - 1)
+               if (firsttmppar->text.size()
+                   && !firsttmppar->IsSeparator(firsttmppar->text.size() - 1)
                    && first_footnote_par_is_not_empty) {
                        firsttmppar->next->InsertChar(0, ' ');
                }
@@ -1182,7 +1160,7 @@ void LyXText::MeltFootnoteEnvironment()
        SetCursor(cursor.par, cursor.pos);
    
        /* sometimes it can happen, that there is a counter change */ 
-       Row *row = cursor.row;
+       Row * row = cursor.row;
        while (row->next && row->par != tmppar && row->next->par != tmppar)
                row = row->next;
        UpdateCounters(row);
@@ -1201,21 +1179,19 @@ void LyXText::MeltFootnoteEnvironment()
 void LyXText::SetParagraph(bool line_top, bool line_bottom,
                           bool pagebreak_top, bool pagebreak_bottom,
                           VSpace space_top, VSpace space_bottom,
-                          char align, 
+                          LyXAlignment align, 
                           string labelwidthstring,
                           bool noindent) 
 {
-       LyXCursor tmpcursor;
-       tmpcursor = cursor;
-       LyXParagraph *tmppar;
+       LyXCursor tmpcursor = cursor;
        if (!selection) {
                sel_start_cursor = cursor;
                sel_end_cursor = cursor;
        }
 
        // make sure that the depth behind the selection are restored, too
-       LyXParagraph *endpar = sel_end_cursor.par->LastPhysicalPar()->Next();
-       LyXParagraph *undoendpar = endpar;
+       LyXParagraph * endpar = sel_end_cursor.par->LastPhysicalPar()->Next();
+       LyXParagraph * undoendpar = endpar;
 
        if (endpar && endpar->GetDepth()) {
                while (endpar && endpar->GetDepth()) {
@@ -1232,42 +1208,41 @@ void LyXText::SetParagraph(bool line_top, bool line_bottom,
                undoendpar);
 
        
-       tmppar = sel_end_cursor.par;
-       while (tmppar != sel_start_cursor.par->FirstPhysicalPar()->Previous())
-               {
-                       SetCursor(tmppar->FirstPhysicalPar(), 0);
-                       status = LyXText::NEED_MORE_REFRESH;
-                       refresh_row = cursor.row;
-                       refresh_y = cursor.y - cursor.row->baseline;
-                       if (cursor.par->footnoteflag ==
-                           sel_start_cursor.par->footnoteflag) {
-                               cursor.par->line_top = line_top;
-                               cursor.par->line_bottom = line_bottom;
-                               cursor.par->pagebreak_top = pagebreak_top;
-                               cursor.par->pagebreak_bottom = pagebreak_bottom;
-                               cursor.par->added_space_top = space_top;
-                               cursor.par->added_space_bottom = space_bottom;
-                               /* does the layout allow the new alignment? */ 
-                               if (align == LYX_ALIGN_LAYOUT)
-                                       align = lyxstyle.Style(parameters->textclass, cursor.par->GetLayout())->align;
-                               if (align & lyxstyle.Style(parameters->textclass, cursor.par->GetLayout())->alignpossible) {
-                                       if (align == lyxstyle.Style(parameters->textclass, cursor.par->GetLayout())->align)
-                                               cursor.par->align = LYX_ALIGN_LAYOUT;
-                                       else
-                                               cursor.par->align = align;
-                               }
-                               cursor.par->SetLabelWidthString(labelwidthstring);
-                               cursor.par->noindent = noindent;
+       LyXParagraph * tmppar = sel_end_cursor.par;
+       while (tmppar != sel_start_cursor.par->FirstPhysicalPar()->Previous()){
+               SetCursor(tmppar->FirstPhysicalPar(), 0);
+               status = LyXText::NEED_MORE_REFRESH;
+               refresh_row = cursor.row;
+               refresh_y = cursor.y - cursor.row->baseline;
+               if (cursor.par->footnoteflag ==
+                   sel_start_cursor.par->footnoteflag) {
+                       cursor.par->line_top = line_top;
+                       cursor.par->line_bottom = line_bottom;
+                       cursor.par->pagebreak_top = pagebreak_top;
+                       cursor.par->pagebreak_bottom = pagebreak_bottom;
+                       cursor.par->added_space_top = space_top;
+                       cursor.par->added_space_bottom = space_bottom;
+                               /* does the layout allow the new alignment? */
+                       if (align == LYX_ALIGN_LAYOUT)
+                               align = textclasslist
+                                       .Style(parameters->textclass,
+                                              cursor.par->GetLayout()).align;
+                       if (align & textclasslist
+                           .Style(parameters->textclass,
+                                  cursor.par->GetLayout()).alignpossible) {
+                               if (align == textclasslist
+                                   .Style(parameters->textclass,
+                                          cursor.par->GetLayout()).align)
+                                       cursor.par->align = LYX_ALIGN_LAYOUT;
+                               else
+                                       cursor.par->align = align;
                        }
-          
-                       /* 
-                          tmprow = cursor.row;
-                          while (tmprow->next && tmprow->next->par->previous != cursor.par->LastPhysicalPar())
-                          tmprow = tmprow->next;
-                          SetHeightOfRow(tmprow);
-                          SetHeightOfRow(cursor.row); */ 
-                       tmppar = cursor.par->FirstPhysicalPar()->Previous();
+                       cursor.par->SetLabelWidthString(labelwidthstring);
+                       cursor.par->noindent = noindent;
                }
+               
+               tmppar = cursor.par->FirstPhysicalPar()->Previous();
+       }
        
        RedoParagraphs(sel_start_cursor, endpar);
        
@@ -1281,22 +1256,21 @@ void LyXText::SetParagraph(bool line_top, bool line_bottom,
 
 
 void LyXText::SetParagraphExtraOpt(int type,
-                                   const char *width,
-                                   const char *widthp,
+                                   char const * width,
+                                   char const * widthp,
                                    int alignment, bool hfill,
                                    bool start_minipage)
 {
-       LyXCursor tmpcursor;
-       tmpcursor = cursor;
-       LyXParagraph *tmppar;
+       LyXCursor tmpcursor = cursor;
+       LyXParagraph * tmppar;
        if (!selection) {
                sel_start_cursor = cursor;
                sel_end_cursor = cursor;
        }
 
        // make sure that the depth behind the selection are restored, too
-       LyXParagraph *endpar = sel_end_cursor.par->LastPhysicalPar()->Next();
-       LyXParagraph *undoendpar = endpar;
+       LyXParagraph * endpar = sel_end_cursor.par->LastPhysicalPar()->Next();
+       LyXParagraph * undoendpar = endpar;
 
        if (endpar && endpar->GetDepth()) {
                while (endpar && endpar->GetDepth()) {
@@ -1320,13 +1294,13 @@ void LyXText::SetParagraphExtraOpt(int type,
                 refresh_y = cursor.y - cursor.row->baseline;
                 if (cursor.par->footnoteflag ==
                     sel_start_cursor.par->footnoteflag) {
-                        if (type == PEXTRA_NONE) {
-                                if (cursor.par->pextra_type != PEXTRA_NONE) {
+                        if (type == LyXParagraph::PEXTRA_NONE) {
+                                if (cursor.par->pextra_type != LyXParagraph::PEXTRA_NONE) {
                                         cursor.par->UnsetPExtraType();
-                                        cursor.par->pextra_type=PEXTRA_NONE;
+                                        cursor.par->pextra_type = LyXParagraph::PEXTRA_NONE;
                                 }
                         } else {
-                                cursor.par->SetPExtraType(type,width,widthp);
+                                cursor.par->SetPExtraType(type, width, widthp);
                                 cursor.par->pextra_hfill = hfill;
                                 cursor.par->pextra_start_minipage = start_minipage;
                                 cursor.par->pextra_alignment = alignment;
@@ -1344,13 +1318,13 @@ void LyXText::SetParagraphExtraOpt(int type,
 }
 
 
-static char alphaCounter(int n){
+static char const * alphaCounter(int n){
   static char result[2];
   result[1] = 0;
   if (n == 0)
     return "";
   else {
-    result[0] = 64+n;
+    result[0] = 64 + n;
     if (n > 26)
       return "??";
   }
@@ -1359,17 +1333,17 @@ static char*  alphaCounter(int n){
 
 
 /* set the counter of a paragraph. This includes the labels */ 
-void LyXText::SetCounter(LyXParagraph *par)
+void LyXText::SetCounter(LyXParagraph * par)
 {
        int i;
    
        /* this is only relevant for the beginning of paragraph */ 
        par = par->FirstPhysicalPar();
 
-       LyXLayout* layout = lyxstyle.Style(parameters->textclass, 
+       LyXLayout const & layout = textclasslist.Style(parameters->textclass, 
                                           par->GetLayout());
 
-       LyXTextClass *textclass = lyxstyle.TextClass(parameters->textclass);
+       LyXTextClass const & textclass = textclasslist.TextClass(parameters->textclass);
 
        /* copy the prev-counters to this one, unless this is the start of a 
           footnote or of a bibliography or the very first paragraph */
@@ -1377,17 +1351,17 @@ void LyXText::SetCounter(LyXParagraph *par)
            && !(par->Previous()->footnoteflag == LyXParagraph::NO_FOOTNOTE 
                    && par->footnoteflag == LyXParagraph::OPEN_FOOTNOTE
                    && par->footnotekind == LyXParagraph::FOOTNOTE)
-           && !(lyxstyle.Style(parameters->textclass,
+           && !(textclasslist.Style(parameters->textclass,
                                par->Previous()->GetLayout()
-                               )->labeltype != LABEL_BIBLIO
-                && layout->labeltype == LABEL_BIBLIO)) {
-               for (i=0; i<10; i++) {
+                               ).labeltype != LABEL_BIBLIO
+                && layout.labeltype == LABEL_BIBLIO)) {
+               for (i = 0; i<10; i++) {
                        par->setCounter(i, par->Previous()->GetFirstCounter(i));
                }
                par->appendix = par->Previous()->FirstPhysicalPar()->appendix;
                if (!par->appendix && par->start_of_appendix){
                  par->appendix = true;
-                 for (i=0; i<10; i++) {
+                 for (i = 0; i<10; i++) {
                    par->setCounter(i, 0);
                  }  
                }
@@ -1395,7 +1369,7 @@ void LyXText::SetCounter(LyXParagraph *par)
                par->itemdepth = par->Previous()->FirstPhysicalPar()->itemdepth;
        }
        else {
-               for (i=0; i<10; i++) {
+               for (i = 0; i<10; i++) {
                        par->setCounter(i, 0);
                }  
                par->appendix = par->start_of_appendix;
@@ -1412,9 +1386,9 @@ void LyXText::SetCounter(LyXParagraph *par)
            && par->Previous()
            && par->Previous()->footnoteflag != LyXParagraph::OPEN_FOOTNOTE
            && (par->PreviousBeforeFootnote()
-               && lyxstyle.Style(parameters->textclass,
+               && textclasslist.Style(parameters->textclass,
                                  par->PreviousBeforeFootnote()->GetLayout()
-                                )->labeltype >= LABEL_COUNTER_ENUMI)) {
+                                ).labeltype >= LABEL_COUNTER_ENUMI)) {
                 // Any itemize or enumerate environment in a marginnote
                 // that is embedded in an itemize or enumerate
                 // paragraph is seen by LaTeX as being at a deeper
@@ -1434,14 +1408,14 @@ void LyXText::SetCounter(LyXParagraph *par)
         */
        if (par->Previous()
            && par->Previous()->GetDepth() < par->GetDepth()
-           && lyxstyle.Style(parameters->textclass,
+           && textclasslist.Style(parameters->textclass,
                              par->Previous()->GetLayout()
-                            )->labeltype == LABEL_COUNTER_ENUMI
+                            ).labeltype == LABEL_COUNTER_ENUMI
            && par->enumdepth < 3
            && !(par->Previous()->footnoteflag == LyXParagraph::NO_FOOTNOTE 
                    && par->footnoteflag == LyXParagraph::OPEN_FOOTNOTE
                    && par->footnotekind == LyXParagraph::FOOTNOTE)
-           && layout->labeltype != LABEL_BIBLIO) {
+           && layout.labeltype != LABEL_BIBLIO) {
                par->enumdepth++;
        }
 
@@ -1451,24 +1425,24 @@ void LyXText::SetCounter(LyXParagraph *par)
            && !(par->Previous()->footnoteflag == LyXParagraph::NO_FOOTNOTE
                    && par->footnoteflag == LyXParagraph::OPEN_FOOTNOTE
                    && par->footnotekind == LyXParagraph::FOOTNOTE)
-           && layout->labeltype != LABEL_BIBLIO) {
+           && layout.labeltype != LABEL_BIBLIO) {
                par->enumdepth = par->DepthHook(par->GetDepth())->enumdepth;
                par->setCounter(6 + par->enumdepth,
                        par->DepthHook(par->GetDepth())->getCounter(6 + par->enumdepth));
                /* reset the counters.
                 * A depth change is like a breaking layout
                 */
-               for (i=6 + par->enumdepth + 1; i<10;i++)
+               for (i = 6 + par->enumdepth + 1; i<10;i++)
                        par->setCounter(i, 0);
        }
    
        if (!par->labelstring.empty()) {
-               par->labelstring.erase();
+               par->labelstring.clear();
        }
    
-       if (layout->margintype == MARGIN_MANUAL) {
+       if (layout.margintype == MARGIN_MANUAL) {
                if (par->labelwidthstring.empty()) {
-                       par->SetLabelWidthString(layout->labelstring);
+                       par->SetLabelWidthString(layout.labelstring());
                }
        }
        else {
@@ -1476,31 +1450,31 @@ void LyXText::SetCounter(LyXParagraph *par)
        }
    
        /* is it a layout that has an automatic label ? */ 
-       if (layout->labeltype >=  LABEL_FIRST_COUNTER) {
+       if (layout.labeltype >=  LABEL_FIRST_COUNTER) {
       
-               i = layout->labeltype - LABEL_FIRST_COUNTER;
-               if (i>=0 && i<=parameters->secnumdepth) {
+               i = layout.labeltype - LABEL_FIRST_COUNTER;
+               if (i >= 0 && i<= parameters->secnumdepth) {
                        par->incCounter(i);     // increment the counter  
         
                        char * s = new char[50];
 
                        // Is there a label? Useful for Chapter layout
                        if (!par->appendix){
-                               if (!layout->labelstring.empty())
-                                       par->labelstring = layout->labelstring;
+                               if (!layout.labelstring().empty())
+                                       par->labelstring = layout.labelstring();
                                else
-                                       par->labelstring.erase();
+                                       par->labelstring.clear();
                         }
                        else {
-                               if (!layout->labelstring_appendix.empty())
-                                       par->labelstring = layout->labelstring_appendix;
+                               if (!layout.labelstring_appendix().empty())
+                                       par->labelstring = layout.labelstring_appendix();
                                else
-                                       par->labelstring.erase();
+                                       par->labelstring.clear();
                        }
  
                        if (!par->appendix){
                                switch (2 * LABEL_FIRST_COUNTER -
-                                       textclass->maxcounter + i) {
+                                       textclass.maxcounter() + i) {
                                case LABEL_COUNTER_CHAPTER:
                                        sprintf(s, "%d",
                                                par->getCounter(i));
@@ -1546,7 +1520,7 @@ void LyXText::SetCounter(LyXParagraph *par)
                                }
                        }
                        else {
-                               switch (2 * LABEL_FIRST_COUNTER - textclass->maxcounter+ i) {
+                               switch (2 * LABEL_FIRST_COUNTER - textclass.maxcounter() + i) {
                                case LABEL_COUNTER_CHAPTER:
                                        sprintf(s, "%s",
                                                alphaCounter(par->getCounter(i)));
@@ -1599,12 +1573,12 @@ void LyXText::SetCounter(LyXParagraph *par)
                                /* reset the following counters  */
                                par->setCounter(i, 0);
                        }
-               } else if (layout->labeltype < LABEL_COUNTER_ENUMI) {
+               } else if (layout.labeltype < LABEL_COUNTER_ENUMI) {
                        for (i++; i<10; i++) {
                                /* reset the following counters  */
                                par->setCounter(i, 0);
                        }
-               } else if (layout->labeltype == LABEL_COUNTER_ENUMI) {
+               } else if (layout.labeltype == LABEL_COUNTER_ENUMI) {
                        par->incCounter(i + par->enumdepth);
                        char * s = new char[25];
                        int number = par->getCounter(i + par->enumdepth);
@@ -1646,23 +1620,23 @@ void LyXText::SetCounter(LyXParagraph *par)
                                par->setCounter(i, 0);  /* reset the following counters  */
         
                } 
-       } else if (layout->labeltype == LABEL_BIBLIO) {// ale970302
+       } else if (layout.labeltype == LABEL_BIBLIO) {// ale970302
            i = LABEL_COUNTER_ENUMI - LABEL_FIRST_COUNTER + par->enumdepth;
            par->incCounter(i);
            int number = par->getCounter(i);
            if (!par->bibkey)
              par->bibkey = new InsetBibKey();
            par->bibkey->setCounter(number);
-           par->labelstring = layout->labelstring;
+           par->labelstring = layout.labelstring();
            
            // In biblio should't be following counters but...
        }                                               
        else  {
-               string s = layout->labelstring;
+               string s = layout.labelstring();
       
                /* the caption hack: */
       
-               if (layout->labeltype == LABEL_SENSITIVE) {
+               if (layout.labeltype == LABEL_SENSITIVE) {
                        if (par->footnoteflag != LyXParagraph::NO_FOOTNOTE
                            && (par->footnotekind == LyXParagraph::FIG
                            || par->footnotekind == LyXParagraph::WIDE_FIG))
@@ -1685,7 +1659,7 @@ void LyXText::SetCounter(LyXParagraph *par)
       
                /* reset the enumeration counter. They are always resetted
                 * when there is any other layout between */ 
-               for (i=6 + par->enumdepth; i<10;i++)
+               for (i = 6 + par->enumdepth; i<10;i++)
                        par->setCounter(i, 0);
        }
 }
@@ -1693,9 +1667,9 @@ void LyXText::SetCounter(LyXParagraph *par)
 
 /* Updates all counters BEHIND the row. Changed paragraphs
 * with a dynamic left margin will be rebroken. */ 
-void LyXText::UpdateCounters(Row *row)
+void LyXText::UpdateCounters(Row * row)
 {
-       LyXParagraph *par;
+       LyXParagraph * par;
        if (!row) {
                row = firstrow;
                par = row->par;
@@ -1717,8 +1691,8 @@ void LyXText::UpdateCounters(Row *row)
                /* now  check for the headline layouts. remember that they
                 * have a dynamic left margin */ 
                if (!par->IsDummy()
-                   && ( lyxstyle.Style(parameters->textclass, par->layout)->margintype == MARGIN_DYNAMIC
-                        || lyxstyle.Style(parameters->textclass, par->layout)->labeltype == LABEL_SENSITIVE)
+                   && ( textclasslist.Style(parameters->textclass, par->layout).margintype == MARGIN_DYNAMIC
+                        || textclasslist.Style(parameters->textclass, par->layout).labeltype == LABEL_SENSITIVE)
                        ){
         
                        /* Rebreak the paragraph */ 
@@ -1751,16 +1725,16 @@ void LyXText::InsertInset(Inset *inset)
        SetUndo(Undo::INSERT, 
                cursor.par->ParFromPos(cursor.pos)->previous, 
                cursor.par->ParFromPos(cursor.pos)->next);
-       cursor.par->InsertChar(cursor.pos, LYX_META_INSET);
+       cursor.par->InsertChar(cursor.pos, LyXParagraph::META_INSET);
        cursor.par->InsertInset(cursor.pos, inset);
-       InsertChar(LYX_META_INSET);  /* just to rebreak and refresh correctly.
+       InsertChar(LyXParagraph::META_INSET);  /* just to rebreak and refresh correctly.
                                      * The character will not be inserted a
                                      * second time */
 }
 
 
 /* this is for the simple cut and paste mechanism */ 
-static LyXParagraph *simple_cut_buffer = 0;
+static LyXParagraph * simple_cut_buffer = 0;
 static char simple_cut_buffer_textclass = 0;
 
 void DeleteSimpleCutBuffer()
@@ -1799,8 +1773,7 @@ void LyXText::CutSelection(bool doclear)
    
        /* OK, we have a selection. This is always between sel_start_cursor
         * and sel_end cursor */
-       LyXParagraph *tmppar;
-       int i;
+       LyXParagraph * tmppar;
    
        /* Check whether there are half footnotes in the selection */
        if (sel_start_cursor.par->footnoteflag != LyXParagraph::NO_FOOTNOTE
@@ -1826,8 +1799,8 @@ void LyXText::CutSelection(bool doclear)
        /* table stuff -- end*/
 
        // make sure that the depth behind the selection are restored, too
-       LyXParagraph *endpar = sel_end_cursor.par->LastPhysicalPar()->Next();
-       LyXParagraph *undoendpar = endpar;
+       LyXParagraph * endpar = sel_end_cursor.par->LastPhysicalPar()->Next();
+       LyXParagraph * undoendpar = endpar;
 
        if (endpar && endpar->GetDepth()) {
                while (endpar && endpar->GetDepth()) {
@@ -1871,13 +1844,13 @@ cut and pasted in a functional manner.
 This is not implemented yet.
 */
 
-       char space_wrapped =
+       bool space_wrapped =
                sel_end_cursor.par->IsLineSeparator(sel_end_cursor.pos);
        if (sel_end_cursor.pos > 0
            && sel_end_cursor.par->IsLineSeparator(sel_end_cursor.pos - 1)) {
                sel_end_cursor.pos--;  /* please break before a space at
                                        * the end */
-               space_wrapped = True;
+               space_wrapped = true;
        }
 
        // cut behind a space if there is one
@@ -1893,8 +1866,10 @@ This is not implemented yet.
        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();
-               for (i=sel_start_cursor.pos; i< sel_end_cursor.pos; i++){
+               simple_cut_buffer = new LyXParagraph;
+               LyXParagraph::size_type i =
+                       sel_start_cursor.pos;
+               for (; i< sel_end_cursor.pos; i++){
                        /* table stuff -- begin*/
                        if (sel_start_cursor.par->table
                            && sel_start_cursor.par->IsNewline(sel_start_cursor.pos)){
@@ -2007,8 +1982,7 @@ This is not implemented yet.
     
 void LyXText::CopySelection()
 {
-       int i=0;
-   
+       LyXParagraph::size_type i = 0;
        /* this doesnt make sense, if there is no selection */ 
        if (!selection) {
                return;
@@ -2016,7 +1990,7 @@ void LyXText::CopySelection()
 
        /* ok we have a selection. This is always between sel_start_cursor
         * and sel_end cursor */
-       LyXParagraph *tmppar;
+       LyXParagraph * tmppar;
    
        /* check wether there are half footnotes in the selection */
        if (sel_start_cursor.par->footnoteflag != LyXParagraph::NO_FOOTNOTE
@@ -2057,8 +2031,8 @@ void LyXText::CopySelection()
        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();
-               for (i=sel_start_cursor.pos; i< sel_end_cursor.pos; i++){
+               simple_cut_buffer = new LyXParagraph;
+               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);
                }
@@ -2075,7 +2049,7 @@ void LyXText::CopySelection()
                        tmppar = tmppar->next;
                        tmppar2->next = tmppar->Clone();
                        tmppar2->next->previous = tmppar2;
-                       tmppar2=tmppar2->next;
+                       tmppar2 = tmppar2->next;
                }
                tmppar2->next = 0;
 
@@ -2089,19 +2063,17 @@ void LyXText::CopySelection()
                }
      
                /* the simple_cut_buffer paragraph is too big */
-               int tmpi2;
-
-               tmpi2 = sel_start_cursor.par->PositionInParFromPos(sel_start_cursor.pos);
+               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->last > tmpi2) {
-                       tmppar2->Erase(tmppar2->last-1);
+               while (tmppar2->size() > tmpi2) {
+                       tmppar2->Erase(tmppar2->text.size() - 1);
                }
-
        }
 }
           
@@ -2112,8 +2084,8 @@ void LyXText::PasteSelection()
        if (!simple_cut_buffer)
                return;
 
-       LyXParagraph *tmppar;
-       LyXParagraph *endpar;
+       LyXParagraph * tmppar;
+       LyXParagraph * endpar;
 
        LyXCursor tmpcursor;
 
@@ -2162,7 +2134,8 @@ void LyXText::PasteSelection()
                /* table stuff -- begin*/
                bool table_too_small = false;
                if (tmpcursor.par->table) {
-                       while (simple_cut_buffer->last && !table_too_small){
+                       while (simple_cut_buffer->text.size()
+                              && !table_too_small) {
                                if (simple_cut_buffer->IsNewline(0)){
                                        while(tmpcursor.pos < tmpcursor.par->Last() && !tmpcursor.par->IsNewline(tmpcursor.pos))
                                                tmpcursor.pos++;
@@ -2180,7 +2153,7 @@ void LyXText::PasteSelection()
                        }
                } else {
                        /* table stuff -- end*/
-                       while (simple_cut_buffer->last){
+                       while (simple_cut_buffer->text.size()){
                                simple_cut_buffer->CutIntoMinibuffer(0);
                                simple_cut_buffer->Erase(0);
                                tmpcursor.par->InsertFromMinibuffer(tmpcursor.pos);
@@ -2196,8 +2169,8 @@ void LyXText::PasteSelection()
 
                /* make a copy of the simple cut_buffer */
                tmppar = simple_cut_buffer;
-               LyXParagraph *simple_cut_clone = tmppar->Clone();
-               LyXParagraph *tmppar2 = simple_cut_clone;
+               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;
@@ -2206,7 +2179,7 @@ void LyXText::PasteSelection()
                        tmppar = tmppar->next;
                        tmppar2->next = tmppar->Clone();
                        tmppar2->next->previous = tmppar2;
-                       tmppar2=tmppar2->next;
+                       tmppar2 = tmppar2->next;
                        if (cursor.par->footnoteflag){
                                tmppar->footnoteflag = cursor.par->footnoteflag;
                                tmppar->footnotekind = cursor.par->footnotekind;
@@ -2225,12 +2198,12 @@ void LyXText::PasteSelection()
                /* find the end of the buffer */ 
                LyXParagraph *lastbuffer = simple_cut_buffer;
                while (lastbuffer->Next())
-                       lastbuffer=lastbuffer->Next();
+                       lastbuffer = lastbuffer->Next();
      
                /* find the physical end of the buffer */ 
                lastbuffer = simple_cut_buffer;
                while (lastbuffer->Next())
-                       lastbuffer=lastbuffer->Next();
+                       lastbuffer = lastbuffer->Next();
      
                /* please break behind a space, if there is one. The space 
                 * should be copied too */
@@ -2250,7 +2223,7 @@ void LyXText::PasteSelection()
                if ((!cursor.par->Last()
                     || cursor.par->IsLineSeparator(cursor.pos - 1)
                     || cursor.par->IsNewline(cursor.pos - 1))
-                   && simple_cut_buffer->last
+                   && simple_cut_buffer->text.size()
                    && simple_cut_buffer->IsLineSeparator(0))
                        simple_cut_buffer->Erase(0);
      
@@ -2336,24 +2309,24 @@ void LyXText::PasteSelection()
    
 
 /* returns a pointer to the very first LyXParagraph */ 
-LyXParagraph* LyXText::FirstParagraph()
+LyXParagraph * LyXText::FirstParagraph()
 {
        return params->paragraph;
 }
 
 
 /* returns true if the specified string is at the specified position */
-bool LyXText::IsStringInText(LyXParagraph *par, int pos, char const* string)
+bool LyXText::IsStringInText(LyXParagraph * par,
+                            LyXParagraph::size_type pos,
+                            char const * str)
 {
        if (par) {
                int i = 0;
-               while (pos+i < par->Last() && string[i] && 
-                      string[i]==par->GetChar(pos+i))
-               {
-                       i++;
+               while (pos + i < par->Last() && str[i] && 
+                      str[i] == par->GetChar(pos + i)) {
+                       ++i;
                }
-
-               if (!string[i])
+               if (!str[i])
                        return true;
        }
        return false;
@@ -2361,18 +2334,17 @@ bool LyXText::IsStringInText(LyXParagraph *par, int pos, char const* string)
 
 
 /* sets the selection over the number of characters of string, no check!! */
-void LyXText::SetSelectionOverString(char const* string)
+void LyXText::SetSelectionOverString(char const * string)
 {
        sel_cursor = cursor;
-       int i;
-       for (i=0; string[i]; i++)
+       for (int i = 0; string[i]; ++i)
                CursorRight();
        SetSelection();
 }
 
 
 /* simple replacing. The font of the first selected character is used */
-void LyXText::ReplaceSelectionWithString(char const* string)
+void LyXText::ReplaceSelectionWithString(char const * str)
 {
        SetCursorParUndo();
        FreezeUndo();
@@ -2383,14 +2355,14 @@ void LyXText::ReplaceSelectionWithString(char const* string)
        }
 
        // Get font setting before we cut
-       int pos = sel_end_cursor.pos;
+       LyXParagraph::size_type pos = sel_end_cursor.pos;
        LyXFont font = sel_start_cursor.par->GetFontSettings(sel_start_cursor.pos);
 
        // Insert the new string
-       for (int i=0; string[i];i++) {
-               sel_end_cursor.par->InsertChar(pos, string[i]);
+       for (int i = 0; str[i]; ++i) {
+               sel_end_cursor.par->InsertChar(pos, str[i]);
                sel_end_cursor.par->SetFont(pos, font);
-               pos++;
+               ++pos;
        }
 
        // Cut the selection
@@ -2402,21 +2374,20 @@ void LyXText::ReplaceSelectionWithString(char const* string)
 
 /* if the string can be found: return true and set the cursor to
  * the new position */
-bool LyXText::SearchForward(char const* string)
+bool LyXText::SearchForward(char const * str)
 {
-       LyXParagraph *par = cursor.par;
-       int pos = cursor.pos;
-
-       while (par && !IsStringInText(par,pos,string)) {
-               if (pos<par->Last()-1)
-                       pos++;
+       LyXParagraph * par = cursor.par;
+       LyXParagraph::size_type pos = cursor.pos;
+       while (par && !IsStringInText(par, pos, str)) {
+               if (pos < par->Last() - 1)
+                       ++pos;
                else {
                        pos = 0;
                        par = par->Next();
                }
        }
        if (par) {
-               SetCursor(par,pos);
+               SetCursor(par, pos);
                return true;
        }
        else
@@ -2424,9 +2395,9 @@ bool LyXText::SearchForward(char const* string)
 }
 
 
-bool LyXText::SearchBackward(char const* string)
+bool LyXText::SearchBackward(char const * string)
 {
-       LyXParagraph *par = cursor.par;
+       LyXParagraph * par = cursor.par;
        int pos = cursor.pos;
 
        do {
@@ -2440,10 +2411,10 @@ bool LyXText::SearchBackward(char const* string)
                                        pos = par->Last()-1;
                        } while (par && pos<0);
                }
-       } while (par && !IsStringInText(par,pos,string));
+       } while (par && !IsStringInText(par, pos, string));
   
        if (par) {
-               SetCursor(par,pos);
+               SetCursor(par, pos);
                return true;
        }
        else
@@ -2451,159 +2422,178 @@ bool LyXText::SearchBackward(char const* string)
 }
 
 
+void LyXText::InsertStringA(LyXParagraph::TextContainer const & text)
+{
+       char * str = new char[text.size() + 1];
+       copy(text.begin(), text.end(), str);
+       str[text.size()] = '\0';
+       InsertStringA(str);
+       delete [] str;
+}
+
+
 /* needed to insert the selection */
-void LyXText::InsertStringA(char* string)
+void LyXText::InsertStringA(char const * s)
 {
-       LyXParagraph *par = cursor.par;
-       int pos = cursor.pos;
-       int a = 0;
+       string str(s);
+       LyXParagraph * par = cursor.par;
+       LyXParagraph::size_type pos = cursor.pos;
+       LyXParagraph::size_type a = 0;
         int cell = 0;
-       LyXParagraph *endpar = cursor.par->Next();
+       LyXParagraph * endpar = cursor.par->Next();
 
        SetCursorParUndo();
 
-       char flag = lyxstyle.Style(parameters->textclass, 
-                                  cursor.par->GetLayout())->isEnvironment();
+       char flag = textclasslist.Style(parameters->textclass, 
+                                  cursor.par->GetLayout()).isEnvironment();
        /* only to be sure, should not be neccessary */ 
        ClearSelection();
    
        /* insert the string, don't insert doublespace */ 
-       int i=0;
-       int i2 = 0;
-
-       for (i2=i;string[i2]&&string[i2]!='\n';i2++);
-       par->Enlarge(pos, i2 - i);
-       while (string[i]) {
-               if (string[i]!='\n') {
-                       if (string[i]==' ' && (string[i+1]!=' ')
-                           && pos && par->GetChar(pos-1)!=' ') {
+       string::size_type i = 0;
+       while (i < str.length()) {
+               if (str[i] != '\n') {
+                       if (str[i] == ' ' 
+                           && i+1<str.length() && str[i+1]!= ' '
+                           && pos && par->GetChar(pos-1)!= ' ') {
                                par->InsertChar(pos,' ');
                                pos++;
                        }
                         else if (par->table) {
-                            if (string[i] == '\t') {
-                                while((pos < par->last) &&
-                                      (par->GetChar(pos) != LYX_META_NEWLINE))
-                                        pos++;
-                                if (pos < par->last)
-                                        pos++;
+                            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 ((string[i] != 13) &&
-                                (((unsigned char) string[i] & 127) >= ' ')) {
-                                par->InsertChar(pos,string[i]);
+                            } else if ((str[i] != 13) &&
+                                ((str[i] & 127) >= ' ')) {
+                                par->InsertChar(pos, str[i]);
                                 pos++;
                             }
                         }
-                       else if (string[i]==' ') {
-                               par->InsertChar(pos,LYX_META_PROTECTED_SEPARATOR);
+                       else if (str[i] == ' ') {
+                               par->InsertChar(pos, LyXParagraph::META_PROTECTED_SEPARATOR);
                                pos++;
                        }
-                       else if (string[i]=='\t') {
-                               for (a=pos; a<(pos/8+1)*8 ; a++) {
-                                       par->InsertChar(a,LYX_META_PROTECTED_SEPARATOR);
+                       else if (str[i] == '\t') {
+                               for (a = pos; a < (pos/8 + 1) * 8 ; ++a) {
+                                       par->InsertChar(a, LyXParagraph::META_PROTECTED_SEPARATOR);
                                }
                                pos = a;
                        }
-                       else if (string[i]!=13 && 
+                       else if (str[i]!= 13 && 
                                 // Ignore unprintables
-                                ((unsigned char) string[i] & 127) >= ' ') {
-                               par->InsertChar(pos,string[i]);
+                                (str[i] & 127) >= ' ') {
+                               par->InsertChar(pos, str[i]);
                                pos++;
                        }
                } else {
                         if (par->table) {
-                                if (!string[i+1]) {
+                                if (i+1>=str.length()) {
                                         pos++;
                                         break;
                                 }
-                                while((pos < par->last) &&
-                                      (par->GetChar(pos) != LYX_META_NEWLINE))
+                                while((pos < par->size()) &&
+                                      (par->GetChar(pos) != LyXParagraph::META_NEWLINE))
                                         pos++;
                                 pos++;
-                                cell=NumberOfCell(par,pos);
-                                while((pos < par->last) &&
+                                cell = NumberOfCell(par, pos);
+                                while((pos < par->size()) &&
                                       !(par->table->IsFirstCell(cell))) {
-                                        while((pos < par->last) &&
-                                              (par->GetChar(pos) != LYX_META_NEWLINE))
-                                                pos++;
-                                        pos++;
-                                        cell=NumberOfCell(par,pos);
+                                        while((pos < par->size()) &&
+                                              (par->GetChar(pos) != LyXParagraph::META_NEWLINE))
+                                                ++pos;
+                                        ++pos;
+                                        cell = NumberOfCell(par, pos);
                                 }
-                                if (pos >= par->last)
+                                if (pos >= par->size())
                                         // no more fields to fill skip the rest
                                         break;
                         } else {
-                                if (!par->last) {
-                                        par->InsertChar(pos,LYX_META_PROTECTED_SEPARATOR);
+                                if (!par->text.size()) {
+                                        par->InsertChar(pos, LyXParagraph::META_PROTECTED_SEPARATOR);
                                         pos++;
                                 }
                                 par->BreakParagraph(pos, flag);
                                 par = par->Next();
                                 pos = 0;
                         }
-                       for (i2=i;string[i2]&&string[i2]!='\n';i2++);
-                       par->Enlarge(pos, i2 - i);
                }
       
                i++;
        }
    
-       RedoParagraphs(cursor,endpar);
+       RedoParagraphs(cursor, endpar);
        SetCursor(cursor.par, cursor.pos);
        sel_cursor = cursor;
        SetCursor(par, pos);
        SetSelection();
 }
 
+
+void LyXText::InsertStringB(LyXParagraph::TextContainer const & text)
+{
+       char * str = new char[text.size() + 1];
+       copy(text.begin(), text.end(), str);
+       str[text.size()] = '\0';
+       InsertStringB(str);
+       delete [] str;
+}
+
+
 /* turns double-CR to single CR, others where converted into one blank and 13s 
  * that are ignored .Double spaces are also converted into one. Spaces at
  * the beginning of a paragraph are forbidden. tabs are converted into one
  * space. then InsertStringA is called */ 
-void LyXText::InsertStringB(char* string)
-{
-       LyXParagraph *par = cursor.par;
-       int i=1;
-       while (string[i]) {
-               if (string[i]=='\t' && !par->table)
-                       string[i] = ' ';
-               if (string[i]==' ' && string[i+1]==' ')
-                       string[i] = 13;
-               if (string[i]=='\n' && string[i+1] && !par->table){
-                       if (string[i+1]!='\n') {
-                               if (string[i-1]!=' ')
-                                       string[i]=' ';
+void LyXText::InsertStringB(char const * s)
+{
+       string str(s);
+       LyXParagraph * par = cursor.par;
+       string::size_type i = 1;
+       while (i < str.length()) {
+               if (str[i] == '\t' && !par->table)
+                       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 + 1] != '\n') {
+                               if (str[i - 1] != ' ')
+                                       str[i] = ' ';
                                else
-                                       string[i]= 13;
+                                       str[i] = 13;
                        }
-                       while (string[i+1] && (string[i+1]==' '
-                                              || string[i+1]=='\t'
-                                              || string[i+1]=='\n'
-                                              || string[i+1]==13)) {
-                               string[i+1]=13;
-                               i++;
+                       while (i+1 < str.length() 
+                              && (str[i + 1] == ' ' 
+                                  || str[i + 1] == '\t'
+                                  || str[i + 1] == '\n' 
+                                  || str[i + 1] == 13)) {
+                               str[i + 1] = 13;
+                               ++i;
                        }
                }
-               i++;
+               ++i;
        }
-       InsertStringA(string);
+       InsertStringA(str.c_str());
 }
 
 
 bool LyXText::GotoNextError()
 {
-       LyXCursor res=cursor;
+       LyXCursor res = cursor;
        do {
-               if (res.pos < res.par->Last()-1) {
+               if (res.pos < res.par->Last() - 1) {
                        res.pos++;
                }
                else  {
-                       res.par=res.par->Next();
+                       res.par = res.par->Next();
                        res.pos = 0;
                }
       
        } while (res.par && 
-                !(res.par->GetChar(res.pos)==LYX_META_INSET
+                !(res.par->GetChar(res.pos) == LyXParagraph::META_INSET
                   && res.par->GetInset(res.pos)->AutoDelete()));
    
        if (res.par) {
@@ -2617,19 +2607,19 @@ bool LyXText::GotoNextError()
 
 bool LyXText::GotoNextNote()
 {
-       LyXCursor res=cursor;
+       LyXCursor res = cursor;
        do {
                if (res.pos < res.par->Last()-1) {
                        res.pos++;
                }
                else  {
-                       res.par=res.par->Next();
+                       res.par = res.par->Next();
                        res.pos = 0;
                }
       
        } while (res.par && 
-                !(res.par->GetChar(res.pos)==LYX_META_INSET
-                  && res.par->GetInset(res.pos)->LyxCode()==Inset::IGNORE_CODE));
+                !(res.par->GetChar(res.pos) == LyXParagraph::META_INSET
+                  && res.par->GetInset(res.pos)->LyxCode() == Inset::IGNORE_CODE));
    
        if (res.par) {
                SetCursor(res.par, res.pos);
@@ -2641,30 +2631,34 @@ bool LyXText::GotoNextNote()
 
 
 int LyXText::SwitchLayoutsBetweenClasses(char class1, char class2,
-                                        LyXParagraph *par)
+                                        LyXParagraph * par)
 {
-       InsetError * new_inset = 0;
        int ret = 0;
        if (!par || class1 == class2)
                return ret;
        par = par->FirstPhysicalPar();
        while (par) {
-               string name = lyxstyle.NameOfLayout(class1, par->layout);
-               int lay = lyxstyle.NumberOfLayout(class2, name);
-               if (lay == -1) // layout not found
-                       // use default layout "Stadard" (0)
+               string name = textclasslist.NameOfLayout(class1, par->layout);
+               int lay = 0;
+               pair<bool, LyXTextClass::LayoutList::size_type> pp =
+                       textclasslist.NumberOfLayout(class2, name);
+               if (pp.first) {
+                       lay = pp.second;
+               } else { // layout not found
+                       // use default layout "Standard" (0)
                        lay = 0;
+               }
                par->layout = lay;
       
-               if (name != lyxstyle.NameOfLayout(class2, par->layout)) {
+               if (name != textclasslist.NameOfLayout(class2, par->layout)) {
                        ret++;
-                       string s= "Layout had to be changed from\n"
-                               + name + " to " + lyxstyle.NameOfLayout(class2, par->layout)
+                       string s = "Layout had to be changed from\n"
+                               + name + " to " + textclasslist.NameOfLayout(class2, par->layout)
                                + "\nbecause of class conversion from\n"
-                               + lyxstyle.NameOfClass(class1) + " to "
-                               + lyxstyle.NameOfClass(class2);
-                       new_inset = new InsetError(s);
-                       par->InsertChar(0, LYX_META_INSET);
+                               + textclasslist.NameOfClass(class1) + " to "
+                               + textclasslist.NameOfClass(class2);
+                       InsetError * new_inset = new InsetError(s);
+                       par->InsertChar(0, LyXParagraph::META_INSET);
                        par->InsertInset(0, new_inset);
                }
       
@@ -2674,7 +2668,8 @@ int LyXText::SwitchLayoutsBetweenClasses(char class1, char class2,
 }
 
 
-void LyXText::CheckParagraph(LyXParagraph* par, int pos)
+void LyXText::CheckParagraph(LyXParagraph * par,
+                            LyXParagraph::size_type pos)
 {
   
        LyXCursor tmpcursor;
@@ -2688,9 +2683,8 @@ void LyXText::CheckParagraph(LyXParagraph* par, int pos)
                /* table stuff -- end*/
      
                long y = 0;
-               int z;
-     
-               Row* row = GetRow(par, pos, y);
+               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) {
@@ -2712,7 +2706,7 @@ void LyXText::CheckParagraph(LyXParagraph* par, int pos)
                }
 
                int tmpheight = row->height;
-               int tmplast = RowLast(row);
+               LyXParagraph::size_type tmplast = RowLast(row);
                refresh_y = y;
                refresh_row = row;
 
@@ -2723,7 +2717,7 @@ void LyXText::CheckParagraph(LyXParagraph* par, int pos)
                        status = LyXText::NEED_MORE_REFRESH; 
    
                /* check the special right address boxes */
-               if (lyxstyle.Style(parameters->textclass, par->GetLayout())->margintype == MARGIN_RIGHT_ADDRESS_BOX) {
+               if (textclasslist.Style(parameters->textclass, par->GetLayout()).margintype == MARGIN_RIGHT_ADDRESS_BOX) {
                        tmpcursor.par = par;
                        tmpcursor.row = row;
                        tmpcursor.y = y;
@@ -2755,13 +2749,10 @@ void LyXText::CheckParagraph(LyXParagraph* par, int pos)
 
 
 /* returns 0 if inset wasn't found */
-int LyXText::UpdateInset(Inset* inset)
+int LyXText::UpdateInset(Inset * inset)
 {
-       int pos;
-       LyXParagraph *par;
-
        /* first check the current paragraph */
-       pos = cursor.par->GetPositionOfInset(inset);
+       int pos = cursor.par->GetPositionOfInset(inset);
        if (pos != -1){
                CheckParagraph(cursor.par, pos);
                return 1;
@@ -2769,7 +2760,7 @@ int LyXText::UpdateInset(Inset* inset)
   
        /* check every paragraph */
   
-       par = FirstParagraph();
+       LyXParagraph * par = FirstParagraph();
        do {
                /* make sure the paragraph is open */
                if (par->footnoteflag != LyXParagraph::CLOSED_FOOTNOTE){
@@ -2786,7 +2777,8 @@ int LyXText::UpdateInset(Inset* inset)
 }
 
 
-void LyXText::SetCursor(LyXParagraph *par, int pos)
+void LyXText::SetCursor(LyXParagraph * par,
+                       LyXParagraph::size_type pos)
 {
        LyXCursor old_cursor = cursor;
        SetCursorIntern(par, pos);
@@ -2794,12 +2786,12 @@ void LyXText::SetCursor(LyXParagraph *par, int pos)
 }
 
 
-void LyXText::SetCursorIntern(LyXParagraph *par, int pos)
+void LyXText::SetCursorIntern(LyXParagraph * par, LyXParagraph::size_type pos)
 {
        long y;
-       Row *row;
+       Row * row;
        int left_margin;
-       LyXParagraph *tmppar;
+       LyXParagraph * tmppar;
    
        /* correct the cursor position if impossible */
        if (pos > par->Last()){
@@ -2807,16 +2799,20 @@ void LyXText::SetCursorIntern(LyXParagraph *par, int pos)
                pos = par->PositionInParFromPos(pos);
                par = tmppar;
        }
-       if (par->IsDummy() && par->previous && 
+       if (par->IsDummy() && par->previous &&
            par->previous->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE) {
-               while (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->text.size() + 1;
                }
                if (par->previous) {
                        par = par->previous;
                }
-               pos += par->last + 1;
+               pos += par->text.size() + 1;
        }
 
        cursor.par = par;
@@ -2835,8 +2831,8 @@ void LyXText::SetCursorIntern(LyXParagraph *par, int pos)
        float fill_separator, fill_hfill, fill_label_hfill;
        left_margin = LabelEnd(row);
        PrepareToPrint(row, x, fill_separator, fill_hfill, fill_label_hfill);
-       int main_body = BeginningOfMainBody(row->par);
-      
+       LyXParagraph::size_type main_body =
+               BeginningOfMainBody(row->par);
        /* table stuff -- begin*/
        if (row->par->table) {
                int cell = NumberOfCell(row->par, row->pos);
@@ -2859,7 +2855,7 @@ void LyXText::SetCursorIntern(LyXParagraph *par, int pos)
                        if (pos && pos == main_body
                            && !row->par->IsLineSeparator(pos - 1)) {
                                x += GetFont(row->par, -2).stringWidth(
-                                                   lyxstyle.Style(parameters->textclass, row->par->GetLayout())->labelsep);
+                                                   textclasslist.Style(parameters->textclass, row->par->GetLayout()).labelsep);
                                if (x < left_margin)
                                        x = left_margin;
                        }
@@ -2878,9 +2874,9 @@ void LyXText::SetCursorIntern(LyXParagraph *par, int pos)
                        if (pos + 1 == main_body
                            && row->par->IsLineSeparator(pos)) {
                                x += GetFont(row->par, -2).stringWidth(
-                                                   lyxstyle.Style(parameters->textclass, row->par->GetLayout())->labelsep);
+                                                   textclasslist.Style(parameters->textclass, row->par->GetLayout()).labelsep);
                                if (row->par->IsLineSeparator(pos))
-                                       x-= SingleWidth(row->par, pos);
+                                       x -= SingleWidth(row->par, pos);
                                if (x < left_margin)
                                        x = left_margin;
                        }
@@ -2907,20 +2903,15 @@ void LyXText::SetCursorIntern(LyXParagraph *par, int pos)
 
 void LyXText::SetCursorFromCoordinates(int x, long y)
 {
-       Row *row;
-       int column;
-
-       LyXCursor old_cursor;
-   
-       old_cursor = cursor;
+       LyXCursor old_cursor = cursor;
    
        /* get the row first */ 
    
-       row = GetRowNearY(y);
+       Row * row = GetRowNearY(y);
    
        cursor.par = row->par;
    
-       column = GetColumnNearX(row, x);
+       int column = GetColumnNearX(row, x);
        cursor.pos = row->pos + column;
        cursor.x = x;
        cursor.y = y + row->baseline;
@@ -3048,7 +3039,7 @@ void LyXText::CursorDownParagraph()
        if (cursor.par->Next()) {
                SetCursor(cursor.par->Next(), 0);
        } else {
-               SetCursor(cursor.par,cursor.par->Last());
+               SetCursor(cursor.par, cursor.par->Last());
        }
 }
 
@@ -3063,8 +3054,8 @@ void LyXText::DeleteEmptyParagraphMechanism(LyXCursor old_cursor)
         return;
 
     // Paragraph should not be deleted if empty
-    if ((lyxstyle.Style(parameters->textclass,
-                        old_cursor.par->GetLayout()))->keepempty)
+    if ((textclasslist.Style(parameters->textclass,
+                        old_cursor.par->GetLayout())).keepempty)
         return;
 
     LyXCursor tmpcursor;
@@ -3074,18 +3065,7 @@ void LyXText::DeleteEmptyParagraphMechanism(LyXCursor old_cursor)
               || (old_cursor.par->Last() == 1
                   && (old_cursor.par->IsLineSeparator(0))))
              && old_cursor.par->FirstPhysicalPar()
-             == old_cursor.par->LastPhysicalPar()
-             //&& (
-             // impossible to insert your own \caption with
-             // this set. made it impossible to use the
-             // optional argument...
-             // also empty pars in fig or tab never was removed(?)(Lgb)
-             //lyxstyle.Style(parameters->textclass,
-//                                     old_cursor.par->GetLayout())->labeltype!=LABEL_SENSITIVE || 
-             //     (old_cursor.par->footnoteflag == LyXParagraph::NO_FOOTNOTE
-             //|| (old_cursor.par->footnotekind != LyXParagraph::FIG
-             //          && old_cursor.par->footnotekind != LyXParagraph::TAB)))
-             ) {
+             == old_cursor.par->LastPhysicalPar()) {
                        
             /* ok, we will delete anything */ 
                        
@@ -3209,10 +3189,10 @@ void LyXText::DeleteEmptyParagraphMechanism(LyXCursor old_cursor)
 }
 
 
-LyXParagraph* LyXText::GetParFromID(int id)
+LyXParagraph * LyXText::GetParFromID(int id)
 {
-       LyXParagraph* result = FirstParagraph();
-       while (result && result->GetID() != id)
+       LyXParagraph * result = FirstParagraph();
+       while (result && result->id() != id)
                result = result->next;
        return result;
 }
@@ -3221,11 +3201,11 @@ LyXParagraph* LyXText::GetParFromID(int id)
 // undo functions
 bool  LyXText::TextUndo()
 { // returns false if no undo possible
-       Undo *undo = params->undostack.Pop();
+       Undo * undo = params->undostack.pop();
        if (undo){
                FinishUndo();
                if (!undo_frozen)
-                       params->redostack.Push(CreateUndo(undo->kind, 
+                       params->redostack.push(CreateUndo(undo->kind, 
                                                          GetParFromID(undo->number_of_before_par),
                                                          GetParFromID(undo->number_of_behind_par)));
        }
@@ -3235,11 +3215,11 @@ bool  LyXText::TextUndo()
 
 bool LyXText::TextRedo()
 { // returns false if no redo possible
-       Undo *undo = params->redostack.Pop();
+       Undo * undo = params->redostack.pop();
        if (undo){
                FinishUndo();
                if (!undo_frozen)
-                       params->undostack.Push(CreateUndo(undo->kind, 
+                       params->undostack.push(CreateUndo(undo->kind, 
                                                          GetParFromID(undo->number_of_before_par),
                                                          GetParFromID(undo->number_of_behind_par)));
        }
@@ -3247,29 +3227,18 @@ bool LyXText::TextRedo()
 }
 
 
-bool LyXText::TextHandleUndo(Undo* undo){ // returns false if no undo possible
+bool LyXText::TextHandleUndo(Undo * undo){ // returns false if no undo possible
        bool result = false;
        if (undo){
-               LyXParagraph* before = GetParFromID(undo->number_of_before_par); 
-               LyXParagraph* behind = GetParFromID(undo->number_of_behind_par); 
-               LyXParagraph* tmppar;
-               LyXParagraph* tmppar2;
-               LyXParagraph* tmppar3;
-               LyXParagraph* tmppar4;
-               LyXParagraph* endpar;
-               LyXParagraph* tmppar5;
+               LyXParagraph * before = GetParFromID(undo->number_of_before_par); 
+               LyXParagraph * behind = GetParFromID(undo->number_of_behind_par); 
+               LyXParagraph * tmppar;
+               LyXParagraph * tmppar2;
+               LyXParagraph * tmppar3;
+               LyXParagraph * tmppar4;
+               LyXParagraph * endpar;
+               LyXParagraph * tmppar5;
     
-               /*
-                 if (before){
-                 before->text[before->last] = 0;
-                 printf("before: %s\n", before->text);
-                 }
-                 if (behind){
-                 behind->text[behind->last] = 0;
-                 printf("behind: %s\n", behind->text);
-                 }
-                 */ 
-
                // if there's no before take the beginning of the document for redoing
                if (!before)
                        SetCursorIntern(FirstParagraph(), 0);
@@ -3298,7 +3267,9 @@ bool LyXText::TextHandleUndo(Undo* undo){ // returns false if no undo possible
                                // is stored in the undo. So restore the text informations.
                                if (undo->kind == Undo::EDIT){
                                        tmppar2->text = tmppar->text;
-                                       tmppar->text = 0;
+                                       tmppar->text.clear();
+                                       //tmppar->text.erase(tmppar->text.begin(),
+                                       //                 tmppar->text.end());
                                        tmppar2 = tmppar2->next;
                                }
                                if ( currentrow && currentrow->par == tmppar )
@@ -3332,7 +3303,7 @@ bool LyXText::TextHandleUndo(Undo* undo){ // returns false if no undo possible
                        // 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){
-                               tmppar4 =before;
+                               tmppar4 = before;
                                while (tmppar4->previous && 
                                       tmppar4->previous->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE)
                                        tmppar4 = tmppar4->previous;
@@ -3395,29 +3366,31 @@ void LyXText::UnFreezeUndo()
 }
 
 
-void LyXText::SetUndo(Undo::undo_kind kind, LyXParagraph *before, LyXParagraph *behind)
+void LyXText::SetUndo(Undo::undo_kind kind, LyXParagraph * before,
+                     LyXParagraph * behind)
 {
        if (!undo_frozen)
-               params->undostack.Push(CreateUndo(kind, before, behind));
-       params->redostack.Clear();
+               params->undostack.push(CreateUndo(kind, before, behind));
+       params->redostack.clear();
 }
 
 
-void LyXText::SetRedo(Undo::undo_kind kind, LyXParagraph *before, LyXParagraph *behind)
+void LyXText::SetRedo(Undo::undo_kind kind, LyXParagraph * before,
+                     LyXParagraph * behind)
 {
-       params->redostack.Push(CreateUndo(kind, before, behind));
+       params->redostack.push(CreateUndo(kind, before, behind));
 }
 
 
-Undo* LyXText::CreateUndo(Undo::undo_kind kind, LyXParagraph *before,
-                         LyXParagraph *behind)
+Undo * LyXText::CreateUndo(Undo::undo_kind kind, LyXParagraph * before,
+                         LyXParagraph * behind)
 {
        int before_number = -1;
        int behind_number = -1;
        if (before)
-               before_number = before->GetID();
+               before_number = before->id();
        if (behind)
-               behind_number = behind->GetID();
+               behind_number = behind->id();
        // Undo::EDIT  and Undo::FINISH are
        // always finished. (no overlapping there)
        // overlapping only with insert and delete inside one paragraph: 
@@ -3428,21 +3401,21 @@ Undo* LyXText::CreateUndo(Undo::undo_kind kind, LyXParagraph *before,
        if (!undo_finished && kind != Undo::EDIT && 
            kind != Undo::FINISH){
                // check wether storing is needed
-               if (params->undostack.Top() && 
-                   params->undostack.Top()->kind == kind &&
-                   params->undostack.Top()->number_of_before_par ==  before_number &&
-                   params->undostack.Top()->number_of_behind_par ==  behind_number ){
+               if (!params->undostack.empty() && 
+                   params->undostack.top()->kind == kind &&
+                   params->undostack.top()->number_of_before_par ==  before_number &&
+                   params->undostack.top()->number_of_behind_par ==  behind_number ){
                        // no undo needed
                        return 0;
                }
        }
        // create a new Undo
-       LyXParagraph* undopar;
-       LyXParagraph* tmppar;
-       LyXParagraph *tmppar2;
+       LyXParagraph * undopar;
+       LyXParagraph * tmppar;
+       LyXParagraph * tmppar2;
 
-       LyXParagraph* start = 0;
-       LyXParagraph* end = 0;
+       LyXParagraph * start = 0;
+       LyXParagraph * end = 0;
   
        if (before)
                start = before->next;
@@ -3459,13 +3432,13 @@ Undo* LyXText::CreateUndo(Undo::undo_kind kind, LyXParagraph *before,
        if (start && end && start != end->next && (before != behind || (!before && !behind))) {
                tmppar = start;
                tmppar2 = tmppar->Clone();
-               tmppar2->SetID(tmppar->GetID());
+               tmppar2->id(tmppar->id());
 
                // a memory optimization: Just store the layout information when only edit
                if (kind == Undo::EDIT){
-                       if (tmppar2->text)
-                               delete[] tmppar2->text;
-                       tmppar2->text = 0;
+                       tmppar2->text.clear();
+                       //tmppar2->text.erase(tmppar2->text.begin(),
+                       //                  tmppar2->text.end());
                }
 
                undopar = tmppar2;
@@ -3473,25 +3446,24 @@ Undo* LyXText::CreateUndo(Undo::undo_kind kind, LyXParagraph *before,
                while (tmppar != end && tmppar->next) {
                        tmppar = tmppar->next;
                        tmppar2->next = tmppar->Clone();
-                       tmppar2->next->SetID(tmppar->GetID());
+                       tmppar2->next->id(tmppar->id());
                        // a memory optimization: Just store the layout information when only edit
                        if (kind == Undo::EDIT){
-                               if (tmppar2->next->text)
-                                       delete[] tmppar2->next->text;
-                               tmppar2->next->text = 0;
+                               tmppar2->next->text.clear();
+                               //tmppar2->next->text.erase(tmppar2->next->text.begin(), tmppar2->next->text.end());
                        }
                        tmppar2->next->previous = tmppar2;
-                       tmppar2=tmppar2->next;
+                       tmppar2 = tmppar2->next;
                }
                tmppar2->next = 0;
        }
        else
                undopar = 0; // nothing to replace (undo of delete maybe)
   
-       int cursor_par = cursor.par->ParFromPos(cursor.pos)->GetID();
+       int cursor_par = cursor.par->ParFromPos(cursor.pos)->id();
        int cursor_pos =  cursor.par->PositionInParFromPos(cursor.pos);
 
-       Undo* undo = new Undo(kind, 
+       Undo * undo = new Undo(kind, 
                              before_number, behind_number,  
                              cursor_par, cursor_pos, 
                              undopar);
@@ -3508,7 +3480,7 @@ void LyXText::SetCursorParUndo()
                cursor.par->ParFromPos(cursor.pos)->next); 
 }
 
-void LyXText::RemoveTableRow(LyXCursor *cursor)
+void LyXText::RemoveTableRow(LyXCursor * cursor)
 {
     int
         cell_act,
@@ -3559,14 +3531,14 @@ void LyXText::RemoveTableRow(LyXCursor *cursor)
     return;
 }
 
-bool LyXText::IsEmptyTableRow(LyXCursor *old_cursor)
+
+bool LyXText::IsEmptyTableRow(LyXCursor * old_cursor) const
 {
        if (!old_cursor->par->table)
                return false;
 #ifdef I_DONT_KNOW_IF_I_SHOULD_DO_THIS
-       int
-               pos = old_cursor->pos,
-               cell = NumberOfCell(old_cursor->par, pos);
+       int pos = old_cursor->pos;
+       int cell = NumberOfCell(old_cursor->par, pos);
 
        // search first charater of this table row
        while (pos && !old_cursor->par->table->IsFirstCell(cell)) {
@@ -3592,23 +3564,23 @@ bool LyXText::IsEmptyTableRow(LyXCursor *old_cursor)
 }
 
 
-bool LyXText::IsEmptyTableCell()
+bool LyXText::IsEmptyTableCell() const
 {
-       int pos = cursor.pos - 1;
-       
-       while ((pos>=0) && (pos < cursor.par->Last()) &&
-              !cursor.par->IsNewline(pos))
-               pos--;
-       return cursor.par->IsNewline(pos+1);
+       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);
 }
 
+
 void LyXText::toggleAppendix(){
-  LyXParagraph* par = cursor.par->FirstPhysicalPar();
+  LyXParagraph * par = cursor.par->FirstPhysicalPar();
   bool start = !par->start_of_appendix;
 
   /* ensure that we have only one start_of_appendix in this document */
-  LyXParagraph* tmp = FirstParagraph();
-  for (;tmp;tmp=tmp->next)
+  LyXParagraph * tmp = FirstParagraph();
+  for (;tmp;tmp = tmp->next)
     tmp->start_of_appendix = 0;
   par->start_of_appendix = start;