]> git.lyx.org Git - lyx.git/blobdiff - src/text2.C
layout as string
[lyx.git] / src / text2.C
index 500b2a7a536e3447f96ff8985b158979741aa150..fab6665f652a685a8928d168620f1f9342677797 100644 (file)
 #include "lyxtext.h"
 #include "LString.h"
 #include "paragraph.h"
-#include "insets/inseterror.h"
-#include "insets/insetbib.h"
-#include "insets/insetspecialchar.h"
-#include "insets/insettext.h"
-#include "insets/insetfloat.h"
-#include "layout.h"
+#include "lyxtextclasslist.h"
 #include "LyXView.h"
-#include "support/textutils.h"
-#include "support/lstrings.h"
 #include "undo_funcs.h"
 #include "buffer.h"
 #include "bufferparams.h"
-#include "lyx_gui_misc.h"
 #include "gettext.h"
 #include "BufferView.h"
 #include "LyXView.h"
 #include "font.h"
 #include "debug.h"
 #include "lyxrc.h"
+#include "lyxrow.h"
 #include "FloatList.h"
 #include "language.h"
 #include "ParagraphParameters.h"
+
+#include "insets/inseterror.h"
+#include "insets/insetbib.h"
+#include "insets/insetspecialchar.h"
+#include "insets/insettext.h"
+#include "insets/insetfloat.h"
+
 #include "support/LAssert.h"
+#include "support/textutils.h"
+#include "support/lstrings.h"
 
+using std::vector;
 using std::copy;
 using std::find;
 using std::endl;
 using std::find;
 using std::pair;
+using lyx::pos_type;
 
 
 LyXText::LyXText(BufferView * bv)
        : number_of_rows(0), height(0), width(0), first(0),
          bv_owner(bv), inset_owner(0), the_locking_inset(0),
          need_break_row(0), refresh_y(0), refresh_row(0),
-         status_(LyXText::UNCHANGED), firstrow(0), lastrow(0),
-         copylayouttype(0)
+         status_(LyXText::UNCHANGED), firstrow(0), lastrow(0)
 {}
 
 
@@ -63,10 +66,10 @@ LyXText::LyXText(InsetText * inset)
        :  number_of_rows(0),  height(0), width(0), first(0),
           bv_owner(0), inset_owner(inset), the_locking_inset(0),
           need_break_row(0), refresh_y(0), refresh_row(0),
-          status_(LyXText::UNCHANGED), firstrow(0), lastrow(0),
-          copylayouttype(0)
+          status_(LyXText::UNCHANGED), firstrow(0), lastrow(0)
 {}
 
+
 void LyXText::init(BufferView * bview, bool reinit)
 {
        if (reinit) {
@@ -78,7 +81,8 @@ void LyXText::init(BufferView * bview, bool reinit)
                        firstrow = tmprow;
                }
                lastrow = refresh_row = need_break_row = 0;
-               width = height = copylayouttype = 0;
+               width = height = 0;
+               copylayouttype.erase();
                number_of_rows = first = refresh_y = 0;
                status_ = LyXText::UNCHANGED;
        } else if (firstrow)
@@ -121,13 +125,12 @@ LyXFont const realizeFont(LyXFont const & font,
                par = par->outerHook();
                if (par) {
 #ifndef INHERIT_LANGUAGE
-                       tmpfont.realize(textclasslist.
-                                       Style(buf->params.textclass,
-                                             par->getLayout()).font);
+                       tmpfont.realize(textclasslist[buf->params.textclass][
+                                             par->layout()].font);
 #else
                        tmpfont.realize(textclasslist.
                                        Style(buf->params.textclass,
-                                             par->getLayout()).font,
+                                             par->layout()).font,
                                        buf->params.language);
 #endif
                        par_depth = par->getDepth();
@@ -135,9 +138,9 @@ LyXFont const realizeFont(LyXFont const & font,
        }
 
 #ifndef INHERIT_LANGUAGE
-       tmpfont.realize(textclasslist.TextClass(buf->params.textclass).defaultfont());
+       tmpfont.realize(textclasslist[buf->params.textclass].defaultfont());
 #else
-       tmpfont.realize(textclasslist.TextClass(buf->params.textclass).defaultfont(),
+       tmpfont.realize(textclasslist[buf->params.textclass].defaultfont(),
                        buf->params.language);
 #endif
 
@@ -155,21 +158,22 @@ LyXFont const realizeFont(LyXFont const & font,
 // 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 const LyXText::getFont(Buffer const * buf, Paragraph * par,
-                               Paragraph::size_type pos) const
+                               pos_type pos) const
 {
        lyx::Assert(pos >= 0);
        
        LyXLayout const & layout = 
-               textclasslist.Style(buf->params.textclass, par->getLayout());
+               textclasslist[buf->params.textclass][par->layout()];
        
        Paragraph::depth_type par_depth = par->getDepth();
        // We specialize the 95% common case:
        if (!par_depth) {
                if (layout.labeltype == LABEL_MANUAL
                    && pos < beginningOfMainBody(buf, par)) {
-                               // 1% goes here
-                       LyXFont f = par->getFontSettings(buf->params,
-                                                        pos);
+                       // 1% goes here
+                       LyXFont f = par->getFontSettings(buf->params, pos);
+                       if (par->inInset())
+                               par->inInset()->getDrawFont(f);
 #ifndef INHERIT_LANGUAGE
                        return f.realize(layout.reslabelfont);
 #else
@@ -177,6 +181,8 @@ LyXFont const LyXText::getFont(Buffer const * buf, Paragraph * par,
 #endif
                } else {
                        LyXFont f = par->getFontSettings(buf->params, pos);
+                       if (par->inInset())
+                               par->inInset()->getDrawFont(f);
 #ifndef INHERIT_LANGUAGE
                        return f.realize(layout.resfont);
 #else
@@ -203,6 +209,8 @@ LyXFont const LyXText::getFont(Buffer const * buf, Paragraph * par,
 #else
        tmpfont.realize(layoutfont, buf->params.language);
 #endif
+       if (par->inInset())
+               par->inInset()->getDrawFont(tmpfont);
        
        return realizeFont(tmpfont, buf, par);
 }
@@ -211,7 +219,7 @@ LyXFont const LyXText::getFont(Buffer const * buf, Paragraph * par,
 LyXFont const LyXText::getLayoutFont(Buffer const * buf, Paragraph * par) const
 {
        LyXLayout const & layout = 
-               textclasslist.Style(buf->params.textclass, par->getLayout());
+               textclasslist[buf->params.textclass][par->layout()];
 
        Paragraph::depth_type par_depth = par->getDepth();
 
@@ -226,7 +234,7 @@ LyXFont const LyXText::getLayoutFont(Buffer const * buf, Paragraph * par) const
 LyXFont const LyXText::getLabelFont(Buffer const * buf, Paragraph * par) const
 {
        LyXLayout const & layout = 
-               textclasslist.Style(buf->params.textclass, par->getLayout());
+               textclasslist[buf->params.textclass][par->layout()];
 
        Paragraph::depth_type par_depth = par->getDepth();
 
@@ -239,77 +247,34 @@ LyXFont const LyXText::getLabelFont(Buffer const * buf, Paragraph * par) const
 
 
 void LyXText::setCharFont(BufferView * bv, Paragraph * par,
-                          Paragraph::size_type pos, LyXFont const & fnt,
+                          pos_type pos, LyXFont const & fnt,
                           bool toggleall)
 {
        Buffer const * buf = bv->buffer();
        LyXFont font = getFont(buf, par, pos);
        font.update(fnt, buf->params.language, toggleall);
        // Let the insets convert their font
-       if (par->getChar(pos) == Paragraph::META_INSET) {
+       if (par->isInset(pos)) {
                Inset * inset = par->getInset(pos);
-               if (inset) {
-                       if (inset->editable()==Inset::IS_EDITABLE) {
-                               UpdatableInset * uinset =
-                                       static_cast<UpdatableInset *>(inset);
-                               uinset->setFont(bv, fnt, toggleall, true);
-                       }
+               if (isEditableInset(inset)) {
+                       UpdatableInset * uinset =
+                               static_cast<UpdatableInset *>(inset);
+                       uinset->setFont(bv, fnt, toggleall, true);
                }
        }
 
-       LyXLayout const & layout =
-               textclasslist.Style(buf->params.textclass,
-                                   par->getLayout());
-
-       // Get concrete layout font to reduce against
-       LyXFont layoutfont;
-
-       if (pos < beginningOfMainBody(buf, par))
-               layoutfont = layout.labelfont;
-       else
-               layoutfont = layout.font;
-
-       // Realize against environment font information
-       if (par->getDepth()){
-               Paragraph * tp = par;
-               while (!layoutfont.resolved() && tp && tp->getDepth()) {
-                       tp = tp->outerHook();
-                       if (tp)
-#ifndef INHERIT_LANGUAGE
-                               layoutfont.realize(textclasslist.
-                                                  Style(buf->params.textclass,
-                                                        tp->getLayout()).font);
-#else
-                               layoutfont.realize(textclasslist.
-                                                  Style(buf->params.textclass,
-                                                        tp->getLayout()).font,
-                                                  buf->params.language);
-#endif
-               }
-       }
-
-#ifndef INHERIT_LANGUAGE
-       layoutfont.realize(textclasslist.TextClass(buf->params.textclass).defaultfont());
-#else
-       layoutfont.realize(textclasslist.TextClass(buf->params.textclass).defaultfont(),
-                          buf->params.language);
-#endif
-
-       // Now, reduce font against full layout font
-       font.reduce(layoutfont);
-
-       par->setFont(pos, font);
+       // Plug thru to version below:
+       setCharFont(buf, par, pos, font);
 }
 
 
 void LyXText::setCharFont(Buffer const * buf, Paragraph * par,
-                          Paragraph::size_type pos, LyXFont const & fnt)
+                          pos_type pos, LyXFont const & fnt)
 {
        LyXFont font(fnt);
 
-       LyXLayout const & layout =
-               textclasslist.Style(buf->params.textclass,
-                                   par->getLayout());
+       LyXTextClass const & tclass = textclasslist[buf->params.textclass];
+       LyXLayout const & layout = tclass[par->layout()];
 
        // Get concrete layout font to reduce against
        LyXFont layoutfont;
@@ -320,29 +285,26 @@ void LyXText::setCharFont(Buffer const * buf, Paragraph * par,
                layoutfont = layout.font;
 
        // Realize against environment font information
-       if (par->getDepth()){
+       if (par->getDepth()) {
                Paragraph * tp = par;
                while (!layoutfont.resolved() && tp && tp->getDepth()) {
                        tp = tp->outerHook();
                        if (tp)
 #ifndef INHERIT_LANGUAGE
-                               layoutfont.realize(textclasslist.
-                                                  Style(buf->params.textclass,
-                                                        tp->getLayout()).font);
+                               layoutfont.realize(tclass[tp->layout()].font);
 #else
                                layoutfont.realize(textclasslist.
                                                   Style(buf->params.textclass,
-                                                        tp->getLayout()).font,
+                                                        tp->layout()).font,
                                                   buf->params.language);
 #endif
                }
        }
 
 #ifndef INHERIT_LANGUAGE
-       layoutfont.realize(textclasslist.TextClass(buf->params.textclass).defaultfont());
+       layoutfont.realize(tclass.defaultfont());
 #else
-       layoutfont.realize(textclasslist.TextClass(buf->params.textclass).defaultfont(),
-                          buf->params.language);
+       layoutfont.realize(tclass.defaultfont(), buf->params.language);
 #endif
 
        // Now, reduce font against full layout font
@@ -355,7 +317,7 @@ void LyXText::setCharFont(Buffer const * buf, Paragraph * par,
 // inserts a new row behind the specified row, increments
 // the touched counters
 void LyXText::insertRow(Row * row, Paragraph * par,
-                        Paragraph::size_type pos) const
+                        pos_type pos) const
 {
        Row * tmprow = new Row;
        if (!row) {
@@ -387,22 +349,24 @@ void LyXText::insertRow(Row * row, Paragraph * par,
 // removes the row and reset the touched counters
 void LyXText::removeRow(Row * row) const
 {
-       /* this must not happen before the currentrow for clear reasons.
-          so the trick is just to set the current row onto the previous
-          row of this row */
-       int unused_y;
-       getRow(row->par(), row->pos(), unused_y);
-   
+       Row * row_prev = row->previous();
        if (row->next())
-               row->next()->previous(row->previous());
-       if (!row->previous()) {
+               row->next()->previous(row_prev);
+       if (!row_prev) {
                firstrow = row->next();
+//             lyx::Assert(firstrow);
        } else  {
-               row->previous()->next(row->next());
+               row_prev->next(row->next());
        }
-       if (row == lastrow)
-               lastrow = row->previous();
-   
+       if (row == lastrow) {
+               lyx::Assert(!row->next());
+               lastrow = row_prev;
+       }
+       if (refresh_row == row) {
+               refresh_row = row_prev ? row_prev : row->next();
+               // what about refresh_y, refresh_height
+       }
+
        height -= row->height(); // the text becomes smaller
    
        delete row;
@@ -451,7 +415,7 @@ Inset * LyXText::getInset() const
        if (cursor.pos() == 0 && cursor.par()->bibkey) {
                inset = cursor.par()->bibkey;
        } else if (cursor.pos() < cursor.par()->size() 
-                  && cursor.par()->getChar(cursor.pos()) == Paragraph::META_INSET) {
+                  && cursor.par()->isInset(cursor.pos())) {
                inset = cursor.par()->getInset(cursor.pos());
        }
        return inset;
@@ -461,12 +425,20 @@ Inset * LyXText::getInset() const
 void LyXText::toggleInset(BufferView * bview)
 {
        Inset * inset = getInset();
-       if (!inset->editable())
+       // is there an editable inset at cursor position?
+       if (!isEditableInset(inset)) {
+               // No, try to see if we are inside a collapsable inset
+               if (inset_owner && inset_owner->owner()
+                   && inset_owner->owner()->isOpen()) {
+                       bview->unlockInset(static_cast<UpdatableInset *>(inset_owner->owner()));
+                       inset_owner->owner()->close(bview);
+               }
                return;
+       }
        //bview->owner()->message(inset->editMessage());
 
        // do we want to keep this?? (JMarc)
-       if (inset->editable() != Inset::HIGHLY_EDITABLE)
+       if (!isHighlyEditableInset(inset))
                setCursorParUndo(bview);
 
        if (inset->isOpen()) {
@@ -486,10 +458,10 @@ void LyXText::makeFontEntriesLayoutSpecific(Buffer const * buf,
                                            Paragraph * par)
 {
        LyXLayout const & layout =
-               textclasslist.Style(buf->params.textclass, par->getLayout());
+               textclasslist[buf->params.textclass][par->layout()];
 
        LyXFont layoutfont;
-       for (Paragraph::size_type pos = 0; pos < par->size(); ++pos) {
+       for (pos_type pos = 0; pos < par->size(); ++pos) {
                if (pos < beginningOfMainBody(buf, par))
                        layoutfont = layout.labelfont;
                else
@@ -505,7 +477,7 @@ void LyXText::makeFontEntriesLayoutSpecific(Buffer const * buf,
 Paragraph * LyXText::setLayout(BufferView * bview,
                               LyXCursor & cur, LyXCursor & sstart_cur,
                               LyXCursor & send_cur,
-                              LyXTextClass::size_type layout)
+                              string const & layout)
 {
        Paragraph * endpar = send_cur.par()->next();
        Paragraph * undoendpar = endpar;
@@ -525,12 +497,12 @@ Paragraph * LyXText::setLayout(BufferView * bview,
        // ok we have a selection. This is always between sstart_cur
        // and sel_end cursor
        cur = sstart_cur;
-       
+
        LyXLayout const & lyxlayout =
-               textclasslist.Style(bview->buffer()->params.textclass, layout);
+               textclasslist[bview->buffer()->params.textclass][layout];
        
-       while (cur.par() != send_cur.par()) {
-               cur.par()->setLayout(layout);
+       do {
+               cur.par()->applyLayout(layout);
                makeFontEntriesLayoutSpecific(bview->buffer(), cur.par());
                Paragraph * fppar = cur.par();
                fppar->params().spaceTop(lyxlayout.fill_top ?
@@ -546,28 +518,16 @@ Paragraph * LyXText::setLayout(BufferView * bview,
                        delete fppar->bibkey;
                        fppar->bibkey = 0;
                }
-               cur.par(cur.par()->next());
-       }
-       cur.par()->setLayout(layout);
-       makeFontEntriesLayoutSpecific(bview->buffer(), cur.par());
-       Paragraph * fppar = cur.par();
-       fppar->params().spaceTop(lyxlayout.fill_top ?
-                                VSpace(VSpace::VFILL) : VSpace(VSpace::NONE));
-       fppar->params().spaceBottom(lyxlayout.fill_bottom ? 
-                                   VSpace(VSpace::VFILL) : VSpace(VSpace::NONE));
-       if (lyxlayout.margintype == MARGIN_MANUAL)
-               cur.par()->setLabelWidthString(lyxlayout.labelstring());
-       if (lyxlayout.labeltype != LABEL_BIBLIO
-           && fppar->bibkey) {
-               delete fppar->bibkey;
-               fppar->bibkey = 0;
-       }
+               if (cur.par() != send_cur.par())
+                       cur.par(cur.par()->next());
+       } while (cur.par() != send_cur.par());
+       
        return endpar;
 }
 
 
 // set layout over selection and make a total rebreak of those paragraphs
-void LyXText::setLayout(BufferView * bview, LyXTextClass::size_type layout)
+void LyXText::setLayout(BufferView * bview, string const & layout)
 {
        LyXCursor tmpcursor = cursor;  /* store the current cursor  */
 
@@ -629,21 +589,20 @@ void  LyXText::incDepth(BufferView * bview)
    
        bool anything_changed = false;
    
+       LyXTextClass const & tclass =
+               textclasslist[bview->buffer()->params.textclass];
+       
        while (true) {
                // NOTE: you can't change the depth of a bibliography entry
-               if (
-                       textclasslist.Style(bview->buffer()->params.textclass,
-                                           cursor.par()->getLayout()
-                               ).labeltype != LABEL_BIBLIO) {
+               if (tclass[cursor.par()->layout()].labeltype != LABEL_BIBLIO) {
                        Paragraph * prev = cursor.par()->previous();
 
-                       if (prev 
-                           && (prev->getDepth() - cursor.par()->getDepth() > 0
-                               || (prev->getDepth() == cursor.par()->getDepth()
-                                   && textclasslist.Style(bview->buffer()->params.textclass,
-                                                          prev->getLayout()).isEnvironment()))) {
-                               cursor.par()->params().depth(cursor.par()->params().depth() + 1);
-                               anything_changed = true;
+                       if (prev) {
+                               if (cursor.par()->getDepth()
+                                   < prev->getMaxDepthAfter(bview->buffer())){
+                                       cursor.par()->params().depth(cursor.par()->getDepth() + 1);
+                                       anything_changed = true;
+                               }
                        }
                }
                if (cursor.par() == selection.end.par())
@@ -793,11 +752,11 @@ void LyXText::setFont(BufferView * bview, LyXFont const & font, bool toggleall)
        redoParagraphs(bview, selection.start, selection.end.par()->next());
    
        // we have to reset the selection, because the
-       // geometry could have changed
+       // geometry could have changed, but we keep
+       // it for user convenience
        setCursor(bview, selection.start.par(), selection.start.pos());
        selection.cursor = cursor;
        setCursor(bview, selection.end.par(), selection.end.pos());
-       clearSelection();
        setSelection(bview);
        setCursor(bview, tmpcursor.par(), tmpcursor.pos(), true,
                  tmpcursor.boundary());
@@ -865,9 +824,10 @@ void LyXText::redoParagraphs(BufferView * bview, LyXCursor const & cur,
 
        if (!tmprow->previous()) {
                // a trick/hack for UNDO
-               // Can somebody please tell me _why_ this solves
-               // anything. (Lgb)
-               first_phys_par = firstParagraph();
+               // This is needed because in an UNDO/REDO we could have changed
+               // the ownerParagrah() so the paragraph inside the row is NOT
+               // my really first par anymore. Got it Lars ;) (Jug 20011206)
+               first_phys_par = ownerParagraph();
        } else {
                first_phys_par = tmprow->par();
                while (tmprow->previous()
@@ -877,7 +837,7 @@ void LyXText::redoParagraphs(BufferView * bview, LyXCursor const & cur,
                        y -= tmprow->height();
                }
        }
-       
+
        // we can set the refreshing parameters now
        status(bview, LyXText::NEED_MORE_REFRESH);
        refresh_y = y;
@@ -889,13 +849,14 @@ void LyXText::redoParagraphs(BufferView * bview, LyXCursor const & cur,
                tmppar = tmprow->next()->par();
        else
                tmppar = 0;
-       while (tmppar != endpar) {
+       while (tmprow->next() && tmppar != endpar) {
                removeRow(tmprow->next());
-               if (tmprow->next())
+               if (tmprow->next()) {
                        tmppar = tmprow->next()->par();
-               else
+               } else {
                        tmppar = 0;
-       }  
+               }
+       }
    
        // remove the first one
        tmprow2 = tmprow;     /* this is because tmprow->previous()
@@ -1014,7 +975,7 @@ void LyXText::setSelection(BufferView * bview)
 
 
 string const LyXText::selectionAsString(Buffer const * buffer,
-                                       bool label) const
+                                        bool label) const
 {
        if (!selection.set()) return string();
        string result;
@@ -1059,7 +1020,7 @@ void LyXText::clearSelection() const
 {
        selection.set(false);
        selection.mark(false);
-       selection.end = selection.start = selection.cursor = cursor;
+       last_sel_cursor = selection.end = selection.start = selection.cursor = cursor;
 }
 
 
@@ -1170,11 +1131,11 @@ string LyXText::getStringToIndex(BufferView * bview)
 }
 
 
-Paragraph::size_type LyXText::beginningOfMainBody(Buffer const * buf,
+pos_type LyXText::beginningOfMainBody(Buffer const * buf,
                             Paragraph const * par) const
 {
-       if (textclasslist.Style(buf->params.textclass,
-                               par->getLayout()).labeltype != LABEL_MANUAL)
+       if (textclasslist[buf->params.textclass][
+                               par->layout()].labeltype != LABEL_MANUAL)
                return 0;
        else
                return par->beginningOfMainBody();
@@ -1222,6 +1183,9 @@ void LyXText::setParagraph(BufferView * bview,
 
        
        Paragraph * tmppar = selection.end.par();
+       LyXTextClass const & tclass =
+               textclasslist[bview->buffer()->params.textclass];
+       
        while (tmppar != selection.start.par()->previous()) {
                setCursor(bview, tmppar, 0);
                status(bview, LyXText::NEED_MORE_REFRESH);
@@ -1235,16 +1199,12 @@ void LyXText::setParagraph(BufferView * bview,
                cursor.par()->params().spaceBottom(space_bottom);
                 cursor.par()->params().spacing(spacing);
                // does the layout allow the new alignment?
+               LyXLayout const & layout = tclass[cursor.par()->layout()];
+               
                if (align == LYX_ALIGN_LAYOUT)
-                       align = textclasslist
-                               .Style(bview->buffer()->params.textclass,
-                                      cursor.par()->getLayout()).align;
-               if (align & textclasslist
-                   .Style(bview->buffer()->params.textclass,
-                          cursor.par()->getLayout()).alignpossible) {
-                       if (align == textclasslist
-                           .Style(bview->buffer()->params.textclass,
-                                  cursor.par()->getLayout()).align)
+                       align = layout.align;
+               if (align & layout.alignpossible) {
+                       if (align == layout.align)
                                cursor.par()->params().align(LYX_ALIGN_LAYOUT);
                        else
                                cursor.par()->params().align(align);
@@ -1324,12 +1284,9 @@ string const romanCounter(int n)
 // set the counter of a paragraph. This includes the labels
 void LyXText::setCounter(Buffer const * buf, Paragraph * par) const
 {
-       LyXLayout const & layout =
-               textclasslist.Style(buf->params.textclass, 
-                                   par->getLayout());
+       LyXTextClass const & textclass = textclasslist[buf->params.textclass];
+       LyXLayout const & layout = textclass[par->layout()];
 
-       LyXTextClass const & textclass =
-               textclasslist.TextClass(buf->params.textclass);
 
        // copy the prev-counters to this one,
        // unless this is the first paragraph
@@ -1364,9 +1321,7 @@ void LyXText::setCounter(Buffer const * buf, Paragraph * par) const
         */
        if (par->previous()
            && par->previous()->getDepth() < par->getDepth()
-           && textclasslist.Style(buf->params.textclass,
-                                  par->previous()->getLayout()
-                   ).labeltype == LABEL_COUNTER_ENUMI
+           && textclass[par->previous()->layout()].labeltype == LABEL_COUNTER_ENUMI
            && par->enumdepth < 3
            && layout.labeltype != LABEL_BIBLIO) {
                par->enumdepth++;
@@ -1608,7 +1563,7 @@ void LyXText::setCounter(Buffer const * buf, Paragraph * par) const
                par->incCounter(i);
                int number = par->getCounter(i);
                if (!par->bibkey) {
-                       InsetCommandParams p( "bibitem" );
+                       InsetCommandParams p("bibitem" );
                        par->bibkey = new InsetBibKey(p);
                }
                par->bibkey->setCounter(number);
@@ -1667,11 +1622,12 @@ void LyXText::updateCounters(BufferView * bview, Row * row) const
                
                // now check for the headline layouts. remember that they
                // have a dynamic left margin
-               if ((textclasslist.Style(bview->buffer()->params.textclass,
-                                        par->layout).margintype == MARGIN_DYNAMIC
-                    || textclasslist.Style(bview->buffer()->params.textclass,
-                                           par->layout).labeltype == LABEL_SENSITIVE)) {
-                       
+               LyXTextClass const & tclass =
+                       textclasslist[bview->buffer()->params.textclass];
+               LyXLayout const & layout = tclass[par->layout()];
+               
+               if (layout.margintype == MARGIN_DYNAMIC
+                   || layout.labeltype == LABEL_SENSITIVE) {
                        // Rebreak the paragraph
                        removeParagraph(row);
                        appendParagraph(bview, row);
@@ -1685,8 +1641,8 @@ void LyXText::insertInset(BufferView * bview, Inset * inset)
 {
        if (!cursor.par()->insetAllowed(inset->lyxCode()))
                return;
-       setUndo(bview, Undo::INSERT,
-               cursor.par(), cursor.par()->next());
+       // I don't know if this is necessary here (Jug 20020102)
+       setUndo(bview, Undo::INSERT, cursor.par(), cursor.par()->next());
        cursor.par()->insertInset(cursor.pos(), inset);
        // Just to rebreak and refresh correctly.
        // The character will not be inserted a second time
@@ -1697,7 +1653,7 @@ void LyXText::insertInset(BufferView * bview, Inset * inset)
        // inset now after the Undo LyX tries to call inset->Edit(...) again
        // and cannot do this as the cursor is behind the inset and GetInset
        // does not return the inset!
-       if (inset->editable() == Inset::HIGHLY_EDITABLE) {
+       if (isHighlyEditableInset(inset)) {
                cursorLeft(bview, true);
        }
 #endif
@@ -1706,7 +1662,7 @@ void LyXText::insertInset(BufferView * bview, Inset * inset)
 
 void LyXText::copyEnvironmentType()
 {
-       copylayouttype = cursor.par()->getLayout();
+       copylayouttype = cursor.par()->layout();
 }
 
 
@@ -1797,14 +1753,7 @@ void LyXText::cutSelection(BufferView * bview, bool doclear, bool realcut)
 
 void LyXText::copySelection(BufferView * bview)
 {
-       // Stuff what we got on the clipboard. Even if there is no selection.
-
-       // There is a problem with having the stuffing here in that the
-       // larger the selection the slower LyX will get. This can be
-       // solved by running the line below only when the selection has
-       // finished. The solution used currently just works, to make it
-       // faster we need to be more clever and probably also have more
-       // calls to stuffClipboard. (Lgb)
+       // stuff the selection onto the X clipboard, from an explicit copy request
        bview->stuffClipboard(selectionAsString(bview->buffer(), true));
 
        // this doesnt make sense, if there is no selection
@@ -1853,13 +1802,6 @@ void LyXText::pasteSelection(BufferView * bview)
 }
 
 
-// returns a pointer to the very first Paragraph
-Paragraph * LyXText::firstParagraph() const
-{
-       return ownerParagraph();
-}
-
-
 // sets the selection over the number of characters of string, no check!!
 void LyXText::setSelectionOverString(BufferView * bview, string const & str)
 {
@@ -1886,7 +1828,7 @@ void LyXText::replaceSelectionWithString(BufferView * bview,
        }
 
        // Get font setting before we cut
-       Paragraph::size_type pos = selection.end.pos();
+       pos_type pos = selection.end.pos();
        LyXFont const font = selection.start.par()
                ->getFontSettings(bview->buffer()->params,
                                  selection.start.pos());
@@ -1908,7 +1850,7 @@ void LyXText::replaceSelectionWithString(BufferView * bview,
 void LyXText::insertStringAsLines(BufferView * bview, string const & str)
 {
        Paragraph * par = cursor.par();
-       Paragraph::size_type pos = cursor.pos();
+       pos_type pos = cursor.pos();
        Paragraph * endpar = cursor.par()->next();
        
        setCursorParUndo(bview);
@@ -1953,7 +1895,7 @@ void LyXText::insertStringAsParagraphs(BufferView * bview, string const & str)
 
 
 bool LyXText::gotoNextInset(BufferView * bview,
-                           std::vector<Inset::Code> const & codes,
+                           vector<Inset::Code> const & codes,
                            string const & contents) const
 {
        LyXCursor res = cursor;
@@ -1967,7 +1909,7 @@ bool LyXText::gotoNextInset(BufferView * bview,
                }
       
        } while (res.par() && 
-                !(res.par()->getChar(res.pos()) == Paragraph::META_INSET
+                !(res.par()->isInset(res.pos())
                   && (inset = res.par()->getInset(res.pos())) != 0
                   && find(codes.begin(), codes.end(), inset->lyxCode())
                   != codes.end()
@@ -1976,7 +1918,7 @@ bool LyXText::gotoNextInset(BufferView * bview,
                       == contents)));
 
        if (res.par()) {
-               setCursor(bview, res.par(), res.pos());
+               setCursor(bview, res.par(), res.pos(), false);
                return true;
        }
        return false;
@@ -1984,12 +1926,12 @@ bool LyXText::gotoNextInset(BufferView * bview,
 
 
 void LyXText::checkParagraph(BufferView * bview, Paragraph * par,
-                             Paragraph::size_type pos)
+                             pos_type pos)
 {
        LyXCursor tmpcursor;                    
 
        int y = 0;
-       Paragraph::size_type z;
+       pos_type z;
        Row * row = getRow(par, pos, y);
        
        // is there a break one row above
@@ -2014,7 +1956,7 @@ void LyXText::checkParagraph(BufferView * bview, Paragraph * par,
        }
 
        int const tmpheight = row->height();
-       Paragraph::size_type const tmplast = rowLast(row);
+       pos_type const tmplast = rowLast(row);
        refresh_y = y;
        refresh_row = row;
        
@@ -2025,8 +1967,8 @@ void LyXText::checkParagraph(BufferView * bview, Paragraph * par,
                status(bview, LyXText::NEED_MORE_REFRESH); 
        
        // check the special right address boxes
-       if (textclasslist.Style(bview->buffer()->params.textclass,
-                               par->getLayout()).margintype
+       if (textclasslist[bview->buffer()->params.textclass][
+                               par->layout()].margintype
            == MARGIN_RIGHT_ADDRESS_BOX)
        {
                tmpcursor.par(par);
@@ -2077,7 +2019,7 @@ bool LyXText::updateInset(BufferView * bview, Inset * inset)
   
        // check every paragraph
   
-       Paragraph * par = firstParagraph();
+       Paragraph * par = ownerParagraph();
        do {
                pos = par->getPositionOfInset(inset);
                if (pos != -1) {
@@ -2091,19 +2033,22 @@ bool LyXText::updateInset(BufferView * bview, Inset * inset)
 }
 
 
-void LyXText::setCursor(BufferView * bview, Paragraph * par,
-                        Paragraph::size_type pos, 
+bool LyXText::setCursor(BufferView * bview, Paragraph * par,
+                        pos_type pos, 
                         bool setfont, bool boundary) const
 {
        LyXCursor old_cursor = cursor;
        setCursorIntern(bview, par, pos, setfont, boundary);
-       deleteEmptyParagraphMechanism(bview, old_cursor);
+       return deleteEmptyParagraphMechanism(bview, old_cursor);
 }
 
 
-void LyXText::setCursor(BufferView *bview, LyXCursor & cur, Paragraph * par,
-                       Paragraph::size_type pos, bool boundary) const
+void LyXText::setCursor(BufferView * bview, LyXCursor & cur, Paragraph * par,
+                       pos_type pos, bool boundary) const
 {
+       lyx::Assert(par);
+       lyx::Assert(bview);
+       
        cur.par(par);
        cur.pos(pos);
        cur.boundary(boundary);
@@ -2115,22 +2060,26 @@ void LyXText::setCursor(BufferView *bview, LyXCursor & cur, Paragraph * par,
        y += row->baseline();
        // y is now the cursor baseline 
        cur.y(y);
-   
+
        // now get the cursors x position
        float x;
        float fill_separator;
        float fill_hfill;
        float fill_label_hfill;
        prepareToPrint(bview, row, x, fill_separator, fill_hfill,
-                      fill_label_hfill);
-       Paragraph::size_type cursor_vpos = 0;
-       Paragraph::size_type last = rowLastPrintable(row);
+                      fill_label_hfill);
+       pos_type cursor_vpos = 0;
+       pos_type last = rowLastPrintable(row);
 
-       if (pos > last + 1)   // This shouldn't happen.
+       if (pos > last + 1) {
+               // This shouldn't happen.
                pos = last + 1;
-       else if (pos < row->pos())
+               cur.pos(pos);
+       } else if (pos < row->pos()) {
                pos = row->pos();
-
+               cur.pos(pos);
+       }
+       
        if (last < row->pos())
                 cursor_vpos = row->pos();
        else if (pos > last && !boundary)
@@ -2146,21 +2095,21 @@ void LyXText::setCursor(BufferView *bview, LyXCursor & cur, Paragraph * par,
                cursor_vpos = (bidi_level(pos) % 2 == 0)
                        ? log2vis(pos) : log2vis(pos) + 1;
        
-       Paragraph::size_type main_body =
+       pos_type main_body =
                beginningOfMainBody(bview->buffer(), row->par());
        if ((main_body > 0) &&
            ((main_body-1 > last) || 
             !row->par()->isLineSeparator(main_body-1)))
                main_body = 0;
        
-       for (Paragraph::size_type vpos = row->pos();
+       for (pos_type vpos = row->pos();
             vpos < cursor_vpos; ++vpos) {
                pos = vis2log(vpos);
                if (main_body > 0 && pos == main_body - 1) {
                        x += fill_label_hfill +
-                               lyxfont::width(textclasslist.Style(
-                                       bview->buffer()->params.textclass,
-                                       row->par()->getLayout())
+                               lyxfont::width(textclasslist[
+                                                      bview->buffer()->params.textclass][
+                                       row->par()->layout()]
                                               .labelsep,
                                               getLabelFont(bview->buffer(), row->par()));
                        if (row->par()->isLineSeparator(main_body-1))
@@ -2181,24 +2130,25 @@ void LyXText::setCursor(BufferView *bview, LyXCursor & cur, Paragraph * par,
        }
        
        cur.x(int(x));
-       cur.x_fix(cur.x());
+       cur.x_fix(cur.x());
        cur.row(row);
 }
 
 
 void LyXText::setCursorIntern(BufferView * bview, Paragraph * par,
-                             Paragraph::size_type pos,
-                             bool setfont, bool boundary) const
+                              pos_type pos, bool setfont, bool boundary) const
 {
        InsetText * it = static_cast<InsetText *>(par->inInset());
        if (it) {
                if (it != inset_owner) {
                        lyxerr << "InsetText   is " << it << endl;
                        lyxerr << "inset_owner is " << inset_owner << endl;
+#ifdef WITH_WARNINGS
 #warning I believe this code is wrong. (Lgb)
 #warning Jürgen, have a look at this. (Lgb)
 #warning Hmmm, I guess you are right but we
 #warning should verify when this is needed
+#endif
                        // Jürgen, would you like to have a look?
                        // I guess we need to move the outer cursor
                        // and open and lock the inset (bla bla bla)
@@ -2207,8 +2157,7 @@ void LyXText::setCursorIntern(BufferView * bview, Paragraph * par,
                        // I moved the lyxerr stuff in here so we can see if
                        // this is actually really needed and where!
                        // (Jug)
-                       it->getLyXText(bview)->setCursorIntern(bview, par, pos, setfont,
-                                                              boundary);
+                       // it->getLyXText(bview)->setCursorIntern(bview, par, pos, setfont, boundary);
                        return;
                }
        }
@@ -2221,7 +2170,7 @@ void LyXText::setCursorIntern(BufferView * bview, Paragraph * par,
 
 void LyXText::setCurrentFont(BufferView * bview) const
 {
-       Paragraph::size_type pos = cursor.pos();
+       pos_type pos = cursor.pos();
        if (cursor.boundary() && pos > 0)
                --pos;
 
@@ -2273,9 +2222,7 @@ void LyXText::setCursorFromCoordinates(BufferView * bview, LyXCursor & cur,
    
        Row * row = getRowNearY(y);
        bool bound = false;
-       Paragraph::size_type const column = getColumnNearX(bview, row, x,
-                                                          bound);
-   
+       pos_type const column = getColumnNearX(bview, row, x, bound);
        cur.par(row->par());
        cur.pos(row->pos() + column);
        cur.x(x);
@@ -2371,18 +2318,19 @@ void LyXText::fixCursorAfterDelete(BufferView * bview,
 }
 
 
-void LyXText::deleteEmptyParagraphMechanism(BufferView * bview,
+bool LyXText::deleteEmptyParagraphMechanism(BufferView * bview,
                                            LyXCursor const & old_cursor) const
 {
        // Would be wrong to delete anything if we have a selection.
-       if (selection.set()) return;
+       if (selection.set()) return false;
 
        // We allow all kinds of "mumbo-jumbo" when freespacing.
-       if (textclasslist.Style(bview->buffer()->params.textclass,
-                               old_cursor.par()->getLayout()).free_spacing)
-               return;
-
-       bool deleted = false;
+       if (textclasslist[bview->buffer()->params.textclass][
+                               old_cursor.par()->layout()].free_spacing
+           || old_cursor.par()->isFreeSpacing())
+       {
+               return false;
+       }
        
        /* Ok I'll put some comments here about what is missing.
           I have fixed BackSpace (and thus Delete) to not delete
@@ -2408,7 +2356,8 @@ void LyXText::deleteEmptyParagraphMechanism(BufferView * bview,
        // MISSING
 
        // If the pos around the old_cursor were spaces, delete one of them.
-       if (old_cursor.par() != cursor.par() || old_cursor.pos() != cursor.pos()) { 
+       if (old_cursor.par() != cursor.par()
+           || old_cursor.pos() != cursor.pos()) { 
                // Only if the cursor has really moved
                
                if (old_cursor.pos() > 0
@@ -2436,19 +2385,28 @@ void LyXText::deleteEmptyParagraphMechanism(BufferView * bview,
                        fixCursorAfterDelete(bview, toggle_cursor, old_cursor);
                        fixCursorAfterDelete(bview, toggle_end_cursor,
                                             old_cursor);
-                       return;
+                       return false;
                }
        }
 
+       // don't delete anything if this is the ONLY paragraph!
+       if (!old_cursor.par()->next() && !old_cursor.par()->previous())
+               return false;
+       
        // Do not delete empty paragraphs with keepempty set.
-       if ((textclasslist.Style(bview->buffer()->params.textclass,
-                                old_cursor.par()->getLayout())).keepempty)
-               return;
+       if (textclasslist
+           [bview->buffer()->params.textclass]
+           [old_cursor.par()->layout()].keepempty)
+               return false;
 
        // only do our magic if we changed paragraph
        if (old_cursor.par() == cursor.par()) 
-               return;
+               return false;
        
+       // record if we have deleted a paragraph
+       // we can't possibly have deleted a paragraph before this point
+       bool deleted = false;
+
        if ((old_cursor.par()->size() == 0
             || (old_cursor.par()->size() == 1
                 && old_cursor.par()->isLineSeparator(0)))) {
@@ -2473,12 +2431,12 @@ void LyXText::deleteEmptyParagraphMechanism(BufferView * bview,
                        setUndo(bview, Undo::DELETE, old_cursor.par(), endpar);
                        cursor = tmpcursor;
 
-                               // delete old row
+                       // delete old row
                        removeRow(old_cursor.row());
                        if (ownerParagraph() == old_cursor.par()) {
                                ownerParagraph(ownerParagraph()->next());
                        }
-                               // delete old par
+                       // delete old par
                        delete old_cursor.par();
                                        
                        /* Breakagain the next par. Needed because of
@@ -2528,7 +2486,7 @@ void LyXText::deleteEmptyParagraphMechanism(BufferView * bview,
                setCursorIntern(bview, cursor.par(), cursor.pos());
 
                if (selection.cursor.par()  == old_cursor.par()
-                   && selection.cursor.pos() == selection.cursor.pos()) {
+                   && selection.cursor.pos() == old_cursor.pos()) {
                        // correct selection
                        selection.cursor = cursor;
                }
@@ -2542,6 +2500,7 @@ void LyXText::deleteEmptyParagraphMechanism(BufferView * bview,
                        selection.cursor = cursor;
                }
        }
+       return deleted;
 }
 
 
@@ -2551,7 +2510,7 @@ void LyXText::toggleAppendix(BufferView * bview)
        bool start = !par->params().startOfAppendix();
 
        // ensure that we have only one start_of_appendix in this document
-       Paragraph * tmp = firstParagraph();
+       Paragraph * tmp = ownerParagraph();
        for (; tmp; tmp = tmp->next()) {
                tmp->params().startOfAppendix(false);
        }
@@ -2592,11 +2551,7 @@ void LyXText::ownerParagraph(int id, Paragraph * p) const
        if (op && op->inInset()) {
                static_cast<InsetText *>(op->inInset())->paragraph(p);
        } else {
-               if (inset_owner) {
-                       inset_owner->paragraph(p);
-               } else {
-                       bv_owner->buffer()->paragraph = p;
-               }
+               ownerParagraph(p);
        }
 }