]> git.lyx.org Git - features.git/blobdiff - src/text2.C
clear()->erase() ; lots of using directives for cxx
[features.git] / src / text2.C
index 2a5a29d428502a30a3feabe8dbe81ea24130ec2a..16ac40d0a53f0100b8de8bcfbe9bcfd09c278c98 100644 (file)
@@ -40,7 +40,6 @@
 #include "font.h"
 #include "debug.h"
 
-#define FIX_DOUBLE_SPACE 1
 //#define USE_OLD_CUT_AND_PASTE 1
 
 using std::copy;
@@ -52,10 +51,7 @@ LyXText::LyXText(BufferView * bv, int pw, Buffer * p)
        owner_ = bv;
        firstrow = 0;
        lastrow = 0;
-       currentrow = 0;
-       currentrow_y = 0;
        paperwidth = pw;
-       bparams = &p->params;
        buffer = p;
        number_of_rows = 0;
        refresh_y = 0;
@@ -88,6 +84,22 @@ LyXText::LyXText(BufferView * bv, int pw, Buffer * p)
 
        // Default layouttype for copy environment type
        copylayouttype = 0;
+
+#if 0
+       // Dump all rowinformation:
+       Row * tmprow = firstrow;
+       lyxerr << "Baseline Paragraph Pos Height Ascent Fill\n";
+       while (tmprow) {
+               lyxerr << tmprow->baseline << '\t'
+                      << tmprow->par << '\t'
+                      << tmprow->pos << '\t'
+                      << tmprow->height << '\t'
+                      << tmprow->ascent_of_text << '\t'
+                      << tmprow->fill << '\n';
+               tmprow = tmprow->next;
+       }
+       lyxerr.flush();
+#endif
 }
 
 
@@ -120,7 +132,8 @@ LyXFont LyXText::GetFont(LyXParagraph * par,
                         LyXParagraph::size_type pos) const
 {
        LyXLayout const & layout = 
-               textclasslist.Style(bparams->textclass, par->GetLayout());
+               textclasslist.Style(buffer->params.textclass,
+                                   par->GetLayout());
 
        char par_depth = par->GetDepth();
        // We specialize the 95% common case:
@@ -174,13 +187,13 @@ LyXFont LyXText::GetFont(LyXParagraph * par,
                par = par->DepthHook(par_depth - 1);
                if (par) {
                        tmpfont.realize(textclasslist.
-                                       Style(bparams->textclass,
+                                       Style(buffer->params.textclass,
                                              par->GetLayout()).font);
                        par_depth = par->GetDepth();
                }
        }
 
-       tmpfont.realize(textclasslist.TextClass(bparams->textclass).defaultfont());
+       tmpfont.realize(textclasslist.TextClass(buffer->params.textclass).defaultfont());
 
        // Cosmetic improvement: If this is an open footnote, make the font 
        // smaller.
@@ -204,8 +217,9 @@ void LyXText::SetCharFont(LyXParagraph * par,
                        font = par->GetInset(pos)->ConvertFont(font);
        }
 
-       LyXLayout const & layout = textclasslist.Style(bparams->textclass,
-                                          par->GetLayout());
+       LyXLayout const & layout =
+               textclasslist.Style(buffer->params.textclass,
+                                   par->GetLayout());
 
        // Get concrete layout font to reduce against
        LyXFont layoutfont;
@@ -222,12 +236,12 @@ void LyXText::SetCharFont(LyXParagraph * par,
                        tp = tp->DepthHook(tp->GetDepth()-1);
                        if (tp)
                                layoutfont.realize(textclasslist.
-                                               Style(bparams->textclass,
+                                               Style(buffer->params.textclass,
                                                      tp->GetLayout()).font);
                }
        }
 
-       layoutfont.realize(textclasslist.TextClass(bparams->textclass).defaultfont());
+       layoutfont.realize(textclasslist.TextClass(buffer->params.textclass).defaultfont());
 
        if (par->footnoteflag == LyXParagraph::OPEN_FOOTNOTE
            && par->footnotekind == LyXParagraph::FOOTNOTE) {
@@ -281,11 +295,6 @@ void LyXText::RemoveRow(Row * row) const
           row of this row */
        long unused_y;
        GetRow(row->par, row->pos, unused_y);
-       currentrow = currentrow->previous;
-       if (currentrow)
-               currentrow_y -= currentrow->height;
-       else
-               currentrow_y = 0;
    
        if (row->next)
                row->next->previous = row->previous;
@@ -448,7 +457,8 @@ void LyXText::MakeFontEntriesLayoutSpecific(LyXParagraph * par)
 {
    
        LyXLayout const & layout =
-               textclasslist.Style(bparams->textclass, par->GetLayout());
+               textclasslist.Style(buffer->params.textclass,
+                                   par->GetLayout());
 
        LyXFont layoutfont, tmpfont;
        for (LyXParagraph::size_type pos = 0;
@@ -464,12 +474,78 @@ void LyXText::MakeFontEntriesLayoutSpecific(LyXParagraph * par)
        }
 }
 
+LyXParagraph * LyXText::SetLayout(LyXCursor & cur, LyXCursor & sstart_cur,
+                                 LyXCursor & send_cur,
+                                 LyXTextClass::size_type layout)
+{
+       LyXParagraph * endpar = send_cur.par->LastPhysicalPar()->Next();
+       LyXParagraph * undoendpar = endpar;
+
+       if (endpar && endpar->GetDepth()) {
+               while (endpar && endpar->GetDepth()) {
+                       endpar = endpar->LastPhysicalPar()->Next();
+                       undoendpar = endpar;
+               }
+       } else if (endpar) {
+               endpar = endpar->Next(); // because of parindents etc.
+       }
+   
+       SetUndo(Undo::EDIT,
+               sstart_cur.par->ParFromPos(sstart_cur.pos)->previous, 
+               undoendpar);
+
+       /* ok we have a selection. This is always between sstart_cur
+        * and sel_end cursor */ 
+       cur = sstart_cur;
+   
+       LyXLayout const & lyxlayout =
+               textclasslist.Style(buffer->params.textclass, layout);
+   
+       while (cur.par != send_cur.par) {
+               if (cur.par->footnoteflag == sstart_cur.par->footnoteflag) {
+                       cur.par->SetLayout(layout);
+                       MakeFontEntriesLayoutSpecific(cur.par);
+                       LyXParagraph* fppar = cur.par->FirstPhysicalPar();
+                       fppar->added_space_top = lyxlayout.fill_top ?
+                               VSpace(VSpace::VFILL) : VSpace(VSpace::NONE);
+                       fppar->added_space_bottom = lyxlayout.fill_bottom ? 
+                               VSpace(VSpace::VFILL) : VSpace(VSpace::NONE); 
+                       if (lyxlayout.margintype == MARGIN_MANUAL)
+                               cur.par->SetLabelWidthString(lyxlayout.labelstring());
+                       if (lyxlayout.labeltype != LABEL_BIBLIO
+                           && fppar->bibkey) {
+                               delete fppar->bibkey;
+                               fppar->bibkey = 0;
+                       }
+               }
+               cur.par = cur.par->Next();
+       }
+       if (cur.par->footnoteflag == sstart_cur.par->footnoteflag) {
+               cur.par->SetLayout(layout);
+               MakeFontEntriesLayoutSpecific(cur.par);
+               LyXParagraph* fppar = cur.par->FirstPhysicalPar();
+               fppar->added_space_top = lyxlayout.fill_top ?
+                       VSpace(VSpace::VFILL) : VSpace(VSpace::NONE);
+               fppar->added_space_bottom = lyxlayout.fill_bottom ? 
+                       VSpace(VSpace::VFILL) : VSpace(VSpace::NONE); 
+               if (lyxlayout.margintype == MARGIN_MANUAL)
+                       cur.par->SetLabelWidthString(lyxlayout.labelstring());
+               if (lyxlayout.labeltype != LABEL_BIBLIO
+                   && fppar->bibkey) {
+                       delete fppar->bibkey;
+                       fppar->bibkey = 0;
+               }
+       }
+       return endpar;
+}
 
 // set layout over selection and make a total rebreak of those paragraphs
 void LyXText::SetLayout(LyXTextClass::size_type layout)
 {
-       LyXCursor tmpcursor;
+       LyXCursor
+               tmpcursor = cursor;  /* store the current cursor  */
 
+#ifdef USE_OLD_SET_LAYOUT
        // if there is no selection just set the layout
        // of the current paragraph  */
        if (!selection) {
@@ -494,14 +570,12 @@ void LyXText::SetLayout(LyXTextClass::size_type layout)
                sel_start_cursor.par->ParFromPos(sel_start_cursor.pos)->previous, 
                undoendpar);
 
-       tmpcursor = cursor;                    /* store the current cursor  */
-
        /* ok we have a selection. This is always between sel_start_cursor
         * and sel_end cursor */ 
        cursor = sel_start_cursor;
    
        LyXLayout const & lyxlayout =
-               textclasslist.Style(bparams->textclass, layout);
+               textclasslist.Style(buffer->params.textclass, layout);
    
        while (cursor.par != sel_end_cursor.par) {
                if (cursor.par->footnoteflag ==
@@ -540,7 +614,16 @@ void LyXText::SetLayout(LyXTextClass::size_type layout)
                        fppar->bibkey = 0;
                }
        }
-   
+#else
+       // if there is no selection just set the layout
+       // of the current paragraph  */
+       if (!selection) {
+               sel_start_cursor = cursor;  // dummy selection
+               sel_end_cursor = cursor;
+       }
+       LyXParagraph *
+       endpar = SetLayout(cursor, sel_start_cursor, sel_end_cursor, layout);
+#endif
        RedoParagraphs(sel_start_cursor, endpar);
    
        // we have to reset the selection, because the
@@ -596,7 +679,7 @@ void  LyXText::IncDepth()
                // NOTE: you can't change the depth of a bibliography entry
                if (cursor.par->footnoteflag ==
                    sel_start_cursor.par->footnoteflag
-                   && textclasslist.Style(bparams->textclass,
+                   && textclasslist.Style(buffer->params.textclass,
                                      cursor.par->GetLayout()
                                     ).labeltype != LABEL_BIBLIO) {
                        LyXParagraph * prev =
@@ -604,7 +687,7 @@ void  LyXText::IncDepth()
                        if (prev 
                            && (prev->GetDepth() - cursor.par->GetDepth() > 0
                                || (prev->GetDepth() == cursor.par->GetDepth()
-                                   && textclasslist.Style(bparams->textclass,
+                                   && textclasslist.Style(buffer->params.textclass,
                                                      prev->GetLayout()).isEnvironment()))) {
                                cursor.par->FirstPhysicalPar()->depth++;
                                anything_changed = true;
@@ -712,7 +795,9 @@ void LyXText::SetFont(LyXFont const & font, bool toggleall)
                else
                        layoutfont = GetFont(cursor.par, -1);
                // Update current font
-               real_current_font.update(font, bparams->language_info, toggleall);
+               real_current_font.update(font,
+                                        buffer->params.language_info,
+                                        toggleall);
 
                // Reduce to implicit settings
                current_font = real_current_font;
@@ -741,7 +826,9 @@ void LyXText::SetFont(LyXFont const & font, bool toggleall)
                        // an open footnote should behave
                        // like a closed one
                        LyXFont newfont = GetFont(cursor.par, cursor.pos);
-                       newfont.update(font, bparams->language_info, toggleall);
+                       newfont.update(font,
+                                      buffer->params.language_info,
+                                      toggleall);
                        SetCharFont(cursor.par, cursor.pos, newfont);
                        cursor.pos++;
                } else {
@@ -904,14 +991,14 @@ void LyXText::RedoParagraphs(LyXCursor const & cur,
 }
 
 
-int LyXText::FullRebreak()
+bool LyXText::FullRebreak()
 {
        if (need_break_row) {
                BreakAgain(need_break_row);
                need_break_row = 0;
-               return 1;
+               return true;
        }
-       return 0;
+       return false;
 }
 
 
@@ -1035,38 +1122,21 @@ void  LyXText::CursorBottom() 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 */ 
+* of this row */
 Row * LyXText::GetRowNearY(long & y) const
 {
-       Row * tmprow;
-       long tmpy;
-   
-       if (currentrow) {
-               tmprow = currentrow;
-               tmpy = currentrow_y;
-       } else {
-               tmprow = firstrow;
-               tmpy = 0;
-       }
-
-       if (tmpy <= y)
-               while (tmprow->next && tmpy + tmprow->height <= y) {
-                       tmpy += tmprow->height;
-                       tmprow = tmprow->next;
-               }
-       else
-               while (tmprow->previous && tmpy > y) {
-                       tmprow = tmprow->previous;
-                       tmpy -= tmprow->height;
-               }
+       Row * tmprow = firstrow;
+       long tmpy = 0;
 
-       currentrow = tmprow;
-       currentrow_y = tmpy;
+       while (tmprow->next && tmpy + tmprow->height <= y) {
+               tmpy += tmprow->height;
+               tmprow = tmprow->next;
+       }
 
        y = tmpy;   // return the real y
        return tmprow;
 }
-   
+
 
 void LyXText::ToggleFree(LyXFont const & font, bool toggleall)
 {
@@ -1080,8 +1150,11 @@ void LyXText::ToggleFree(LyXFont const & font, bool toggleall)
        }
 
        // Try implicit word selection
+       // If there is a change in the language the implicit word selection 
+       // is disabled.
        LyXCursor resetCursor = cursor;
-       int implicitSelection = SelectWordWhenUnderCursor();
+       bool implicitSelection = (font.language() == ignore_language)
+               ? SelectWordWhenUnderCursor() : false;
 
        // Set font
        SetFont(font, toggleall);
@@ -1099,7 +1172,7 @@ void LyXText::ToggleFree(LyXFont const & font, bool toggleall)
 
 LyXParagraph::size_type LyXText::BeginningOfMainBody(LyXParagraph * par) const
 {
-       if (textclasslist.Style(bparams->textclass,
+       if (textclasslist.Style(buffer->params.textclass,
                                par->GetLayout()).labeltype != LABEL_MANUAL)
                return 0;
        else
@@ -1146,7 +1219,7 @@ void LyXText::MeltFootnoteEnvironment()
                tmppar->footnoteflag = LyXParagraph::NO_FOOTNOTE;
       
                /* remember the captions and empty paragraphs */
-               if ((textclasslist.Style(bparams->textclass,
+               if ((textclasslist.Style(buffer->params.textclass,
                                         tmppar->GetLayout())
                     .labeltype == LABEL_SENSITIVE)
                    || !tmppar->Last())
@@ -1260,13 +1333,13 @@ void LyXText::SetParagraph(bool line_top, bool line_bottom,
                        // does the layout allow the new alignment?
                        if (align == LYX_ALIGN_LAYOUT)
                                align = textclasslist
-                                       .Style(bparams->textclass,
+                                       .Style(buffer->params.textclass,
                                               cursor.par->GetLayout()).align;
                        if (align & textclasslist
-                           .Style(bparams->textclass,
+                           .Style(buffer->params.textclass,
                                   cursor.par->GetLayout()).alignpossible) {
                                if (align == textclasslist
-                                   .Style(bparams->textclass,
+                                   .Style(buffer->params.textclass,
                                           cursor.par->GetLayout()).align)
                                        cursor.par->align = LYX_ALIGN_LAYOUT;
                                else
@@ -1403,11 +1476,12 @@ void LyXText::SetCounter(LyXParagraph * par) const
        // this is only relevant for the beginning of paragraph
        par = par->FirstPhysicalPar();
 
-       LyXLayout const & layout = textclasslist.Style(bparams->textclass, 
-                                                      par->GetLayout());
+       LyXLayout const & layout =
+               textclasslist.Style(buffer->params.textclass, 
+                                   par->GetLayout());
 
        LyXTextClass const & textclass =
-               textclasslist.TextClass(bparams->textclass);
+               textclasslist.TextClass(buffer->params.textclass);
 
        /* copy the prev-counters to this one, unless this is the start of a 
           footnote or of a bibliography or the very first paragraph */
@@ -1415,7 +1489,7 @@ void LyXText::SetCounter(LyXParagraph * par) const
            && !(par->Previous()->footnoteflag == LyXParagraph::NO_FOOTNOTE 
                    && par->footnoteflag == LyXParagraph::OPEN_FOOTNOTE
                    && par->footnotekind == LyXParagraph::FOOTNOTE)
-           && !(textclasslist.Style(bparams->textclass,
+           && !(textclasslist.Style(buffer->params.textclass,
                                par->Previous()->GetLayout()
                                ).labeltype != LABEL_BIBLIO
                 && layout.labeltype == LABEL_BIBLIO)) {
@@ -1450,7 +1524,7 @@ void LyXText::SetCounter(LyXParagraph * par) const
            && par->Previous()
            && par->Previous()->footnoteflag != LyXParagraph::OPEN_FOOTNOTE
            && (par->PreviousBeforeFootnote()
-               && textclasslist.Style(bparams->textclass,
+               && textclasslist.Style(buffer->params.textclass,
                                  par->PreviousBeforeFootnote()->GetLayout()
                                 ).labeltype >= LABEL_COUNTER_ENUMI)) {
                 // Any itemize or enumerate environment in a marginnote
@@ -1472,7 +1546,7 @@ void LyXText::SetCounter(LyXParagraph * par) const
         */
        if (par->Previous()
            && par->Previous()->GetDepth() < par->GetDepth()
-           && textclasslist.Style(bparams->textclass,
+           && textclasslist.Style(buffer->params.textclass,
                              par->Previous()->GetLayout()
                             ).labeltype == LABEL_COUNTER_ENUMI
            && par->enumdepth < 3
@@ -1501,7 +1575,7 @@ void LyXText::SetCounter(LyXParagraph * par) const
        }
    
        if (!par->labelstring.empty()) {
-               par->labelstring.clear();
+               par->labelstring.erase();
        }
    
        if (layout.margintype == MARGIN_MANUAL) {
@@ -1516,7 +1590,7 @@ void LyXText::SetCounter(LyXParagraph * par) const
        if (layout.labeltype >=  LABEL_FIRST_COUNTER) {
       
                int i = layout.labeltype - LABEL_FIRST_COUNTER;
-               if (i >= 0 && i<= bparams->secnumdepth) {
+               if (i >= 0 && i<= buffer->params.secnumdepth) {
                        par->incCounter(i);     // increment the counter  
         
                        // Is there a label? Useful for Chapter layout
@@ -1524,12 +1598,12 @@ void LyXText::SetCounter(LyXParagraph * par) const
                                if (!layout.labelstring().empty())
                                        par->labelstring = layout.labelstring();
                                else
-                                       par->labelstring.clear();
+                                       par->labelstring.erase();
                         } else {
                                if (!layout.labelstring_appendix().empty())
                                        par->labelstring = layout.labelstring_appendix();
                                else
-                                       par->labelstring.clear();
+                                       par->labelstring.erase();
                        }
 
 #ifdef HAVE_SSTREAM
@@ -1805,9 +1879,11 @@ void LyXText::UpdateCounters(Row * row) const
                /* now  check for the headline layouts. remember that they
                 * have a dynamic left margin */ 
                if (!par->IsDummy()
-                   && ( textclasslist.Style(bparams->textclass, par->layout).margintype == MARGIN_DYNAMIC
-                        || textclasslist.Style(bparams->textclass, par->layout).labeltype == LABEL_SENSITIVE)
-                       ){
+                   && ( textclasslist.Style(buffer->params.textclass,
+                                            par->layout).margintype == MARGIN_DYNAMIC
+                        || textclasslist.Style(buffer->params.textclass,
+                                               par->layout).labeltype == LABEL_SENSITIVE)
+                       ) {
         
                        /* Rebreak the paragraph */ 
                        RemoveParagraph(row);
@@ -1940,7 +2016,7 @@ void LyXText::CutSelection(bool doclear)
        DeleteSimpleCutBuffer();
    
        // set the textclass
-       simple_cut_buffer_textclass = bparams->textclass;
+       simple_cut_buffer_textclass = buffer->params.textclass;
 
 #ifdef WITH_WARNINGS
 #warning Asger: Make cut more intelligent here.
@@ -1985,22 +2061,6 @@ void LyXText::CutSelection(bool doclear)
           (Lgb)
        */
 
-#ifndef FIX_DOUBLE_SPACE
-       bool space_wrapped =
-               sel_end_cursor.par->IsLineSeparator(sel_end_cursor.pos);
-       if (sel_end_cursor.pos > 0
-           && sel_end_cursor.par->IsLineSeparator(sel_end_cursor.pos - 1)) {
-               // please break before a space at the end
-               sel_end_cursor.pos--;
-               space_wrapped = true;
-       }
-       // cut behind a space if there is one
-       while (sel_start_cursor.par->Last() > sel_start_cursor.pos
-              && sel_start_cursor.par->IsLineSeparator(sel_start_cursor.pos)
-              && (sel_start_cursor.par != sel_end_cursor.par
-                  || sel_start_cursor.pos < sel_end_cursor.pos))
-               sel_start_cursor.pos++; 
-#endif
        // there are two cases: cut only within one paragraph or
        // more than one paragraph
    
@@ -2023,55 +2083,19 @@ void LyXText::CutSelection(bool doclear)
                        }
                        simple_cut_buffer->InsertFromMinibuffer(simple_cut_buffer->Last());
                }
-#ifndef FIX_DOUBLE_SPACE
-               // check for double spaces
-               if (sel_start_cursor.pos &&
-                   sel_start_cursor.par->Last() > sel_start_cursor.pos
-                   && sel_start_cursor.par
-                   ->IsLineSeparator(sel_start_cursor.pos - 1)
-                   && sel_start_cursor.par
-                   ->IsLineSeparator(sel_start_cursor.pos)) {
-                       sel_start_cursor.par->Erase(sel_start_cursor.pos);
-               }
-               if (space_wrapped)
-                       simple_cut_buffer->InsertChar(i - sel_start_cursor.pos,
-                                                     ' ');
-#endif
                endpar = sel_end_cursor.par->Next();
        } else {
                // cut more than one paragraph
    
                sel_end_cursor.par
                        ->BreakParagraphConservative(sel_end_cursor.pos);
-#ifndef FIX_DOUBLE_SPACE
-               // insert a space at the end if there was one
-               if (space_wrapped)
-                       sel_end_cursor.par
-                               ->InsertChar(sel_end_cursor.par->Last(), ' ');
-#endif
                sel_end_cursor.par = sel_end_cursor.par->Next();
                sel_end_cursor.pos = 0;
    
                cursor = sel_end_cursor;
 
-#ifndef FIX_DOUBLE_SPACE
-               // please break behind a space, if there is one.
-               // The space should be copied too
-               if (sel_start_cursor.par
-                   ->IsLineSeparator(sel_start_cursor.pos))
-                       sel_start_cursor.pos++;
-#endif   
                sel_start_cursor.par
                        ->BreakParagraphConservative(sel_start_cursor.pos);
-#ifndef FIX_DOUBLE_SPACE
-               if (!sel_start_cursor.pos
-                   || sel_start_cursor.par
-                   ->IsLineSeparator(sel_start_cursor.pos - 1)
-                   || sel_start_cursor.par
-                   ->IsNewline(sel_start_cursor.pos - 1)) {
-                       sel_start_cursor.par->Next()->InsertChar(0, ' ');
-               }
-#endif
                // store the endparagraph for redoing later
                endpar = sel_end_cursor.par->Next(); /* needed because
                                                        the sel_end_
@@ -2110,17 +2134,6 @@ void LyXText::CutSelection(bool doclear)
                    || 
                    !sel_start_cursor.par->Next()->Last())
                        sel_start_cursor.par->ParFromPos(sel_start_cursor.pos)->PasteParagraph();
-
-#ifndef FIX_DOUBLE_SPACE
-               // maybe a forgotten blank
-               if (sel_start_cursor.pos 
-                   && sel_start_cursor.par
-                   ->IsLineSeparator(sel_start_cursor.pos)
-                   && sel_start_cursor.par
-                   ->IsLineSeparator(sel_start_cursor.pos - 1)) {
-                       sel_start_cursor.par->Erase(sel_start_cursor.pos);
-               }
-#endif
        }
 
        // sometimes necessary
@@ -2201,13 +2214,13 @@ void LyXText::CutSelection(bool doclear)
        endpar = sel_start_cursor.par;
        cap.cutSelection(sel_start_cursor.par, &endpar,
                         sel_start_cursor.pos, sel_end_cursor.pos,
-                        bparams->textclass, doclear);
+                        buffer->params.textclass, doclear);
     } else {
        endpar = sel_end_cursor.par;
 
        cap.cutSelection(sel_start_cursor.par, &endpar,
                         sel_start_cursor.pos, sel_end_cursor.pos,
-                        bparams->textclass, doclear);
+                        buffer->params.textclass, doclear);
        cursor.par = sel_end_cursor.par = endpar;
        cursor.pos = sel_end_cursor.pos;
     }
@@ -2270,16 +2283,15 @@ void LyXText::CopySelection()
        DeleteSimpleCutBuffer();
 
        // set the textclass
-       simple_cut_buffer_textclass = bparams->textclass;
+       simple_cut_buffer_textclass = buffer->params.textclass;
 
-#ifdef FIX_DOUBLE_SPACE
        // copy behind a space if there is one
        while (sel_start_cursor.par->Last() > sel_start_cursor.pos
               && sel_start_cursor.par->IsLineSeparator(sel_start_cursor.pos)
               && (sel_start_cursor.par != sel_end_cursor.par
                   || sel_start_cursor.pos < sel_end_cursor.pos))
                sel_start_cursor.pos++; 
-#endif
+
        // there are two cases: copy only within one paragraph
        // or more than one paragraph
        if (sel_start_cursor.par->ParFromPos(sel_start_cursor.pos) 
@@ -2373,20 +2385,18 @@ void LyXText::CopySelection()
        }
        /* table stuff -- end */
    
-#ifdef FIX_DOUBLE_SPACE
        // copy behind a space if there is one
        while (sel_start_cursor.par->Last() > sel_start_cursor.pos
               && sel_start_cursor.par->IsLineSeparator(sel_start_cursor.pos)
               && (sel_start_cursor.par != sel_end_cursor.par
                   || sel_start_cursor.pos < sel_end_cursor.pos))
                sel_start_cursor.pos++; 
-#endif
 
        CutAndPaste cap;
 
        cap.copySelection(sel_start_cursor.par, sel_end_cursor.par,
                          sel_start_cursor.pos, sel_end_cursor.pos,
-                         bparams->textclass);
+                         buffer->params.textclass);
 }
 #endif          
 
@@ -2440,12 +2450,6 @@ void LyXText::PasteSelection()
        if (!simple_cut_buffer->next) {
                // only within a paragraph
 
-#ifndef FIX_DOUBLE_SPACE
-               // please break behind a space, if there is one
-               while (tmpcursor.par->Last() > tmpcursor.pos
-                      && tmpcursor.par->IsLineSeparator(tmpcursor.pos))
-                       tmpcursor.pos++; 
-#endif
                tmppar = simple_cut_buffer->Clone();
                /* table stuff -- begin */
                bool table_too_small = false;
@@ -2461,7 +2465,6 @@ void LyXText::PasteSelection()
                                        else
                                                table_too_small = true;
                                } else {
-#ifdef FIX_DOUBLE_SPACE
                                        // This is an attempt to fix the
                                        // "never insert a space at the
                                        // beginning of a paragraph" problem.
@@ -2474,12 +2477,6 @@ void LyXText::PasteSelection()
                                                tmpcursor.par->InsertFromMinibuffer(tmpcursor.pos);
                                                tmpcursor.pos++;
                                        }
-#else
-                                       simple_cut_buffer->CutIntoMinibuffer(0);
-                                       simple_cut_buffer->Erase(0);
-                                       tmpcursor.par->InsertFromMinibuffer(tmpcursor.pos);
-                                       tmpcursor.pos++;
-#endif
                                }
                        }
                } else {
@@ -2491,7 +2488,6 @@ void LyXText::PasteSelection()
                        // the beginning of the paragraph the space should
                        // be removed.
                        while (simple_cut_buffer->size()) {
-#ifdef FIX_DOUBLE_SPACE
                                // This is an attempt to fix the
                                // "never insert a space at the
                                // beginning of a paragraph" problem.
@@ -2504,12 +2500,6 @@ void LyXText::PasteSelection()
                                        tmpcursor.par->InsertFromMinibuffer(tmpcursor.pos);
                                        tmpcursor.pos++;
                                }
-#else
-                               simple_cut_buffer->CutIntoMinibuffer(0);
-                               simple_cut_buffer->Erase(0);
-                               tmpcursor.par->InsertFromMinibuffer(tmpcursor.pos);
-                               tmpcursor.pos++;
-#endif
                        }
                }
                delete simple_cut_buffer;
@@ -2540,7 +2530,7 @@ void LyXText::PasteSelection()
      
                // make sure there is no class difference
                cap.SwitchLayoutsBetweenClasses(simple_cut_buffer_textclass,
-                                               bparams->textclass,
+                                               buffer->params.textclass,
                                                simple_cut_buffer);
      
                // make the simple_cut_buffer exactly the same layout than
@@ -2552,13 +2542,6 @@ void LyXText::PasteSelection()
                while (lastbuffer->Next())
                        lastbuffer = lastbuffer->Next();
      
-#ifndef FIX_DOUBLE_SPACE
-               // Please break behind a space, if there is one. The space 
-               // should be copied too.
-               if (cursor.par->Last() > cursor.pos
-                   && cursor.par->IsLineSeparator(cursor.pos))
-                       cursor.pos++; 
-#endif
                bool paste_the_end = false;
 
                // open the paragraph for inserting the simple_cut_buffer
@@ -2568,15 +2551,6 @@ void LyXText::PasteSelection()
                        paste_the_end = true;
                }
 
-#ifndef FIX_DOUBLE_SPACE
-               // be careful with double spaces
-               if ((!cursor.par->Last()
-                    || cursor.par->IsLineSeparator(cursor.pos - 1)
-                    || cursor.par->IsNewline(cursor.pos - 1))
-                   && simple_cut_buffer->text.size()
-                   && simple_cut_buffer->IsLineSeparator(0))
-                       simple_cut_buffer->Erase(0);
-#endif
                // set the end for redoing later
                endpar = cursor.par->ParFromPos(cursor.pos)->next->Next();
      
@@ -2602,41 +2576,14 @@ void LyXText::PasteSelection()
                // maybe some pasting
                if (lastbuffer->Next() && paste_the_end) {
                        if (lastbuffer->Next()->HasSameLayout(lastbuffer)) {
-#ifndef FIX_DOUBLE_SPACE
-                               // be careful with double spaces
-                               if ((!lastbuffer->Last()
-                                    || lastbuffer->IsLineSeparator(lastbuffer->Last() - 1)
-                                    || lastbuffer->IsNewline(lastbuffer->Last() - 1))
-                                   && lastbuffer->Next()->Last()
-                                   && lastbuffer->Next()->IsLineSeparator(0))
-                                       lastbuffer->Next()->Erase(0);
-#endif
                                lastbuffer->ParFromPos(lastbuffer->Last())->PasteParagraph();
         
                        } else if (!lastbuffer->Next()->Last()) {
                                lastbuffer->Next()->MakeSameLayout(lastbuffer);
-#ifndef FIX_DOUBLE_SPACE
-                               // be careful witth double spaces
-                               if ((!lastbuffer->Last()
-                                    || lastbuffer->IsLineSeparator(lastbuffer->Last() - 1)
-                                    || lastbuffer->IsNewline(lastbuffer->Last() - 1))
-                                   && lastbuffer->Next()->Last()
-                                   && lastbuffer->Next()->IsLineSeparator(0))
-                                       lastbuffer->Next()->Erase(0);
-#endif
                                lastbuffer->ParFromPos(lastbuffer->Last())->PasteParagraph();
         
                        } else if (!lastbuffer->Last()) {
                                lastbuffer->MakeSameLayout(lastbuffer->next);
-#ifndef FIX_DOUBLE_SPACE
-                               // be careful witth double spaces
-                               if ((!lastbuffer->Last()
-                                    || lastbuffer->IsLineSeparator(lastbuffer->Last() - 1)
-                                    || lastbuffer->IsNewline(lastbuffer->Last() - 1))
-                                   && lastbuffer->Next()->Last()
-                                   && lastbuffer->Next()->IsLineSeparator(0))
-                                       lastbuffer->Next()->Erase(0);
-#endif
                                lastbuffer->ParFromPos(lastbuffer->Last())->PasteParagraph();
         
                        } else
@@ -2676,7 +2623,7 @@ void LyXText::PasteSelection()
     LyXParagraph *actpar = cursor.par;
     int endpos = cursor.pos;
 
-    cap.pasteSelection(&actpar, &endpar, endpos, bparams->textclass);
+    cap.pasteSelection(&actpar, &endpar, endpos, buffer->params.textclass);
 
     RedoParagraphs(cursor, endpar);
     
@@ -2815,7 +2762,7 @@ void LyXText::InsertStringA(string const & str)
        SetCursorParUndo();
        
        bool flag =
-               textclasslist.Style(bparams->textclass, 
+               textclasslist.Style(buffer->params.textclass, 
                                    cursor.par->GetLayout()).isEnvironment();
        // only to be sure, should not be neccessary
        ClearSelection();
@@ -2846,7 +2793,6 @@ void LyXText::InsertStringA(string const & str)
                                        ++pos;
                                }
                         } else if (str[i] == ' ') {
-#if 1
                                InsetSpecialChar * new_inset =
                                        new InsetSpecialChar(InsetSpecialChar::PROTECTED_SEPARATOR);
                                if (par->InsertInsetAllowed(new_inset)) {
@@ -2856,14 +2802,9 @@ void LyXText::InsertStringA(string const & str)
                                } else {
                                        delete new_inset;
                                }
-#else
-                               par->InsertChar(pos, LyXParagraph::META_PROTECTED_SEPARATOR);
-                               par->SetFont(pos, current_font);
-#endif
                                ++pos;
                        } else if (str[i] == '\t') {
                                for (a = pos; a < (pos / 8 + 1) * 8 ; ++a) {
-#if 1
                                InsetSpecialChar * new_inset =
                                        new InsetSpecialChar(InsetSpecialChar::PROTECTED_SEPARATOR);
                                if (par->InsertInsetAllowed(new_inset)) {
@@ -2873,10 +2814,6 @@ void LyXText::InsertStringA(string const & str)
                                } else {
                                        delete new_inset;
                                }
-#else
-                                       par->InsertChar(a, LyXParagraph::META_PROTECTED_SEPARATOR);
-                                       par->SetFont(a, current_font);
-#endif
                                }
                                pos = a;
                        } else if (str[i] != 13 && 
@@ -2911,7 +2848,6 @@ void LyXText::InsertStringA(string const & str)
                                         break;
                         } else {
                                 if (!par->size()) { // par is empty
-#if 1
                                        InsetSpecialChar * new_inset =
                                                new InsetSpecialChar(InsetSpecialChar::PROTECTED_SEPARATOR);
                                        if (par->InsertInsetAllowed(new_inset)) {
@@ -2921,10 +2857,6 @@ void LyXText::InsertStringA(string const & str)
                                        } else {
                                                delete new_inset;
                                        }
-#else
-                                        par->InsertChar(pos, LyXParagraph::META_PROTECTED_SEPARATOR);
-                                       par->SetFont(pos, current_font);
-#endif
                                         ++pos;
                                 }
                                 par->BreakParagraph(pos, flag);
@@ -3029,8 +2961,8 @@ bool LyXText::GotoNextNote() const
 void LyXText::CheckParagraph(LyXParagraph * par,
                             LyXParagraph::size_type pos)
 {
-  
-       LyXCursor tmpcursor;
+       LyXCursor tmpcursor;                    
+
 
        /* table stuff -- begin*/
    
@@ -3076,7 +3008,7 @@ void LyXText::CheckParagraph(LyXParagraph * par,
                        status = LyXText::NEED_MORE_REFRESH; 
    
                // check the special right address boxes
-               if (textclasslist.Style(bparams->textclass,
+               if (textclasslist.Style(buffer->params.textclass,
                                        par->GetLayout()).margintype
                    == MARGIN_RIGHT_ADDRESS_BOX) {
                        tmpcursor.par = par;
@@ -3147,9 +3079,136 @@ void LyXText::SetCursor(LyXParagraph * par,
 }
 
 
+void LyXText::SetCursor(LyXCursor & cur, LyXParagraph * par,
+                       LyXParagraph::size_type pos) const
+{
+       // correct the cursor position if impossible
+       if (pos > par->Last()){
+               LyXParagraph * tmppar = par->ParFromPos(pos);
+               pos = par->PositionInParFromPos(pos);
+               par = tmppar;
+       }
+       if (par->IsDummy() && par->previous &&
+           par->previous->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE) {
+               while (par->previous &&
+                      ((par->previous->IsDummy() &&
+                        (par->previous->previous->footnoteflag ==
+                         LyXParagraph::CLOSED_FOOTNOTE)) ||
+                       (par->previous->footnoteflag ==
+                        LyXParagraph::CLOSED_FOOTNOTE))) {
+                       par = par->previous ;
+                       if (par->IsDummy() &&
+                           (par->previous->footnoteflag ==
+                            LyXParagraph::CLOSED_FOOTNOTE))
+                               pos += par->size() + 1;
+               }
+               if (par->previous) {
+                       par = par->previous;
+               }
+               pos += par->size() + 1;
+       }
+
+       cur.par = par;
+       cur.pos = pos;
+
+       /* get the cursor y position in text  */
+       long y = 0;
+       Row * row = GetRow(par, pos, y);
+       /* y is now the beginning of the cursor row */ 
+       y += row->baseline;
+       /* y is now the cursor baseline */ 
+       cur.y = y;
+   
+       /* now get the cursors x position */
+       float x;
+       float fill_separator, fill_hfill, fill_label_hfill;
+       PrepareToPrint(row, x, fill_separator, fill_hfill, fill_label_hfill);
+       LyXParagraph::size_type cursor_vpos;
+       LyXParagraph::size_type last = RowLastPrintable(row);
+
+       if (pos > last + 1)   // This shouldn't happen.
+               pos = last+1;
+
+       if (last < row->pos)
+                cursor_vpos = 0;
+       else if ((pos > last) ||
+                ((pos - 1 >= row->pos) &&
+                 (row->par->IsSeparator(pos) ||
+                  (row->par->table && row->par->IsNewline(pos)))))
+               /// Place cursor after char at (logical) position pos-1
+               cursor_vpos = !(bidi_level(pos-1) % 2)
+                       ? log2vis(pos-1) + 1 : log2vis(pos-1);
+       else
+               /// Place cursor before char at (logical) position pos
+               cursor_vpos = !(bidi_level(pos) % 2)
+                       ? log2vis(pos) : log2vis(pos) + 1;
+
+       /* 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(row->par, pos);
+                       }
+               }
+       } else {
+               /* table stuff -- end*/
+               LyXParagraph::size_type main_body =
+                       BeginningOfMainBody(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(
+                                               buffer->params.textclass,
+                                               row->par->GetLayout())
+                                                      .labelsep,
+                                                      GetFont(row->par, -2));
+                               if (row->par->IsLineSeparator(main_body-1))
+                                       x -= SingleWidth(row->par,main_body-1);
+                       }
+                       if (HfillExpansion(row, pos)) {
+                               x += SingleWidth(row->par, pos);
+                               if (pos >= main_body)
+                                       x += fill_hfill;
+                               else 
+                                       x += fill_label_hfill;
+                       } else if (row->par->IsSeparator(pos)) {
+                               x += SingleWidth(row->par, pos);
+                               if (pos >= main_body)
+                                       x += fill_separator;
+                       } else
+                               x += SingleWidth(row->par, pos);
+               }
+       }
+   
+       cur.x = int(x);
+       cur.x_fix = cur.x;
+       cur.row = row;
+}
+
+
 void LyXText::SetCursorIntern(LyXParagraph * par,
                              LyXParagraph::size_type pos, bool setfont) const
 {
+       SetCursor(cursor, par, pos);
+#warning Remove this when verified working (Jug 20000413)
+#if 0
        // correct the cursor position if impossible
        if (pos > par->Last()){
                LyXParagraph * tmppar = par->ParFromPos(pos);
@@ -3175,18 +3234,6 @@ void LyXText::SetCursorIntern(LyXParagraph * par,
        cursor.par = par;
        cursor.pos = pos;
 
-       if (setfont)
-               if (cursor.pos && 
-                   (cursor.pos == cursor.par->Last() || cursor.par->IsSeparator(cursor.pos)
-                    || (cursor.par->table && cursor.par->IsNewline(cursor.pos))
-                    )) {
-                       current_font = cursor.par->GetFontSettings(cursor.pos - 1);
-                       real_current_font = GetFont(cursor.par, cursor.pos - 1);
-               } else {
-                       current_font = cursor.par->GetFontSettings(cursor.pos);
-                       real_current_font = GetFont(cursor.par, cursor.pos);
-               }
-
        /* get the cursor y position in text  */
        long y = 0;
        Row * row = GetRow(par, pos, y);
@@ -3250,7 +3297,7 @@ void LyXText::SetCursorIntern(LyXParagraph * par,
                        if (main_body > 0 && pos == main_body-1) {
                                x += fill_label_hfill +
                                        lyxfont::width(textclasslist
-                                                      .Style(bparams->textclass,
+                                                      .Style(buffer->params.textclass,
                                                              row->par->GetLayout())
                                                       .labelsep,
                                                       GetFont(row->par, -2));
@@ -3277,6 +3324,19 @@ void LyXText::SetCursorIntern(LyXParagraph * par,
    
        cursor.x_fix = cursor.x;
        cursor.row = row;
+#endif
+       if (setfont) {
+               if (cursor.pos && 
+                   (cursor.pos == cursor.par->Last() || cursor.par->IsSeparator(cursor.pos)
+                    || (cursor.par->table && cursor.par->IsNewline(cursor.pos))
+                    )) {
+                       current_font = cursor.par->GetFontSettings(cursor.pos - 1);
+                       real_current_font = GetFont(cursor.par, cursor.pos - 1);
+               } else {
+                       current_font = cursor.par->GetFontSettings(cursor.pos);
+                       real_current_font = GetFont(cursor.par, cursor.pos);
+               }
+       }
 }
 
 
@@ -3445,13 +3505,12 @@ void LyXText::DeleteEmptyParagraphMechanism(LyXCursor const & old_cursor) const
        if (selection) return;
 
        // We allow all kinds of "mumbo-jumbo" when freespacing.
-       if (textclasslist.Style(bparams->textclass,
+       if (textclasslist.Style(buffer->params.textclass,
                                old_cursor.par->GetLayout()).free_spacing)
                return;
 
        bool deleted = false;
        
-#ifdef FIX_DOUBLE_SPACE
        /* Ok I'll put some comments here about what is missing.
           I have fixed BackSpace (and thus Delete) to not delete
           double-spaces automagically. I have also changed Cut,
@@ -3483,20 +3542,19 @@ void LyXText::DeleteEmptyParagraphMechanism(LyXCursor const & old_cursor) const
                    && old_cursor.par->IsLineSeparator(old_cursor.pos)
                    && old_cursor.par->IsLineSeparator(old_cursor.pos - 1)) {
                        old_cursor.par->Erase(old_cursor.pos - 1);
-                       status = LyXText::NEED_MORE_REFRESH;
                        RedoParagraphs(old_cursor, old_cursor.par->Next());
                        // correct cursor
                        if (old_cursor.par == cursor.par &&
                            cursor.pos > old_cursor.pos) {
                                SetCursorIntern(cursor.par, cursor.pos - 1);
-                       }
+                       } else
+                               SetCursorIntern(cursor.par, cursor.pos);
                        return;
                }
        }
-#endif
-#if 1
+
        // Do not delete empty paragraphs with keepempty set.
-       if ((textclasslist.Style(bparams->textclass,
+       if ((textclasslist.Style(buffer->params.textclass,
                                 old_cursor.par->GetLayout())).keepempty)
                return;
 
@@ -3597,14 +3655,9 @@ void LyXText::DeleteEmptyParagraphMechanism(LyXCursor const & old_cursor) const
                                }
                                
                                // correct cursor y
-#if 1
+
                                SetCursorIntern(cursor.par, cursor.pos);
-#else
-                               SetCursor(cursor.par, cursor.pos);
-#endif                 
-                               /* if (cursor.y > old_cursor.y)
-                                  cursor.y -= old_cursor.row->height; */ 
-        
+
                                if (sel_cursor.par  == old_cursor.par
                                    && sel_cursor.pos == sel_cursor.pos) {
                                        // correct selection
@@ -3616,16 +3669,11 @@ void LyXText::DeleteEmptyParagraphMechanism(LyXCursor const & old_cursor) const
                        if (old_cursor.par->ClearParagraph()) {
                                RedoParagraphs(old_cursor, old_cursor.par->Next());
                                // correct cursor y
-#if 1
                                SetCursorIntern(cursor.par, cursor.pos);
-#else
-                               SetCursor(cursor.par, cursor.pos);
-#endif
                                sel_cursor = cursor;
                        }
                }
        }
-#endif
 }
 
 
@@ -3715,17 +3763,8 @@ bool LyXText::TextHandleUndo(Undo * undo)
                                if (undo->kind == Undo::EDIT) {
                                        tmppar2->setContentsFromPar(tmppar);
                                        tmppar->clearContents();
-                                       //tmppar2->text = tmppar->text;
-                                       //tmppar->text.clear();
                                        tmppar2 = tmppar2->next;
                                }
-                               if ( currentrow && currentrow->par == tmppar )
-                                       currentrow = currentrow -> previous;
-                               // Commenting out this might remove the error
-                               // reported by Purify, but it might also
-                               // introduce a memory leak. We need to
-                               // check this (Lgb)
-                               //delete tmppar;
                        }
                }