]> git.lyx.org Git - lyx.git/blobdiff - src/text2.C
More fixes to insettabular/text (and some missing features added).
[lyx.git] / src / text2.C
index bbd6061d3732657d817e0875b1617a28d1854da9..16fe687dc3057cc29c3c0af02da180ff8c9a6ce2 100644 (file)
@@ -67,6 +67,7 @@ LyXText::LyXText(InsetText * inset)
 
 void LyXText::init()
 {
+       the_locking_inset = 0;
        firstrow = 0;
        lastrow = 0;
        number_of_rows = 0;
@@ -172,7 +173,7 @@ LyXText::~LyXText()
 // smaller. (Asger)
 // If position is -1, we get the layout font of the paragraph.
 // If position is -2, we get the font of the manual label of the paragraph.
-LyXFont LyXText::GetFont(Buffer const * buf, LyXParagraph * par,
+LyXFont const LyXText::GetFont(Buffer const * buf, LyXParagraph * par,
                         LyXParagraph::size_type pos) const
 {
        LyXLayout const & layout = 
@@ -190,11 +191,14 @@ LyXFont LyXText::GetFont(Buffer const * buf, LyXParagraph * par,
                        if (layout.labeltype == LABEL_MANUAL
                            && pos < BeginningOfMainBody(buf, par)) {
                                // 1% goes here
-                               return par->GetFontSettings(buf->params, pos).
-                                       realize(layout.reslabelfont);
-                       } else
-                               return par->GetFontSettings(buf->params, pos).
-                                       realize(layout.resfont);
+                               LyXFont f = par->GetFontSettings(buf->params,
+                                                                pos);
+                               return f.realize(layout.reslabelfont);
+                       } else {
+                               LyXFont f = par->GetFontSettings(buf->params, pos);
+                               return f.realize(layout.resfont);
+                       }
+                       
                } else {
                        // 5% goes here.
                        // process layoutfont for pos == -1 and labelfont for pos < -1
@@ -342,7 +346,7 @@ void LyXText::RemoveRow(Row * row) const
        /* this must not happen before the currentrow for clear reasons.
           so the trick is just to set the current row onto the previous
           row of this row */
-       long unused_y;
+       int unused_y;
        GetRow(row->par(), row->pos(), unused_y);
    
        if (row->next())
@@ -866,7 +870,7 @@ void LyXText::SetFont(BufferView * bview, LyXFont const & font, bool toggleall)
                        layoutfont = GetFont(bview->buffer(), cursor.par(),-1);
                // Update current font
                real_current_font.update(font,
-                                        bview->buffer()->params.language_info,
+                                        bview->buffer()->params.language,
                                         toggleall);
 
                // Reduce to implicit settings
@@ -910,7 +914,7 @@ void LyXText::SetFont(BufferView * bview, LyXFont const & font, bool toggleall)
                        LyXFont newfont = GetFont(bview->buffer(), 
                                                  cursor.par(), cursor.pos());
                        newfont.update(font,
-                                      bview->buffer()->params.language_info,
+                                      bview->buffer()->params.language,
                                       toggleall);
                        SetCharFont(bview->buffer(),
                                    cursor.par(), cursor.pos(), newfont);
@@ -938,7 +942,7 @@ void LyXText::SetFont(BufferView * bview, LyXFont const & font, bool toggleall)
 void LyXText::RedoHeightOfParagraph(BufferView * bview, LyXCursor const & cur)
 {
        Row * tmprow = cur.row();
-       long y = cur.y() - tmprow->baseline();
+       int y = cur.y() - tmprow->baseline();
 
        SetHeightOfRow(bview, tmprow);
 #ifndef NEW_INSETS
@@ -972,7 +976,7 @@ void LyXText::RedoDrawingOfParagraph(BufferView * bview, LyXCursor const & cur)
 {
        Row * tmprow = cur.row();
    
-       long y = cur.y() - tmprow->baseline();
+       int y = cur.y() - tmprow->baseline();
        SetHeightOfRow(bview, tmprow);
 #ifndef NEW_INSETS
        LyXParagraph * first_phys_par = tmprow->par()->FirstPhysicalPar();
@@ -1011,7 +1015,7 @@ void LyXText::RedoParagraphs(BufferView * bview, LyXCursor const & cur,
    
        Row * tmprow = cur.row();
    
-       long y = cur.y() - tmprow->baseline();
+       int y = cur.y() - tmprow->baseline();
    
        if (!tmprow->previous()){
                first_phys_par = FirstParagraph();   // a trick/hack for UNDO
@@ -1161,7 +1165,7 @@ void LyXText::SetSelection()
 }
 
 
-string LyXText::selectionAsString(Buffer const * buffer) const
+string const LyXText::selectionAsString(Buffer const * buffer) const
 {
        if (!selection) return string();
        string result;
@@ -1199,6 +1203,8 @@ string LyXText::selectionAsString(Buffer const * buffer) const
 
 void LyXText::ClearSelection() const
 {
+       if (selection)
+               status = LyXText::NEED_MORE_REFRESH;
        selection = false;
        mark_set = false;
 }
@@ -1222,24 +1228,6 @@ void LyXText::CursorEnd(BufferView * bview) const
                else
                        SetCursor(bview,cursor.par(), RowLast(cursor.row()) + 1);
        }
-#ifndef NEW_TABULAR
-        if (cursor.par()->table) {
-                int cell = NumberOfCell(cursor.par(), cursor.pos());
-                if (cursor.par()->table->RowHasContRow(cell) &&
-                    cursor.par()->table->CellHasContRow(cell)<0) {
-                        if (!cursor.row()->next() || cursor.row()->next()->par() != cursor.row()->par())
-                                SetCursor(bview, cursor.par(), RowLast(cursor.row()) + 1);
-                        else {
-                                if (cursor.par()->Last() && 
-                                    (cursor.par()->GetChar(RowLast(cursor.row())) == ' '
-                                     || cursor.par()->IsNewline(RowLast(cursor.row()))))
-                                        SetCursor(bview, cursor.par(), RowLast(cursor.row()));
-                                else
-                                        SetCursor(bview, cursor.par(), RowLast(cursor.row()) + 1);
-                        }
-                }
-        }
-#endif
 }
 
 
@@ -1262,10 +1250,10 @@ void  LyXText::CursorBottom(BufferView * bview) const
 /* returns a pointer to the row near the specified y-coordinate
 * (relative to the whole text). y is set to the real beginning
 * of this row */
-Row * LyXText::GetRowNearY(long & y) const
+Row * LyXText::GetRowNearY(int & y) const
 {
        Row * tmprow = firstrow;
-       long tmpy = 0;
+       int tmpy = 0;
 
        while (tmprow->next() && tmpy + tmprow->height() <= y) {
                tmpy += tmprow->height();
@@ -1293,7 +1281,8 @@ void LyXText::ToggleFree(BufferView * bview,
        // If there is a change in the language the implicit word selection 
        // is disabled.
        LyXCursor resetCursor = cursor;
-       bool implicitSelection = (font.language() == ignore_language)
+       bool implicitSelection = (font.language() == ignore_language
+                                 && font.number() == LyXFont::IGNORE)
                ? SelectWordWhenUnderCursor(bview) : false;
 
        // Set font
@@ -1376,7 +1365,7 @@ void LyXText::MeltFootnoteEnvironment(BufferView * bview)
                tmppar->next->MakeSameLayout(cursor.par());
 
        // first the end
-       if ((!tmppar->GetLayout() && !tmppar->table)
+       if (!tmppar->GetLayout()
            || (tmppar->Next()
                && (!tmppar->Next()->Last()
                    || tmppar->Next()->HasSameLayout(tmppar)))) {
@@ -1393,7 +1382,7 @@ void LyXText::MeltFootnoteEnvironment(BufferView * bview)
        /* if there is no space between the text and the footnote, so we insert
         * a blank 
         * (only if the previous par and the footnotepar are not empty!) */
-       if ((!firsttmppar->next->GetLayout() && !firsttmppar->next->table)
+       if (!firsttmppar->next->GetLayout()
            || firsttmppar->HasSameLayout(firsttmppar->next)) {
                if (firsttmppar->size()
                    && !firsttmppar->IsSeparator(firsttmppar->size() - 1)
@@ -1534,8 +1523,8 @@ void LyXText::SetParagraph(BufferView * bview,
 
 
 void LyXText::SetParagraphExtraOpt(BufferView * bview, int type,
-                                   char const * width,
-                                   char const * widthp,
+                                   string const & width,
+                                   string const & widthp,
                                    int alignment, bool hfill,
                                    bool start_minipage)
 {
@@ -1630,6 +1619,7 @@ char loweralphaCounter(int n)
 }
 
 
+static inline
 char alphaCounter(int n)
 {
        if (n < 1 || n > 26)
@@ -1639,6 +1629,7 @@ char alphaCounter(int n)
 }
 
 
+static inline
 char hebrewCounter(int n)
 {
        static const char hebrew[22] = {
@@ -1653,8 +1644,8 @@ char hebrewCounter(int n)
 }
 
 
-static
-char const * romanCounter(int n)
+static inline
+string const romanCounter(int n)
 {
        static char const * roman[20] = {
                "i",   "ii",  "iii", "iv", "v",
@@ -1730,7 +1721,7 @@ void LyXText::SetCounter(Buffer const * buf, LyXParagraph * par) const
         // entry in the marginnote and the enclosing
         // environment is an enum/item then correct for the
         // LaTeX behaviour (ARRae)
-        if(par->footnoteflag == LyXParagraph::OPEN_FOOTNOTE
+        if (par->footnoteflag == LyXParagraph::OPEN_FOOTNOTE
           && par->footnotekind == LyXParagraph::MARGIN
            && par->Previous()
            && par->Previous()->footnoteflag != LyXParagraph::OPEN_FOOTNOTE
@@ -1821,11 +1812,8 @@ void LyXText::SetCounter(Buffer const * buf, LyXParagraph * par) const
                                        par->labelstring.erase();
                        }
 
-#ifdef HAVE_SSTREAM
                        std::ostringstream s;
-#else
-                       ostrstream s;
-#endif
+
                        if (!par->appendix) {
                                switch (2 * LABEL_COUNTER_CHAPTER -
                                        textclass.maxcounter() + i) {
@@ -1948,16 +1936,10 @@ void LyXText::SetCounter(Buffer const * buf, LyXParagraph * par) const
                                        break;
                                }
                        }
-#ifdef HAVE_SSTREAM
+
                        par->labelstring += s.str().c_str();
                        // We really want to remove the c_str as soon as
                        // possible...
-#else
-                       s << '\0';
-                       char * tmps = s.str();
-                       par->labelstring += tmps;
-                       delete [] tmps;
-#endif
                        
                        for (i++; i < 10; ++i) {
                                // reset the following counters
@@ -1972,11 +1954,8 @@ void LyXText::SetCounter(Buffer const * buf, LyXParagraph * par) const
                        par->incCounter(i + par->enumdepth);
                        int number = par->getCounter(i + par->enumdepth);
 
-#ifdef HAVE_SSTREAM
                        std::ostringstream s;
-#else
-                       ostrstream s;
-#endif
+
                        switch (par->enumdepth) {
                        case 1:
                                if (par->isRightToLeftPar(buf->params))
@@ -2009,15 +1988,9 @@ void LyXText::SetCounter(Buffer const * buf, LyXParagraph * par) const
                                        s << number << '.';
                                break;
                        }
-#ifdef HAVE_SSTREAM
+
                        par->labelstring = s.str().c_str();
                        // we really want to get rid of that c_str()
-#else
-                       s << '\0';
-                       char * tmps = s.str();
-                       par->labelstring = tmps;
-                       delete [] tmps;
-#endif
 
                        for (i += par->enumdepth + 1; i < 10; ++i)
                                par->setCounter(i, 0);  /* reset the following counters  */
@@ -2027,8 +2000,10 @@ void LyXText::SetCounter(Buffer const * buf, LyXParagraph * par) const
                int i = LABEL_COUNTER_ENUMI - LABEL_COUNTER_CHAPTER + par->enumdepth;
                par->incCounter(i);
                int number = par->getCounter(i);
-               if (!par->bibkey)
-                       par->bibkey = new InsetBibKey();
+               if (!par->bibkey) {
+                       InsetCommandParams p( "bibitem" );
+                       par->bibkey = new InsetBibKey(p);
+               }
                par->bibkey->setCounter(number);
                par->labelstring = layout.labelstring();
                
@@ -2062,7 +2037,7 @@ void LyXText::SetCounter(Buffer const * buf, LyXParagraph * par) const
                                Floating const & fl
                                        = floatList.getType(tmp->type());
                                // We should get the correct number here too.
-                               s = fl.name + " #:";
+                               s = fl.name() + " #:";
                        } else {
                                /* par->SetLayout(0); 
                                   s = layout->labelstring;  */
@@ -2169,6 +2144,19 @@ void LyXText::InsertInset(BufferView * bview, Inset * inset)
        InsertChar(bview, LyXParagraph::META_INSET);  /* just to rebreak and refresh correctly.
                                      * The character will not be inserted a
                                      * second time */
+#if 1
+       // if we enter a text-inset the cursor should be to the left side
+       // of it! This couldn't happen before as Undo was not handled inside
+       // inset now after the Undo LyX tries to call inset->Edit(...) again
+       // and cannot do this as the cursor is behind the inset and GetInset
+       // does not return the inset!
+       if (inset->IsTextInset()) {
+               if (cursor.par()->isRightToLeftPar(bview->buffer()->params))
+                       CursorRight(bview);
+               else
+                       CursorLeft(bview);
+       }
+#endif
 }
 
 
@@ -2201,7 +2189,7 @@ void LyXText::CutSelection(BufferView * bview, bool doclear)
                return;
    
        // OK, we have a selection. This is always between sel_start_cursor
-       // and sel_end cursor
+       // and sel_end_cursor
 #ifndef NEW_INSETS
        // Check whether there are half footnotes in the selection
        if (sel_start_cursor.par()->footnoteflag != LyXParagraph::NO_FOOTNOTE
@@ -2218,19 +2206,7 @@ void LyXText::CutSelection(BufferView * bview, bool doclear)
                }
        }
 #endif
-#ifndef NEW_TABULAR
-       /* table stuff -- begin */
-       if (sel_start_cursor.par()->table || sel_end_cursor.par()->table) {
-               if ( sel_start_cursor.par() != sel_end_cursor.par()) {
-                       WriteAlert(_("Impossible operation"),
-                                  _("Don't know what to do with half tables."),
-                                  _("sorry."));
-                       return;
-               }
-               sel_start_cursor.par()->table->Reinit();
-       }
-       /* table stuff -- end */
-#endif
+
        // make sure that the depth behind the selection are restored, too
 #ifndef NEW_INSETS
        LyXParagraph * endpar = sel_end_cursor.par()->LastPhysicalPar()->Next();
@@ -2344,18 +2320,6 @@ void LyXText::CopySelection(BufferView * bview)
                }
        }
 #endif
-#ifndef NEW_TABULAR
-       /* table stuff -- begin */
-       if (sel_start_cursor.par()->table || sel_end_cursor.par()->table){
-               if ( sel_start_cursor.par() != sel_end_cursor.par()){
-                       WriteAlert(_("Impossible operation"),
-                                  _("Don't know what to do with half tables."),
-                                  _("sorry."));
-                       return;
-               }
-       }
-       /* table stuff -- end */
-#endif
    
        // copy behind a space if there is one
        while (sel_start_cursor.par()->Last() > sel_start_cursor.pos()
@@ -2418,33 +2382,36 @@ LyXParagraph * LyXText::FirstParagraph() const
 // returns true if the specified string is at the specified position
 bool LyXText::IsStringInText(LyXParagraph * par,
                             LyXParagraph::size_type pos,
-                            char const * str) const
+                            string const & str) const
 {
-       if (par) {
-               int i = 0;
-               while (pos + i < par->Last() && str[i] && 
-                      str[i] == par->GetChar(pos + i)) {
-                       ++i;
-               }
-               if (!str[i])
-                       return true;
+       if (!par)
+               return false;
+
+       LyXParagraph::size_type i = 0;
+       while (pos + i < par->Last()
+              && string::size_type(i) < str.length()
+              && str[i] == par->GetChar(pos + i)) {
+               ++i;
        }
+       if (str.length() == string::size_type(i))
+               return true;
        return false;
 }
 
 
 // sets the selection over the number of characters of string, no check!!
-void LyXText::SetSelectionOverString(BufferView * bview, char const * string)
+void LyXText::SetSelectionOverString(BufferView * bview, string const & str)
 {
        sel_cursor = cursor;
-       for (int i = 0; string[i]; ++i)
+       for (int i = 0; str[i]; ++i)
                CursorRight(bview);
        SetSelection();
 }
 
 
 // simple replacing. The font of the first selected character is used
-void LyXText::ReplaceSelectionWithString(BufferView * bview, char const * str)
+void LyXText::ReplaceSelectionWithString(BufferView * bview,
+                                        string const & str)
 {
        SetCursorParUndo(bview->buffer());
        FreezeUndo();
@@ -2456,15 +2423,16 @@ void LyXText::ReplaceSelectionWithString(BufferView * bview, char const * str)
 
        // Get font setting before we cut
        LyXParagraph::size_type pos = sel_end_cursor.pos();
-       LyXFont font = sel_start_cursor.par()->GetFontSettings(bview->buffer()->params,
-                                                            sel_start_cursor.pos());
+       LyXFont const font = sel_start_cursor.par()
+               ->GetFontSettings(bview->buffer()->params,
+                                 sel_start_cursor.pos());
 
        // Insert the new string
-       for (int i = 0; str[i]; ++i) {
-               sel_end_cursor.par()->InsertChar(pos, str[i], font);
+       for (string::const_iterator cit = str.begin(); cit != str.end(); ++cit) {
+               sel_end_cursor.par()->InsertChar(pos, (*cit), font);
                ++pos;
        }
-
+       
        // Cut the selection
        CutSelection(bview);
 
@@ -2474,7 +2442,7 @@ void LyXText::ReplaceSelectionWithString(BufferView * bview, char const * str)
 
 // if the string can be found: return true and set the cursor to
 // the new position
-bool LyXText::SearchForward(BufferView * bview, char const * str) const
+bool LyXText::SearchForward(BufferView * bview, string const & str) const
 {
        LyXParagraph * par = cursor.par();
        LyXParagraph::size_type pos = cursor.pos();
@@ -2495,7 +2463,7 @@ bool LyXText::SearchForward(BufferView * bview, char const * str) const
 }
 
 
-bool LyXText::SearchBackward(BufferView * bview, char const * string) const
+bool LyXText::SearchBackward(BufferView * bview, string const & str) const
 {
        LyXParagraph * par = cursor.par();
        int pos = cursor.pos();
@@ -2511,7 +2479,7 @@ bool LyXText::SearchBackward(BufferView * bview, char const * string) const
                                        pos = par->Last() - 1;
                        } while (par && pos < 0);
                }
-       } while (par && !IsStringInText(par, pos, string));
+       } while (par && !IsStringInText(par, pos, str));
   
        if (par) {
                SetCursor(bview, par, pos);
@@ -2546,23 +2514,6 @@ void LyXText::InsertStringA(BufferView * bview, string const & str)
                            && pos && par->GetChar(pos - 1)!= ' ') {
                                par->InsertChar(pos, ' ', current_font);
                                ++pos;
-#ifndef NEW_TABULAR
-                       } else if (par->table) {
-                               if (str[i] == '\t') {
-                                       while((pos < par->size()) &&
-                                             (par->GetChar(pos) != LyXParagraph::META_NEWLINE))
-                                               ++pos;
-                                       if (pos < par->size())
-                                               ++pos;
-                                       else // no more fields to fill skip the rest
-                                               break;
-                               } else if ((str[i] != 13) &&
-                                          ((str[i] & 127) >= ' ')) {
-                                       par->InsertChar(pos, str[i],
-                                                       current_font);
-                                       ++pos;
-                               }
-#endif
                         } else if (str[i] == ' ') {
                                InsetSpecialChar * new_inset =
                                        new InsetSpecialChar(InsetSpecialChar::PROTECTED_SEPARATOR);
@@ -2592,50 +2543,21 @@ void LyXText::InsertStringA(BufferView * bview, string const & str)
                                ++pos;
                        }
                } else {
-#ifndef NEW_TABULAR
-                        if (par->table) {
-                                if ((i + 1) >= str.length()) {
-                                       if (pos < par->size())
-                                               ++pos;
-                                        break;
-                                }
-                                while((pos < par->size()) &&
-                                      (par->GetChar(pos) != LyXParagraph::META_NEWLINE))
-                                        ++pos;
-                                ++pos;
-                                int cell = NumberOfCell(par, pos);
-                                while((pos < par->size()) &&
-                                      !(par->table->IsFirstCell(cell))) {
-
-                                        while((pos < par->size()) &&
-                                              (par->GetChar(pos) != LyXParagraph::META_NEWLINE))
-                                                ++pos;
-                                        ++pos;
-                                        cell = NumberOfCell(par, pos);
-                                }
-                                if (pos >= par->size())
-                                        // no more fields to fill skip the rest
-                                        break;
-                        } else {
-#endif
-                                if (!par->size()) { // par is empty
-                                       InsetSpecialChar * new_inset =
-                                               new InsetSpecialChar(InsetSpecialChar::PROTECTED_SEPARATOR);
-                                       if (par->InsertInsetAllowed(new_inset)) {
-                                               par->InsertInset(pos,
-                                                                new_inset,
-                                                                current_font);
-                                       } else {
-                                               delete new_inset;
-                                       }
-                                        ++pos;
-                                }
-                                par->BreakParagraph(bview->buffer()->params, pos, flag);
-                                par = par->Next();
-                                pos = 0;
-#ifndef NEW_TABULAR
-                        }
-#endif
+                       if (!par->size()) { // par is empty
+                               InsetSpecialChar * new_inset =
+                                       new InsetSpecialChar(InsetSpecialChar::PROTECTED_SEPARATOR);
+                               if (par->InsertInsetAllowed(new_inset)) {
+                                       par->InsertInset(pos,
+                                                        new_inset,
+                                                        current_font);
+                               } else {
+                                       delete new_inset;
+                               }
+                               ++pos;
+                       }
+                       par->BreakParagraph(bview->buffer()->params, pos, flag);
+                       par = par->Next();
+                       pos = 0;
                }
                ++i;
        }
@@ -2655,24 +2577,13 @@ void LyXText::InsertStringA(BufferView * bview, string const & str)
 void LyXText::InsertStringB(BufferView * bview, string const & s)
 {
        string str(s);
-#ifndef NEW_TABULAR
-       LyXParagraph * par = cursor.par();
-#endif
        string::size_type i = 1;
        while (i < str.length()) {
-               if (str[i] == '\t'
-#ifndef NEW_TABULAR
-                   && !par->table
-#endif
-                       )
+               if (str[i] == '\t')
                        str[i] = ' ';
                if (str[i] == ' ' && i + 1 < str.length() && str[i + 1] == ' ')
                        str[i] = 13;
-               if (str[i] == '\n' && i + 1 < str.length()
-#ifndef NEW_TABULAR
-                   && !par->table
-#endif
-                       ){
+               if (str[i] == '\n' && i + 1 < str.length()) {
                        if (str[i + 1] != '\n') {
                                if (str[i - 1] != ' ')
                                        str[i] = ' ';
@@ -2745,86 +2656,79 @@ void LyXText::CheckParagraph(BufferView * bview, LyXParagraph * par,
 {
        LyXCursor tmpcursor;                    
 
-#ifndef NEW_TABULAR
-       /* table stuff -- begin*/
-   
-       if (par->table) {
-               CheckParagraphInTable(bview, par, pos);
-       }
-       else {
-#endif
-               /* table stuff -- end*/
-     
-               long y = 0;
-               LyXParagraph::size_type z;
-               Row * row = GetRow(par, pos, y);
-     
-               // is there a break one row above
-               if (row->previous() && row->previous()->par() == row->par()) {
-                       z = NextBreakPoint(bview, row->previous(), workWidth(bview));
-                       if ( z >= row->pos()) {
-                               // set the dimensions of the row above
-                               y -= row->previous()->height();
-                               refresh_y = y;
-                               refresh_row = row->previous();
-                               status = LyXText::NEED_MORE_REFRESH;
-       
-                               BreakAgain(bview, row->previous());
-
-                               // set the cursor again. Otherwise
-                               // dangling pointers are possible
-                               SetCursor(bview, cursor.par(), cursor.pos());
-                               sel_cursor = cursor;
-                               return;
-                       }
+       int y = 0;
+       LyXParagraph::size_type z;
+       Row * row = GetRow(par, pos, y);
+       
+       // is there a break one row above
+       if (row->previous() && row->previous()->par() == row->par()) {
+               z = NextBreakPoint(bview, row->previous(), workWidth(bview));
+               if (z >= row->pos()) {
+                       // set the dimensions of the row above
+                       y -= row->previous()->height();
+                       refresh_y = y;
+                       refresh_row = row->previous();
+                       status = LyXText::NEED_MORE_REFRESH;
+                       
+                       BreakAgain(bview, row->previous());
+                       
+                       // set the cursor again. Otherwise
+                       // dangling pointers are possible
+                       SetCursor(bview, cursor.par(), cursor.pos(),
+                                 false, cursor.boundary());
+                       sel_cursor = cursor;
+                       return;
                }
+       }
 
-               int tmpheight = row->height();
-               LyXParagraph::size_type tmplast = RowLast(row);
-               refresh_y = y;
-               refresh_row = row;
-
-               BreakAgain(bview, row);
-               if (row->height() == tmpheight && RowLast(row) == tmplast)
-                       status = LyXText::NEED_VERY_LITTLE_REFRESH;
-               else
-                       status = LyXText::NEED_MORE_REFRESH; 
-   
-               // check the special right address boxes
-               if (textclasslist.Style(bview->buffer()->params.textclass,
-                                       par->GetLayout()).margintype
-                   == MARGIN_RIGHT_ADDRESS_BOX) {
-                       tmpcursor.par(par);
-                       tmpcursor.row(row);
-                       tmpcursor.y(y);
-                       tmpcursor.x(0);
-                       tmpcursor.x_fix(0);
-                       tmpcursor.pos(pos);
-                       RedoDrawingOfParagraph(bview, tmpcursor); 
-               }
-#ifndef NEW_TABULAR
+       int const tmpheight = row->height();
+       LyXParagraph::size_type const tmplast = RowLast(row);
+       refresh_y = y;
+       refresh_row = row;
+       
+       BreakAgain(bview, row);
+       if (row->height() == tmpheight && RowLast(row) == tmplast)
+               status = LyXText::NEED_VERY_LITTLE_REFRESH;
+       else
+               status = LyXText::NEED_MORE_REFRESH; 
+       
+       // check the special right address boxes
+       if (textclasslist.Style(bview->buffer()->params.textclass,
+                               par->GetLayout()).margintype
+           == MARGIN_RIGHT_ADDRESS_BOX) {
+               tmpcursor.par(par);
+               tmpcursor.row(row);
+               tmpcursor.y(y);
+               tmpcursor.x(0);
+               tmpcursor.x_fix(0);
+               tmpcursor.pos(pos);
+               RedoDrawingOfParagraph(bview, tmpcursor); 
        }
-#endif
 
        // set the cursor again. Otherwise dangling pointers are possible
        // also set the selection
    
        if (selection) {
                tmpcursor = cursor;
-               SetCursorIntern(bview, sel_cursor.par(), sel_cursor.pos());
+               SetCursorIntern(bview, sel_cursor.par(), sel_cursor.pos(),
+                               false, sel_cursor.boundary());
                sel_cursor = cursor; 
                SetCursorIntern(bview, sel_start_cursor.par(),
-                               sel_start_cursor.pos());
+                               sel_start_cursor.pos(),
+                               false, sel_start_cursor.boundary());
                sel_start_cursor = cursor; 
                SetCursorIntern(bview, sel_end_cursor.par(),
-                               sel_end_cursor.pos());
+                               sel_end_cursor.pos(),
+                               false, sel_end_cursor.boundary());
                sel_end_cursor = cursor; 
                SetCursorIntern(bview, last_sel_cursor.par(),
-                               last_sel_cursor.pos());
+                               last_sel_cursor.pos(),
+                               false, last_sel_cursor.boundary());
                last_sel_cursor = cursor; 
                cursor = tmpcursor;
        }
-       SetCursorIntern(bview, cursor.par(), cursor.pos());
+       SetCursorIntern(bview, cursor.par(), cursor.pos(),
+                       false, cursor.boundary());
 }
 
 
@@ -2906,7 +2810,7 @@ void LyXText::SetCursor(BufferView *bview, LyXCursor & cur, LyXParagraph * par,
        cur.boundary(boundary);
 
        /* get the cursor y position in text  */
-       long y = 0;
+       int y = 0;
        Row * row = GetRow(par, pos, y);
        /* y is now the beginning of the cursor row */ 
        y += row->baseline();
@@ -2932,11 +2836,7 @@ void LyXText::SetCursor(BufferView *bview, LyXCursor & cur, LyXParagraph * par,
                cursor_vpos = (row->par()->isRightToLeftPar(bview->buffer()->params))
                        ? row->pos() : last + 1; 
        else if (pos > row->pos() &&
-                (pos > last || boundary
-#ifndef NEW_TABULAR
-                 || (row->par()->table && row->par()->IsNewline(pos))
-#endif
-                        ))
+                (pos > last || boundary))
                /// Place cursor after char at (logical) position pos - 1
                cursor_vpos = (bidi_level(pos - 1) % 2 == 0)
                        ? log2vis(pos - 1) + 1 : log2vis(pos - 1);
@@ -2944,65 +2844,41 @@ void LyXText::SetCursor(BufferView *bview, LyXCursor & cur, LyXParagraph * par,
                /// Place cursor before char at (logical) position pos
                cursor_vpos = (bidi_level(pos) % 2 == 0)
                        ? log2vis(pos) : log2vis(pos) + 1;
-
-#ifndef NEW_TABULAR
-       /* table stuff -- begin*/
-       if (row->par()->table) {
-               int cell = NumberOfCell(row->par(), row->pos());
-               float x_old = x;
-               x += row->par()->table->GetBeginningOfTextInCell(cell);
-               for (LyXParagraph::size_type vpos = row->pos();
-                    vpos < cursor_vpos; ++vpos) {
-                       pos = vis2log(vpos);
-                       if (row->par()->IsNewline(pos)) {
-                               x = x_old + row->par()->table->WidthOfColumn(cell);
-                               x_old = x;
-                               ++cell;
-                               x += row->par()->table->GetBeginningOfTextInCell(cell);
-                       } else {
-                               x += SingleWidth(bview, row->par(), pos);
-                       }
-               }
-       } else {
-               /* table stuff -- end*/
-#endif
-               LyXParagraph::size_type main_body =
-                       BeginningOfMainBody(bview->buffer(), row->par());
-               if ((main_body > 0) &&
-                   ((main_body-1 > last) || 
-                    !row->par()->IsLineSeparator(main_body-1)))
-                       main_body = 0;
-
-               for (LyXParagraph::size_type vpos = row->pos();
-                    vpos < cursor_vpos; ++vpos) {
-                       pos = vis2log(vpos);
-                       if (main_body > 0 && pos == main_body-1) {
-                               x += fill_label_hfill +
-                                       lyxfont::width(textclasslist.Style(
-                                               bview->buffer()->params.textclass,
-                                               row->par()->GetLayout())
-                                                      .labelsep,
-                                                      GetFont(bview->buffer(), row->par(), -2));
-                               if (row->par()->IsLineSeparator(main_body-1))
-                                       x -= SingleWidth(bview, row->par(),main_body-1);
-                       }
-                       if (HfillExpansion(bview->buffer(), row, pos)) {
-                               x += SingleWidth(bview, row->par(), pos);
-                               if (pos >= main_body)
-                                       x += fill_hfill;
-                               else 
-                                       x += fill_label_hfill;
-                       } else if (row->par()->IsSeparator(pos)) {
-                               x += SingleWidth(bview, row->par(), pos);
-                               if (pos >= main_body)
-                                       x += fill_separator;
-                       } else
-                               x += SingleWidth(bview, row->par(), pos);
-               }
-#ifndef NEW_TABULAR
+       
+       LyXParagraph::size_type main_body =
+               BeginningOfMainBody(bview->buffer(), row->par());
+       if ((main_body > 0) &&
+           ((main_body-1 > last) || 
+            !row->par()->IsLineSeparator(main_body-1)))
+               main_body = 0;
+       
+       for (LyXParagraph::size_type vpos = row->pos();
+            vpos < cursor_vpos; ++vpos) {
+               pos = vis2log(vpos);
+               if (main_body > 0 && pos == main_body - 1) {
+                       x += fill_label_hfill +
+                               lyxfont::width(textclasslist.Style(
+                                       bview->buffer()->params.textclass,
+                                       row->par()->GetLayout())
+                                              .labelsep,
+                                              GetFont(bview->buffer(), row->par(), -2));
+                       if (row->par()->IsLineSeparator(main_body-1))
+                               x -= SingleWidth(bview, row->par(),main_body-1);
+               }
+               if (HfillExpansion(bview->buffer(), row, pos)) {
+                       x += SingleWidth(bview, row->par(), pos);
+                       if (pos >= main_body)
+                               x += fill_hfill;
+                       else 
+                               x += fill_label_hfill;
+               } else if (row->par()->IsSeparator(pos)) {
+                       x += SingleWidth(bview, row->par(), pos);
+                       if (pos >= main_body)
+                               x += fill_separator;
+               } else
+                       x += SingleWidth(bview, row->par(), pos);
        }
-#endif
-   
+       
        cur.x(int(x));
        cur.x_fix(cur.x());
        cur.row(row);
@@ -3018,6 +2894,7 @@ void LyXText::SetCursorIntern(BufferView * bview, LyXParagraph * par,
                SetCurrentFont(bview);
 }
 
+
 void LyXText::SetCurrentFont(BufferView * bview) const
 {
        LyXParagraph::size_type pos = cursor.pos();
@@ -3025,11 +2902,7 @@ void LyXText::SetCurrentFont(BufferView * bview) const
                --pos;
 
        if (pos > 0) {
-               if (pos == cursor.par()->Last()
-#ifndef NEW_TABULAR
-                   || (cursor.par()->table && cursor.par()->IsNewline(pos))
-#endif
-                       )
+               if (pos == cursor.par()->Last())
                        --pos;
                else if (cursor.par()->IsSeparator(pos)) {
                        if (pos > cursor.row()->pos() &&
@@ -3056,7 +2929,7 @@ void LyXText::SetCurrentFont(BufferView * bview) const
 }
 
 
-void LyXText::SetCursorFromCoordinates(BufferView * bview, int x, long y) const
+void LyXText::SetCursorFromCoordinates(BufferView * bview, int x, int y) const
 {
        LyXCursor old_cursor = cursor;
    
@@ -3078,7 +2951,7 @@ void LyXText::SetCursorFromCoordinates(BufferView * bview, int x, long y) const
 
 
 void LyXText::SetCursorFromCoordinates(BufferView * bview, LyXCursor & cur,
-                                      int x, long y) const
+                                      int x, int y) const
 {
        /* get the row first */ 
    
@@ -3096,21 +2969,6 @@ void LyXText::SetCursorFromCoordinates(BufferView * bview, LyXCursor & cur,
 
 
 void LyXText::CursorLeft(BufferView * bview, bool internal) const
-{
-       CursorLeftIntern(bview, internal);
-#ifndef NEW_TABULAR
-        if (cursor.par()->table) {
-                int cell = NumberOfCell(cursor.par(), cursor.pos());
-                if (cursor.par()->table->IsContRow(cell)
-                    && cursor.par()->table->CellHasContRow(cursor.par()->table->GetCellAbove(cell)) < 0) {
-                        CursorUp(bview);
-                }
-        }
-#endif
-}
-
-
-void LyXText::CursorLeftIntern(BufferView * bview, bool internal) const
 {
        if (cursor.pos() > 0) {
                bool boundary = cursor.boundary();
@@ -3126,28 +2984,9 @@ void LyXText::CursorLeftIntern(BufferView * bview, bool internal) const
 
 
 void LyXText::CursorRight(BufferView * bview, bool internal) const
-{
-       CursorRightIntern(bview, internal);
-#ifndef NEW_TABULAR
-        if (cursor.par()->table) {
-                int cell = NumberOfCell(cursor.par(), cursor.pos());
-                if (cursor.par()->table->IsContRow(cell) &&
-                    cursor.par()->table->CellHasContRow(cursor.par()->table->GetCellAbove(cell))<0) {
-                        CursorUp(bview);
-                }
-        }
-#endif
-}
-
-
-void LyXText::CursorRightIntern(BufferView * bview, bool internal) const
 {
        if (!internal && cursor.boundary() &&
-           (
-#ifndef NEW_TABULAR
-                   !cursor.par()->table ||
-#endif
-                   !cursor.par()->IsNewline(cursor.pos())))
+           !cursor.par()->IsNewline(cursor.pos()))
                SetCursor(bview, cursor.par(), cursor.pos(), true, false);
        else if (cursor.pos() < cursor.par()->Last()) {
                SetCursor(bview, cursor.par(), cursor.pos() + 1, true, false);
@@ -3163,47 +3002,14 @@ void LyXText::CursorUp(BufferView * bview) const
 {
        SetCursorFromCoordinates(bview, cursor.x_fix(), 
                                 cursor.y() - cursor.row()->baseline() - 1);
-#ifndef NEW_TABULAR
-        if (cursor.par()->table) {
-                int cell = NumberOfCell(cursor.par(), cursor.pos());
-                if (cursor.par()->table->IsContRow(cell) &&
-                    cursor.par()->table->CellHasContRow(cursor.par()->table->GetCellAbove(cell))<0) {
-                        CursorUp(bview);
-                }
-        }
-#endif
 }
 
 
 void LyXText::CursorDown(BufferView * bview) const
 {
-#ifndef NEW_TABULAR
-        if (cursor.par()->table &&
-            cursor.par()->table->ShouldBeVeryLastRow(NumberOfCell(cursor.par(), cursor.pos())) &&
-            !cursor.par()->next)
-                return;
-#endif
-       
        SetCursorFromCoordinates(bview, cursor.x_fix(), 
                                 cursor.y() - cursor.row()->baseline()
                                 + cursor.row()->height() + 1);
-#ifndef NEW_TABULAR
-        if (cursor.par()->table) {
-                int cell = NumberOfCell(cursor.par(), cursor.pos());
-                int cell_above = cursor.par()->table->GetCellAbove(cell);
-                while(cursor.par()->table &&
-                      cursor.par()->table->IsContRow(cell) &&
-                      (cursor.par()->table->CellHasContRow(cell_above)<0)) {
-                    SetCursorFromCoordinates(bview, cursor.x_fix(), 
-                                             cursor.y() - cursor.row()->baseline()
-                                             + cursor.row()->height() + 1);
-                    if (cursor.par()->table) {
-                        cell = NumberOfCell(cursor.par(), cursor.pos());
-                        cell_above = cursor.par()->table->GetCellAbove(cell);
-                    }
-                }
-        }
-#endif
 }
 
 
@@ -3294,7 +3100,7 @@ void LyXText::DeleteEmptyParagraphMechanism(BufferView * bview,
        LyXCursor tmpcursor;
 
        if (old_cursor.par() != cursor.par()) {
-               if ( (old_cursor.par()->Last() == 0
+               if ((old_cursor.par()->Last() == 0
                      || (old_cursor.par()->Last() == 1
                          && old_cursor.par()->IsLineSeparator(0)))
 #ifndef NEW_INSETS
@@ -3701,9 +3507,9 @@ Undo * LyXText::CreateUndo(Buffer * buf, Undo::undo_kind kind,
                        end = end->next;
        }
 
-       if (start && end
-           && start != end->next
-           && (before != behind || (!before && !behind))) {
+       if (start && end && (start != end->next) &&
+           ((before != behind) || (!before && !behind)))
+       {
                tmppar = start;
                tmppar2 = tmppar->Clone();
                tmppar2->id(tmppar->id());
@@ -3768,70 +3574,6 @@ void LyXText::SetCursorParUndo(Buffer * buf)
 }
 
 
-#ifndef NEW_TABULAR
-void LyXText::RemoveTableRow(LyXCursor & cur) const
-{
-       int cell = -1;
-       int cell_org = 0;
-       int ocell = 0;
-    
-       // move to the previous row
-       int cell_act = NumberOfCell(cur.par(), cur.pos());
-       if (cell < 0)
-               cell = cell_act;
-       while (cur.pos() && !cur.par()->IsNewline(cur.pos() - 1))
-               cur.pos(cur.pos() - 1);
-       while (cur.pos() && 
-              !cur.par()->table->IsFirstCell(cell_act)) {
-               cur.pos(cur.pos() - 1);
-               while (cur.pos() && !cur.par()->IsNewline(cur.pos() - 1))
-                       cur.pos(cur.pos() - 1);
-               --cell;
-               --cell_act;
-       }
-       // now we have to pay attention if the actual table is the
-       //   main row of TableContRows and if yes to delete all of them
-       if (!cell_org)
-               cell_org = cell;
-       do {
-               ocell = cell;
-               // delete up to the next row
-               while (cur.pos() < cur.par()->Last() && 
-                      (cell_act == ocell
-                       || !cur.par()->table->IsFirstCell(cell_act))) {
-                       while (cur.pos() < cur.par()->Last() &&
-                              !cur.par()->IsNewline(cur.pos()))
-                               cur.par()->Erase(cur.pos());
-                       ++cell;
-                       ++cell_act;
-                       if (cur.pos() < cur.par()->Last())
-                               cur.par()->Erase(cur.pos());
-               }
-               if (cur.pos() && cur.pos() == cur.par()->Last()) {
-                       cur.pos(cur.pos() - 1);
-                       cur.par()->Erase(cur.pos()); // no newline at very end!
-               }
-       } while (((cell + 1) < cur.par()->table->GetNumberOfCells()) &&
-                !cur.par()->table->IsContRow(cell_org) &&
-                cur.par()->table->IsContRow(cell));
-       cur.par()->table->DeleteRow(cell_org);
-       return;
-}
-#endif
-
-
-#ifndef NEW_TABULAR
-bool LyXText::IsEmptyTableCell() const
-{
-       LyXParagraph::size_type pos = cursor.pos() - 1;
-       while (pos >= 0 && pos < cursor.par()->Last()
-              && !cursor.par()->IsNewline(pos))
-               --pos;
-       return cursor.par()->IsNewline(pos + 1);
-}
-#endif
-
-
 void LyXText::toggleAppendix(BufferView * bview)
 {
 #ifndef NEW_INSETS