]> git.lyx.org Git - lyx.git/blobdiff - src/text.C
Two little things:
[lyx.git] / src / text.C
index 8a1027265571d66ac9bbe8bffdcaf528dbbc060d..f96bd6408944550d04dc6a6f847e7cc5eb916754 100644 (file)
@@ -14,7 +14,6 @@
 #include "lyxrow.h"
 #include "lyxtextclasslist.h"
 #include "paragraph.h"
-#include "lyx_gui_misc.h"
 #include "gettext.h"
 #include "bufferparams.h"
 #include "buffer.h"
@@ -80,7 +79,7 @@ int LyXText::workWidth(BufferView * bview, Inset * inset) const
                        " fall back to the brute force method" << endl;
                Buffer::inset_iterator it = bview->buffer()->inset_iterator_begin();
                Buffer::inset_iterator end = bview->buffer()->inset_iterator_end();
-               for ( ; it != end; ++it) {
+               for (; it != end; ++it) {
                        if (*it == inset) {
                                par = it.getPar();
                                pos = it.getPos();
@@ -94,8 +93,7 @@ int LyXText::workWidth(BufferView * bview, Inset * inset) const
        }
        
        LyXLayout const & layout =
-               textclasslist.Style(bview->buffer()->params.textclass,
-                                   par->getLayout());
+               textclasslist[bview->buffer()->params.textclass][par->layout()];
 
        if (layout.margintype != MARGIN_RIGHT_ADDRESS_BOX) {
                // Optimization here: in most cases, the real row is
@@ -476,18 +474,26 @@ void LyXText::drawInset(DrawRowParams & p, pos_type const pos)
        }
  
        LyXFont const & font = getFont(p.bv->buffer(), p.row->par(), pos);
+
        inset->update(p.bv, font, false);
        inset->draw(p.bv, font, p.yo + p.row->baseline(), p.x, p.cleared);
  
        if (!need_break_row && !inset_owner 
-               && p.bv->text->status() == CHANGED_IN_DRAW) {
+           && p.bv->text->status() == CHANGED_IN_DRAW) {
                Row * prev = p.row->previous();
                if (prev && prev->par() == p.row->par()) {
                        breakAgainOneRow(p.bv, prev);
-               } 
+                       if (prev->next() != p.row) {
+                               // breakAgainOneRow() has removed p.row
+                               p.row = 0;  // see what this breaks
+                               need_break_row = prev;
+                       } else {
+                               need_break_row = p.row;
+                       }
+               } else {
+                       need_break_row = p.row;
+               }
                setCursor(p.bv, cursor.par(), cursor.pos());
-               need_break_row = p.row;
        }
 }
 
@@ -573,7 +579,7 @@ void LyXText::drawArabicComposeChar(DrawRowParams & p, pos_type & vpos)
  
  
 void LyXText::drawChars(DrawRowParams & p, pos_type & vpos,
-       bool hebrew, bool arabic)
+                        bool hebrew, bool arabic)
 {
        pos_type pos = vis2log(vpos);
        pos_type const last = rowLastPrintable(p.row);
@@ -674,8 +680,8 @@ void LyXText::draw(DrawRowParams & p, pos_type & vpos)
 int LyXText::leftMargin(BufferView * bview, Row const * row) const
 {
        LyXTextClass const & tclass =
-               textclasslist.TextClass(bview->buffer()->params.textclass);
-       LyXLayout const & layout = tclass[row->par()->getLayout()];
+               textclasslist[bview->buffer()->params.textclass];
+       LyXLayout const & layout = tclass[row->par()->layout()];
        
        string parindent = layout.parindent; 
 
@@ -687,13 +693,13 @@ int LyXText::leftMargin(BufferView * bview, Row const * row) const
        // I have had this idea very late, so it seems to be a
        // later added hack and this is true
        if (!row->par()->getDepth()) {
-               if (!row->par()->getLayout()) {
+               if (row->par()->layout() == tclass.defaultLayoutName()) {
                        // find the previous same level paragraph
                        if (row->par()->previous()) {
                                Paragraph * newpar = row->par()
                                        ->depthHook(row->par()->getDepth());
                                if (newpar &&
-                                   tclass[newpar->getLayout()].nextnoindent)
+                                   tclass[newpar->layout()].nextnoindent)
                                        parindent.erase();
                        }
                }
@@ -705,9 +711,8 @@ int LyXText::leftMargin(BufferView * bview, Row const * row) const
                
                // make a corresponding row. Needed to call LeftMargin()
                
-               // check wether it is a sufficent paragraph 
-               if (newpar && tclass[newpar->getLayout()].isEnvironment())
-               {
+               // check wether it is a sufficent paragraph
+               if (newpar && tclass[newpar->layout()].isEnvironment()) {
                        Row dummyrow;
                        dummyrow.par(newpar);
                        dummyrow.pos(newpar->size());
@@ -719,14 +724,13 @@ int LyXText::leftMargin(BufferView * bview, Row const * row) const
                        // LeftMargin() is always called
                        row->par()->params().depth(0);
                }
-               
-               if (newpar && !row->par()->getLayout()) {
+
+               if (newpar && row->par()->layout() == tclass.defaultLayoutName()) {
                        if (newpar->params().noindent())
                                parindent.erase();
                        else
-                               parindent = tclass[newpar->getLayout()].parindent;
+                               parindent = tclass[newpar->layout()].parindent;
                }
-               
        }
        
        LyXFont const labelfont = getLabelFont(bview->buffer(), row->par());
@@ -830,26 +834,30 @@ int LyXText::leftMargin(BufferView * bview, Row const * row) const
                         && ! row->par()->isFirstInSequence()))
                    && align == LYX_ALIGN_BLOCK
                    && !row->par()->params().noindent()
-                   && (row->par()->layout ||
+                       // in tabulars and ert paragraphs are never indented!
+                       && (!row->par()->inInset() || !row->par()->inInset()->owner() ||
+                               (row->par()->inInset()->owner()->lyxCode() != Inset::TABULAR_CODE &&
+                                row->par()->inInset()->owner()->lyxCode() != Inset::ERT_CODE))
+                   && (row->par()->layout() != tclass.defaultLayoutName() ||
                        bview->buffer()->params.paragraph_separation ==
-                       BufferParams::PARSEP_INDENT))
+                       BufferParams::PARSEP_INDENT)) {
                        x += lyxfont::signedWidth(parindent,
                                                  tclass.defaultfont());
-               else if (layout.labeltype == LABEL_BIBLIO) {
+               else if (layout.labeltype == LABEL_BIBLIO) {
                        // ale970405 Right width for bibitems
                        x += bibitemMaxWidth(bview, tclass.defaultfont());
                }
        }
+       
        return x;
 }
 
 
 int LyXText::rightMargin(Buffer const * buf, Row const * row) const
 {
-       LyXTextClass const & tclass =
-               textclasslist.TextClass(buf->params.textclass);
-       LyXLayout const & layout = tclass[row->par()->getLayout()];
-               
+       LyXTextClass const & tclass = textclasslist[buf->params.textclass];
+       LyXLayout const & layout = tclass[row->par()->layout()];
+       
        int x = LYX_PAPER_MARGIN
                + lyxfont::signedWidth(tclass.rightmargin(),
                                       tclass.defaultfont());
@@ -870,8 +878,7 @@ int LyXText::rightMargin(Buffer const * buf, Row const * row) const
                // make a corresponding row. Needed to call LeftMargin()
                
                // check wether it is a sufficent paragraph
-               if (newpar
-                   && tclass[newpar->getLayout()].isEnvironment()) {
+               if (newpar && tclass[newpar->layout()].isEnvironment()) {
                        Row dummyrow;
                        dummyrow.par(newpar);
                        dummyrow.pos(0);
@@ -894,8 +901,7 @@ int LyXText::rightMargin(Buffer const * buf, Row const * row) const
 
 int LyXText::labelEnd(BufferView * bview, Row const * row) const
 {
-       if (textclasslist.Style(bview->buffer()->params.textclass,
-                               row->par()->getLayout()).margintype
+       if (textclasslist[bview->buffer()->params.textclass][row->par()->layout()].margintype
            == MARGIN_MANUAL) {
                Row tmprow;
                tmprow = *row;
@@ -928,10 +934,9 @@ LyXText::nextBreakPoint(BufferView * bview, Row const * row, int width) const
        pos_type const main_body =
                beginningOfMainBody(bview->buffer(), par);
        LyXLayout const & layout =
-               textclasslist.Style(bview->buffer()->params.textclass,
-                                   par->getLayout());
+               textclasslist[bview->buffer()->params.textclass][par->layout()];
        pos_type i = pos;
-
+       
        if (layout.margintype == MARGIN_RIGHT_ADDRESS_BOX) {
                /* special code for right address boxes, only newlines count */
                while (i < par->size()) {
@@ -955,29 +960,30 @@ LyXText::nextBreakPoint(BufferView * bview, Row const * row, int width) const
                while (doitonetime || ((x < width) && (i < last))) {
                        doitonetime = false;
                        char const c = par->getChar(i);
+                       Inset * in = 0;
+                       if (c == Paragraph::META_INSET)
+                               in = par->getInset(i);
                        if (IsNewlineChar(c)) {
                                last_separator = i;
                                x = width; // this means break
-                       } else if (c == Paragraph::META_INSET &&
-                                  par->getInset(i)) {
-                               
+                       } else if (in && !in->isChar()) {
                                // check wether a Display() inset is
                                // valid here. if not, change it to
                                // non-display
-                               if (par->getInset(i)->display() &&
+                               if (in->display() &&
                                    (layout.isCommand() ||
                                     (layout.labeltype == LABEL_MANUAL
-                                     && i < beginningOfMainBody(bview->buffer(), par)))) {
+                                     && i < beginningOfMainBody(bview->buffer(), par))))
+                               {
                                        // display istn't allowd
-                                       par->getInset(i)->display(false);
+                                       in->display(false);
                                        x += singleWidth(bview, par, i, c);
-                               } else if (par->getInset(i)->display() ||
-                                        par->getInset(i)->needFullRow()) {
+                               } else if (in->display() || in->needFullRow()) {
                                        // So break the line here
                                        if (i == pos) {
                                                if (pos < last-1) {
                                                        last_separator = i;
-                                                       if (IsLineSeparatorChar(par->getChar(i+1)))
+                                                       if (par->isLineSeparator(i+1))
                                                                ++last_separator;
                                                } else
                                                        last_separator = last; // to avoid extra rows
@@ -986,9 +992,19 @@ LyXText::nextBreakPoint(BufferView * bview, Row const * row, int width) const
                                        x = width;  // this means break
                                } else {
                                        x += singleWidth(bview, par, i, c);
+                                       // we have to check this separately as we could have a
+                                       // lineseparator and then the algorithm below would prefer
+                                       // that which IS wrong! We should always break on an inset
+                                       // if it's too long and not on the last separator.
+                                       // Maybe the only exeption is insets used as chars but
+                                       // then we would have to have a special function inside
+                                       // the inset to tell us this. Till then we leave it as
+                                       // it is now. (Jug 20020106)
+                                       if (pos < i && x >= width && last_separator >= 0)
+                                               last_separator = i - 1;
                                }
                        } else  {
-                               if (IsLineSeparatorChar(c))
+                               if (par->isLineSeparator(i))
                                        last_separator = i;
                                x += singleWidth(bview, par, i, c);
                        }
@@ -1003,8 +1019,12 @@ LyXText::nextBreakPoint(BufferView * bview, Row const * row, int width) const
                                        x = left_margin;
                        }
                }
+               if ((pos+1 < i) && (last_separator < 0) && (x >= width))
+                       last_separator = i - 2;
+               else if ((pos < i) && (last_separator < 0) && (x >= width))
+                       last_separator = i - 1;
                // end of paragraph is always a suitable separator
-               if (i == last && x < width)
+               else if (i == last && x < width)
                        last_separator = i;
        }
        
@@ -1033,8 +1053,7 @@ int LyXText::fill(BufferView * bview, Row * row, int paper_width) const
        pos_type const last = rowLastPrintable(row);
        
        // special handling of the right address boxes
-       if (textclasslist.Style(bview->buffer()->params.textclass,
-                               row->par()->getLayout()).margintype
+       if (textclasslist[bview->buffer()->params.textclass][row->par()->layout()].margintype
            == MARGIN_RIGHT_ADDRESS_BOX)
        {
                int const tmpfill = row->fill();
@@ -1044,8 +1063,7 @@ int LyXText::fill(BufferView * bview, Row * row, int paper_width) const
        } else
                w = leftMargin(bview, row);
        
-       LyXLayout const & layout = textclasslist.Style(bview->buffer()->params.textclass,
-                                                      row->par()->getLayout());
+       LyXLayout const & layout = textclasslist[bview->buffer()->params.textclass][row->par()->layout()];
        pos_type const main_body = 
                beginningOfMainBody(bview->buffer(), row->par());
        pos_type i = row->pos();
@@ -1206,8 +1224,7 @@ bool LyXText::hfillExpansion(Buffer const * buf, Row const * row_ptr,
                return true;
        
        // in some labels  it does not count
-       if (textclasslist.Style(buf->params.textclass,
-                               row_ptr->par()->getLayout()).margintype
+       if (textclasslist[buf->params.textclass][row_ptr->par()->layout()].margintype
            != MARGIN_MANUAL
            && pos < beginningOfMainBody(buf, row_ptr->par()))
                return false; 
@@ -1253,8 +1270,7 @@ void LyXText::setHeightOfRow(BufferView * bview, Row * row_ptr) const
        Paragraph * par = row_ptr->par();
        Paragraph * firstpar = row_ptr->par();
    
-       LyXLayout const & layout = textclasslist.Style(bview->buffer()->params.textclass,
-                                                      firstpar->getLayout());
+       LyXLayout const & layout = textclasslist[bview->buffer()->params.textclass][firstpar->layout()];
 
        // as max get the first character of this row then it can increes but not
        // decrees the height. Just some point to start with so we don't have to
@@ -1340,9 +1356,7 @@ void LyXText::setHeightOfRow(BufferView * bview, Row * row_ptr) const
                        {
                                maxasc += bview->buffer()->params.getDefSkip().inPixels(bview);
                        } else if (firstpar->previous() &&
-                                  textclasslist.Style(bview->buffer()->params.textclass,
-                                                      firstpar->previous()->
-                                                      getLayout()).isParagraph() &&
+                                  textclasslist[bview->buffer()->params.textclass][firstpar->previous()->layout()].isParagraph() &&
                                   firstpar->previous()->getDepth() == 0)
                        {
                                // is it right to use defskip here too? (AS)
@@ -1419,7 +1433,7 @@ void LyXText::setHeightOfRow(BufferView * bview, Row * row_ptr) const
                        Paragraph * prev = row_ptr->par()->previous();
                        if (prev)
                                prev = row_ptr->par()->depthHook(row_ptr->par()->getDepth());
-                       if (prev && prev->getLayout() == firstpar->getLayout() &&
+                       if (prev && prev->layout() == firstpar->layout() &&
                                prev->getDepth() == firstpar->getDepth() &&
                                prev->getLabelWidthString() == firstpar->getLabelWidthString())
                        {
@@ -1428,9 +1442,7 @@ void LyXText::setHeightOfRow(BufferView * bview, Row * row_ptr) const
                                tmptop = layout.topsep;
            
                                if (row_ptr->previous()->par()->getDepth() >= row_ptr->par()->getDepth())
-                                       tmptop -= textclasslist.Style(bview->buffer()->params.textclass,
-                                                                     row_ptr->previous()->par()->
-                                                                     getLayout()).bottomsep;
+                                       tmptop -= textclasslist[bview->buffer()->params.textclass][row_ptr->previous()->par()->layout()].bottomsep;
            
                                if (tmptop > 0)
                                        layoutasc = (tmptop * defaultHeight());
@@ -1443,13 +1455,12 @@ void LyXText::setHeightOfRow(BufferView * bview, Row * row_ptr) const
         
                        prev = row_ptr->par()->outerHook();
                        if (prev)  {
-                               maxasc += int(textclasslist.Style(bview->buffer()->params.textclass,
-                                             prev->getLayout()).parsep * defaultHeight());
+                               maxasc += int(textclasslist[bview->buffer()->params.textclass][prev->layout()].parsep * defaultHeight());
                        } else {
                                if (firstpar->previous() &&
                                        firstpar->previous()->getDepth() == 0 &&
-                                       firstpar->previous()->getLayout() !=
-                                       firstpar->getLayout())
+                                       firstpar->previous()->layout() !=
+                                       firstpar->layout())
                                {
                                        // avoid parsep
                                } else if (firstpar->previous()) {
@@ -1493,15 +1504,13 @@ void LyXText::setHeightOfRow(BufferView * bview, Row * row_ptr) const
                        float unusual = 0;
             
                        if (comparepar->getDepth() > nextpar->getDepth()) {
-                               usual = (textclasslist.Style(bview->buffer()->params.textclass,
-                                        comparepar->getLayout()).bottomsep * defaultHeight());
+                               usual = (textclasslist[bview->buffer()->params.textclass][comparepar->layout()].bottomsep * defaultHeight());
                                comparepar = comparepar->depthHook(nextpar->getDepth());
-                               if (comparepar->getLayout()!= nextpar->getLayout()
+                               if (comparepar->layout()!= nextpar->layout()
                                        || nextpar->getLabelWidthString() != 
                                        comparepar->getLabelWidthString())
                                {
-                                       unusual = (textclasslist.Style(bview->buffer()->params.textclass,
-                                                  comparepar->getLayout()).bottomsep * defaultHeight());
+                                       unusual = (textclasslist[bview->buffer()->params.textclass][comparepar->layout()].bottomsep * defaultHeight());
                                }
                                if (unusual > usual)
                                        layoutdesc = unusual;
@@ -1509,11 +1518,10 @@ void LyXText::setHeightOfRow(BufferView * bview, Row * row_ptr) const
                                        layoutdesc = usual;
                        } else if (comparepar->getDepth() ==  nextpar->getDepth()) {
                                
-                               if (comparepar->getLayout()!= nextpar->getLayout()
+                               if (comparepar->layout() != nextpar->layout()
                                        || nextpar->getLabelWidthString() != 
                                        comparepar->getLabelWidthString())
-                                       layoutdesc = int(textclasslist.Style(bview->buffer()->params.textclass,
-                                                                                                                comparepar->getLayout()).bottomsep * defaultHeight());
+                                       layoutdesc = int(textclasslist[bview->buffer()->params.textclass][comparepar->layout()].bottomsep * defaultHeight());
                        }
                }
        }
@@ -1532,6 +1540,8 @@ void LyXText::setHeightOfRow(BufferView * bview, Row * row_ptr) const
        float x = 0;
        if (layout.margintype != MARGIN_RIGHT_ADDRESS_BOX) {
                float dummy;
+               // this IS needed
+               row_ptr->width(maxwidth);
                prepareToPrint(bview, row_ptr, x, dummy, dummy, dummy, false);
        }
        row_ptr->width(int(maxwidth + x));
@@ -1644,7 +1654,7 @@ void LyXText::breakAgainOneRow(BufferView * bview, Row * row)
                        row = row->next();
                        row->height(0);
                } else  {
-                       row= row->next();
+                       row = row->next();
                        ++z;
                        if (row->pos() != z)
                                row->pos(z);
@@ -1671,16 +1681,16 @@ void LyXText::breakAgainOneRow(BufferView * bview, Row * row)
 
 void LyXText::breakParagraph(BufferView * bview, char keep_layout)
 {
-   LyXLayout const & layout =
-          textclasslist.Style(bview->buffer()->params.textclass,
-                              cursor.par()->getLayout());
+       LyXTextClass const & tclass =
+               textclasslist[bview->buffer()->params.textclass];
+   LyXLayout const & layout = tclass[cursor.par()->layout()];
 
    // this is only allowed, if the current paragraph is not empty or caption
    if ((cursor.par()->size() <= 0)
        && layout.labeltype!= LABEL_SENSITIVE)
           return;
    
-   setUndo(bview, Undo::INSERT,cursor.par(),cursor.par()->next()); 
+   setUndo(bview, Undo::FINISH, cursor.par(), cursor.par()->next()); 
 
    // Always break behind a space
    //
@@ -1702,10 +1712,10 @@ void LyXText::breakParagraph(BufferView * bview, char keep_layout)
    if (layout.labeltype == LABEL_SENSITIVE) {
      if (!cursor.pos())
             // set to standard-layout
-            cursor.par()->setLayout(0);
+            cursor.par()->applyLayout(tclass.defaultLayoutName());
      else
             // set to standard-layout
-            cursor.par()->next()->setLayout(0);
+            cursor.par()->next()->applyLayout(tclass.defaultLayoutName());
    }
    
    /* if the cursor is at the beginning of a row without prior newline, 
@@ -1771,18 +1781,15 @@ void LyXText::redoParagraph(BufferView * bview) const
  * same Paragraph one to the right and make a rebreak */
 void LyXText::insertChar(BufferView * bview, char c)
 {
-       setUndo(bview, Undo::INSERT,
-               cursor.par(), cursor.par()->next());
+       setUndo(bview, Undo::INSERT, cursor.par(), cursor.par()->next());
 
        // When the free-spacing option is set for the current layout,
        // disable the double-space checking
 
        bool const freeSpacing = 
-               textclasslist.Style(bview->buffer()->params.textclass,
-                              cursor.row()->par()->getLayout()).free_spacing ||
+               textclasslist[bview->buffer()->params.textclass][cursor.row()->par()->layout()].free_spacing ||
                cursor.row()->par()->isFreeSpacing();
 
-
        if (lyxrc.auto_number) {
                static string const number_operators = "+-/*";
                static string const number_unary_operators = "+-";
@@ -1799,7 +1806,7 @@ void LyXText::insertChar(BufferView * bview, char c)
                              getFont(bview->buffer(),
                                      cursor.par(),
                                      cursor.pos() - 1).number() == LyXFont::ON)
-                           )
+                          )
                                number(bview); // Set current_font.number to OFF
                } else if (IsDigit(c) &&
                           real_current_font.isVisibleRightToLeft()) {
@@ -1810,8 +1817,8 @@ void LyXText::insertChar(BufferView * bview, char c)
                                if (contains(number_unary_operators, c) &&
                                    (cursor.pos() == 1 ||
                                     cursor.par()->isSeparator(cursor.pos() - 2) ||
-                                    cursor.par()->isNewline(cursor.pos() - 2) )
-                                  ) {
+                                    cursor.par()->isNewline(cursor.pos() - 2))
+                                 ) {
                                        setCharFont(bview->buffer(),
                                                    cursor.par(),
                                                    cursor.pos() - 1,
@@ -1875,8 +1882,8 @@ void LyXText::insertChar(BufferView * bview, char c)
                        return;
                }
        } else if (IsNewlineChar(c)) {
-               if (cursor.par() == cursor.par()
-                   && cursor.pos() <= beginningOfMainBody(bview->buffer(), cursor.par())) {
+               if (cursor.pos() <= beginningOfMainBody(bview->buffer(),
+                                                       cursor.par())) {
                        charInserted();
                        return;
                }
@@ -1904,7 +1911,7 @@ void LyXText::insertChar(BufferView * bview, char c)
        Row * row = cursor.row();
        int y = cursor.y() - row->baseline();
        if (c != Paragraph::META_INSET) /* Here case LyXText::InsertInset 
-                                           * already insertet the character */
+                                        * already insertet the character */
                cursor.par()->insertChar(cursor.pos(), c);
        setCharFont(bview->buffer(), cursor.par(), cursor.pos(), rawtmpfont);
 
@@ -1920,8 +1927,11 @@ void LyXText::insertChar(BufferView * bview, char c)
        // Is there a break one row above
        if ((cursor.par()->isLineSeparator(cursor.pos())
             || cursor.par()->isNewline(cursor.pos())
+                || ((cursor.pos() < cursor.par()->size()) &&
+                        cursor.par()->isInset(cursor.pos()+1))
             || cursor.row()->fill() == -1)
-           && row->previous() && row->previous()->par() == row->par()) {
+           && row->previous() && row->previous()->par() == row->par())
+       {
                pos_type z = nextBreakPoint(bview,
                                                           row->previous(),
                                                           workWidth(bview));
@@ -1965,8 +1975,8 @@ void LyXText::insertChar(BufferView * bview, char c)
    
        // recalculate the fill of the row
        if (row->fill() >= 0)  /* needed because a newline
-                             * will set fill to -1. Otherwise
-                             * we would not get a rebreak! */
+                               * will set fill to -1. Otherwise
+                               * we would not get a rebreak! */
                row->fill(fill(bview, row, workWidth(bview)));
        if (row->fill() < 0) {
                refresh_y = y;
@@ -2020,8 +2030,7 @@ void LyXText::insertChar(BufferView * bview, char c)
                redoHeightOfParagraph(bview, cursor);
        } else {
                // now the special right address boxes
-               if (textclasslist.Style(bview->buffer()->params.textclass,
-                                  cursor.par()->getLayout()).margintype
+               if (textclasslist[bview->buffer()->params.textclass][cursor.par()->layout()].margintype
                    == MARGIN_RIGHT_ADDRESS_BOX) {
                        redoDrawingOfParagraph(bview, cursor); 
                }
@@ -2061,7 +2070,7 @@ void LyXText::prepareToPrint(BufferView * bview,
        fill_separator = 0;
        fill_label_hfill = 0;
 
-        bool const is_rtl =
+       bool const is_rtl =
                row->par()->isRightToLeftPar(bview->buffer()->params);
        if (is_rtl) {
                x = (workWidth(bview) > 0)
@@ -2070,12 +2079,12 @@ void LyXText::prepareToPrint(BufferView * bview,
                x = (workWidth(bview) > 0) ? leftMargin(bview, row) : 0;
        
        // is there a manual margin with a manual label
-       if (textclasslist.Style(bview->buffer()->params.textclass,
-                          row->par()->getLayout()).margintype == MARGIN_MANUAL
-           && textclasslist.Style(bview->buffer()->params.textclass,
-                             row->par()->getLayout()).labeltype == LABEL_MANUAL) {
-              
-               /* one more since labels are left aligned */ 
+       LyXTextClass const & tclass = textclasslist[bview->buffer()->params.textclass];
+       LyXLayout const & layout = tclass[row->par()->layout()];
+       
+       if (layout.margintype == MARGIN_MANUAL
+           && layout.labeltype == LABEL_MANUAL) {
+               // one more since labels are left aligned
                nlh = numberOfLabelHfills(bview->buffer(), row) + 1;
                if (nlh && !row->par()->getLabelWidthString().empty()) {
                        fill_label_hfill = labelFill(bview, row) / nlh;
@@ -2088,12 +2097,15 @@ void LyXText::prepareToPrint(BufferView * bview,
        if (nh) {
                if (w > 0)
                        fill_hfill = w / nh;
-       } else  {
+       // we don't have to look at the alignment if it is ALIGN_LEFT and
+       // if the row is already larger then the permitted width as then
+       // we force the LEFT_ALIGN'edness!
+       } else if (static_cast<int>(row->width()) < workWidth(bview)) {
                // is it block, flushleft or flushright? 
                // set x how you need it
                int align;
                if (row->par()->params().align() == LYX_ALIGN_LAYOUT) {
-                       align = textclasslist.Style(bview->buffer()->params.textclass, row->par()->getLayout()).align;
+                       align = layout.align;
                } else {
                        align = row->par()->params().align();
                }
@@ -2105,6 +2117,13 @@ void LyXText::prepareToPrint(BufferView * bview,
                    && (inset->display())) // || (inset->scroll() < 0)))
                    align = (inset->lyxCode() == Inset::MATHMACRO_CODE)
                        ? LYX_ALIGN_BLOCK : LYX_ALIGN_CENTER;
+               // ERT insets should always be LEFT ALIGNED on screen
+               inset = row->par()->inInset();
+               if (inset && inset->owner() &&
+                       inset->owner()->lyxCode() == Inset::ERT_CODE)
+               {
+                       align = LYX_ALIGN_LEFT;
+               }
                
                switch (align) {
            case LYX_ALIGN_BLOCK:
@@ -2139,14 +2158,11 @@ void LyXText::prepareToPrint(BufferView * bview,
                pos_type last = rowLast(row);
 
                if (main_body > 0 &&
-                   (main_body-1 > last || 
-                    !row->par()->isLineSeparator(main_body-1))) {
-                       LyXLayout const & layout =
-                               textclasslist.Style(bview->buffer()->params.textclass,
-                                                   row->par()->getLayout());
+                   (main_body - 1 > last || 
+                    !row->par()->isLineSeparator(main_body - 1))) {
                        x += lyxfont::width(layout.labelsep,
                                            getLabelFont(bview->buffer(), row->par()));
-                       if (main_body-1 <= last)
+                       if (main_body - 1 <= last)
                                x += fill_label_hfill;
                }
        }
@@ -2181,7 +2197,7 @@ void LyXText::cursorRightOneWord(BufferView * bview) const
                }
                // Advance through word.
                while (tmpcursor.pos() < tmpcursor.par()->size() &&
-                       tmpcursor.par()->isWord( tmpcursor.pos())) {
+                       tmpcursor.par()->isWord(tmpcursor.pos())) {
                  //     printf("Current pos2 %d", tmpcursor.pos()) ;
                        tmpcursor.pos(tmpcursor.pos() + 1);
                        ++steps;
@@ -2198,7 +2214,7 @@ void LyXText::cursorTab(BufferView * bview) const
            && !tmpcursor.par()->isNewline(tmpcursor.pos()))
         tmpcursor.pos(tmpcursor.pos() + 1);
 
-    if (tmpcursor.pos() == tmpcursor.par()->size()){
+    if (tmpcursor.pos() == tmpcursor.par()->size()) {
         if (tmpcursor.par()->next()) {
             tmpcursor.par(tmpcursor.par()->next());
             tmpcursor.pos(0);
@@ -2235,13 +2251,13 @@ void LyXText::cursorLeftOneWord(LyXCursor  & cur)  const
                || cur.par()->isHfill(cur.pos() - 1))) {
                cur.pos(cur.pos() - 1);
        } else if (!cur.pos()) {
-               if (cur.par()->previous()){
+               if (cur.par()->previous()) {
                        cur.par(cur.par()->previous());
                        cur.pos(cur.par()->size());
                }
        } else {                // Here, cur != 0 
                while (cur.pos() > 0 &&
-                      cur.par()->isWord(cur.pos()-1) )
+                      cur.par()->isWord(cur.pos()-1))
                        cur.pos(cur.pos() - 1);
        }
 }
@@ -2253,7 +2269,7 @@ void LyXText::getWord(LyXCursor & from, LyXCursor & to,
 {
        // first put the cursor where we wana start to select the word
        from = cursor;
-       switch(loc) {
+       switch (loc) {
        case WHOLE_WORD_STRICT:
                if (cursor.pos() == 0 || cursor.pos() == cursor.par()->size()
                    || cursor.par()->isSeparator(cursor.pos())
@@ -2285,7 +2301,7 @@ void LyXText::getWord(LyXCursor & from, LyXCursor & to,
        while (to.pos() < to.par()->size()
               && !to.par()->isSeparator(to.pos())
               && !to.par()->isKomma(to.pos())
-              && !to.par()->isHfill(to.pos()) )
+              && !to.par()->isHfill(to.pos()))
        {
                to.pos(to.pos() + 1);
        }
@@ -2302,7 +2318,7 @@ void LyXText::selectWord(BufferView * bview, word_location const loc)
        if (to == from)
                return;
        selection.cursor = cursor;
-       setCursor(bview, to.par(), to.pos() );
+       setCursor(bview, to.par(), to.pos());
        setSelection(bview);
 }
 
@@ -2331,9 +2347,7 @@ string const LyXText::selectNextWordToSpellcheck(BufferView * bview,
                        value += float(cursor.y())/float(height);
                        return str;
                }
-#warning Dekel please have a look on this one RTL? (Jug)
-#warning DEKEL!
-               // we have to go on checking so move cusor to the right
+               // we have to go on checking so move cusor to the next char
                if (cursor.pos() == cursor.par()->size()) {
                        if (!cursor.par()->next())
                                return str;
@@ -2380,7 +2394,7 @@ string const LyXText::selectNextWordToSpellcheck(BufferView * bview,
        }               
   
        // Update the value if we changed paragraphs
-       if (cursor.par() != tmppar){
+       if (cursor.par() != tmppar) {
                setCursor(bview, cursor.par(), cursor.pos());
                value = float(cursor.y())/float(height);
        }
@@ -2530,8 +2544,7 @@ void LyXText::changeRegionCase(BufferView * bview,
 {
        lyx::Assert(from <= to);
        
-       setUndo(bview, Undo::FINISH,
-               from.par(), to.par()->next());
+       setUndo(bview, Undo::FINISH, from.par(), to.par()->next());
 
        pos_type pos = from.pos();
        Paragraph * par = from.par();
@@ -2573,8 +2586,7 @@ void LyXText::transposeChars(BufferView & bview)
 {
        Paragraph * tmppar = cursor.par();
 
-       setUndo(&bview, Undo::FINISH,
-               tmppar, tmppar->next()); 
+       setUndo(&bview, Undo::FINISH, tmppar, tmppar->next()); 
 
        pos_type tmppos = cursor.pos();
 
@@ -2631,7 +2643,7 @@ void LyXText::Delete(BufferView * bview)
                // to make sure undo gets the right cursor position
                cursor = old_cursor;
                setUndo(bview, Undo::DELETE,
-                       cursor.par(), cursor.par()->next()); 
+                       cursor.par(), cursor.par()->next());
                cursor = tmpcursor;
                backspace(bview);
        }
@@ -2662,7 +2674,7 @@ void LyXText::backspace(BufferView * bview)
                        
                        if (cursor.par()->previous()) {
                                Paragraph * tmppar = cursor.par()->previous();
-                               if (cursor.par()->getLayout() == tmppar->getLayout()
+                               if (cursor.par()->layout() == tmppar->layout()
                                    && cursor.par()->getAlign() == tmppar->getAlign()) {
                                        // Inherit bottom DTD from the paragraph below.
                                        // (the one we are deleting)
@@ -2714,11 +2726,12 @@ void LyXText::backspace(BufferView * bview)
                  
                   Correction: Pasting is always allowed with standard-layout
                */
+               LyXTextClass const & tclass = textclasslist[bview->buffer()->params.textclass];
+               
                if (cursor.par() != tmppar
-                   && (cursor.par()->getLayout() == tmppar->getLayout()
-                       || tmppar->getLayout() == 0 /*standard*/)
-                   && cursor.par()->getAlign() == tmppar->getAlign())
-               {
+                   && (cursor.par()->layout() == tmppar->layout()
+                       || tmppar->layout() == tclass.defaultLayoutName())
+                   && cursor.par()->getAlign() == tmppar->getAlign()) {
                        removeParagraph(tmprow);
                        removeRow(tmprow);
                        cursor.par()->pasteParagraph(bview->buffer()->params);
@@ -2948,8 +2961,9 @@ void LyXText::backspace(BufferView * bview)
                redoHeightOfParagraph(bview, cursor);
        } else {
                // now the special right address boxes
-               if (textclasslist.Style(bview->buffer()->params.textclass,
-                                       cursor.par()->getLayout()).margintype == MARGIN_RIGHT_ADDRESS_BOX) {
+               if (textclasslist
+                   [bview->buffer()->params.textclass]
+                   [cursor.par()->layout()].margintype == MARGIN_RIGHT_ADDRESS_BOX) {
                        redoDrawingOfParagraph(bview, cursor); 
                }
        }
@@ -3082,8 +3096,10 @@ void LyXText::paintRowSelection(DrawRowParams & p)
                pos_type pos = vis2log(vpos);
                float const old_tmpx = tmpx;
                if (main_body > 0 && pos == main_body - 1) {
-                       LyXLayout const & layout = textclasslist.Style(buffer->params.textclass,
-                               par->getLayout());
+                       LyXLayout const & layout =
+                               textclasslist
+                               [buffer->params.textclass]
+                               [par->layout()];
                        LyXFont const lfont = getLabelFont(buffer, par);
                         
  
@@ -3182,14 +3198,14 @@ int LyXText::getLengthMarkerHeight(BufferView * bv, VSpace const & vsp) const
  
        LyXFont font;
        font.decSize();
-       int const min_size = std::max(3 * arrow_size,
+       int const min_size = max(3 * arrow_size,
                                      lyxfont::maxAscent(font)
                                      + lyxfont::maxDescent(font));
 
        if (vsp.length().len().value() < 0.0)
                return min_size;
        else 
-               return std::max(min_size, space_size);
+               return max(min_size, space_size);
 }
 
  
@@ -3303,9 +3319,9 @@ void LyXText::paintFirstRow(DrawRowParams & p)
                                  parparams.spaceTop(), p.yo + y_top);
        
        Buffer const * buffer = p.bv->buffer();
-       LyXLayout const & layout =
-               textclasslist.Style(buffer->params.textclass, par->getLayout());
+
+       LyXTextClass const & tclass = textclasslist[buffer->params.textclass];
+       LyXLayout const & layout = tclass[par->layout()];
 
        // think about the parskip
        // some parskips VERY EASY IMPLEMENTATION
@@ -3316,8 +3332,7 @@ void LyXText::paintFirstRow(DrawRowParams & p)
                                y_top += buffer->params.getDefSkip().inPixels(p.bv);
                        } else {
                                LyXLayout const & playout =
-                                       textclasslist.Style(buffer->params.textclass,
-                                               par->previous()->getLayout()); 
+                                       tclass[par->previous()->layout()]; 
                                if (playout.latextype == LATEX_PARAGRAPH
                                        && !par->previous()->getDepth()) {
                                        // is it right to use defskip here, too? (AS) 
@@ -3419,7 +3434,7 @@ void LyXText::paintFirstRow(DrawRowParams & p)
                        float x = p.x;
                        if (layout.labeltype == LABEL_CENTERED_TOP_ENVIRONMENT) {
                                x = ((is_rtl ? leftMargin(p.bv, p.row) : p.x)
-                                        + ww - rightMargin(buffer, p.row) ) / 2; 
+                                        + ww - rightMargin(buffer, p.row)) / 2; 
                                x -= lyxfont::width(str, font) / 2;
                        } else if (is_rtl) {
                                x = ww - leftMargin(p.bv, p.row) - 
@@ -3532,9 +3547,8 @@ void LyXText::paintLastRow(DrawRowParams & p)
        case END_LABEL_STATIC:
        {
                LyXFont font(LyXFont::ALL_SANE);
-               LyXTextClass::LayoutList::size_type layout = par->getLayout();
-               string const str = textclasslist.
-                       Style(buffer->params.textclass, layout).endlabelstring();
+               string const & layout = par->layout();
+               string const str = textclasslist[buffer->params.textclass][layout].endlabelstring();
                font = getLabelFont(buffer, par);
                int const x = is_rtl ?
                        int(p.x) - lyxfont::width(str, font)
@@ -3562,7 +3576,7 @@ void LyXText::paintRowText(DrawRowParams & p)
        }
        
        LyXLayout const & layout =
-               textclasslist.Style(buffer->params.textclass, par->getLayout());
+               textclasslist[buffer->params.textclass][par->layout()];
 
        pos_type vpos = p.row->pos();
        while (vpos <= last) {
@@ -3699,8 +3713,8 @@ LyXText::getColumnNearX(BufferView * bview, Row * row, int & x,
        pos_type last = rowLastPrintable(row);
        pos_type c = 0;
        LyXLayout const & layout =
-               textclasslist.Style(bview->buffer()->params.textclass,
-                                   row->par()->getLayout());
+               textclasslist[bview->buffer()->params.textclass][
+                                   row->par()->layout()];
        bool left_side = false;
 
        pos_type main_body = beginningOfMainBody(bview->buffer(), row->par());
@@ -3758,8 +3772,8 @@ LyXText::getColumnNearX(BufferView * bview, Row * row, int & x,
        if (row->pos() > last)  // Row is empty?
                c = row->pos();
        else if (lastrow &&
-                ( ( rtl &&  left_side && vc == row->pos() && x < tmpx - 5) ||
-                  (!rtl && !left_side && vc == last + 1   && x > tmpx + 5) ))
+                ((rtl &&  left_side && vc == row->pos() && x < tmpx - 5) ||
+                  (!rtl && !left_side && vc == last + 1   && x > tmpx + 5)))
                c = last + 1;
        else if (vc == row->pos()) {
                c = vis2log(vc);
@@ -3819,6 +3833,7 @@ Row * LyXText::getRow(Paragraph * par, pos_type pos, int & y) const
 
 Row * LyXText::getRowNearY(int & y) const
 {
+#if 1
        // If possible we should optimize this method. (Lgb)
        Row * tmprow = firstrow;
        int tmpy = 0;
@@ -3829,7 +3844,53 @@ Row * LyXText::getRowNearY(int & y) const
        }
        
        y = tmpy;   // return the real y
+
+       //lyxerr << "returned y = " << y << endl;
+       
        return tmprow;
+#else
+       // Search from the current cursor position.
+       
+       Row * tmprow = cursor.row();
+       int tmpy = cursor.y() - tmprow->baseline();
+
+       lyxerr << "cursor.y() = " << tmpy << endl;
+       lyxerr << "tmprow->height() = " << tmprow->height() << endl;
+       lyxerr << "tmprow->baseline() = " << tmprow->baseline() << endl;
+       lyxerr << "first = " << first << endl;
+       lyxerr << "y = " << y << endl;
+       
+       if (y < tmpy) {
+               lyxerr << "up" << endl;
+#if 0
+               while (tmprow && tmpy - tmprow->height() >= y) {
+                       tmpy -= tmprow->height();
+                       tmprow = tmprow->previous();
+               }
+#else
+               do {
+                       tmpy -= tmprow->height();
+                       tmprow = tmprow->previous();
+               } while (tmprow && tmpy - tmprow->height() >= y);
+#endif
+       } else if (y > tmpy) {
+               lyxerr << "down" << endl;
+               
+               while (tmprow->next() && tmpy + tmprow->height() <= y) {
+                       tmpy += tmprow->height();
+                       tmprow = tmprow->next();
+               }
+       } else {
+               lyxerr << "equal" << endl;
+       }
+       
+       y = tmpy; // return the real y
+
+       lyxerr << "returned y = " << y << endl;
+       
+       return tmprow;
+
+#endif
 }