]> git.lyx.org Git - lyx.git/blobdiff - src/Paragraph.cpp
Remove indentation on screen after a plain separator inset
[lyx.git] / src / Paragraph.cpp
index 434e515ccb8101296b9d5492fbe65915ddf42b45..c43421b3764c36cda14988eb442427156aba0f3f 100644 (file)
@@ -1094,16 +1094,21 @@ void Paragraph::Private::latexInset(BufferParams const & bparams,
        odocstream::pos_type const len = os.os().tellp();
 
        if (inset->forceLTR(runparams)
+           // babel with Xe/LuaTeX does not need a switch
+           // and \L is not defined there.
+           && (!runparams.isFullUnicode() || !runparams.use_babel)
            && running_font.isRightToLeft()
            // ERT is an exception, it should be output with no
            // decorations at all
            && inset->lyxCode() != ERT_CODE) {
-               if (runparams.use_polyglossia) {
+               if (runparams.use_polyglossia)
+                       // (lua)bidi
                        os << "\\LRE{";
-               else if (running_font.language()->lang() == "farsi"
-                          || running_font.language()->lang() == "arabic_arabi")
+               else if (running_font.language()->lang() == "farsi"
+                        || running_font.language()->lang() == "arabic_arabi")
                        os << "\\textLR{" << termcmd;
                else
+                       // babel classic
                        os << "\\L{";
                close = true;
        }
@@ -1209,26 +1214,41 @@ void Paragraph::Private::latexSpecialChar(otexstream & os,
 {
        char_type const c = owner_->getUChar(bparams, runparams, i);
 
-       if (style.pass_thru || runparams.pass_thru || (runparams.for_search != OutputParams::NoSearch)
+       if (style.pass_thru || runparams.pass_thru || runparams.find_effective()
            || contains(style.pass_thru_chars, c)
            || contains(runparams.pass_thru_chars, c)) {
-               if (runparams.for_search != OutputParams::NoSearch) {
-                       if (c == '\\')
+               if (runparams.find_effective()) {
+                       switch (c) {
+                       case '\\':
                                os << "\\\\";
-                       else if (c == '{')
+                               return;
+                       case '{':
                                os << "\\braceleft ";
-                       else if (c == '}')
+                               return;
+                       case '}':
                                os << "\\braceright ";
-                       else if (c != '\0')
+                               return;
+                       case '$':
+                               os << "\\lyxdollar ";
+                               return;
+                       case '~':
+                               os << "\\lyxtilde ";
+                               return;
+                       case ' ':
+                       case '\0':
+                               break;
+                       default:
                                os.put(c);
+                               return;
+                       }
                }
                else if (c != '\0') {
                        Encoding const * const enc = runparams.encoding;
                        if (enc && !enc->encodable(c))
                                throw EncodingException(c);
                        os.put(c);
+                       return;
                }
-               return;
        }
 
        // TIPA uses its own T3 encoding
@@ -1731,16 +1751,34 @@ void Paragraph::write(ostream & os, BufferParams const & bparams,
                        column = 0;
                        break;
                case '.':
+               case '!':
+               case '?':
+               case ':':
+               case ';':
+               case ',':
+               case 0x061F:  // ؟ U+061F  ARABIC QUESTION MARK
+               case 0x061B:  // ؛ U+061B  ARABIC SEMICOLON
+               case 0x060C:  // ، U+060C  ARABIC COMMA
                        flushString(os, write_buffer);
                        if (i + 1 < size() && d->text_[i + 1] == ' ') {
-                               os << ".\n";
+                               os << to_utf8(docstring(1, c)) << '\n';
                                column = 0;
                        } else
-                               os << '.';
+                               os << to_utf8(docstring(1, c));
+                       break;
+               case 0x2014:  // — U+2014  EM DASH
+               case 0x3002:  // 。 U+3002  IDEOGRAPHIC FULL STOP
+               case 0xFF01:  // ! U+FF01  FULLWIDTH EXCLAMATION MARK
+               case 0xFF1F:  // ? U+FF1F  FULLWIDTH QUESTION MARK
+               case 0xFF1A:  // : U+FF1A  FULLWIDTH COLON
+               case 0xFF1B:  // ; U+FF1B  FULLWIDTH SEMICOLON
+               case 0xFF0C:  // , U+FF0C  FULLWIDTH COMMA
+                       flushString(os, write_buffer);
+                       os << to_utf8(docstring(1, c)) << '\n';
+                       column = 0;
                        break;
                default:
-                       if ((column > 70 && c == ' ')
-                           || column > 79) {
+                       if (column > 500) {
                                flushString(os, write_buffer);
                                os << '\n';
                                column = 0;
@@ -1912,7 +1950,8 @@ FontSpan Paragraph::fontSpan(pos_type pos) const
 
        // This should not happen, but if so, we take no chances.
        LYXERR0("Paragraph::fontSpan: position not found in fontinfo table!");
-       LASSERT(false, return FontSpan(pos, pos));
+       LASSERT(false, /**/);
+       return FontSpan(pos, pos);
 }
 
 
@@ -1988,9 +2027,11 @@ char_type Paragraph::getUChar(BufferParams const & bparams,
        char_type c = d->text_[pos];
 
        // Return unchanged character in LTR languages
-       // or if we use poylglossia/bidi (XeTeX).
-       if (rp.useBidiPackage()
-           || !getFontSettings(bparams, pos).isRightToLeft())
+       // or if we use poylglossia/bidi (XeTeX)
+       // or with babel and Xe/LuaTeX.
+       if (!getFontSettings(bparams, pos).isRightToLeft()
+           || rp.useBidiPackage()
+           || (rp.use_babel && rp.isFullUnicode()))
                return c;
 
        // Without polyglossia/bidi, we need to account for some special cases.
@@ -2610,7 +2651,7 @@ void Paragraph::latex(BufferParams const & bparams,
                                                runparams);
                                runningChange = Change(Change::UNCHANGED);
 
-                               os << (isEnvSeparator(i) ? "}]~" : "}] ");
+                               os << ((isEnvSeparator(i) && !runparams.find_effective()) ? "}]~" : "}] ");
                                column +=3;
                        }
                        // For InTitle commands, we have already opened a group
@@ -2640,10 +2681,10 @@ void Paragraph::latex(BufferParams const & bparams,
 
                // Check whether a display math inset follows
                bool output_changes;
-               if (runparams.for_search == OutputParams::NoSearch)
+               if (!runparams.find_effective())
                        output_changes = bparams.output_changes;
                else
-                       output_changes = ((runparams.for_search & OutputParams::SearchWithDeleted) != 0);
+                       output_changes = runparams.find_with_deleted();
                if (c == META_INSET
                    && i >= start_pos && (end_pos == -1 || i < end_pos)) {
                        if (isDeleted(i))
@@ -2773,9 +2814,11 @@ void Paragraph::latex(BufferParams const & bparams,
                                os << '}';
                                column += 1;
                        }
-                       if (closeLanguage)
+                       if (closeLanguage) {
                                // Force language closing
                                current_font.setLanguage(basefont.language());
+                               langClosed = true;
+                       }
                        Font const nextfont = (i == body_pos-1) ? basefont : current_font;
                        bool needPar = false;
                        column += running_font.latexWriteEndChanges(
@@ -2789,7 +2832,7 @@ void Paragraph::latex(BufferParams const & bparams,
                                        Change(Change::UNCHANGED), Change(Change::DELETED), rp);
                        }
                        // Has the language been closed in the latexWriteEndChanges() call above?
-                       langClosed = running_font.language() != basefont.language()
+                       langClosed |= running_font.language() != basefont.language()
                                        && running_font.language() != nextfont.language()
                                        && (running_font.language()->encoding()->package() != Encoding::CJK);
                        running_font = basefont;
@@ -2893,9 +2936,14 @@ void Paragraph::latex(BufferParams const & bparams,
                                        column += Changes::latexMarkChange(os, bparams,
                                                Change(Change::UNCHANGED), change, rp);
                                }
-                       } else {
+                       } else {// if fontswitch_inset
+                               if (current_font != running_font || !langClosed)
+                                       // font is still open in fontswitch_insets if we have
+                                       // a non-lang font difference or if the language
+                                       // is the only difference but has not been forcedly
+                                       // closed meanwhile
+                                       open_font = true;
                                running_font = current_font;
-                               open_font &= !langClosed;
                        }
                }
 
@@ -2964,7 +3012,7 @@ void Paragraph::latex(BufferParams const & bparams,
                                d->latexInset(bparams, os, rp, running_font,
                                                basefont, real_outerfont, open_font,
                                                runningChange, style, i, column, fontswitch_inset,
-                                               closeLanguage, lang_switched_at_inset);
+                                               closeLanguage, (lang_switched_at_inset || langClosed));
                                if (fontswitch_inset) {
                                        if (open_font) {
                                                bool needPar = false;
@@ -3482,9 +3530,12 @@ std::tuple<std::vector<docstring>, std::vector<docstring>, std::vector<docstring
                                                       bool is_last_par,
                                                       bool ignore_fonts) const
 {
-       std::vector<docstring> prependedParagraphs;
-       std::vector<docstring> generatedParagraphs;
-       std::vector<docstring> appendedParagraphs;
+       // Return values: segregation of the content of this paragraph.
+       std::vector<docstring> prependedParagraphs; // Anything that must be output before the main tag of this paragraph.
+       std::vector<docstring> generatedParagraphs; // The main content of the paragraph.
+       std::vector<docstring> appendedParagraphs;  // Anything that must be output after the main tag of this paragraph.
+
+       // Internal string stream to store the output before being added to one of the previous lists.
        odocstringstream os;
 
        // If there is an argument that must be output before the main tag, do it before handling the rest of the paragraph.
@@ -3511,16 +3562,16 @@ std::tuple<std::vector<docstring>, std::vector<docstring>, std::vector<docstring
     // State variables for the main loop.
     auto xs = new XMLStream(os); // XMLStream has no copy constructor: to create a new object, the only solution
     // is to hold a pointer to the XMLStream (xs = XMLStream(os) is not allowed once the first object is built).
-    std::vector<char_type> delayedChars; // When a font tag ends with a space, output it after the closing font tag.
+    std::vector<docstring> delayedChars; // When a font tag ends with a space, output it after the closing font tag.
     // This requires to store delayed characters at some point.
 
-    DocBookFontState fs; // Track whether we have opened font tags
+       // Track whether we have opened font tags
+    DocBookFontState fs;
     DocBookFontState old_fs = fs;
 
     Layout const & style = *d->layout_;
-    FontInfo font_old = style.labeltype == LABEL_MANUAL ? style.labelfont : style.font;
-    string const default_family = buf.masterBuffer()->params().fonts_default_family;
 
+       // Conversion of the font opening/closing into DocBook tags.
     vector<xml::FontTag> tagsToOpen;
     vector<xml::EndFontTag> tagsToClose;
 
@@ -3543,7 +3594,7 @@ std::tuple<std::vector<docstring>, std::vector<docstring>, std::vector<docstring
                        // Output one paragraph (i.e. one string entry in generatedParagraphs).
                        generatedParagraphs.push_back(os.str());
 
-                       // Create a new XMLStream for the new paragraph, completely independent from the previous one. This implies
+                       // Create a new XMLStream for the new paragraph, completely independent of the previous one. This implies
                        // that the string stream must be reset.
                        os.str(from_ascii(""));
                        delete xs;
@@ -3551,14 +3602,17 @@ std::tuple<std::vector<docstring>, std::vector<docstring>, std::vector<docstring
 
                        // Restore the fonts for the new paragraph, so that the right tags are opened for the new entry.
                        if (!ignore_fonts_i) {
-                               font_old = outerfont.fontInfo();
                                fs = old_fs;
                        }
                }
 
                // Determine which tags should be opened or closed regarding fonts.
+               FontInfo const font_old = (i == 0 ?
+                               (style.labeltype == LABEL_MANUAL ? style.labelfont : style.font) :
+                               getFont(buf.masterBuffer()->params(), i - 1, outerfont).fontInfo());
                Font const font = getFont(buf.masterBuffer()->params(), i, outerfont);
-        tie(tagsToOpen, tagsToClose) = computeDocBookFontSwitch(font_old, font, default_family, fs);
+        tie(tagsToOpen, tagsToClose) = computeDocBookFontSwitch(
+                               font_old, font, buf.masterBuffer()->params().fonts_default_family, fs);
 
                if (!ignore_fonts_i) {
             vector<xml::EndFontTag>::const_iterator cit = tagsToClose.begin();
@@ -3569,8 +3623,8 @@ std::tuple<std::vector<docstring>, std::vector<docstring>, std::vector<docstring
 
         // Deal with the delayed characters *after* closing font tags.
         if (!delayedChars.empty()) {
-            for (char_type c: delayedChars)
-                *xs << c;
+            for (const docstring& c: delayedChars)
+                *xs << XMLStream::ESCAPE_NONE << c;
             delayedChars.clear();
         }
 
@@ -3601,12 +3655,19 @@ std::tuple<std::vector<docstring>, std::vector<docstring>, std::vector<docstring
                        }
                } else {
                        char_type c = getUChar(buf.masterBuffer()->params(), rp, i);
-                       if (lyx::isSpace(c) && !ignore_fonts)
-                               delayedChars.push_back(c);
-                       else
-                               *xs << c;
+                       if (lyx::isSpace(c) && !ignore_fonts) { // Delay spaces *after* the font-tag closure for cleaner output.
+                               if (c == ' ' && (style.free_spacing || rp.free_spacing)) {
+                                       delayedChars.push_back(from_ascii("&#160;"));
+                               } else {
+                                       delayedChars.emplace_back(1, c);
+                               }
+                       } else { // No need to delay the character.
+                               if (c == '\'' && !ignore_fonts)
+                                       *xs << XMLStream::ESCAPE_NONE << "&#8217;";
+                               else
+                                       *xs << c;
+                       }
                }
-               font_old = font.fontInfo();
        }
 
        // FIXME, this code is just imported from XHTML
@@ -3616,9 +3677,11 @@ std::tuple<std::vector<docstring>, std::vector<docstring>, std::vector<docstring
                xs->closeFontTags();
 
        // Deal with the delayed characters *after* closing font tags.
-       if (!delayedChars.empty())
-               for (char_type c: delayedChars)
-                       *xs << c;
+       if (!delayedChars.empty()) {
+               for (const docstring &c: delayedChars)
+                       *xs << XMLStream::ESCAPE_NONE << c;
+               delayedChars.clear();
+       }
 
        // In listings, new lines (i.e. \n characters in the output) are very important. Avoid generating one for the
        // last line to get a clean output.
@@ -3968,16 +4031,10 @@ docstring Paragraph::simpleLyXHTMLOnePar(Buffer const & buf,
                // FIXME XHTML
                // Other such tags? What about the other text ranges?
 
-               vector<xml::EndFontTag>::const_iterator cit = tagsToClose.begin();
-               vector<xml::EndFontTag>::const_iterator cen = tagsToClose.end();
-               for (; cit != cen; ++cit)
-                       xs << *cit;
-
-               vector<xml::FontTag>::const_iterator sit = tagsToOpen.begin();
-               vector<xml::FontTag>::const_iterator sen = tagsToOpen.end();
-               for (; sit != sen; ++sit)
-                       xs << *sit;
-
+               for (auto const & t : tagsToClose)
+                       xs << t;
+               for (auto const & t : tagsToOpen)
+                       xs << t;
                tagsToClose.clear();
                tagsToOpen.clear();
 
@@ -3996,7 +4053,9 @@ docstring Paragraph::simpleLyXHTMLOnePar(Buffer const & buf,
                        char_type c = getUChar(buf.masterBuffer()->params(),
                                               runparams, i);
                        if (c == ' ' && (style.free_spacing || runparams.free_spacing))
-                               xs << XMLStream::ESCAPE_NONE << "&nbsp;";
+                               xs << XMLStream::ESCAPE_NONE << "&#160;";
+                       else if (c == '\'')
+                               xs << XMLStream::ESCAPE_NONE << "&#8217;";
                        else
                                xs << c;
                }
@@ -4231,7 +4290,10 @@ docstring Paragraph::asString(pos_type beg, pos_type end, int options, const Out
                else if (c == META_INSET && (options & AS_STR_INSETS)) {
                        if (c == META_INSET && (options & AS_STR_PLAINTEXT)) {
                                LASSERT(runparams != nullptr, return docstring());
-                               getInset(i)->plaintext(os, *runparams);
+                               if (runparams->find_effective())
+                                       getInset(i)->toString(os);
+                               else
+                                       getInset(i)->plaintext(os, *runparams);
                        } else if (c == META_INSET && (options & AS_STR_MATHED)
                                   && getInset(i)->lyxCode() == REF_CODE) {
                                Buffer const & buf = getInset(i)->buffer();
@@ -4586,7 +4648,7 @@ int Paragraph::find(docstring const & str, bool cs, bool mw,
                        odocstringstream os;
                        if (inset->lyxCode() == lyx::QUOTE_CODE) {
                                OutputParams op(0);
-                               op.for_search = OutputParams::SearchQuick;
+                               op.find_set_feature(OutputParams::SearchQuick);
                                inset->plaintext(os, op);
                        }
                        else {