]> git.lyx.org Git - lyx.git/blobdiff - src/Paragraph.cpp
Get rid of unused SessionInfoSection.
[lyx.git] / src / Paragraph.cpp
index 09c13cc43b10635bf1b42bb2eb81becbdb2f0fc0..7c9780e1ae2874bb02af9723f03e24c83562e549 100644 (file)
@@ -52,6 +52,7 @@
 #include "support/lassert.h"
 #include "support/convert.h"
 #include "support/debug.h"
+#include "support/ExceptionMessage.h"
 #include "support/gettext.h"
 #include "support/lstrings.h"
 #include "support/Messages.h"
@@ -83,6 +84,8 @@ public:
        Private(Paragraph * owner, Layout const & layout);
        /// "Copy constructor"
        Private(Private const &, Paragraph * owner);
+       /// Copy constructor from \p beg  to \p end
+       Private(Private const &, Paragraph * owner, pos_type beg, pos_type end);
 
        ///
        void insertChar(pos_type pos, char_type c, Change const & change);
@@ -110,11 +113,11 @@ public:
 
        /// This could go to ParagraphParameters if we want to.
        int startTeXParParams(BufferParams const &, odocstream &, TexRow &,
-                             bool) const;
+                             OutputParams const &) const;
 
        /// This could go to ParagraphParameters if we want to.
        int endTeXParParams(BufferParams const &, odocstream &, TexRow &,
-                           bool) const;
+                           OutputParams const &) const;
 
        ///
        void latexInset(BufferParams const &,
@@ -173,7 +176,7 @@ public:
        Paragraph * owner_;
 
        /// In which Inset?
-       Inset * inset_owner_;
+       Inset const * inset_owner_;
 
        ///
        FontList fontlist_;
@@ -247,6 +250,35 @@ Paragraph::Private::Private(Private const & p, Paragraph * owner)
 }
 
 
+Paragraph::Private::Private(Private const & p, Paragraph * owner,
+       pos_type beg, pos_type end)
+       : owner_(owner), inset_owner_(p.inset_owner_),
+         params_(p.params_), changes_(p.changes_),
+         insetlist_(p.insetlist_, beg, end),
+         begin_of_body_(p.begin_of_body_), words_(p.words_),
+         layout_(p.layout_)
+{
+       id_ = paragraph_id++;
+       if (beg >= pos_type(p.text_.size()))
+               return;
+       text_ = p.text_.substr(beg, end - beg);
+
+       FontList::const_iterator fcit = fontlist_.begin();
+       FontList::const_iterator fend = fontlist_.end();
+       for (; fcit != fend; ++fcit) {
+               if (fcit->pos() < beg)
+                       continue;
+               if (fcit->pos() >= end) {
+                       // Add last entry in the fontlist_.
+                       fontlist_.set(text_.size() - 1, fcit->font());
+                       break;
+               }
+               // Add a new entry in the fontlist_.
+               fontlist_.set(fcit->pos() - beg, fcit->font());
+       }
+}
+
+
 bool Paragraph::isChanged(pos_type start, pos_type end) const
 {
        LASSERT(start >= 0 && start <= size(), /**/);
@@ -426,7 +458,7 @@ void Paragraph::insertInset(pos_type pos, Inset * inset,
 
 bool Paragraph::eraseChar(pos_type pos, bool trackChanges)
 {
-       LASSERT(pos >= 0 && pos <= size(), /**/);
+       LASSERT(pos >= 0 && pos <= size(), return false);
 
        // keep the logic here in sync with the logic of isMergedOnEndOfParDeletion()
 
@@ -941,6 +973,11 @@ bool Paragraph::Private::latexSpecialT1(char_type const c, odocstream & os,
        case '|':
                os.put(c);
                return true;
+       case '\"':
+               // soul.sty breaks with \char`\"
+               os << "\\textquotedbl{}";
+               column += 14;
+               return true;
        default:
                return false;
        }
@@ -1060,6 +1097,14 @@ Paragraph::Paragraph(Paragraph const & par)
 }
 
 
+Paragraph::Paragraph(Paragraph const & par, pos_type beg, pos_type end)
+       : itemdepth(par.itemdepth),
+       d(new Paragraph::Private(*par.d, this, beg, end))
+{
+       registerWords();
+}
+
+
 Paragraph & Paragraph::operator=(Paragraph const & par)
 {
        // needed as we will destroy the private part before copying it
@@ -1260,12 +1305,6 @@ void Paragraph::insertInset(pos_type pos, Inset * inset,
 }
 
 
-bool Paragraph::insetAllowed(InsetCode code)
-{
-       return !d->inset_owner_ || d->inset_owner_->insetAllowed(code);
-}
-
-
 void Paragraph::resetFonts(Font const & font)
 {
        d->fontlist_.clear();
@@ -1618,28 +1657,19 @@ void Paragraph::setBeginOfBody()
 
 bool Paragraph::forcePlainLayout() const
 {
-       Inset const * const inset = inInset();
-       if (!inset)
-               return true;
-       return inset->forcePlainLayout();
+       return inInset().forcePlainLayout();
 }
 
 
 bool Paragraph::allowParagraphCustomization() const
 {
-       Inset const * const inset = inInset();
-       if (!inset)
-               return true;
-       return inset->allowParagraphCustomization();
+       return inInset().allowParagraphCustomization();
 }
 
 
 bool Paragraph::usePlainLayout() const
 {
-       Inset const * const inset = inInset();
-       if (!inset)
-               return false;
-       return inset->usePlainLayout();
+       return inInset().usePlainLayout();
 }
 
 
@@ -1650,7 +1680,9 @@ namespace {
 
 bool noTrivlistCentering(InsetCode code)
 {
-       return code == FLOAT_CODE || code == WRAP_CODE;
+       return code == FLOAT_CODE
+              || code == WRAP_CODE
+              || code == CELL_CODE;
 }
 
 
@@ -1667,12 +1699,19 @@ string correction(string const & orig)
 
 
 string const corrected_env(string const & suffix, string const & env,
-       InsetCode code)
+       InsetCode code, bool const lastpar)
 {
        string output = suffix + "{";
-       if (noTrivlistCentering(code))
+       if (noTrivlistCentering(code)) {
+               if (lastpar) {
+                       // the last paragraph in non-trivlist-aligned
+                       // context is special (to avoid unwanted whitespace)
+                       if (suffix == "\\begin")
+                               return "\\" + correction(env) + "{}";
+                       return string();
+               }
                output += correction(env);
-       else
+       else
                output += env;
        output += "}";
        if (suffix == "\\begin")
@@ -1697,7 +1736,7 @@ void adjust_row_column(string const & str, TexRow & texrow, int & column)
 
 int Paragraph::Private::startTeXParParams(BufferParams const & bparams,
                                 odocstream & os, TexRow & texrow,
-                                bool moving_arg) const
+                                OutputParams const & runparams) const
 {
        int column = 0;
 
@@ -1720,13 +1759,17 @@ int Paragraph::Private::startTeXParParams(BufferParams const & bparams,
        case LYX_ALIGN_LEFT:
        case LYX_ALIGN_RIGHT:
        case LYX_ALIGN_CENTER:
-               if (moving_arg) {
+               if (runparams.moving_arg) {
                        os << "\\protect";
                        column += 8;
                }
                break;
        }
 
+       string const begin_tag = "\\begin";
+       InsetCode code = owner_->ownerCode();
+       bool const lastpar = runparams.isLastPar;
+
        switch (curAlign) {
        case LYX_ALIGN_NONE:
        case LYX_ALIGN_BLOCK:
@@ -1736,24 +1779,24 @@ int Paragraph::Private::startTeXParParams(BufferParams const & bparams,
        case LYX_ALIGN_LEFT: {
                string output;
                if (owner_->getParLanguage(bparams)->babel() != "hebrew")
-                       output = corrected_env("\\begin", "flushleft", owner_->ownerCode());
+                       output = corrected_env(begin_tag, "flushleft", code, lastpar);
                else
-                       output = corrected_env("\\begin", "flushright", owner_->ownerCode());
+                       output = corrected_env(begin_tag, "flushright", code, lastpar);
                os << from_ascii(output);
                adjust_row_column(output, texrow, column);
                break;
        } case LYX_ALIGN_RIGHT: {
                string output;
                if (owner_->getParLanguage(bparams)->babel() != "hebrew")
-                       output = corrected_env("\\begin", "flushright", owner_->ownerCode());
+                       output = corrected_env(begin_tag, "flushright", code, lastpar);
                else
-                       output = corrected_env("\\begin", "flushleft", owner_->ownerCode());
+                       output = corrected_env(begin_tag, "flushleft", code, lastpar);
                os << from_ascii(output);
                adjust_row_column(output, texrow, column);
                break;
        } case LYX_ALIGN_CENTER: {
                string output;
-               output = corrected_env("\\begin", "center", owner_->ownerCode());
+               output = corrected_env(begin_tag, "center", code, lastpar);
                os << from_ascii(output);
                adjust_row_column(output, texrow, column);
                break;
@@ -1766,7 +1809,7 @@ int Paragraph::Private::startTeXParParams(BufferParams const & bparams,
 
 int Paragraph::Private::endTeXParParams(BufferParams const & bparams,
                               odocstream & os, TexRow & texrow,
-                              bool moving_arg) const
+                              OutputParams const & runparams) const
 {
        int column = 0;
 
@@ -1779,13 +1822,17 @@ int Paragraph::Private::endTeXParParams(BufferParams const & bparams,
        case LYX_ALIGN_LEFT:
        case LYX_ALIGN_RIGHT:
        case LYX_ALIGN_CENTER:
-               if (moving_arg) {
+               if (runparams.moving_arg) {
                        os << "\\protect";
                        column = 8;
                }
                break;
        }
 
+       string const end_tag = "\n\\par\\end";
+       InsetCode code = owner_->ownerCode();
+       bool const lastpar = runparams.isLastPar;
+
        switch (params_.align()) {
        case LYX_ALIGN_NONE:
        case LYX_ALIGN_BLOCK:
@@ -1795,24 +1842,24 @@ int Paragraph::Private::endTeXParParams(BufferParams const & bparams,
        case LYX_ALIGN_LEFT: {
                string output;
                if (owner_->getParLanguage(bparams)->babel() != "hebrew")
-                       output = corrected_env("\n\\par\\end", "flushleft", owner_->ownerCode());
+                       output = corrected_env(end_tag, "flushleft", code, lastpar);
                else
-                       output = corrected_env("\n\\par\\end", "flushright", owner_->ownerCode());
+                       output = corrected_env(end_tag, "flushright", code, lastpar);
                os << from_ascii(output);
                adjust_row_column(output, texrow, column);
                break;
        } case LYX_ALIGN_RIGHT: {
                string output;
                if (owner_->getParLanguage(bparams)->babel() != "hebrew")
-                       output = corrected_env("\n\\par\\end", "flushright", owner_->ownerCode());
+                       output = corrected_env(end_tag, "flushright", code, lastpar);
                else
-                       output = corrected_env("\n\\par\\end", "flushleft", owner_->ownerCode());
+                       output = corrected_env(end_tag, "flushleft", code, lastpar);
                os << from_ascii(output);
                adjust_row_column(output, texrow, column);
                break;
        } case LYX_ALIGN_CENTER: {
                string output;
-               output = corrected_env("\n\\par\\end", "center", owner_->ownerCode());
+               output = corrected_env(end_tag, "center", code, lastpar);
                os << from_ascii(output);
                adjust_row_column(output, texrow, column);
                break;
@@ -1836,7 +1883,7 @@ bool Paragraph::latex(BufferParams const & bparams,
        bool asdefault = forcePlainLayout();
 
        Layout const & style = asdefault ?
-               bparams.documentClass().emptyLayout() :
+               bparams.documentClass().plainLayout() :
                *d->layout_;
 
        // Current base font for all inherited font changes, without any
@@ -1878,7 +1925,7 @@ bool Paragraph::latex(BufferParams const & bparams,
                }
                if (!asdefault)
                        column += d->startTeXParParams(bparams, os, texrow,
-                                                   runparams.moving_arg);
+                                                   runparams);
        }
 
        for (pos_type i = 0; i < size(); ++i) {
@@ -1909,7 +1956,7 @@ bool Paragraph::latex(BufferParams const & bparams,
                        if (!asdefault)
                                column += d->startTeXParParams(bparams, os,
                                                            texrow,
-                                                           runparams.moving_arg);
+                                                           runparams);
                }
 
                Change const & change = runparams.inDeletedInset ? runparams.changeOfDeletedInset
@@ -2086,7 +2133,7 @@ bool Paragraph::latex(BufferParams const & bparams,
 
        if (!asdefault) {
                column += d->endTeXParParams(bparams, os, texrow,
-                                         runparams.moving_arg);
+                                         runparams);
        }
 
        LYXERR(Debug::LATEX, "SimpleTeXOnePar...done " << this);
@@ -2352,7 +2399,7 @@ docstring Paragraph::asString(pos_type beg, pos_type end, int options) const
 }
 
 
-void Paragraph::setInsetOwner(Inset * inset)
+void Paragraph::setInsetOwner(Inset const * inset)
 {
        d->inset_owner_ = inset;
 }
@@ -2379,15 +2426,17 @@ void Paragraph::setLayout(Layout const & layout)
 void Paragraph::setPlainOrDefaultLayout(DocumentClass const & tclass)
 {
        if (usePlainLayout())
-               setLayout(tclass.emptyLayout());
+               setLayout(tclass.plainLayout());
        else
                setLayout(tclass.defaultLayout());
 }
 
 
-Inset * Paragraph::inInset() const
+Inset const & Paragraph::inInset() const
 {
-       return d->inset_owner_;
+       LASSERT(d->inset_owner_, throw ExceptionMessage(BufferException,
+               _("Memory problem"), _("Paragraph not properly initiliazed")));
+       return *d->inset_owner_;
 }