]> git.lyx.org Git - lyx.git/blobdiff - src/paragraph.C
\layout -> \begin_layout and missing \end_layout
[lyx.git] / src / paragraph.C
index 4034a5624cbdeb836cdc1c2cfcda8a749dd963b2..5579d6deeea234e75227382628a16d643d69cfa7 100644 (file)
@@ -46,6 +46,8 @@
 #include <csignal>
 #include <ctime>
 
+using namespace lyx::support;
+
 using std::ostream;
 using std::endl;
 using std::fstream;
@@ -62,7 +64,7 @@ namespace {
 
 char minibuffer_char;
 LyXFont minibuffer_font;
-Inset * minibuffer_inset;
+InsetOld * minibuffer_inset;
 
 } // namespace anon
 
@@ -70,10 +72,6 @@ Inset * minibuffer_inset;
 Paragraph::Paragraph()
        : pimpl_(new Paragraph::Pimpl(this))
 {
-#ifdef NO_STD_LIST
-       next_par_ = 0;
-       prev_par_ = 0;
-#endif
        enumdepth = 0;
        itemdepth = 0;
        params().clear();
@@ -85,27 +83,49 @@ Paragraph::Paragraph(Paragraph const & lp)
 {
        enumdepth = 0;
        itemdepth = 0;
-#ifdef NO_STD_LIST
-       next_par_ = 0;
-       prev_par_ = 0;
-#endif
        // this is because of the dummy layout of the paragraphs that
        // follow footnotes
        layout_ = lp.layout();
-       buffer_ = lp.buffer_;
 
        // copy everything behind the break-position to the new paragraph
        insetlist = lp.insetlist;
        InsetList::iterator it = insetlist.begin();
        InsetList::iterator end = insetlist.end();
        for (; it != end; ++it) {
-               it.setInset(it.getInset()->clone(**buffer_, false));
+               // currently we hold Inset*, not InsetBase*
+               it->inset = static_cast<InsetOld*>(it->inset->clone().release());
                // tell the new inset who is the boss now
-               it.getInset()->parOwner(this);
+               it->inset->parOwner(this);
        }
 }
 
 
+void Paragraph::operator=(Paragraph const & lp)
+{
+       // needed as we will destroy the pimpl_ before copying it
+       if (&lp != this)
+               return;
+       lyxerr << "Paragraph::operator=()\n";
+       delete pimpl_;
+       pimpl_ = new Pimpl(*lp.pimpl_, this);
+
+       enumdepth = lp.enumdepth;
+       itemdepth = lp.itemdepth;
+       // this is because of the dummy layout of the paragraphs that
+       // follow footnotes
+       layout_ = lp.layout();
+
+       // copy everything behind the break-position to the new paragraph
+       insetlist = lp.insetlist;
+       InsetList::iterator it = insetlist.begin();
+       InsetList::iterator end = insetlist.end();
+       for (; it != end; ++it) {
+               it->inset = static_cast<InsetOld*>(it->inset->clone().release());
+               // tell the new inset who is the boss now
+               it->inset->parOwner(this);
+       }
+}
+
 // the destructor removes the new paragraph from the list
 Paragraph::~Paragraph()
 {
@@ -136,7 +156,7 @@ void Paragraph::write(Buffer const * buf, ostream & os,
        }
 
        // First write the layout
-       os << "\n\\layout " << layout()->name() << '\n';
+       os << "\n\\begin_layout " << layout()->name() << '\n';
 
        params().write(os);
 
@@ -168,7 +188,7 @@ void Paragraph::write(Buffer const * buf, ostream & os,
                switch (c) {
                case META_INSET:
                {
-                       Inset const * inset = getInset(i);
+                       InsetOld const * inset = getInset(i);
                        if (inset)
                                if (inset->directWrite()) {
                                        // international char, let it write
@@ -220,6 +240,8 @@ void Paragraph::write(Buffer const * buf, ostream & os,
        }
        Changes::lyxMarkChange(os, column, curtime,
                running_change, Change(Change::UNCHANGED));
+
+       os << "\n\\end_layout\n";
 }
 
 
@@ -239,7 +261,7 @@ void Paragraph::copyIntoMinibuffer(Buffer const & buffer, pos_type pos) const
        minibuffer_inset = 0;
        if (minibuffer_char == Paragraph::META_INSET) {
                if (getInset(pos)) {
-                       minibuffer_inset = getInset(pos)->clone(buffer);
+                       minibuffer_inset = static_cast<InsetOld *>(getInset(pos)->clone().release());
                } else {
                        minibuffer_inset = 0;
                        minibuffer_char = ' ';
@@ -264,25 +286,20 @@ void Paragraph::cutIntoMinibuffer(BufferParams const & bparams, pos_type pos)
                        minibuffer_char = ' ';
                        // This reflects what GetInset() does (ARRae)
                }
-
        }
-
-       // Erase(pos); now the caller is responsible for that.
 }
 
 
 bool Paragraph::insertFromMinibuffer(pos_type pos)
 {
        if (minibuffer_char == Paragraph::META_INSET) {
-               if (!insetAllowed(minibuffer_inset->lyxCode())) {
+               if (!insetAllowed(minibuffer_inset->lyxCode()))
                        return false;
-               }
                insertInset(pos, minibuffer_inset, minibuffer_font);
        } else {
                LyXFont f = minibuffer_font;
-               if (!checkInsertChar(f)) {
+               if (!checkInsertChar(f))
                        return false;
-               }
                insertChar(pos, minibuffer_char, f);
        }
        return true;
@@ -319,8 +336,7 @@ bool Paragraph::checkInsertChar(LyXFont & font)
 
 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c)
 {
-       LyXFont const f(LyXFont::ALL_INHERIT);
-       insertChar(pos, c, f);
+       insertChar(pos, c, LyXFont(LyXFont::ALL_INHERIT));
 }
 
 
@@ -331,20 +347,19 @@ void Paragraph::insertChar(pos_type pos, Paragraph::value_type c,
 }
 
 
-void Paragraph::insertInset(pos_type pos, Inset * inset)
+void Paragraph::insertInset(pos_type pos, InsetOld * inset)
 {
-       LyXFont const f(LyXFont::ALL_INHERIT);
-       insertInset(pos, inset, f);
+       insertInset(pos, inset, LyXFont(LyXFont::ALL_INHERIT));
 }
 
 
-void Paragraph::insertInset(pos_type pos, Inset * inset, LyXFont const & font, Change change)
+void Paragraph::insertInset(pos_type pos, InsetOld * inset, LyXFont const & font, Change change)
 {
        pimpl_->insertInset(pos, inset, font, change);
 }
 
 
-bool Paragraph::insetAllowed(Inset::Code code)
+bool Paragraph::insetAllowed(InsetOld::Code code)
 {
        //lyxerr << "Paragraph::InsertInsetAllowed" << endl;
        if (pimpl_->inset_owner)
@@ -353,16 +368,16 @@ bool Paragraph::insetAllowed(Inset::Code code)
 }
 
 
-Inset * Paragraph::getInset(pos_type pos)
+InsetOld * Paragraph::getInset(pos_type pos)
 {
-       lyx::Assert(pos < size());
+       Assert(pos < size());
        return insetlist.get(pos);
 }
 
 
-Inset const * Paragraph::getInset(pos_type pos) const
+InsetOld const * Paragraph::getInset(pos_type pos) const
 {
-       lyx::Assert(pos < size());
+       Assert(pos < size());
        return insetlist.get(pos);
 }
 
@@ -371,24 +386,38 @@ Inset const * Paragraph::getInset(pos_type pos) const
 LyXFont const Paragraph::getFontSettings(BufferParams const & bparams,
                                         pos_type pos) const
 {
-       lyx::Assert(pos <= size());
+       Assert(pos <= size());
 
        Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
        Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
-       for (; cit != end; ++cit) {
+       for (; cit != end; ++cit)
                if (cit->pos() >= pos)
                        break;
-       }
 
-       LyXFont retfont;
        if (cit != end)
-               retfont = cit->font();
-       else if (pos == size() && !empty())
-               retfont = getFontSettings(bparams, pos - 1);
-       else
-               retfont = LyXFont(LyXFont::ALL_INHERIT, getParLanguage(bparams));
+               return cit->font();
+
+       if (pos == size() && !empty())
+               return getFontSettings(bparams, pos - 1);
 
-       return retfont;
+       return LyXFont(LyXFont::ALL_INHERIT, getParLanguage(bparams));
+}
+
+
+lyx::pos_type
+Paragraph::getEndPosOfFontSpan(lyx::pos_type pos) const
+{
+       Assert(pos <= size());
+
+       Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
+       Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
+       for (; cit != end; ++cit)
+               if (cit->pos() >= pos)
+                       return cit->pos();
+
+       // This should not happen, but if so, we take no chances.
+       lyxerr << "Pararaph::getEndPosOfFontSpan: This should not happen!\n";
+       return pos;
 }
 
 
@@ -410,7 +439,7 @@ LyXFont const Paragraph::getFirstFontSettings() const
 LyXFont const Paragraph::getFont(BufferParams const & bparams, pos_type pos,
                                 LyXFont const & outerfont) const
 {
-       lyx::Assert(pos >= 0);
+       Assert(pos >= 0);
 
        LyXLayout_ptr const & lout = layout();
 
@@ -425,34 +454,31 @@ LyXFont const Paragraph::getFont(BufferParams const & bparams, pos_type pos,
        LyXFont tmpfont = getFontSettings(bparams, pos);
        tmpfont.realize(layoutfont);
        tmpfont.realize(outerfont);
+       tmpfont.realize(bparams.getLyXTextClass().defaultfont());
 
-       return realizeFont(tmpfont, bparams);
+       return tmpfont;
 }
 
 
 LyXFont const Paragraph::getLabelFont(BufferParams const & bparams,
                                      LyXFont const & outerfont) const
 {
-       LyXLayout_ptr const & lout = layout();
-
-       LyXFont tmpfont = lout->labelfont;
+       LyXFont tmpfont = layout()->labelfont;
        tmpfont.setLanguage(getParLanguage(bparams));
        tmpfont.realize(outerfont);
-
-       return realizeFont(tmpfont, bparams);
+       tmpfont.realize(bparams.getLyXTextClass().defaultfont());
+       return tmpfont;
 }
 
 
 LyXFont const Paragraph::getLayoutFont(BufferParams const & bparams,
                                       LyXFont const & outerfont) const
 {
-       LyXLayout_ptr const & lout = layout();
-
-       LyXFont tmpfont = lout->font;
+       LyXFont tmpfont = layout()->font;
        tmpfont.setLanguage(getParLanguage(bparams));
        tmpfont.realize(outerfont);
-
-       return realizeFont(tmpfont, bparams);
+       tmpfont.realize(bparams.getLyXTextClass().defaultfont());
+       return tmpfont;
 }
 
 
@@ -535,7 +561,7 @@ Paragraph::getUChar(BufferParams const & bparams, pos_type pos) const
 
 void Paragraph::setFont(pos_type pos, LyXFont const & font)
 {
-       lyx::Assert(pos <= size());
+       Assert(pos <= size());
 
        // First, reduce font against layout/label font
        // Update: The SetCharFont() routine in text2.C already
@@ -606,7 +632,7 @@ void Paragraph::makeSameLayout(Paragraph const & par)
 
 int Paragraph::stripLeadingSpaces()
 {
-       if (layout()->free_spacing || isFreeSpacing())
+       if (isFreeSpacing())
                return 0;
 
        int i = 0;
@@ -715,23 +741,23 @@ int Paragraph::beginningOfBody() const
 
 
 // returns -1 if inset not found
-int Paragraph::getPositionOfInset(Inset const * inset) const
+int Paragraph::getPositionOfInset(InsetOld const * inset) const
 {
        // Find the entry.
-       InsetList::iterator it = insetlist.begin();
-       InsetList::iterator end = insetlist.end();
+       InsetList::const_iterator it = insetlist.begin();
+       InsetList::const_iterator end = insetlist.end();
        for (; it != end; ++it)
-               if (it.getInset() == inset)
-                       return it.getPos();
+               if (it->inset == inset)
+                       return it->pos;
        return -1;
 }
 
 
-InsetBibitem * Paragraph::bibitem()
+InsetBibitem * Paragraph::bibitem() const
 {
-       InsetList::iterator it = insetlist.begin();
-       if (it != insetlist.end() && it.getInset()->lyxCode() == Inset::BIBTEX_CODE)
-               return static_cast<InsetBibitem *>(it.getInset());
+       InsetList::const_iterator it = insetlist.begin();
+       if (it != insetlist.end() && it->inset->lyxCode() == InsetOld::BIBTEX_CODE)
+               return static_cast<InsetBibitem *>(it->inset);
        return 0;
 }
 
@@ -1060,7 +1086,7 @@ bool Paragraph::simpleTeXOnePar(Buffer const * buf,
 bool Paragraph::isHfill(pos_type pos) const
 {
        return IsInsetChar(getChar(pos))
-              && getInset(pos)->lyxCode() == Inset::HFILL_CODE;
+              && getInset(pos)->lyxCode() == InsetOld::HFILL_CODE;
 }
 
 
@@ -1073,7 +1099,7 @@ bool Paragraph::isInset(pos_type pos) const
 bool Paragraph::isNewline(pos_type pos) const
 {
        return IsInsetChar(getChar(pos))
-              && getInset(pos)->lyxCode() == Inset::NEWLINE_CODE;
+              && getInset(pos)->lyxCode() == InsetOld::NEWLINE_CODE;
 }
 
 
@@ -1121,15 +1147,10 @@ bool Paragraph::isWord(pos_type pos) const
 Language const *
 Paragraph::getParLanguage(BufferParams const & bparams) const
 {
-       if (!empty()) {
+       if (!empty())
                return getFirstFontSettings().language();
 #warning FIXME we should check the prev par as well (Lgb)
-#if 0
-       } else if (previous_) {
-               return previous_->getParLanguage(bparams);
-#endif
-       }else
-               return bparams.language;
+       return bparams.language;
 }
 
 
@@ -1138,7 +1159,7 @@ bool Paragraph::isRightToLeftPar(BufferParams const & bparams) const
        return lyxrc.rtl_support
                && getParLanguage(bparams)->RightToLeft()
                && !(inInset() && inInset()->owner() &&
-                    inInset()->owner()->lyxCode() == Inset::ERT_CODE);
+                    inInset()->owner()->lyxCode() == InsetOld::ERT_CODE);
 }
 
 
@@ -1174,6 +1195,7 @@ bool Paragraph::isMultiLingual(BufferParams const & bparams)
 // Used for building the table of contents
 string const Paragraph::asString(Buffer const * buffer, bool label) const
 {
+#if 0
        string s;
        if (label && !params().labelString().empty())
                s += params().labelString() + ' ';
@@ -1183,7 +1205,7 @@ string const Paragraph::asString(Buffer const * buffer, bool label) const
                if (IsPrintable(c))
                        s += c;
                else if (c == META_INSET &&
-                        getInset(i)->lyxCode() == Inset::MATH_CODE) {
+                        getInset(i)->lyxCode() == InsetOld::MATH_CODE) {
                        ostringstream ost;
                        getInset(i)->ascii(buffer, ost);
                        s += subst(STRCONV(ost.str()),'\n',' ');
@@ -1191,6 +1213,11 @@ string const Paragraph::asString(Buffer const * buffer, bool label) const
        }
 
        return s;
+#else
+       // This should really be done by the caller and not here.
+       string ret(asString(buffer, 0, size(), label));
+       return subst(ret, '\n', ' ');
+#endif
 }
 
 
@@ -1214,27 +1241,25 @@ string const Paragraph::asString(Buffer const * buffer,
 }
 
 
-void Paragraph::setInsetOwner(Inset * i)
+void Paragraph::setInsetOwner(UpdatableInset * inset)
 {
-       pimpl_->inset_owner = i;
+       pimpl_->inset_owner = inset;
        InsetList::iterator it = insetlist.begin();
        InsetList::iterator end = insetlist.end();
        for (; it != end; ++it)
-               if (it.getInset())
-                       it.getInset()->setOwner(i);
+               if (it->inset)
+                       it->inset->setOwner(inset);
 }
 
 
 void Paragraph::deleteInsetsLyXText(BufferView * bv)
 {
-       // then the insets
        insetlist.deleteInsetsLyXText(bv);
 }
 
 
 void Paragraph::resizeInsetsLyXText(BufferView * bv)
 {
-       // then the insets
        insetlist.resizeInsetsLyXText(bv);
 }
 
@@ -1265,14 +1290,14 @@ void Paragraph::cleanChanges()
 
 Change::Type Paragraph::lookupChange(lyx::pos_type pos) const
 {
-       lyx::Assert(!size() || pos < size());
+       Assert(!size() || pos < size());
        return pimpl_->lookupChange(pos);
 }
 
 
 Change const Paragraph::lookupChangeFull(lyx::pos_type pos) const
 {
-       lyx::Assert(!size() || pos < size());
+       Assert(!size() || pos < size());
        return pimpl_->lookupChangeFull(pos);
 }
 
@@ -1346,9 +1371,11 @@ void Paragraph::id(int i)
 
 LyXLayout_ptr const & Paragraph::layout() const
 {
-       Inset * inset = inInset();
-       if (inset && inset->lyxCode() == Inset::ENVIRONMENT_CODE)
+/*
+       InsetOld * inset = inInset();
+       if (inset && inset->lyxCode() == InsetOld::ENVIRONMENT_CODE)
                return static_cast<InsetEnvironment*>(inset)->layout();
+*/
        return layout_;
 }
 
@@ -1359,7 +1386,7 @@ void Paragraph::layout(LyXLayout_ptr const & new_layout)
 }
 
 
-Inset * Paragraph::inInset() const
+UpdatableInset * Paragraph::inInset() const
 {
        return pimpl_->inset_owner;
 }
@@ -1390,9 +1417,29 @@ ParagraphParameters const & Paragraph::params() const
 
 bool Paragraph::isFreeSpacing() const
 {
+       if (layout()->free_spacing)
+               return true;
+
        // for now we just need this, later should we need this in some
-       // other way we can always add a function to Inset::() too.
+       // other way we can always add a function to InsetOld::() too.
+       if (pimpl_->inset_owner && pimpl_->inset_owner->owner())
+               return (pimpl_->inset_owner->owner()->lyxCode() == InsetOld::ERT_CODE);
+       return false;
+}
+
+
+bool Paragraph::allowEmpty() const
+{
+       if (layout()->keepempty)
+               return true;
        if (pimpl_->inset_owner && pimpl_->inset_owner->owner())
-               return (pimpl_->inset_owner->owner()->lyxCode() == Inset::ERT_CODE);
+               return (pimpl_->inset_owner->owner()->lyxCode() == InsetOld::ERT_CODE);
        return false;
 }
+
+
+bool operator==(Paragraph const & lhs, Paragraph const & rhs)
+{
+#warning FIXME this implementatoin must be completely wrong...
+       return &lhs == &rhs;
+}