]> git.lyx.org Git - lyx.git/blobdiff - src/Paragraph.cpp
Revert part of 21965 which was debugging code.
[lyx.git] / src / Paragraph.cpp
index 779c60f989cf04496506199b46fa2fa264a12bad..b28082f83e4de5e6071756de9e9f389ec78d8f18 100644 (file)
 #include "Changes.h"
 #include "Counters.h"
 #include "Encoding.h"
-#include "debug.h"
-#include "gettext.h"
 #include "InsetList.h"
 #include "Language.h"
 #include "LaTeXFeatures.h"
-#include "Color.h"
 #include "Layout.h"
 #include "Length.h"
 #include "Font.h"
 #include "FontList.h"
 #include "LyXRC.h"
-#include "Messages.h"
 #include "OutputParams.h"
 #include "output_latex.h"
 #include "paragraph_funcs.h"
 #include "ParagraphParameters.h"
 #include "sgml.h"
+#include "TextClass.h"
 #include "TexRow.h"
 #include "VSpace.h"
 
 
 #include "insets/InsetBibitem.h"
 #include "insets/InsetLabel.h"
-#include "insets/InsetOptArg.h"
 
+#include "support/convert.h"
+#include "support/debug.h"
+#include "support/gettext.h"
 #include "support/lstrings.h"
+#include "support/Messages.h"
 #include "support/textutils.h"
-#include "support/convert.h"
 #include "support/unicode.h"
 
 #include <sstream>
@@ -102,7 +101,7 @@ public:
        /// Output a space in appropriate formatting (or a surrogate pair
        /// if the next character is a combining character).
        /// \return whether a surrogate pair was output.
-       bool simpleTeXBlanks(Encoding const &,
+       bool simpleTeXBlanks(OutputParams const &,
                             odocstream &, TexRow & texrow,
                             pos_type i,
                             unsigned int & column,
@@ -110,10 +109,10 @@ public:
                             Layout const & style);
 
        /// Output consecutive unicode chars, belonging to the same script as
-       /// specified by the latex macro \p ltx, to \p os starting from \p c.
+       /// specified by the latex macro \p ltx, to \p os starting from \p i.
        /// \return the number of characters written.
-       int writeScriptChars(odocstream & os, char_type c, docstring const & ltx,
-                          Change &, Encoding const &, pos_type &);
+       int writeScriptChars(odocstream & os, docstring const & ltx,
+                          Change &, Encoding const &, pos_type & i);
 
        /// This could go to ParagraphParameters if we want to.
        int startTeXParParams(BufferParams const &, odocstream &, TexRow &,
@@ -162,7 +161,8 @@ public:
        bool latexSpecialPhrase(
                odocstream & os,
                pos_type & i,
-               unsigned int & column);
+               unsigned int & column,
+               OutputParams & runparams);
 
        ///
        void validate(LaTeXFeatures & features,
@@ -191,6 +191,10 @@ public:
        ///
        ParagraphParameters params_;
 
+       /// position of the paragraph in the buffer. Only macros from
+       /// paragraphs strictly smaller are visible in this paragraph
+       unsigned int macrocontext_position_;
+       
        /// for recording and looking up changes
        Changes changes_;
 
@@ -229,10 +233,10 @@ struct special_phrase {
 };
 
 special_phrase const special_phrases[] = {
-       { "LyX", from_ascii("\\protect\\LyX{}"), false },
-       { "TeX", from_ascii("\\protect\\TeX{}"), true },
-       { "LaTeX2e", from_ascii("\\protect\\LaTeXe{}"), true },
-       { "LaTeX", from_ascii("\\protect\\LaTeX{}"), true },
+       { "LyX", from_ascii("\\LyX{}"), false },
+       { "TeX", from_ascii("\\TeX{}"), true },
+       { "LaTeX2e", from_ascii("\\LaTeXe{}"), true },
+       { "LaTeX", from_ascii("\\LaTeX{}"), true },
 };
 
 size_t const phrases_nr = sizeof(special_phrases)/sizeof(special_phrase);
@@ -244,6 +248,7 @@ Paragraph::Private::Private(Paragraph * owner)
        : owner_(owner), inset_owner_(0), begin_of_body_(0)
 {
        id_ = paragraph_id++;
+       macrocontext_position_ = 0;
        text_.reserve(100);
 }
 
@@ -413,7 +418,7 @@ void Paragraph::Private::insertChar(pos_type pos, char_type c,
 
        // This is actually very common when parsing buffers (and
        // maybe inserting ascii text)
-       if (pos == text_.size()) {
+       if (pos == pos_type(text_.size())) {
                // when appending characters, no need to update tables
                text_.push_back(c);
                return;
@@ -523,19 +528,20 @@ int Paragraph::Private::latexSurrogatePair(odocstream & os, char_type c,
 }
 
 
-bool Paragraph::Private::simpleTeXBlanks(Encoding const & encoding,
+bool Paragraph::Private::simpleTeXBlanks(OutputParams const & runparams,
                                       odocstream & os, TexRow & texrow,
                                       pos_type i,
                                       unsigned int & column,
                                       Font const & font,
                                       Layout const & style)
 {
-       if (style.pass_thru)
+       if (style.pass_thru || runparams.verbatim)
                return false;
 
        if (i + 1 < int(text_.size())) {
                char_type next = text_[i + 1];
                if (Encodings::isCombiningChar(next)) {
+                       Encoding const & encoding = *(runparams.encoding);
                        // This space has an accent, so we must always output it.
                        column += latexSurrogatePair(os, ' ', next, encoding) - 1;
                        return true;
@@ -551,7 +557,7 @@ bool Paragraph::Private::simpleTeXBlanks(Encoding const & encoding,
            && !owner_->isFreeSpacing()
            // In typewriter mode, we want to avoid
            // ! . ? : at the end of a line
-           && !(font.family() == Font::TYPEWRITER_FAMILY
+           && !(font.fontInfo().family() == TYPEWRITER_FAMILY
                 && (text_[i - 1] == '.'
                     || text_[i - 1] == '?'
                     || text_[i - 1] == ':'
@@ -570,18 +576,19 @@ bool Paragraph::Private::simpleTeXBlanks(Encoding const & encoding,
 
 
 int Paragraph::Private::writeScriptChars(odocstream & os,
-                                        char_type c,
                                         docstring const & ltx,
                                         Change & runningChange,
                                         Encoding const & encoding,
                                         pos_type & i)
 {
-       // We only arrive here when a proper language for character c has not
-       // been specified (i.e., it could not be translated in the current
+       // FIXME: modifying i here is not very nice...
+
+       // We only arrive here when a proper language for character text_[i] has
+       // not been specified (i.e., it could not be translated in the current
        // latex encoding) and it belongs to a known script.
-       // Parameter ltx contains the latex translation of c as specified in
+       // Parameter ltx contains the latex translation of text_[i] as specified in
        // the unicodesymbols file and is something like "\textXXX{<spec>}".
-       // The latex macro name "textXXX" specifies the script to which c
+       // The latex macro name "textXXX" specifies the script to which text_[i]
        // belongs and we use it in order to check whether characters from the
        // same script immediately follow, such that we can collect them in a
        // single "\textXXX" macro. So, we have to retain "\textXXX{<spec>"
@@ -686,7 +693,7 @@ void Paragraph::Private::latexInset(Buffer const & buf,
                                open_font = false;
                        }
 
-                       if (running_font.family() == Font::TYPEWRITER_FAMILY)
+                       if (running_font.fontInfo().family() == TYPEWRITER_FAMILY)
                                os << '~';
 
                        basefont = owner_->getLayoutFont(bparams, outerfont);
@@ -695,7 +702,6 @@ void Paragraph::Private::latexInset(Buffer const & buf,
                        if (runparams.moving_arg)
                                os << "\\protect ";
 
-                       os << "\\\\\n";
                }
                texrow.newline();
                texrow.start(owner_->id(), i + 1);
@@ -716,10 +722,10 @@ void Paragraph::Private::latexInset(Buffer const & buf,
        bool close = false;
        odocstream::pos_type const len = os.tellp();
 
-       if ((inset->lyxCode() == GRAPHICS_CODE
-            || inset->lyxCode() == MATH_CODE
-            || inset->lyxCode() == HYPERLINK_CODE)
-           && running_font.isRightToLeft()) {
+       if (inset->forceLTR() 
+           && running_font.isRightToLeft()
+               // ERT is an exception, it should be output with no decorations at all
+               && inset->lyxCode() != ERT_CODE) {
                if (running_font.language()->lang() == "farsi")
                        os << "\\beginL{}";
                else
@@ -813,7 +819,7 @@ void Paragraph::Private::latexSpecialChar(
        if (lyxrc.fontenc == "T1" && latexSpecialT1(c, os, i, column))
                return;
 
-       if (running_font.family() == Font::TYPEWRITER_FAMILY
+       if (running_font.fontInfo().family() == TYPEWRITER_FAMILY
                && latexSpecialTypewriter(c, os, i, column))
                return;
 
@@ -880,7 +886,7 @@ void Paragraph::Private::latexSpecialChar(
        default:
 
                // LyX, LaTeX etc.
-               if (latexSpecialPhrase(os, i, column))
+               if (latexSpecialPhrase(os, i, column, runparams))
                        return;
 
                if (c == '\0')
@@ -899,7 +905,7 @@ void Paragraph::Private::latexSpecialChar(
                docstring const latex = encoding.latexChar(c);
                if (Encodings::isKnownScriptChar(c, script)
                    && prefixIs(latex, from_ascii("\\" + script)))
-                       column += writeScriptChars(os, c, latex,
+                       column += writeScriptChars(os, latex,
                                        running_change, encoding, i) - 1;
                else if (latex.length() > 1 && latex[latex.length() - 1] != '}') {
                        // Prevent eating of a following
@@ -983,7 +989,7 @@ bool Paragraph::Private::latexSpecialTypewriter(char_type const c, odocstream &
 
 
 bool Paragraph::Private::latexSpecialPhrase(odocstream & os, pos_type & i,
-       unsigned int & column)
+       unsigned int & column, OutputParams & runparams)
 {
        // FIXME: if we have "LaTeX" with a font
        // change in the middle (before the 'T', then
@@ -994,6 +1000,8 @@ bool Paragraph::Private::latexSpecialPhrase(odocstream & os, pos_type & i,
        for (size_t pnr = 0; pnr < phrases_nr; ++pnr) {
                if (!isTextAt(special_phrases[pnr].phrase, i))
                        continue;
+               if (runparams.moving_arg)
+                       os << "\\protect";
                os << special_phrases[pnr].macro;
                i += special_phrases[pnr].phrase.length() - 1;
                column += special_phrases[pnr].macro.length() - 1;
@@ -1109,7 +1117,7 @@ void Paragraph::write(Buffer const & buf, ostream & os,
 
        params().write(os);
 
-       Font font1(Font::ALL_INHERIT, bparams.language);
+       Font font1(inherit_font, bparams.language);
 
        Change running_change = Change(Change::UNCHANGED);
 
@@ -1215,7 +1223,7 @@ void Paragraph::appendChar(char_type c, Font const & font,
 void Paragraph::appendString(docstring const & s, Font const & font,
                Change const & change)
 {
-       size_t end = s.size();
+       pos_type end = s.size();
        size_t oldsize = d->text_.size();
        size_t newsize = oldsize + end;
        size_t capacity = d->text_.capacity();
@@ -1230,7 +1238,8 @@ void Paragraph::appendString(docstring const & s, Font const & font,
                // track change
                d->changes_.insert(change, i);
        }
-       d->fontlist_.setRange(oldsize, newsize, font);
+       d->fontlist_.set(oldsize, font);
+       d->fontlist_.set(newsize - 1, font);
 }
 
 
@@ -1274,6 +1283,13 @@ bool Paragraph::insetAllowed(InsetCode code)
 }
 
 
+void Paragraph::resetFonts(Font const & font)
+{
+       d->fontlist_.clear();
+       d->fontlist_.set(0, font);
+       d->fontlist_.set(d->text_.size() - 1, font);
+}
+
 // Gets uninstantiated font setting at position.
 Font const Paragraph::getFontSettings(BufferParams const & bparams,
                                         pos_type pos) const
@@ -1290,7 +1306,7 @@ Font const Paragraph::getFontSettings(BufferParams const & bparams,
        if (pos == size() && !empty())
                return getFontSettings(bparams, pos - 1);
 
-       return Font(Font::ALL_INHERIT, getParLanguage(bparams));
+       return Font(inherit_font, getParLanguage(bparams));
 }
 
 
@@ -1327,7 +1343,7 @@ Font const Paragraph::getFirstFontSettings(BufferParams const & bparams) const
        if (!empty() && !d->fontlist_.empty())
                return d->fontlist_.begin()->font();
 
-       return Font(Font::ALL_INHERIT, bparams.language);
+       return Font(inherit_font, bparams.language);
 }
 
 
@@ -1345,12 +1361,12 @@ Font const Paragraph::getFont(BufferParams const & bparams, pos_type pos,
 
        pos_type const body_pos = beginOfBody();
        if (pos < body_pos)
-               font.realize(d->layout_->labelfont);
+               font.fontInfo().realize(d->layout_->labelfont);
        else
-               font.realize(d->layout_->font);
+               font.fontInfo().realize(d->layout_->font);
 
-       font.realize(outerfont);
-       font.realize(bparams.getFont());
+       font.fontInfo().realize(outerfont.fontInfo());
+       font.fontInfo().realize(bparams.getFont().fontInfo());
 
        return font;
 }
@@ -1359,28 +1375,26 @@ Font const Paragraph::getFont(BufferParams const & bparams, pos_type pos,
 Font const Paragraph::getLabelFont
        (BufferParams const & bparams, Font const & outerfont) const
 {
-       Font tmpfont = layout()->labelfont;
-       tmpfont.setLanguage(getParLanguage(bparams));
-       tmpfont.realize(outerfont);
-       tmpfont.realize(bparams.getFont());
-       return tmpfont;
+       FontInfo tmpfont = layout()->labelfont;
+       tmpfont.realize(outerfont.fontInfo());
+       tmpfont.realize(bparams.getFont().fontInfo());
+       return Font(tmpfont, getParLanguage(bparams));
 }
 
 
 Font const Paragraph::getLayoutFont
        (BufferParams const & bparams, Font const & outerfont) const
 {
-       Font tmpfont = layout()->font;
-       tmpfont.setLanguage(getParLanguage(bparams));
-       tmpfont.realize(outerfont);
-       tmpfont.realize(bparams.getFont());
-       return tmpfont;
+       FontInfo tmpfont = layout()->font;
+       tmpfont.realize(outerfont.fontInfo());
+       tmpfont.realize(bparams.getFont().fontInfo());
+       return Font(tmpfont, getParLanguage(bparams));
 }
 
 
 /// Returns the height of the highest font in range
-Font_size Paragraph::highestFontInRange
-       (pos_type startpos, pos_type endpos, Font_size def_size) const
+FontSize Paragraph::highestFontInRange
+       (pos_type startpos, pos_type endpos, FontSize def_size) const
 {
        return d->fontlist_.highestInRange(startpos, endpos, def_size);
 }
@@ -1815,7 +1829,7 @@ bool Paragraph::latex(Buffer const & buf,
                                odocstream & os, TexRow & texrow,
                                OutputParams const & runparams) const
 {
-       LYXERR(Debug::LATEX) << "SimpleTeXOnePar...     " << this << endl;
+       LYXERR(Debug::LATEX, "SimpleTeXOnePar...     " << this);
 
        bool return_value = false;
 
@@ -1992,7 +2006,7 @@ bool Paragraph::latex(Buffer const & buf,
                        // style->pass_thru is false.
                        if (i != body_pos - 1) {
                                if (d->simpleTeXBlanks(
-                                               *(runparams.encoding), os, texrow,
+                                               runparams, os, texrow,
                                                i, column, font, *style)) {
                                        // A surrogate pair was output. We
                                        // must not call latexSpecialChar
@@ -2060,7 +2074,7 @@ bool Paragraph::latex(Buffer const & buf,
                                          runparams.moving_arg);
        }
 
-       LYXERR(Debug::LATEX) << "SimpleTeXOnePar...done " << this << endl;
+       LYXERR(Debug::LATEX, "SimpleTeXOnePar...done " << this);
        return return_value;
 }
 
@@ -2152,7 +2166,7 @@ void Paragraph::simpleDocBookOnePar(Buffer const & buf,
        bool emph_flag = false;
 
        LayoutPtr const & style = layout();
-       Font font_old =
+       FontInfo font_old =
                style->labeltype == LABEL_MANUAL ? style->labelfont : style->font;
 
        if (style->pass_thru && !d->onlyText(buf, outerfont, initial))
@@ -2163,8 +2177,8 @@ void Paragraph::simpleDocBookOnePar(Buffer const & buf,
                Font font = getFont(buf.params(), i, outerfont);
 
                // handle <emphasis> tag
-               if (font_old.emph() != font.emph()) {
-                       if (font.emph() == Font::ON) {
+               if (font_old.emph() != font.fontInfo().emph()) {
+                       if (font.fontInfo().emph() == FONT_ON) {
                                os << "<emphasis>";
                                emph_flag = true;
                        } else if (i != initial) {
@@ -2184,7 +2198,7 @@ void Paragraph::simpleDocBookOnePar(Buffer const & buf,
                        else
                                os << sgml::escapeChar(c);
                }
-               font_old = font;
+               font_old = font.fontInfo();
        }
 
        if (emph_flag) {
@@ -2482,6 +2496,18 @@ int Paragraph::checkBiblio(bool track_changes)
 }
 
 
+unsigned int Paragraph::macrocontextPosition() const
+{
+       return d->macrocontext_position_;
+}
+
+
+void Paragraph::setMacrocontextPosition(unsigned int pos)
+{
+       d->macrocontext_position_ = pos;
+}
+
+
 void Paragraph::checkAuthors(AuthorList const & authorList)
 {
        d->changes_.checkAuthors(authorList);