]> git.lyx.org Git - lyx.git/blobdiff - src/Paragraph.cpp
Implement IsTocCaption for normal layouts.
[lyx.git] / src / Paragraph.cpp
index 73157e6b7d60f5605b698b419e157d93e4509564..5bd9224c17b17a0cc75a724b8c569f9a80636af5 100644 (file)
@@ -87,7 +87,7 @@ namespace {
 /// Inset identifier (above 0x10ffff, for ucs-4)
 char_type const META_INSET = 0x200001;
 
-}
+} // namespace
 
 
 /////////////////////////////////////////////////////////////////////
@@ -1238,17 +1238,15 @@ void Paragraph::Private::latexSpecialChar(otexstream & os,
                if (i + 1 < static_cast<pos_type>(text_.size()) &&
                    (end_pos == -1 || i + 1 < end_pos) &&
                    text_[i+1] == '-') {
-                       // Prevent "--" becoming an endash and "---" becoming
-                       // an emdash.
-                       // Within \ttfamily, "--" is merged to "-" (no endash)
-                       // so we avoid this rather irritating ligature as well
+                       // Prevent "--" becoming an en dash and "---" an em dash.
+                       // (Within \ttfamily, "---" is merged to en dash + hyphen.)
                        os << "{}";
                        column += 2;
                }
                break;
        case '\"':
-               os << "\\char34" << termcmd;
-               column += 9;
+               os << "\\textquotedbl" << termcmd;
+               column += 14;
                break;
 
        case '$': case '&':
@@ -1289,7 +1287,13 @@ 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
+                   && !runparams.inIPA
+                       // TODO #10961: && not in inset Flex Code
+                       // TODO #10961: && not in layout LyXCode
+                   && (!bparams.useNonTeXFonts || runparams.flavor != OutputParams::XETEX)) {
                        if (c == 0x2013) {
                                // en-dash
                                os << "--";
@@ -1426,43 +1430,18 @@ 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, buf.text(), 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 +1469,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,8 +1488,24 @@ void Paragraph::Private::validate(LaTeXFeatures & features) const
        }
 
        // then the contents
+       BufferParams const bp = features.runparams().is_child
+               ? features.buffer().masterParams() : features.buffer().params();
        for (pos_type i = 0; i < int(text_.size()) ; ++i) {
-               BufferEncodings::validate(text_[i], features);
+               char_type c = text_[i];
+               if (c == 0x0022) {
+                       if (features.runparams().isFullUnicode() && bp.useNonTeXFonts)
+                               features.require("textquotedblp");
+                       else if (bp.main_font_encoding() != "T1"
+                                || ((&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);
        }
 }
 
@@ -1569,7 +1574,7 @@ void flushString(ostream & os, docstring & s)
        s.erase();
 }
 
-}
+} // namespace
 
 
 void Paragraph::write(ostream & os, BufferParams const & bparams,
@@ -2200,7 +2205,7 @@ bool corrected_env(otexstream & os, string const & suffix, string const & env,
        return true;
 }
 
-} // namespace anon
+} // namespace
 
 
 int Paragraph::Private::startTeXParParams(BufferParams const & bparams,
@@ -2389,7 +2394,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;
                }
@@ -2427,7 +2434,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;
                        }
@@ -2445,6 +2454,8 @@ void Paragraph::latex(BufferParams const & bparams,
                runparams.wasDisplayMath = runparams.inDisplayMath;
                runparams.inDisplayMath = false;
                bool deleted_display_math = false;
+               Change const & change = runparams.inDeletedInset
+                       ? runparams.changeOfDeletedInset : lookupChange(i);
 
                // Check whether a display math inset follows
                if (d->text_[i] == META_INSET
@@ -2459,11 +2470,28 @@ void Paragraph::latex(BufferParams const & bparams,
                                // cannot set it here because it is a counter.
                                deleted_display_math = isDeleted(i);
                        }
+                       if (bparams.output_changes && deleted_display_math
+                           && runningChange == change
+                           && change.type == Change::DELETED
+                           && !os.afterParbreak()) {
+                               // A display math in the same paragraph follows.
+                               // We have to close and then reopen \lyxdeleted,
+                               // otherwise the math will be shifted up.
+                               OutputParams rp = runparams;
+                               if (open_font) {
+                                       bool needPar = false;
+                                       column += running_font.latexWriteEndChanges(
+                                               os, bparams, rp, basefont,
+                                               basefont, needPar);
+                                       open_font = false;
+                               }
+                               basefont = getLayoutFont(bparams, outerfont);
+                               running_font = basefont;
+                               column += Changes::latexMarkChange(os, bparams,
+                                       Change(Change::INSERTED), change, rp);
+                       }
                }
 
-               Change const & change = runparams.inDeletedInset
-                       ? runparams.changeOfDeletedInset : lookupChange(i);
-
                if (bparams.output_changes && runningChange != change) {
                        if (open_font) {
                                bool needPar = false;
@@ -2514,6 +2542,7 @@ void Paragraph::latex(BufferParams const & bparams,
                bool const using_begin_end = runparams.use_polyglossia ||
                                                !lang_end_command.empty();
                if (!running_lang.empty() &&
+                   (!using_begin_end || running_lang == openLanguageName()) &&
                    current_font.language()->encoding()->package() == Encoding::CJK) {
                                string end_tag = subst(lang_end_command,
                                                        "$$lang",
@@ -2540,7 +2569,7 @@ void Paragraph::latex(BufferParams const & bparams,
                char_type const c = d->text_[i];
 
                // A display math inset inside an ulem command will be output
-               // as a box of width \columnwidth, so we have to either disable
+               // as a box of width \linewidth, so we have to either disable
                // indentation if the inset starts a paragraph, or start a new
                // line to accommodate such box. This has to be done before
                // writing any font changing commands.
@@ -2928,7 +2957,7 @@ void doFontSwitch(vector<html::FontTag> & tagsToOpen,
                flag = false;
        }
 }
-}
+} // namespace
 
 
 docstring Paragraph::simpleLyXHTMLOnePar(Buffer const & buf,
@@ -3474,6 +3503,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;
@@ -3664,7 +3695,7 @@ InsetList const & Paragraph::insetList() const
 }
 
 
-void Paragraph::setBuffer(Buffer & b)
+void Paragraph::setInsetBuffers(Buffer & b)
 {
        d->insetlist_.setBuffer(b);
 }
@@ -3862,13 +3893,13 @@ void Paragraph::locateWord(pos_type & from, pos_type & to,
                        to = from;
                        return;
                }
-               // no break here, we go to the next
+               // fall through
 
        case WHOLE_WORD:
                // If we are already at the beginning of a word, do nothing
                if (!from || isWordSeparator(from - 1))
                        break;
-               // no break here, we go to the next
+               // fall through
 
        case PREVIOUS_WORD:
                // always move the cursor to the beginning of previous word
@@ -4119,6 +4150,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,