]> git.lyx.org Git - lyx.git/blobdiff - src/Paragraph.cpp
some more small optimization.
[lyx.git] / src / Paragraph.cpp
index 5ab65b49a4603e75ea020d7cbf9d352f127416f8..e46faab4b922848e6487ea7077d4ab64f138aa4b 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"
@@ -108,15 +109,15 @@ public:
        /// specified by the latex macro \p ltx, to \p os starting from \p i.
        /// \return the number of characters written.
        int writeScriptChars(odocstream & os, docstring const & ltx,
-                          Change &, Encoding const &, pos_type & i);
+                          Change const &, Encoding const &, pos_type & i);
 
        /// 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 &,
@@ -134,9 +135,9 @@ public:
        ///
        void latexSpecialChar(
                                   odocstream & os,
-                                  OutputParams & runparams,
-                                  Font & running_font,
-                                  Change & running_change,
+                                  OutputParams const & runparams,
+                                  Font const & running_font,
+                                  Change const & running_change,
                                   Layout const & style,
                                   pos_type & i,
                                   unsigned int & column);
@@ -145,20 +146,20 @@ public:
        bool latexSpecialT1(
                char_type const c,
                odocstream & os,
-               pos_type i,
+               pos_type i,
                unsigned int & column);
        ///
        bool latexSpecialTypewriter(
                char_type const c,
                odocstream & os,
-               pos_type i,
+               pos_type i,
                unsigned int & column);
        ///
        bool latexSpecialPhrase(
                odocstream & os,
                pos_type & i,
                unsigned int & column,
-               OutputParams & runparams);
+               OutputParams const & runparams);
 
        ///
        void validate(LaTeXFeatures & features,
@@ -278,6 +279,13 @@ Paragraph::Private::Private(Private const & p, Paragraph * owner,
 }
 
 
+void Paragraph::addChangesToToc(DocIterator const & cdit,
+       Buffer const & buf) const
+{
+       d->changes_.addToToc(cdit, buf);
+}
+
+
 bool Paragraph::isChanged(pos_type start, pos_type end) const
 {
        LASSERT(start >= 0 && start <= size(), /**/);
@@ -441,23 +449,29 @@ void Paragraph::Private::insertChar(pos_type pos, char_type c,
 }
 
 
-void Paragraph::insertInset(pos_type pos, Inset * inset,
+bool Paragraph::insertInset(pos_type pos, Inset * inset,
                                   Change const & change)
 {
        LASSERT(inset, /**/);
        LASSERT(pos >= 0 && pos <= size(), /**/);
 
+       // Paragraph::insertInset() can be used in cut/copy/paste operation where
+       // d->inset_owner_ is not set yet.
+       if (d->inset_owner_ && !d->inset_owner_->insetAllowed(inset->lyxCode()))
+               return false;
+
        d->insertChar(pos, META_INSET, change);
        LASSERT(d->text_[pos] == META_INSET, /**/);
 
        // Add a new entry in the insetlist_.
        d->insetlist_.insert(inset, pos);
+       return true;
 }
 
 
 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()
 
@@ -589,7 +603,7 @@ bool Paragraph::Private::simpleTeXBlanks(OutputParams const & runparams,
 
 int Paragraph::Private::writeScriptChars(odocstream & os,
                                         docstring const & ltx,
-                                        Change & runningChange,
+                                        Change const & runningChange,
                                         Encoding const & encoding,
                                         pos_type & i)
 {
@@ -826,9 +840,9 @@ void Paragraph::Private::latexInset(
 
 void Paragraph::Private::latexSpecialChar(
                                             odocstream & os,
-                                            OutputParams & runparams,
-                                            Font & running_font,
-                                            Change & running_change,
+                                            OutputParams const & runparams,
+                                            Font const & running_font,
+                                            Change const & running_change,
                                             Layout const & style,
                                             pos_type & i,
                                             unsigned int & column)
@@ -956,7 +970,7 @@ void Paragraph::Private::latexSpecialChar(
 
 
 bool Paragraph::Private::latexSpecialT1(char_type const c, odocstream & os,
-       pos_type i, unsigned int & column)
+       pos_type i, unsigned int & column)
 {
        switch (c) {
        case '>':
@@ -984,7 +998,7 @@ bool Paragraph::Private::latexSpecialT1(char_type const c, odocstream & os,
 
 
 bool Paragraph::Private::latexSpecialTypewriter(char_type const c, odocstream & os,
-       pos_type i, unsigned int & column)
+       pos_type i, unsigned int & column)
 {
        switch (c) {
        case '-':
@@ -1006,7 +1020,7 @@ bool Paragraph::Private::latexSpecialTypewriter(char_type const c, odocstream &
 
 
 bool Paragraph::Private::latexSpecialPhrase(odocstream & os, pos_type & i,
-       unsigned int & column, OutputParams & runparams)
+       unsigned int & column, OutputParams const & runparams)
 {
        // FIXME: if we have "LaTeX" with a font
        // change in the middle (before the 'T', then
@@ -1164,7 +1178,7 @@ void Paragraph::write(ostream & os, BufferParams const & bparams,
                        break;
 
                // Write font changes
-               Font font2 = getFontSettings(bparams, i);
+               Font const & font2 = getFontSettings(bparams, i);
                if (font2 != font1) {
                        font2.lyxWriteChanges(font1, os);
                        column = 0;
@@ -1295,12 +1309,13 @@ void Paragraph::insertChar(pos_type pos, char_type c,
 }
 
 
-void Paragraph::insertInset(pos_type pos, Inset * inset,
+bool Paragraph::insertInset(pos_type pos, Inset * inset,
                            Font const & font, Change const & change)
 {
-       insertInset(pos, inset, change);
+       bool const success = insertInset(pos, inset, change);
        // Set the font/language of the inset...
        setFont(pos, font);
+       return success;
 }
 
 
@@ -1312,7 +1327,7 @@ void Paragraph::resetFonts(Font const & font)
 }
 
 // Gets uninstantiated font setting at position.
-Font const Paragraph::getFontSettings(BufferParams const & bparams,
+Font const Paragraph::getFontSettings(BufferParams const & bparams,
                                         pos_type pos) const
 {
        if (pos > size()) {
@@ -1327,7 +1342,16 @@ Font const Paragraph::getFontSettings(BufferParams const & bparams,
        if (pos == size() && !empty())
                return getFontSettings(bparams, pos - 1);
 
-       return Font(inherit_font, getParLanguage(bparams));
+       // Optimisation: avoid a full font instantiation if there is no
+       // language change from previous call.
+       static Font previous_font;
+       static Language const * previous_lang = 0;
+       Language const * lang = getParLanguage(bparams);
+       if (lang != previous_lang) {
+               previous_lang = lang;
+               previous_font = Font(inherit_font, lang);
+       }
+       return previous_font;
 }
 
 
@@ -1358,12 +1382,21 @@ FontSpan Paragraph::fontSpan(pos_type pos) const
 
 
 // Gets uninstantiated font setting at position 0
-Font const Paragraph::getFirstFontSettings(BufferParams const & bparams) const
+Font const Paragraph::getFirstFontSettings(BufferParams const & bparams) const
 {
        if (!empty() && !d->fontlist_.empty())
                return d->fontlist_.begin()->font();
 
-       return Font(inherit_font, bparams.language);
+       // Optimisation: avoid a full font instantiation if there is no
+       // language change from previous call.
+       static Font previous_font;
+       static Language const * previous_lang = 0;
+       if (bparams.language != previous_lang) {
+               previous_lang = bparams.language;
+               previous_font = Font(inherit_font, bparams.language);
+       }
+
+       return previous_font;
 }
 
 
@@ -1380,13 +1413,14 @@ Font const Paragraph::getFont(BufferParams const & bparams, pos_type pos,
        Font font = getFontSettings(bparams, pos);
 
        pos_type const body_pos = beginOfBody();
+       FontInfo & fi = font.fontInfo();
        if (pos < body_pos)
-               font.fontInfo().realize(d->layout_->labelfont);
+               fi.realize(d->layout_->labelfont);
        else
-               font.fontInfo().realize(d->layout_->font);
+               fi.realize(d->layout_->font);
 
-       font.fontInfo().realize(outerfont.fontInfo());
-       font.fontInfo().realize(bparams.getFont().fontInfo());
+       fi.realize(outerfont.fontInfo());
+       fi.realize(bparams.getFont().fontInfo());
 
        return font;
 }
@@ -1679,7 +1713,9 @@ namespace {
 
 bool noTrivlistCentering(InsetCode code)
 {
-       return code == FLOAT_CODE || code == WRAP_CODE;
+       return code == FLOAT_CODE
+              || code == WRAP_CODE
+              || code == CELL_CODE;
 }
 
 
@@ -1696,12 +1732,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")
@@ -1726,7 +1769,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;
 
@@ -1749,13 +1792,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:
@@ -1765,24 +1812,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;
@@ -1795,7 +1842,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;
 
@@ -1808,13 +1855,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:
@@ -1824,24 +1875,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;
@@ -1858,7 +1909,7 @@ bool Paragraph::latex(BufferParams const & bparams,
                                odocstream & os, TexRow & texrow,
                                OutputParams const & runparams) const
 {
-       LYXERR(Debug::LATEX, "SimpleTeXOnePar...     " << this);
+       LYXERR(Debug::LATEX, "Paragraph::latex...     " << this);
 
        bool return_value = false;
 
@@ -1907,7 +1958,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) {
@@ -1938,7 +1989,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
@@ -2115,10 +2166,10 @@ bool Paragraph::latex(BufferParams const & bparams,
 
        if (!asdefault) {
                column += d->endTeXParParams(bparams, os, texrow,
-                                         runparams.moving_arg);
+                                         runparams);
        }
 
-       LYXERR(Debug::LATEX, "SimpleTeXOnePar...done " << this);
+       LYXERR(Debug::LATEX, "Paragraph::latex... done " << this);
        return return_value;
 }
 
@@ -2371,7 +2422,7 @@ docstring Paragraph::asString(pos_type beg, pos_type end, int options) const
 
        for (pos_type i = beg; i < end; ++i) {
                char_type const c = d->text_[i];
-               if (isPrintable(c))
+               if (isPrintable(c) || c == '\t')
                        os.put(c);
                else if (c == META_INSET && options & AS_STR_INSETS)
                        getInset(i)->textString(os);
@@ -2416,7 +2467,8 @@ void Paragraph::setPlainOrDefaultLayout(DocumentClass const & tclass)
 
 Inset const & Paragraph::inInset() const
 {
-       LASSERT(d->inset_owner_, exit(10));
+       LASSERT(d->inset_owner_, throw ExceptionMessage(BufferException,
+               _("Memory problem"), _("Paragraph not properly initiliazed")));
        return *d->inset_owner_;
 }