]> git.lyx.org Git - lyx.git/blobdiff - src/text.C
More fixes to insettabular/text (and some missing features added).
[lyx.git] / src / text.C
index e9ef5677e6cb37ca4f54771df4950527289a4bc1..be6bb3b6a327410124bc246034bbc5a53e213455 100644 (file)
@@ -33,6 +33,7 @@
 #include "font.h"
 #include "encoding.h"
 #include "lyxscreen.h"
+#include "bufferview_funcs.h"
 
 using std::max;
 using std::min;
@@ -56,30 +57,30 @@ int LyXText::workWidth(BufferView * bview) const
 unsigned char LyXText::TransformChar(unsigned char c, LyXParagraph * par,
                        LyXParagraph::size_type pos) const
 {
-       if (!Encoding::is_arabic(c))
+       if (!Encodings::is_arabic(c))
                if (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 && isdigit(c))
                        return c + (0xb0 - '0');
                else
                        return c;
 
-       unsigned char prev_char = pos > 0 ? par->GetChar(pos-1) : ' ';
+       unsigned char const prev_char = pos > 0 ? par->GetChar(pos-1) : ' ';
        unsigned char next_char = ' ';
        for (LyXParagraph::size_type i = pos+1; i < par->Last(); ++i)
-               if (!Encoding::IsComposeChar_arabic(par->GetChar(i))) {
+               if (!Encodings::IsComposeChar_arabic(par->GetChar(i))) {
                        next_char = par->GetChar(i);
                        break;
                }
 
-       if (Encoding::is_arabic(next_char)) {
-               if (Encoding::is_arabic(prev_char))
-                       return Encoding::TransformChar(c, Encoding::FORM_MEDIAL);
+       if (Encodings::is_arabic(next_char)) {
+               if (Encodings::is_arabic(prev_char))
+                       return Encodings::TransformChar(c, Encodings::FORM_MEDIAL);
                else
-                       return Encoding::TransformChar(c, Encoding::FORM_INITIAL);
+                       return Encodings::TransformChar(c, Encodings::FORM_INITIAL);
        } else {
-               if (Encoding::is_arabic(prev_char))
-                       return Encoding::TransformChar(c, Encoding::FORM_FINAL);
+               if (Encodings::is_arabic(prev_char))
+                       return Encodings::TransformChar(c, Encodings::FORM_FINAL);
                else
-                       return Encoding::TransformChar(c, Encoding::FORM_ISOLATED);
+                       return Encodings::TransformChar(c, Encodings::FORM_ISOLATED);
        }
 }
 
@@ -127,12 +128,12 @@ int LyXText::SingleWidth(BufferView * bview, LyXParagraph * par,
                        if (font.language()->lang() == "arabic" &&
                            (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 ||
                             lyxrc.font_norm_type == LyXRC::ISO_10646_1))
-                               if (Encoding::IsComposeChar_arabic(c))
+                               if (Encodings::IsComposeChar_arabic(c))
                                        return 0;
                                else
                                        c = TransformChar(c, par, pos);
                        else if (font.language()->lang() == "hebrew" &&
-                                Encoding::IsComposeChar_hebrew(c))
+                                Encodings::IsComposeChar_hebrew(c))
                                return 0;
                }
                return lyxfont::width(c, font);
@@ -206,7 +207,7 @@ LyXParagraph::size_type LyXText::RowLast(Row const * row) const
 
 LyXParagraph::size_type LyXText::RowLastPrintable(Row const * row) const
 {
-       LyXParagraph::size_type last = RowLast(row);
+       LyXParagraph::size_type const last = RowLast(row);
        if (last >= row->pos()
            && row->next()
            && row->next()->par() == row->par()
@@ -247,28 +248,39 @@ void LyXText::ComputeBidiTables(Buffer const * buf, Row * row) const
        log2vis_list[bidi_end + 1 - bidi_start] = -1;
 
        LyXParagraph::size_type stack[2];
-       bool rtl_par = row->par()->getParLanguage(buf->params)->RightToLeft();
+       bool const rtl_par =
+               row->par()->getParLanguage(buf->params)->RightToLeft();
        int level = 0;
        bool rtl = false;
        bool rtl0 = false;
-       LyXParagraph::size_type main_body = BeginningOfMainBody(buf, row->par());
+       LyXParagraph::size_type const main_body =
+               BeginningOfMainBody(buf, row->par());
 
        for (LyXParagraph::size_type lpos = bidi_start;
             lpos <= bidi_end; ++lpos) {
                bool is_space = row->par()->IsLineSeparator(lpos);
-               LyXParagraph::size_type pos =
+               LyXParagraph::size_type const pos =
                        (is_space && lpos + 1 <= bidi_end &&
                         !row->par()->IsLineSeparator(lpos + 1) &&
                         !row->par()->IsNewline(lpos + 1))
                        ? lpos + 1 : lpos;
                LyXFont font = row->par()->GetFontSettings(buf->params, pos);
+               if (pos != lpos && 0 < lpos && rtl0 && font.isRightToLeft() &&
+                   font.number() == LyXFont::ON &&
+                   row->par()->GetFontSettings(buf->params, lpos-1).number()
+                   == LyXFont::ON) {
+                       font = row->par()->GetFontSettings(buf->params, lpos);
+                       is_space = false;
+               }
+
+
                bool new_rtl = font.isVisibleRightToLeft();
                bool new_rtl0 = font.isRightToLeft();
                int new_level;
 
                if (lpos == main_body - 1
                    && row->pos() < main_body - 1
-                   && row->par()->IsLineSeparator(lpos)) {
+                   && is_space) {
                        new_level = (rtl_par) ? 1 : 0;
                        new_rtl = new_rtl0 = rtl_par;
                } else if (new_rtl0)
@@ -311,7 +323,7 @@ void LyXText::ComputeBidiTables(Buffer const * buf, Row * row) const
        }
 
        while (level > 0) {
-               LyXParagraph::size_type old_lpos = stack[--level];
+               LyXParagraph::size_type const old_lpos = stack[--level];
                int delta = bidi_end - old_lpos;
                if (level % 2)
                        delta = -delta;
@@ -332,18 +344,18 @@ void LyXText::ComputeBidiTables(Buffer const * buf, Row * row) const
 bool LyXText::IsBoundary(Buffer const * buf, LyXParagraph * par,
                         LyXParagraph::size_type pos) const
 {
-       if (!lyxrc.rtl_support)
-               return false;    // This is just for speedup
+       if (!lyxrc.rtl_support || pos == 0)
+               return false;
 
-       if (!bidi_InRange(pos - 1))
+       if (!bidi_InRange(pos - 1)) {
+               lyxerr << "LyXText::IsBoundary This shouldn't happen." << endl;
                return false;
+       }
 
        bool const rtl = bidi_level(pos - 1) % 2;
-       bool rtl2 = rtl;
-       if (pos == par->Last())
-               rtl2 = par->isRightToLeftPar(buf->params);
-       else if (bidi_InRange(pos))
-               rtl2 = bidi_level(pos) % 2;
+       bool rtl2 = bidi_InRange(pos)
+               ? bidi_level(pos) % 2
+               : par->isRightToLeftPar(buf->params);
        return rtl != rtl2;
 }
 
@@ -356,11 +368,9 @@ bool LyXText::IsBoundary(Buffer const * buf, LyXParagraph * par,
                return false;    // This is just for speedup
 
        bool const rtl = font.isVisibleRightToLeft();
-       bool rtl2 = rtl;
-       if (pos == par->Last())
-               rtl2 = par->isRightToLeftPar(buf->params);
-       else if (bidi_InRange(pos))
-               rtl2 =  bidi_level(pos) % 2;
+       bool rtl2 = bidi_InRange(pos)
+               ? bidi_level(pos) % 2
+               : par->isRightToLeftPar(buf->params);
        return rtl != rtl2;
 }
 
@@ -378,10 +388,10 @@ void LyXText::draw(BufferView * bview, Row const * row,
        if (IsNewlineChar(c)) {
                ++vpos;
                // Draw end-of-line marker
-               LyXFont font = GetFont(bview->buffer(), row->par(), pos);
-               int wid = lyxfont::width('n', font);
-               int asc = lyxfont::maxAscent(font);
-               int y = offset + row->baseline();
+               LyXFont const font = GetFont(bview->buffer(), row->par(), pos);
+               int const wid = lyxfont::width('n', font);
+               int const asc = lyxfont::maxAscent(font);
+               int const y = offset + row->baseline();
                int xp[3], yp[3];
                
                if (bidi_level(pos) % 2 == 0) {
@@ -471,8 +481,8 @@ void LyXText::draw(BufferView * bview, Row const * row,
                font.decSize();
          
                // calculate the position of the footnotemark
-               int y = (row->baseline() - lyxfont::maxAscent(font2) 
-                        + lyxfont::maxAscent(font));
+               int const y = (row->baseline() - lyxfont::maxAscent(font2) 
+                              + lyxfont::maxAscent(font));
          
                font.setColor(LColor::footnote);
 
@@ -494,16 +504,18 @@ void LyXText::draw(BufferView * bview, Row const * row,
 //                     tmpinset->update(bview, font, false);
                        tmpinset->draw(bview, font, offset+row->baseline(), x,
                                       cleared);
+#ifdef SEEMS_TO_BE_NOT_NEEDED
                        if (status == CHANGED_IN_DRAW) {
                                UpdateInset(bview, tmpinset);
                                status = CHANGED_IN_DRAW;
                        }
+#endif
                }
                ++vpos;
 
                if (lyxrc.mark_foreign_language &&
-                   font.language() != bview->buffer()->params.language_info) {
-                       int y = offset + row->height() - 1;
+                   font.language() != bview->buffer()->params.language) {
+                       int const y = offset + row->height() - 1;
                        pain.line(int(tmpx), y, int(x), y,
                                  LColor::language);
                }
@@ -528,17 +540,20 @@ void LyXText::draw(BufferView * bview, Row const * row,
        textstring = c;
        ++vpos;
 
-       LyXParagraph::size_type last = RowLastPrintable(row);
+       LyXParagraph::size_type const last = RowLastPrintable(row);
 
        if (font.language()->lang() == "hebrew") {
-               if (Encoding::IsComposeChar_hebrew(c)) {
-                       int width = lyxfont::width(c, font2);
+               if (Encodings::IsComposeChar_hebrew(c)) {
+                       int const width = lyxfont::width(c, font2);
                        int dx = 0;
                        for (LyXParagraph::size_type i = pos-1; i >= 0; --i) {
                                c = row->par()->GetChar(i);
-                               if (!Encoding::IsComposeChar_hebrew(c)) {
+                               if (!Encodings::IsComposeChar_hebrew(c)) {
                                        if (IsPrintableNonspace(c)) {
-                                               int width2 = SingleWidth(bview, row->par(), i, c);
+                                               int const width2 =
+                                                       SingleWidth(bview,
+                                                                   row->par(),
+                                                                   i, c);
                                                dx = (c == 'ø' || c == 'ã') // dalet / resh
                                                        ? width2 - width : (width2 - width) / 2;
                                        }
@@ -552,7 +567,7 @@ void LyXText::draw(BufferView * bview, Row const * row,
                        while (vpos <= last &&
                               (pos = vis2log(vpos)) >= 0
                               && IsPrintableNonspace(c = row->par()->GetChar(pos))
-                              && !Encoding::IsComposeChar_hebrew(c)
+                              && !Encodings::IsComposeChar_hebrew(c)
                               && font2 == GetFont(bview->buffer(), row->par(), pos)) {
                                textstring += c;
                                ++vpos;
@@ -565,16 +580,19 @@ void LyXText::draw(BufferView * bview, Row const * row,
        } else if (font.language()->lang() == "arabic" &&
                   (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 ||
                    lyxrc.font_norm_type == LyXRC::ISO_10646_1)) {
-               if (Encoding::IsComposeChar_arabic(c)) {
+               if (Encodings::IsComposeChar_arabic(c)) {
                        c = TransformChar(c, row->par(), pos);
                        textstring = c;
-                       int width = lyxfont::width(c, font2);
+                       int const width = lyxfont::width(c, font2);
                        int dx = 0;
                        for (LyXParagraph::size_type i = pos-1; i >= 0; --i) {
                                c = row->par()->GetChar(i);
-                               if (!Encoding::IsComposeChar_arabic(c)) {
+                               if (!Encodings::IsComposeChar_arabic(c)) {
                                        if (IsPrintableNonspace(c)) {
-                                               int width2 = SingleWidth(bview, row->par(), i, c);
+                                               int const width2 =
+                                                       SingleWidth(bview,
+                                                                   row->par(),
+                                                                   i, c);
                                                dx = (width2 - width) / 2;
                                        }
                                        break;
@@ -588,7 +606,7 @@ void LyXText::draw(BufferView * bview, Row const * row,
                        while (vpos <= last &&
                               (pos = vis2log(vpos)) >= 0
                               && IsPrintableNonspace(c = row->par()->GetChar(pos))
-                              && !Encoding::IsComposeChar_arabic(c)
+                              && !Encodings::IsComposeChar_arabic(c)
                               && font2 == GetFont(bview->buffer(), row->par(), pos)) {
                                c = TransformChar(c, row->par(), pos);
                                textstring += c;
@@ -613,8 +631,8 @@ void LyXText::draw(BufferView * bview, Row const * row,
        }
 
        if (lyxrc.mark_foreign_language &&
-           font.language() != bview->buffer()->params.language_info) {
-               int y = offset + row->height() - 1;
+           font.language() != bview->buffer()->params.language) {
+               int const y = offset + row->height() - 1;
                pain.line(int(tmpx), y, int(x), y,
                          LColor::language);
        }
@@ -946,7 +964,7 @@ int LyXText::RightMargin(Buffer const * buf, Row const * row) const
 }
 
 
-int LyXText::LabelEnd (BufferView * bview, Row const * row) const
+int LyXText::LabelEnd(BufferView * bview, Row const * row) const
 {
        if (textclasslist.Style(bview->buffer()->params.textclass,
                                row->par()->GetLayout()).margintype
@@ -968,17 +986,20 @@ LyXParagraph::size_type
 LyXText::NextBreakPoint(BufferView * bview, Row const * row, int width) const
 {
        LyXParagraph * par = row->par();
-       LyXParagraph::size_type pos = row->pos();
 
        if (width < 0)
                return par->Last();
 
+       LyXParagraph::size_type const pos = row->pos();
+
+
        // position of the last possible breakpoint 
        // -1 isn't a suitable value, but a flag
        LyXParagraph::size_type last_separator = -1;
        width -= RightMargin(bview->buffer(), row);
        
-       LyXParagraph::size_type main_body = BeginningOfMainBody(bview->buffer(), par);
+       LyXParagraph::size_type const main_body =
+               BeginningOfMainBody(bview->buffer(), par);
        LyXLayout const & layout =
                textclasslist.Style(bview->buffer()->params.textclass, par->GetLayout());
        LyXParagraph::size_type i = pos;
@@ -1005,7 +1026,7 @@ LyXText::NextBreakPoint(BufferView * bview, Row const * row, int width) const
                bool doitonetime = true;
                while (doitonetime || ((x < width) && (i < last))) {
                        doitonetime = false;
-                       char c = par->GetChar(i);
+                       char const c = par->GetChar(i);
                        if (IsNewlineChar(c)) {
                                last_separator = i;
                                x = width; // this means break
@@ -1017,8 +1038,8 @@ LyXText::NextBreakPoint(BufferView * bview, Row const * row, int width) const
                                // non-display
                                if (par->GetInset(i)->display() &&
                                    (layout.isCommand() ||
-                                    ((layout.labeltype == LABEL_MANUAL) &&
-                                     (i < BeginningOfMainBody(bview->buffer(), par))))) {
+                                    (layout.labeltype == LABEL_MANUAL
+                                     && i < BeginningOfMainBody(bview->buffer(), par)))) {
                                        // display istn't allowd
                                        par->GetInset(i)->display(false);
                                        x += SingleWidth(bview, par, i, c);
@@ -1086,13 +1107,13 @@ int LyXText::Fill(BufferView * bview, Row * row, int paper_width) const
 
        int w;
        // get the pure distance
-       LyXParagraph::size_type last = RowLastPrintable(row);
+       LyXParagraph::size_type const last = RowLastPrintable(row);
        
        // special handling of the right address boxes
        if (textclasslist.Style(bview->buffer()->params.textclass,
                                row->par()->GetLayout()).margintype
            == MARGIN_RIGHT_ADDRESS_BOX) {
-               int tmpfill = row->fill();
+               int const tmpfill = row->fill();
                row->fill(0); // the minfill in MarginLeft()
                w = LeftMargin(bview, row);
                row->fill(tmpfill);
@@ -1101,7 +1122,7 @@ int LyXText::Fill(BufferView * bview, Row * row, int paper_width) const
        
        LyXLayout const & layout = textclasslist.Style(bview->buffer()->params.textclass,
                                                       row->par()->GetLayout());
-       LyXParagraph::size_type main_body = 
+       LyXParagraph::size_type const main_body = 
                BeginningOfMainBody(bview->buffer(), row->par());
        LyXParagraph::size_type i = row->pos();
 
@@ -1121,7 +1142,7 @@ int LyXText::Fill(BufferView * bview, Row * row, int paper_width) const
                w += lyxfont::width(layout.labelsep, GetFont(bview->buffer(), row->par(), -2));
                if (last >= 0 && row->par()->IsLineSeparator(last))
                        w -= SingleWidth(bview, row->par(), last);
-               int left_margin = LabelEnd(bview, row);
+               int const left_margin = LabelEnd(bview, row);
                if (w < left_margin)
                        w = left_margin;
        }
@@ -1281,7 +1302,7 @@ void LyXText::SetHeightOfRow(BufferView * bview, Row * row_ptr) const
 
    /* this must not happen before the currentrow for clear reasons.
       so the trick is just to set the current row onto this row */
-   long unused_y;
+   int unused_y;
    GetRow(row_ptr->par(), row_ptr->pos(), unused_y);
 
    /* ok , let us initialize the maxasc and maxdesc value. 
@@ -1303,7 +1324,7 @@ void LyXText::SetHeightOfRow(BufferView * bview, Row * row_ptr) const
                                                  firstpar->GetLayout());
    
    LyXFont font = GetFont(bview->buffer(), par, par->Last() - 1);
-   LyXFont::FONT_SIZE size = font.size();
+   LyXFont::FONT_SIZE const size = font.size();
    font = GetFont(bview->buffer(), par, -1);
    font.setSize(size);
 
@@ -1323,7 +1344,7 @@ void LyXText::SetHeightOfRow(BufferView * bview, Row * row_ptr) const
    int maxdesc = int(lyxfont::maxDescent(font) *
                    layout.spacing.getValue() *
                    spacing_val);
-   int pos_end = RowLast(row_ptr);
+   int const pos_end = RowLast(row_ptr);
    int labeladdon = 0;
    int maxwidth = 0;
 
@@ -1348,7 +1369,7 @@ void LyXText::SetHeightOfRow(BufferView * bview, Row * row_ptr) const
    // Check if any custom fonts are larger (Asger)
    // This is not completely correct, but we can live with the small,
    // cosmetic error for now.
-   LyXFont::FONT_SIZE maxsize =
+   LyXFont::FONT_SIZE const maxsize =
           row_ptr->par()->HighestFontInRange(row_ptr->pos(),
                                              pos_end);
    if (maxsize > font.size()) {
@@ -1623,7 +1644,7 @@ void LyXText::BreakAgain(BufferView * bview, Row * row) const
        LyXParagraph::size_type z = NextBreakPoint(bview, row, workWidth(bview));
       Row * tmprow = row;
       
-      if (z < row->par()->Last() ) {
+      if (z < row->par()->Last()) {
         if (!row->next() || (row->next() && row->next()->par() != row->par())) {
                 // insert a new row
            ++z;
@@ -1667,7 +1688,7 @@ void LyXText::BreakAgainOneRow(BufferView * bview, Row * row)
        LyXParagraph::size_type z = NextBreakPoint(bview, row, workWidth(bview));
        Row * tmprow = row;
        
-       if (z < row->par()->Last() ) {
+       if (z < row->par()->Last()) {
                if (!row->next()
                    || (row->next() && row->next()->par() != row->par())) {
                        /* insert a new row */ 
@@ -1754,8 +1775,8 @@ void LyXText::BreakParagraph(BufferView * bview, char keep_layout)
     * move one row up! 
     * This touches only the screen-update. Otherwise we would may have
     * an empty row on the screen */
-   if (cursor.pos() && !cursor.row()->par()->IsNewline(cursor.row()->pos() - 1) &&
-       cursor.row()->pos() == cursor.pos()) {
+   if (cursor.pos() && !cursor.row()->par()->IsNewline(cursor.row()->pos() - 1)
+       && cursor.row()->pos() == cursor.pos()) {
           CursorLeft(bview);
    } 
    
@@ -1884,10 +1905,55 @@ void LyXText::InsertChar(BufferView * bview, char c)
        // When the free-spacing option is set for the current layout,
        // disable the double-space checking
 
-       bool freeSpacing = 
+       bool const freeSpacing = 
                textclasslist.Style(bview->buffer()->params.textclass,
                               cursor.row()->par()->GetLayout()).free_spacing;
 
+
+       if (lyxrc.auto_number) {
+               if (current_font.number() == LyXFont::ON) {
+                       if (!isdigit(c) && !strchr("+-/*", c) &&
+                           !(strchr(".,",c) &&
+                             cursor.pos() >= 1 &&
+                             cursor.pos() < cursor.par()->size() &&
+                             GetFont(bview->buffer(),
+                                     cursor.par(),
+                                     cursor.pos()).number() == LyXFont::ON &&
+                             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()) {
+                       Number(bview); // Set current_font.number to ON
+
+                       if (cursor.pos() > 0) {
+                               char const c = cursor.par()->GetChar(cursor.pos() - 1);
+                               if (strchr("+-",c) &&
+                                   (cursor.pos() == 1 ||
+                                    cursor.par()->IsSeparator(cursor.pos() - 2) ||
+                                    cursor.par()->IsNewline(cursor.pos() - 2) )
+                                  ) {
+                                       SetCharFont(bview->buffer(),
+                                                   cursor.par(),
+                                                   cursor.pos() - 1,
+                                                   current_font);
+                               } else if (strchr(".,", c) &&
+                                          cursor.pos() >= 2 &&
+                                          GetFont(bview->buffer(),
+                                                  cursor.par(),
+                                                  cursor.pos()-2).number() == LyXFont::ON) {
+                                       SetCharFont(bview->buffer(),
+                                                   cursor.par(),
+                                                   cursor.pos() - 1,
+                                                   current_font);
+                               }
+                       }
+               }
+       }
+
+
        /* First check, if there will be two blanks together or a blank at 
          the beginning of a paragraph. 
          I decided to handle blanks like normal characters, the main 
@@ -1907,7 +1973,7 @@ void LyXText::InsertChar(BufferView * bview, char c)
                                     * current font */
 
        // Get the font that is used to calculate the baselineskip
-       LyXParagraph::size_type lastpos = cursor.par()->Last();
+       LyXParagraph::size_type const lastpos = cursor.par()->Last();
        LyXFont rawparfont = cursor.par()->GetFontSettings(bview->buffer()->params,
                                                           lastpos - 1);
 
@@ -1962,7 +2028,7 @@ void LyXText::InsertChar(BufferView * bview, char c)
 
        // get the cursor row fist
        Row * row = cursor.row();
-       long y = cursor.y() - row->baseline();
+       int y = cursor.y() - row->baseline();
        if (c != LyXParagraph::META_INSET) /* Here case LyXText::InsertInset 
                                            * already insertet the character */
                cursor.par()->InsertChar(cursor.pos(), c);
@@ -1985,7 +2051,7 @@ void LyXText::InsertChar(BufferView * bview, char c)
                LyXParagraph::size_type z = NextBreakPoint(bview,
                                                           row->previous(),
                                                           workWidth(bview));
-               if ( z >= row->pos()) {
+               if (z >= row->pos()) {
                        row->pos(z + 1);
                        
                        // set the dimensions of the row above
@@ -2028,7 +2094,7 @@ void LyXText::InsertChar(BufferView * bview, char c)
                              * will set fill to -1. Otherwise
                              * we would not get a rebreak! */
                row->fill(Fill(bview, row, workWidth(bview)));
-       if (row->fill() < 0 ) {
+       if (row->fill() < 0) {
                refresh_y = y;
                refresh_row = row; 
                refresh_x = cursor.x();
@@ -2047,6 +2113,10 @@ void LyXText::InsertChar(BufferView * bview, char c)
 
                SetCursor(bview, cursor.par(), cursor.pos() + 1, false,
                          cursor.boundary());
+               if (IsBoundary(bview->buffer(), cursor.par(), cursor.pos())
+                   != cursor.boundary())
+                       SetCursor(bview, cursor.par(), cursor.pos(), false,
+                         !cursor.boundary());
                if (row->next() && row->next()->par() == row->par())
                        need_break_row = row->next();
                else
@@ -2057,7 +2127,7 @@ void LyXText::InsertChar(BufferView * bview, char c)
                refresh_row = row;
                refresh_pos = cursor.pos();
                
-               int tmpheight = row->height();
+               int const tmpheight = row->height();
                SetHeightOfRow(bview, row);
                if (tmpheight == row->height())
                        status = LyXText::NEED_VERY_LITTLE_REFRESH;
@@ -2116,7 +2186,8 @@ void LyXText::PrepareToPrint(BufferView * bview,
        fill_separator = 0;
        fill_label_hfill = 0;
 
-        bool is_rtl = row->par()->isRightToLeftPar(bview->buffer()->params);
+        bool const is_rtl =
+               row->par()->isRightToLeftPar(bview->buffer()->params);
 #ifndef NEW_INSETS
 
        if (is_rtl) {
@@ -2172,7 +2243,8 @@ void LyXText::PrepareToPrint(BufferView * bview,
           if (row->par()->GetChar(row->pos()) == LyXParagraph::META_INSET
               && (inset=row->par()->GetInset(row->pos()))
               && (inset->display())) // || (inset->scroll() < 0)))
-            align = LYX_ALIGN_CENTER;
+            align = (inset->LyxCode() == Inset::MATHMACRO_CODE)
+                    ? LYX_ALIGN_BLOCK : LYX_ALIGN_CENTER;
 
           switch (align) {
            case LYX_ALIGN_BLOCK:
@@ -2239,7 +2311,7 @@ void LyXText::CursorRightOneWord(BufferView * bview) const
                int steps = 0;
 
                // Skip through initial nonword stuff.
-               while ( tmpcursor.pos() < tmpcursor.par()->Last() &&
+               while (tmpcursor.pos() < tmpcursor.par()->Last() &&
                        ! tmpcursor.par()->IsWord( tmpcursor.pos() ) ) 
                {
                  //    printf("Current pos1 %d", tmpcursor.pos()) ;
@@ -2247,7 +2319,7 @@ void LyXText::CursorRightOneWord(BufferView * bview) const
                        ++steps;
                }
                // Advance through word.
-               while ( tmpcursor.pos() < tmpcursor.par()->Last() &&
+               while (tmpcursor.pos() < tmpcursor.par()->Last() &&
                        tmpcursor.par()->IsWord( tmpcursor.pos() ) )
                {
                  //     printf("Current pos2 %d", tmpcursor.pos()) ;
@@ -2320,17 +2392,17 @@ void LyXText::CursorLeftOneWord(BufferView * bview)  const
 void LyXText::SelectWord(BufferView * bview) 
 {
        // Move cursor to the beginning, when not already there.
-       if ( cursor.pos()
-            && !cursor.par()->IsSeparator(cursor.pos()-1)
-            && !cursor.par()->IsKomma(cursor.pos()-1) )
+       if (cursor.pos()
+           && !cursor.par()->IsSeparator(cursor.pos()-1)
+           && !cursor.par()->IsKomma(cursor.pos()-1) )
                CursorLeftOneWord(bview);
 
        // set the sel cursor
        sel_cursor = cursor;
 
-       while ( cursor.pos() < cursor.par()->Last()
-                       && !cursor.par()->IsSeparator(cursor.pos())
-                       && !cursor.par()->IsKomma(cursor.pos()) )
+       while (cursor.pos() < cursor.par()->Last()
+              && !cursor.par()->IsSeparator(cursor.pos())
+              && !cursor.par()->IsKomma(cursor.pos()) )
                cursor.pos(cursor.pos() + 1);
        SetCursor(bview, cursor.par(), cursor.pos() );
        
@@ -2350,7 +2422,7 @@ bool LyXText::SelectWordWhenUnderCursor(BufferView * bview)
            && !cursor.par()->IsSeparator(cursor.pos())
            && !cursor.par()->IsKomma(cursor.pos())
            && !cursor.par()->IsSeparator(cursor.pos() -1)
-           && !cursor.par()->IsKomma(cursor.pos() -1) ) {
+           && !cursor.par()->IsKomma(cursor.pos() -1)) {
                SelectWord(bview);
                return true;
        }
@@ -2573,8 +2645,8 @@ void LyXText::Delete(BufferView * bview)
        // this is a very easy implementation
 
        LyXCursor old_cursor = cursor;
-       int old_cur_par_id = old_cursor.par()->id();
-       int old_cur_par_prev_id = old_cursor.par()->previous ?
+       int const old_cur_par_id = old_cursor.par()->id();
+       int const old_cur_par_prev_id = old_cursor.par()->previous ?
                old_cursor.par()->previous->id() : 0;
        
        // just move to the right
@@ -2658,7 +2730,7 @@ void LyXText::Backspace(BufferView * bview)
                                CursorLeft(bview);
                     
                                // the layout things can change the height of a row !
-                               int tmpheight = cursor.row()->height();
+                               int const tmpheight = cursor.row()->height();
                                SetHeightOfRow(bview, cursor.row());
                                if (cursor.row()->height() != tmpheight) {
                                        refresh_y = cursor.y() - cursor.row()->baseline();
@@ -2780,7 +2852,7 @@ void LyXText::Backspace(BufferView * bview)
                }
                
                Row * row = cursor.row();
-               long y = cursor.y() - row->baseline();
+               int y = cursor.y() - row->baseline();
                LyXParagraph::size_type z;
                /* remember that a space at the end of a row doesnt count
                 * when calculating the fill */ 
@@ -2844,7 +2916,7 @@ void LyXText::Backspace(BufferView * bview)
                if (row->previous() && row->previous()->par() == row->par()) {
                        z = NextBreakPoint(bview, row->previous(),
                                           workWidth(bview));
-                       if ( z >= row->pos()) {
+                       if (z >= row->pos()) {
                                row->pos(z + 1);
                                
                                Row * tmprow = row->previous();
@@ -2901,14 +2973,23 @@ void LyXText::Backspace(BufferView * bview)
                        status = LyXText::NEED_MORE_REFRESH;
                        
                        BreakAgainOneRow(bview, row);
+                       // will the cursor be in another row now?
+                       if (row->next() && row->next()->par() == row->par() &&
+                           RowLast(row) <= cursor.pos()) {
+                               row = row->next();
+                               BreakAgainOneRow(bview, row);
+                       }
+
                        SetCursor(bview, cursor.par(), cursor.pos(), false, cursor.boundary());
-                       // cursor MUST be in row now
-                       
-                       need_break_row = row->next();
+
+                       if (row->next() && row->next()->par() == row->par())
+                               need_break_row = row->next();
+                       else
+                               need_break_row = 0;
                } else  {
                        // set the dimensions of the row
                        row->fill(Fill(bview, row, workWidth(bview)));
-                       int tmpheight = row->height();
+                       int const tmpheight = row->height();
                        SetHeightOfRow(bview, row);
                        if (tmpheight == row->height())
                                status = LyXText::NEED_VERY_LITTLE_REFRESH;
@@ -2923,14 +3004,16 @@ void LyXText::Backspace(BufferView * bview)
        // current_font = rawtmpfont;
        // real_current_font = realtmpfont;
 
+       if (IsBoundary(bview->buffer(), cursor.par(), cursor.pos())
+           != cursor.boundary())
+               SetCursor(bview, cursor.par(), cursor.pos(), false,
+                         !cursor.boundary());
+
        lastpos = cursor.par()->Last();
-       if (cursor.pos() == lastpos) {
-               if (IsBoundary(bview->buffer(), cursor.par(), cursor.pos()) != cursor.boundary())
-                       SetCursor(bview, cursor.par(), cursor.pos(), false, !cursor.boundary());
+       if (cursor.pos() == lastpos)
                SetCurrentFont(bview);
-       }
        
-       // check, wether the last characters font has changed.
+       // check, whether the last characters font has changed.
        if (rawparfont != 
            cursor.par()->GetFontSettings(bview->buffer()->params, lastpos - 1)) {
                RedoHeightOfParagraph(bview, cursor);
@@ -2945,13 +3028,15 @@ void LyXText::Backspace(BufferView * bview)
 
 
 void LyXText::GetVisibleRow(BufferView * bview, int y_offset, int x_offset,
-                           Row * row_ptr, long y, bool cleared)
+                           Row * row_ptr, int y, bool cleared)
 {
        /* returns a printed row */
        Painter & pain = bview->painter();
        
-       bool is_rtl = row_ptr->par()->isRightToLeftPar(bview->buffer()->params);
-       LyXParagraph::size_type last = RowLastPrintable(row_ptr);
+       bool const is_rtl =
+               row_ptr->par()->isRightToLeftPar(bview->buffer()->params);
+       
+       LyXParagraph::size_type const last = RowLastPrintable(row_ptr);
 
        LyXParagraph::size_type vpos, pos;
        float x;
@@ -2983,39 +3068,45 @@ void LyXText::GetVisibleRow(BufferView * bview, int y_offset, int x_offset,
        bool clear_area = true;
        Inset * inset = 0;
 
-       if (!bview->screen()->forceClear() && (last == row_ptr->pos()) &&
-           (row_ptr->par()->GetChar(row_ptr->pos()) == LyXParagraph::META_INSET) &&
-           (inset = row_ptr->par()->GetInset(row_ptr->pos()))) {
+       if (!bview->screen()->forceClear() && last == row_ptr->pos()
+           && row_ptr->par()->GetChar(row_ptr->pos()) == LyXParagraph::META_INSET
+           && (inset = row_ptr->par()->GetInset(row_ptr->pos()))) {
                clear_area = inset->doClearArea();
        }
        // we don't need to clear it's already done!!!
        if (cleared) {
                clear_area = true;
        } else if (clear_area) {
-               int w = (inset_owner ? inset_owner->width(bview, font) : ww);
+#ifdef WITH_WARNINGS
+#warning Should be fixed with a lyxinset::clear_width(bv, font) function! (Jug)
+#endif
+               int const w = (inset_owner ?
+                              inset_owner->width(bview, font)-2 : ww);
                pain.fillRectangle(x_offset, y_offset, w, row_ptr->height());
        } else if (inset != 0) {
                int h = row_ptr->baseline() - inset->ascent(bview, font);
                if (h > 0) {
-                       int w = (inset_owner ?
+                       int const w = (inset_owner ?
                                 inset_owner->width(bview, font) : ww);
                        pain.fillRectangle(x_offset, y_offset, w, h);
                }
                h += inset->ascent(bview, font) + inset->descent(bview, font);
                if ((row_ptr->height() - h) > 0) {
-                       int w = (inset_owner ?
+                       int const w = (inset_owner ?
                                 inset_owner->width(bview, font) : ww);
-                       pain.fillRectangle(x_offset,y_offset+h, w, row_ptr->height()-h);
+                       pain.fillRectangle(x_offset, y_offset + h,
+                                          w, row_ptr->height() - h);
                }
                if (!inset_owner && !inset->display() && !inset->needFullRow())
                {
-                       int w = inset->width(bview, font) + int(x);
+                       int const w = inset->width(bview, font) + int(x);
                        pain.fillRectangle(w, y_offset, ww - w, row_ptr->height());
                }
        }
        
        if (selection) {
-               int w = (inset_owner ? inset_owner->width(bview, font) : ww);
+               int const w = (inset_owner ?
+                              inset_owner->width(bview, font) : ww);
                /* selection code */
                if (bidi_same_direction) {
                        if (sel_start_cursor.row() == row_ptr &&
@@ -3056,23 +3147,23 @@ void LyXText::GetVisibleRow(BufferView * bview, int y_offset, int x_offset,
                                                           sel_end_cursor.x(),
                                                           row_ptr->height(),
                                                           LColor::selection);
-                       } else if (y > long(sel_start_cursor.y())
-                                  && y < long(sel_end_cursor.y())) {
+                       } else if (y > sel_start_cursor.y()
+                                  && y < sel_end_cursor.y()) {
                                pain.fillRectangle(x_offset, y_offset, w,
                                                   row_ptr->height(),
                                                   LColor::selection);
                        }
-               } else if ( sel_start_cursor.row() != row_ptr &&
+               } else if (sel_start_cursor.row() != row_ptr &&
                            sel_end_cursor.row() != row_ptr &&
-                           y > long(sel_start_cursor.y())
-                           && y < long(sel_end_cursor.y())) {
+                           y > sel_start_cursor.y()
+                           && y < sel_end_cursor.y()) {
                        pain.fillRectangle(x_offset, y_offset, w,
                                           row_ptr->height(),
                                           LColor::selection);
                } else if (sel_start_cursor.row() == row_ptr ||
                           sel_end_cursor.row() == row_ptr) {
                        float tmpx = x;
-                       if ( (sel_start_cursor.row() != row_ptr && !is_rtl) ||
+                       if ((sel_start_cursor.row() != row_ptr && !is_rtl) ||
                             (sel_end_cursor.row() != row_ptr && is_rtl))
                                pain.fillRectangle(x_offset, y_offset,
                                                   int(tmpx),
@@ -3084,7 +3175,7 @@ void LyXText::GetVisibleRow(BufferView * bview, int y_offset, int x_offset,
                        
                        for (vpos = row_ptr->pos(); vpos <= last; ++vpos)  {
                                pos = vis2log(vpos);
-                               float old_tmpx = tmpx;
+                               float const old_tmpx = tmpx;
                                if (main_body > 0 && pos == main_body-1) {
                                        tmpx += fill_label_hfill +
                                                lyxfont::width(textclasslist.Style(bview->buffer()->params.textclass,
@@ -3107,7 +3198,7 @@ void LyXText::GetVisibleRow(BufferView * bview, int y_offset, int x_offset,
                                } else
                                        tmpx += SingleWidth(bview, row_ptr->par(), pos);
                                
-                               if ( (sel_start_cursor.row() != row_ptr ||
+                               if ((sel_start_cursor.row() != row_ptr ||
                                      sel_start_cursor.pos() <= pos) &&
                                     (sel_end_cursor.row() != row_ptr ||
                                      pos < sel_end_cursor.pos()) )
@@ -3118,7 +3209,7 @@ void LyXText::GetVisibleRow(BufferView * bview, int y_offset, int x_offset,
                                                           LColor::selection);
                        }
 
-                       if ( (sel_start_cursor.row() != row_ptr && is_rtl) ||
+                       if ((sel_start_cursor.row() != row_ptr && is_rtl) ||
                             (sel_end_cursor.row() != row_ptr && !is_rtl) )
                                pain.fillRectangle(x_offset + int(tmpx),
                                                   y_offset,
@@ -3219,7 +3310,8 @@ void LyXText::GetVisibleRow(BufferView * bview, int y_offset, int x_offset,
                }
 
                // Draw minipage line
-               bool minipage = p->pextra_type == LyXParagraph::PEXTRA_MINIPAGE;
+               bool const minipage =
+                       (p->pextra_type == LyXParagraph::PEXTRA_MINIPAGE);
                if (minipage)
                        pain.line(LYX_PAPER_MARGIN/5, y_offset,
                                  LYX_PAPER_MARGIN/5, 
@@ -3227,9 +3319,10 @@ void LyXText::GetVisibleRow(BufferView * bview, int y_offset, int x_offset,
                                  LColor::minipageline);
 
                // Draw depth lines
-               int depth = p->GetDepth();
+               int const depth = p->GetDepth();
                for (int i = 1; i <= depth; ++i) {
-                       int line_x = (LYX_PAPER_MARGIN / 5) * (i + minipage);
+                       int const line_x = (LYX_PAPER_MARGIN / 5) *
+                               (i + minipage);
                        pain.line(line_x, y_offset, line_x,
                                  y_offset + row_ptr->height() - 1,
                                  LColor::depthbar);
@@ -3240,8 +3333,8 @@ void LyXText::GetVisibleRow(BufferView * bview, int y_offset, int x_offset,
                LyXFont font(LyXFont::ALL_SANE);
                font.setSize(LyXFont::SIZE_FOOTNOTE);
                
-               int box_x = LYX_PAPER_MARGIN;
-               box_x += lyxfont::width(" wide-tab ", font);
+               int const box_x = LYX_PAPER_MARGIN
+                       + lyxfont::width(" wide-tab ", font);
                
                pain.line(box_x, y_offset,
                          workWidth(bview) - LYX_PAPER_MARGIN,
@@ -3264,7 +3357,8 @@ void LyXText::GetVisibleRow(BufferView * bview, int y_offset, int x_offset,
        }
 
        // Draw minipage line
-       bool minipage = firstpar->pextra_type == LyXParagraph::PEXTRA_MINIPAGE;
+       bool const minipage =
+               (firstpar->pextra_type == LyXParagraph::PEXTRA_MINIPAGE);
        if (minipage)
                pain.line(LYX_PAPER_MARGIN/5 + box_x, y_offset,
                          LYX_PAPER_MARGIN/5 + box_x, 
@@ -3272,7 +3366,7 @@ void LyXText::GetVisibleRow(BufferView * bview, int y_offset, int x_offset,
                          LColor::minipageline);
 
        // Draw depth lines
-       int depth = firstpar->GetDepth();
+       int const depth = firstpar->GetDepth();
        if (depth > 0) {
                int next_depth = 0;
                int prev_depth = 0;
@@ -3298,7 +3392,8 @@ void LyXText::GetVisibleRow(BufferView * bview, int y_offset, int x_offset,
 #endif
 
                for (int i = 1; i <= depth; ++i) {
-                       int line_x = (LYX_PAPER_MARGIN / 5) * (i + minipage) + box_x;
+                       int const line_x = (LYX_PAPER_MARGIN / 5) *
+                               (i + minipage) + box_x + x_offset;
                        pain.line(line_x, y_offset, line_x,
                                  y_offset + row_ptr->height() - 1 - (i - next_depth - 1) * 3,
                                  LColor::depthbar);
@@ -3401,8 +3496,9 @@ void LyXText::GetVisibleRow(BufferView * bview, int y_offset, int x_offset,
                        y_top +=  lyxfont::ascent('x',
                                                  GetFont(bview->buffer(),
                                                          row_ptr->par(), 0));
-                       int w = (inset_owner ? inset_owner->width(bview, font) : ww);
-                       int xp = (inset_owner ? x : 0);
+                       int const w = (inset_owner ?
+                                      inset_owner->width(bview, font) : ww);
+                       int const xp = static_cast<int>(inset_owner ? x : 0);
                        pain.line(xp, y_offset + y_top,
                                  w, y_offset + y_top,
                                  LColor::topline,
@@ -3421,7 +3517,8 @@ void LyXText::GetVisibleRow(BufferView * bview, int y_offset, int x_offset,
                        font = GetFont(bview->buffer(), row_ptr->par(), -2);
                        if (!row_ptr->par()->GetLabelstring().empty()) {
                                tmpx = x;
-                               string tmpstring = row_ptr->par()->GetLabelstring();
+                               string const tmpstring =
+                                       row_ptr->par()->GetLabelstring();
                                
                                if (layout.labeltype == LABEL_COUNTER_CHAPTER) {
                                        if (bview->buffer()->params.secnumdepth >= 0) {
@@ -3472,7 +3569,8 @@ void LyXText::GetVisibleRow(BufferView * bview, int y_offset, int x_offset,
                                font = GetFont(bview->buffer(),
                                               row_ptr->par(), -2);
                                if (!row_ptr->par()->GetLabelstring().empty()) {
-                                       string tmpstring = row_ptr->par()->GetLabelstring();
+                                       string const tmpstring =
+                                               row_ptr->par()->GetLabelstring();
                                        float spacing_val = 1.0;
                                        if (!row_ptr->par()->spacing.isDefault()) {
                                                spacing_val = row_ptr->par()->spacing.getValue();
@@ -3521,11 +3619,11 @@ void LyXText::GetVisibleRow(BufferView * bview, int y_offset, int x_offset,
 #endif
        if (
 #ifndef NEW_INSETS
-               (row_ptr->par()->ParFromPos(last + 1) == par)
+               row_ptr->par()->ParFromPos(last + 1) == par
 #else
-               (row_ptr->par() == par)
+               row_ptr->par() == par
 #endif
-           && (!row_ptr->next() || (row_ptr->next()->par() != row_ptr->par())))
+               && (!row_ptr->next() || row_ptr->next()->par() != row_ptr->par()))
        {
                /* think about the margins */ 
                if (!row_ptr->next() && bv_owner)
@@ -3582,26 +3680,30 @@ void LyXText::GetVisibleRow(BufferView * bview, int y_offset, int x_offset,
                        /* draw a bottom line */
                        y_bottom -= lyxfont::ascent('x', GetFont(bview->buffer(),
                                                                 par, par->Last() - 1));
-                       int w = (inset_owner ? inset_owner->width(bview, font) : ww);
-                       int xp = (inset_owner ? x : 0);
+                       int const w = (inset_owner ?
+                                      inset_owner->width(bview, font) : ww);
+                       int const xp = static_cast<int>(inset_owner ? x : 0);
                        pain.line(xp, y_offset + y_bottom,
                                  w, y_offset + y_bottom,
                                  LColor::topline, Painter::line_solid,
                                  Painter::line_thick);
-                       y_bottom -= lyxfont::ascent('x', GetFont(bview->buffer(),
-                                                                par, par->Last() - 1));
+                       y_bottom -= lyxfont::ascent('x',
+                                                   GetFont(bview->buffer(),
+                                                           par,
+                                                           par->Last() - 1));
                }
 
                // draw an endlabel
-               int endlabel = row_ptr->par()->GetEndLabel(bview->buffer()->params);
+               int const endlabel =
+                       row_ptr->par()->GetEndLabel(bview->buffer()->params);
                switch (endlabel) {
                case END_LABEL_BOX:
                case END_LABEL_FILLED_BOX:
                {
-                       LyXFont font = GetFont(bview->buffer(),
-                                              row_ptr->par(), last);
-                       int size = int(0.75 * lyxfont::maxAscent(font));
-                       int y = (y_offset + row_ptr->baseline()) - size;
+                       LyXFont const font = GetFont(bview->buffer(),
+                                                    row_ptr->par(), last);
+                       int const size = int(0.75 * lyxfont::maxAscent(font));
+                       int const y = (y_offset + row_ptr->baseline()) - size;
                        int x = is_rtl ? LYX_PAPER_MARGIN 
                                : ww - LYX_PAPER_MARGIN - size;
 #ifndef NEW_INSETS
@@ -3632,10 +3734,12 @@ void LyXText::GetVisibleRow(BufferView * bview, int y_offset, int x_offset,
                case END_LABEL_STATIC:
                {
                        LyXTextClass::LayoutList::size_type layout = row_ptr->par()->GetLayout();
-                       string tmpstring = textclasslist.Style(bview->buffer()->params.textclass,
-                                                        layout).endlabelstring();
+                       string const tmpstring = textclasslist.
+                               Style(bview->buffer()->params.textclass,
+                                     layout).endlabelstring();
                        font = GetFont(bview->buffer(), row_ptr->par(), -2);
-                       int tmpx = is_rtl ? int(x) - lyxfont::width(tmpstring, font)
+                       int const tmpx = is_rtl ?
+                               int(x) - lyxfont::width(tmpstring, font)
                                : ww - RightMargin(bview->buffer(), row_ptr) - row_ptr->fill();
                        pain.text( tmpx, y_offset + row_ptr->baseline(), tmpstring, font);
                        break;
@@ -3778,7 +3882,7 @@ int LyXText::GetColumnNearX(BufferView * bview, Row * row, int & x,
                ++vc;
        }
        
-       if (vc > row->pos() && (tmpx + last_tmpx) / 2 > x) {
+       if ((tmpx + last_tmpx) / 2 > x) {
                tmpx = last_tmpx;
                left_side = true;
        }
@@ -3787,29 +3891,27 @@ int LyXText::GetColumnNearX(BufferView * bview, Row * row, int & x,
                vc = last + 1;
 
        boundary = false;
-#if 0 // currently unused
-       bool lastrow = (!row->next() || row->next()->par() != row->par());
-       bool rtl = (lastrow)
+       bool const lastrow = lyxrc.rtl_support // This is not needed, but gives
+                                        // some speedup if rtl_support=false
+               && (!row->next() || row->next()->par() != row->par());
+       bool const rtl = (lastrow)
                ? row->par()->isRightToLeftPar(bview->buffer()->params)
-               : false;
-#endif
-       
+               : false; // If lastrow is false, we don't need to compute
+                        // the value of rtl.
+
        if (row->pos() > last)  // Row is empty?
                c = row->pos();
-#warning This is wrong, please have a look Dekel (Jug)
-#if 0
        else if (lastrow &&
-                ( (rtl && vc == row->pos()&& x < tmpx - 5) ||
-                  (!rtl && 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;
-#endif
        else if (vc == row->pos()) {
                c = vis2log(vc);
                if (bidi_level(c) % 2 == 1)
                        ++c;
        } else {
                c = vis2log(vc - 1);
-               bool rtl = (bidi_level(c) % 2 == 1);
+               bool const rtl = (bidi_level(c) % 2 == 1);
                if (left_side == rtl) {
                        ++c;
                        boundary = IsBoundary(bview->buffer(), row->par(), c);
@@ -3973,8 +4075,11 @@ void LyXText::InsertFootnoteEnvironment(BufferView * bview,
 
 // returns pointer to a specified row
 Row * LyXText::GetRow(LyXParagraph * par,
-                     LyXParagraph::size_type pos, long & y) const
+                     LyXParagraph::size_type pos, int & y) const
 {
+       if (!firstrow)
+               return 0;
+
        Row * tmprow = firstrow;
        y = 0;