]> git.lyx.org Git - lyx.git/blobdiff - src/Paragraph.cpp
Update status
[lyx.git] / src / Paragraph.cpp
index 6f514863de831fb780cfc3cfe027406bd92835e2..c77fbf974da8b48cd0eb1daffea106cf0f413b60 100644 (file)
@@ -1089,14 +1089,16 @@ void Paragraph::Private::latexInset(BufferParams const & bparams,
        bool close = false;
        odocstream::pos_type const len = os.os().tellp();
 
-       if (inset->forceLTR()
-           && !runparams.use_polyglossia
+       if (inset->forceLTR(runparams)
            && 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" << termcmd;
+               if (runparams.use_polyglossia) {
+                       os << "\\LRE{";
+               } else if (running_font.language()->lang() == "farsi"
+                          || running_font.language()->lang() == "arabic_arabi")
+                       os << "\\textLR{" << termcmd;
                else
                        os << "\\L{";
                close = true;
@@ -1152,12 +1154,8 @@ void Paragraph::Private::latexInset(BufferParams const & bparams,
                throw(e);
        }
 
-       if (close) {
-               if (running_font.language()->lang() == "farsi")
-                               os << "\\endL" << termcmd;
-                       else
-                               os << '}';
-       }
+       if (close)
+               os << '}';
 
        if (os.texrow().rows() > previous_row_count) {
                os.texrow().start(owner_->id(), i + 1);
@@ -1181,10 +1179,7 @@ void Paragraph::Private::latexSpecialChar(otexstream & os,
                                          pos_type end_pos,
                                          unsigned int & column)
 {
-       // With polyglossia, brackets and stuff need not be reversed
-       // in RTL scripts (see bug #8251)
-       char_type const c = (runparams.use_polyglossia) ?
-               owner_->getUChar(bparams, i) : text_[i];
+       char_type const c = owner_->getUChar(bparams, runparams, i);
 
        if (style.pass_thru || runparams.pass_thru
            || contains(style.pass_thru_chars, c)
@@ -1289,7 +1284,10 @@ void Paragraph::Private::latexSpecialChar(otexstream & os,
 
        case 0x2013:
        case 0x2014:
-               if (bparams.use_dash_ligatures && !bparams.useNonTeXFonts) {
+               // XeTeX's dash behaviour is determined via a global setting
+               if (bparams.use_dash_ligatures
+                   && owner_->getFontSettings(bparams, i).fontInfo().family() != TYPEWRITER_FAMILY
+                   && (!bparams.useNonTeXFonts || runparams.flavor != OutputParams::XETEX)) {
                        if (c == 0x2013) {
                                // en-dash
                                os << "--";
@@ -1417,6 +1415,14 @@ bool Paragraph::Private::latexSpecialT3(char_type const c, otexstream & os,
                os << "\\textvertline" << termcmd;
                column += 14;
                return true;
+       case 0x2013:
+               os << "\\textendash" << termcmd;
+               column += 12;
+               return true;
+       case 0x2014:
+               os << "\\textemdash" << termcmd;
+               column += 12;
+               return true;
        default:
                return false;
        }
@@ -1426,43 +1432,19 @@ bool Paragraph::Private::latexSpecialT3(char_type const c, otexstream & os,
 void Paragraph::Private::validate(LaTeXFeatures & features) const
 {
        if (layout_->inpreamble && inset_owner_) {
-               bool const is_command = layout_->latextype == LATEX_COMMAND;
-               Buffer const & buf = inset_owner_->buffer();
-               BufferParams const & bp = features.runparams().is_child
-                       ? buf.masterParams() : buf.params();
-               Font f;
-               // Using a string stream here circumvents the encoding
+               // FIXME: Using a string stream here circumvents the encoding
                // switching machinery of odocstream. Therefore the
                // output is wrong if this paragraph contains content
                // that needs to switch encoding.
+               Buffer const & buf = inset_owner_->buffer();
                otexstringstream os;
                os << layout_->preamble();
-               if (is_command) {
-                       os << '\\' << from_ascii(layout_->latexname());
-                       // we have to provide all the optional arguments here, even though
-                       // the last one is the only one we care about.
-                       // Separate handling of optional argument inset.
-                       if (!layout_->latexargs().empty()) {
-                               OutputParams rp = features.runparams();
-                               rp.local_font = &owner_->getFirstFontSettings(bp);
-                               latexArgInsets(*owner_, os, rp, layout_->latexargs());
-                       }
-                       os << from_ascii(layout_->latexparam());
-               }
                size_t const length = os.length();
-               // this will output "{" at the beginning, but not at the end
-               owner_->latex(bp, f, os, features.runparams(), 0, -1, true);
-               if (os.length() > length) {
-                       if (is_command) {
-                               os << '}';
-                               if (!layout_->postcommandargs().empty()) {
-                                       OutputParams rp = features.runparams();
-                                       rp.local_font = &owner_->getFirstFontSettings(bp);
-                                       latexArgInsets(*owner_, os, rp, layout_->postcommandargs(), "post:");
-                               }
-                       }
+               TeXOnePar(buf, *inset_owner_->getText(int(buf.getParFromID(owner_->id()).idx())),
+                         buf.getParFromID(owner_->id()).pit(), os,
+                         features.runparams(), string(), 0, -1, true);
+               if (os.length() > length)
                        features.addPreambleSnippet(os.release(), true);
-               }
        }
 
        if (features.runparams().flavor == OutputParams::HTML
@@ -1490,6 +1472,16 @@ void Paragraph::Private::validate(LaTeXFeatures & features) const
        for (; icit != iend; ++icit) {
                if (icit->inset) {
                        features.inDeletedInset(owner_->isDeleted(icit->pos));
+                       if (icit->inset->lyxCode() == FOOT_CODE) {
+                               // FIXME: an item inset would make things much easier.
+                               if ((layout_->latextype == LATEX_LIST_ENVIRONMENT
+                                    || (layout_->latextype == LATEX_ITEM_ENVIRONMENT
+                                        && layout_->margintype == MARGIN_FIRST_DYNAMIC))
+                                   && (icit->pos < begin_of_body_
+                                       || (icit->pos == begin_of_body_
+                                           && (icit->pos == 0 || text_[icit->pos - 1] != ' '))))
+                                       features.saveNoteEnv("description");
+                       }
                        icit->inset->validate(features);
                        features.inDeletedInset(false);
                        if (layout_->needprotect &&
@@ -1499,7 +1491,8 @@ void Paragraph::Private::validate(LaTeXFeatures & features) const
        }
 
        // then the contents
-       BufferParams const bp = features.buffer().masterParams();
+       BufferParams const bp = features.runparams().is_child
+               ? features.buffer().masterParams() : features.buffer().params();
        for (pos_type i = 0; i < int(text_.size()) ; ++i) {
                char_type c = text_[i];
                if (c == 0x0022) {
@@ -1509,6 +1502,12 @@ void Paragraph::Private::validate(LaTeXFeatures & features) const
                                 || ((&owner_->getFontSettings(bp, i))->language()->internalFontEncoding()))
                                features.require("textquotedbl");
                }
+               if (!bp.use_dash_ligatures
+                   && (c == 0x2013 || c == 0x2014)
+                   && bp.useNonTeXFonts
+                   && features.runparams().flavor == OutputParams::XETEX)
+                       // XeTeX's dash behaviour is determined via a global setting
+                       features.require("xetexdashbreakstate");
                BufferEncodings::validate(c, features);
        }
 }
@@ -1900,32 +1899,74 @@ Font const Paragraph::getLayoutFont
 }
 
 
-char_type Paragraph::getUChar(BufferParams const & bparams, pos_type pos) const
+char_type Paragraph::getUChar(BufferParams const & bparams,
+                             OutputParams const & rp,
+                             pos_type pos) const
 {
        char_type c = d->text_[pos];
+
+       // Return unchanged character in LTR languages.
        if (!getFontSettings(bparams, pos).isRightToLeft())
                return c;
 
-       // FIXME: The arabic special casing is due to the difference of arabic
-       // round brackets input introduced in r18599. Check if this should be
-       // unified with Hebrew or at least if all bracket types should be
-       // handled the same (file format change in either case).
+       // FIXME This is a complete mess due to all the language-specific
+       // special cases. We need to unify this eventually, but this
+       // requires a file format change and some thought.
+       // We also need to unify the input of parentheses in different RTL
+       // languages. Currently, some have their own methods (Arabic:
+       // 18599/lyxsvn, Hebrew: e5f42f67d/lyxgit), some don't (Urdu, Syriac).
+       // Also note that the representation in the LyX file is probably wrong
+       // (see FIXME in TextMetrics::breakRow).
+       // Most likely, we should simply rely on Qt's unicode handling here.
        string const & lang = getFontSettings(bparams, pos).language()->lang();
-       bool const arabic = lang == "arabic_arabtex" || lang == "arabic_arabi"
-               || lang == "farsi";
+
+       // With polyglossia and XeTeX (bidi), brackets and stuff need not be 
+       // reversed in RTL scripts
+       // FIXME: The special casing for Hebrew parens is due to the special
+       // handling on input (for Hebrew in e5f42f67d/lyxgit); see #8251.
        char_type uc = c;
+       if (rp.use_polyglossia && rp.flavor == OutputParams::XETEX) {
+               switch (c) {
+               case '(':
+                       if (lang == "hebrew")
+                               uc = ')';
+                       break;
+               case ')':
+                       if (lang == "hebrew")
+                               uc = '(';
+                       break;
+               }
+               return uc;
+       }
+       // LuaTeX (luabidi) is different
+       if (rp.use_polyglossia && rp.flavor != OutputParams::XETEX) {
+               switch (c) {
+               case '(':
+                       if (lang != "hebrew")
+                               uc = ')';
+                       break;
+               case ')':
+                       if (lang != "hebrew")
+                               uc = '(';
+                       break;
+               }
+       }
+
+       // In the following languages, brackets don't need to be reversed.
+       // Furthermore, in arabic_arabi, they are transformed to Arabic
+       // Ornate Parentheses (dunno if this is really wanted)
+       bool const reversebrackets = lang != "arabic_arabtex"
+                       && lang != "arabic_arabi"
+                       && lang != "farsi"; 
+
        switch (c) {
-       case '(':
-               uc = arabic ? c : ')';
-               break;
-       case ')':
-               uc = arabic ? c : '(';
-               break;
        case '[':
-               uc = ']';
+               if (reversebrackets)
+                       uc = ']';
                break;
        case ']':
-               uc = '[';
+               if (reversebrackets)
+                       uc = '[';
                break;
        case '{':
                uc = '}';
@@ -2005,15 +2046,29 @@ depth_type Paragraph::getMaxDepthAfter() const
 }
 
 
-LyXAlignment Paragraph::getAlign() const
+LyXAlignment Paragraph::getAlign(BufferParams const & bparams) const
 {
        if (d->params_.align() == LYX_ALIGN_LAYOUT)
-               return d->layout_->align;
+               return getDefaultAlign(bparams);
        else
                return d->params_.align();
 }
 
 
+LyXAlignment Paragraph::getDefaultAlign(BufferParams const & bparams) const
+{
+       LyXAlignment res = layout().align;
+       if (isRTL(bparams)) {
+               // Swap sides
+               if (res == LYX_ALIGN_LEFT)
+                       res = LYX_ALIGN_RIGHT;
+               else if  (res == LYX_ALIGN_RIGHT)
+                       res = LYX_ALIGN_LEFT;
+       }
+       return res;
+}
+
+
 docstring const & Paragraph::labelString() const
 {
        return d->params_.labelString();
@@ -2252,6 +2307,10 @@ int Paragraph::Private::startTeXParParams(BufferParams const & bparams,
        string const begin_tag = "\\begin";
        InsetCode code = ownerCode();
        bool const lastpar = runparams.isLastPar;
+       // RTL in classic (PDF)LaTeX (without the Bidi package)
+       // Luabibdi (used by LuaTeX) behaves like classic
+       bool const rtl_classic = owner_->getParLanguage(bparams)->rightToLeft()
+               && (!runparams.use_polyglossia || runparams.flavor != OutputParams::XETEX);
 
        switch (curAlign) {
        case LYX_ALIGN_NONE:
@@ -2261,13 +2320,13 @@ int Paragraph::Private::startTeXParParams(BufferParams const & bparams,
        case LYX_ALIGN_DECIMAL:
                break;
        case LYX_ALIGN_LEFT: {
-               if (owner_->getParLanguage(bparams)->babel() != "hebrew")
+               if (!rtl_classic)
                        corrected_env(os, begin_tag, "flushleft", code, lastpar, column);
                else
                        corrected_env(os, begin_tag, "flushright", code, lastpar, column);
                break;
        } case LYX_ALIGN_RIGHT: {
-               if (owner_->getParLanguage(bparams)->babel() != "hebrew")
+               if (!rtl_classic)
                        corrected_env(os, begin_tag, "flushright", code, lastpar, column);
                else
                        corrected_env(os, begin_tag, "flushleft", code, lastpar, column);
@@ -2310,6 +2369,10 @@ bool Paragraph::Private::endTeXParParams(BufferParams const & bparams,
        string const end_tag = "\\par\\end";
        InsetCode code = ownerCode();
        bool const lastpar = runparams.isLastPar;
+       // RTL in classic (PDF)LaTeX (without the Bidi package)
+       // Luabibdi (used by LuaTeX) behaves like classic
+       bool const rtl_classic = owner_->getParLanguage(bparams)->rightToLeft()
+               && (!runparams.use_polyglossia || runparams.flavor != OutputParams::XETEX);
 
        switch (curAlign) {
        case LYX_ALIGN_NONE:
@@ -2319,13 +2382,13 @@ bool Paragraph::Private::endTeXParParams(BufferParams const & bparams,
        case LYX_ALIGN_DECIMAL:
                break;
        case LYX_ALIGN_LEFT: {
-               if (owner_->getParLanguage(bparams)->babel() != "hebrew")
+               if (!rtl_classic)
                        output = corrected_env(os, end_tag, "flushleft", code, lastpar, col);
                else
                        output = corrected_env(os, end_tag, "flushright", code, lastpar, col);
                break;
        } case LYX_ALIGN_RIGHT: {
-               if (owner_->getParLanguage(bparams)->babel() != "hebrew")
+               if (!rtl_classic)
                        output = corrected_env(os, end_tag, "flushright", code, lastpar, col);
                else
                        output = corrected_env(os, end_tag, "flushleft", code, lastpar, col);
@@ -2398,7 +2461,9 @@ void Paragraph::latex(BufferParams const & bparams,
 
        // if the paragraph is empty, the loop will not be entered at all
        if (empty()) {
-               if (style.isCommand()) {
+               // For InTitle commands, we have already opened a group
+               // in output_latex::TeXOnePar.
+               if (style.isCommand() && !style.intitle) {
                        os << '{';
                        ++column;
                }
@@ -2436,7 +2501,9 @@ void Paragraph::latex(BufferParams const & bparams,
                                os << "}] ";
                                column +=3;
                        }
-                       if (style.isCommand()) {
+                       // For InTitle commands, we have already opened a group
+                       // in output_latex::TeXOnePar.
+                       if (style.isCommand() && !style.intitle) {
                                os << '{';
                                ++column;
                        }
@@ -2680,6 +2747,14 @@ void Paragraph::latex(BufferParams const & bparams,
                                                runningChange, style, i, column);
                                if (incremented)
                                        --parInline;
+
+                               if (deleted_display_math) {
+                                       // Close \lyxdeleted and force its
+                                       // reopening (if needed)
+                                       os << '}';
+                                       column++;
+                                       runningChange = Change(Change::UNCHANGED);
+                               }
                        }
                } else if (i >= start_pos && (end_pos == -1 || i < end_pos)) {
                        try {
@@ -2709,6 +2784,10 @@ void Paragraph::latex(BufferParams const & bparams,
                // such as Note that do not produce any output, so that no
                // command is ever executed but its opening was recorded.
                runparams.inulemcmd = rp.inulemcmd;
+
+               // These need to be passed upstream as well
+               runparams.need_maketitle = rp.need_maketitle;
+               runparams.have_maketitle = rp.have_maketitle;
        }
 
        // If we have an open font definition, we have to close it
@@ -3286,8 +3365,12 @@ docstring Paragraph::simpleLyXHTMLOnePar(Buffer const & buf,
                                retval += inset->xhtml(xs, np);
                        }
                } else {
-                       char_type c = getUChar(buf.masterBuffer()->params(), i);
-                       xs << c;
+                       char_type c = getUChar(buf.masterBuffer()->params(),
+                                              runparams, i);
+                       if (c == ' ' && (style.free_spacing || runparams.free_spacing))
+                               xs << XHTMLStream::ESCAPE_NONE << "&nbsp;";
+                       else
+                               xs << c;
                }
                font_old = font.fontInfo();
        }
@@ -3503,6 +3586,8 @@ void Paragraph::forOutliner(docstring & os, size_t const maxlen,
        size_t tmplen = shorten ? maxlen + 1 : maxlen;
        if (label && !labelString().empty())
                os += labelString() + ' ';
+       if (!layout().isTocCaption())
+               return;
        for (pos_type i = 0; i < size() && os.length() < tmplen; ++i) {
                if (isDeleted(i))
                        continue;
@@ -3605,32 +3690,43 @@ bool Paragraph::allowEmpty() const
 
 bool Paragraph::brokenBiblio() const
 {
-       // there is a problem if there is no bibitem at position 0 or
-       // if there is another bibitem in the paragraph.
-       return d->layout_->labeltype == LABEL_BIBLIO
+       // There is a problem if there is no bibitem at position 0 in
+       // paragraphs that need one, if there is another bibitem in the
+       // paragraph or if this paragraph is not supposed to have
+       // a bibitem inset at all.
+       return ((d->layout_->labeltype == LABEL_BIBLIO
                && (d->insetlist_.find(BIBITEM_CODE) != 0
-                   || d->insetlist_.find(BIBITEM_CODE, 1) > 0);
+                   || d->insetlist_.find(BIBITEM_CODE, 1) > 0))
+               || (d->layout_->labeltype != LABEL_BIBLIO
+                   && d->insetlist_.find(BIBITEM_CODE) != -1));
 }
 
 
 int Paragraph::fixBiblio(Buffer const & buffer)
 {
-       // FIXME: What about the case where paragraph is not BIBLIO
-       // but there is an InsetBibitem?
        // FIXME: when there was already an inset at 0, the return value is 1,
-       // which does not tell whether another inset has been remove; the
+       // which does not tell whether another inset has been removed; the
        // cursor cannot be correctly updated.
 
-       if (d->layout_->labeltype != LABEL_BIBLIO)
-               return 0;
-
        bool const track_changes = buffer.params().track_changes;
        int bibitem_pos = d->insetlist_.find(BIBITEM_CODE);
-       bool const hasbibitem0 = bibitem_pos == 0;
 
+       // The case where paragraph is not BIBLIO
+       if (d->layout_->labeltype != LABEL_BIBLIO) {
+               if (bibitem_pos == -1)
+                       // No InsetBibitem => OK
+                       return 0;
+               // There is an InsetBibitem: remove it!
+               d->insetlist_.release(bibitem_pos);
+               eraseChar(bibitem_pos, track_changes);
+               return (bibitem_pos == 0) ? -1 : -bibitem_pos;
+       }
+
+       bool const hasbibitem0 = bibitem_pos == 0;
        if (hasbibitem0) {
                bibitem_pos = d->insetlist_.find(BIBITEM_CODE, 1);
-               // There was an InsetBibitem at pos 0, and no other one => OK
+               // There was an InsetBibitem at pos 0,
+               // and no other one => OK
                if (bibitem_pos == -1)
                        return 0;
                // there is a bibitem at the 0 position, but since
@@ -3641,11 +3737,13 @@ int Paragraph::fixBiblio(Buffer const & buffer)
                // than keep the first? (JMarc)
                Inset * inset = releaseInset(bibitem_pos);
                d->insetlist_.begin()->inset = inset;
+               // This needs to be done to update the counter (#8499)
+               buffer.updateBuffer();
                return -bibitem_pos;
        }
 
        // We need to create an inset at the beginning
-       Inset * inset = 0;
+       Inset * inset = nullptr;
        if (bibitem_pos > 0) {
                // there was one somewhere in the paragraph, let's move it
                inset = d->insetlist_.release(bibitem_pos);
@@ -3659,6 +3757,8 @@ int Paragraph::fixBiblio(Buffer const & buffer)
        insertInset(0, inset, font, Change(track_changes ? Change::INSERTED
                                                   : Change::UNCHANGED));
 
+       // This is needed to get the counters right
+       buffer.updateBuffer();
        return 1;
 }
 
@@ -4148,6 +4248,15 @@ SpellChecker::Result Paragraph::spellCheck(pos_type & from, pos_type & to,
 }
 
 
+void Paragraph::anonymize()
+{
+       // This is a very crude anonymization for now
+       for (char_type & c : d->text_)
+               if (isLetterChar(c) || isNumber(c))
+                       c = 'a';
+}
+
+
 void Paragraph::Private::markMisspelledWords(
        pos_type const & first, pos_type const & last,
        SpellChecker::Result result,