]> git.lyx.org Git - lyx.git/blobdiff - src/output_latex.C
Fix bug 2474; partial fix for 1777. Added last_reference_ member to QRef class and...
[lyx.git] / src / output_latex.C
index e78773eea6862420ab9a675bdd53bfd8e168901b..f1f43f48a596a728cfc0804f38f22686c0f924f1 100644 (file)
@@ -64,7 +64,7 @@ TeXDeeper(Buffer const & buf,
          odocstream & os, TexRow & texrow,
          OutputParams const & runparams)
 {
-       lyxerr[Debug::LATEX] << "TeXDeeper...     " << &*pit << endl;
+       LYXERR(Debug::LATEX) << "TeXDeeper...     " << &*pit << endl;
        ParagraphList::const_iterator par = pit;
 
        while (par != paragraphs.end() &&
@@ -77,16 +77,12 @@ TeXDeeper(Buffer const & buf,
                                             os, texrow, runparams);
                }
        }
-       lyxerr[Debug::LATEX] << "TeXDeeper...done " << endl;
+       LYXERR(Debug::LATEX) << "TeXDeeper...done " << endl;
 
        return par;
 }
 
 
-int latexOptArgInsets(Buffer const & buf, Paragraph const & par,
-                     odocstream & os, OutputParams const & runparams, int number);
-
-
 ParagraphList::const_iterator
 TeXEnvironment(Buffer const & buf,
               ParagraphList const & paragraphs,
@@ -94,36 +90,36 @@ TeXEnvironment(Buffer const & buf,
               odocstream & os, TexRow & texrow,
               OutputParams const & runparams)
 {
-       lyxerr[Debug::LATEX] << "TeXEnvironment...     " << &*pit << endl;
+       LYXERR(Debug::LATEX) << "TeXEnvironment...     " << &*pit << endl;
 
        BufferParams const & bparams = buf.params();
 
        LyXLayout_ptr const & style = pit->layout();
 
-       Language const * language = pit->getParLanguage(bparams);
-       Language const * doc_language = bparams.language;
-       Language const * previous_language =
+       Language const * const par_language = pit->getParLanguage(bparams);
+       Language const * const doc_language = bparams.language;
+       Language const * const prev_par_language =
                (pit != paragraphs.begin())
                ? boost::prior(pit)->getParLanguage(bparams)
                : doc_language;
-       if (language->babel() != previous_language->babel()) {
+       if (par_language->babel() != prev_par_language->babel()) {
 
                if (!lyxrc.language_command_end.empty() &&
-                   previous_language->babel() != doc_language->babel()) {
+                   prev_par_language->babel() != doc_language->babel()) {
                        os << from_ascii(subst(
                                lyxrc.language_command_end,
                                "$$lang",
-                               previous_language->babel()))
+                               prev_par_language->babel()))
                           << '\n';
                        texrow.newline();
                }
 
                if (lyxrc.language_command_end.empty() ||
-                   language->babel() != doc_language->babel()) {
+                   par_language->babel() != doc_language->babel()) {
                        os << from_ascii(subst(
                                lyxrc.language_command_begin,
                                "$$lang",
-                               language->babel()))
+                               par_language->babel()))
                           << '\n';
                        texrow.newline();
                }
@@ -207,11 +203,13 @@ TeXEnvironment(Buffer const & buf,
                texrow.newline();
        }
 
-       if (par != paragraphs.end() && lyxerr.debugging(Debug::LATEX))
-               lyxerr << "TeXEnvironment...done " << &*par << endl;
+       if (par != paragraphs.end())
+               LYXERR(Debug::LATEX) << "TeXEnvironment...done " << &*par << endl;
        return par;
 }
 
+}
+
 
 int latexOptArgInsets(Buffer const & buf, Paragraph const & par,
                      odocstream & os, OutputParams const & runparams, int number)
@@ -232,6 +230,8 @@ int latexOptArgInsets(Buffer const & buf, Paragraph const & par,
 }
 
 
+namespace {
+
 ParagraphList::const_iterator
 TeXOnePar(Buffer const & buf,
          ParagraphList const & paragraphs,
@@ -240,7 +240,7 @@ TeXOnePar(Buffer const & buf,
          OutputParams const & runparams_in,
          string const & everypar)
 {
-       lyxerr[Debug::LATEX] << "TeXOnePar...     " << &*pit << " '"
+       LYXERR(Debug::LATEX) << "TeXOnePar...     " << &*pit << " '"
                << everypar << "'" << endl;
        BufferParams const & bparams = buf.params();
        LyXLayout_ptr style;
@@ -255,14 +255,14 @@ TeXOnePar(Buffer const & buf,
        OutputParams runparams = runparams_in;
        runparams.moving_arg |= style->needprotect;
 
-       Language const * language = pit->getParLanguage(bparams);
-       Language const * doc_language = bparams.language;
-       Language const * previous_language =
+       Language const * const par_language = pit->getParLanguage(bparams);
+       Language const * const doc_language = bparams.language;
+       Language const * const prev_par_language =
                (pit != paragraphs.begin())
                ? boost::prior(pit)->getParLanguage(bparams)
                : doc_language;
 
-       if (language->babel() != previous_language->babel()
+       if (par_language->babel() != prev_par_language->babel()
            // check if we already put language command in TeXEnvironment()
            && !(style->isEnvironment()
                 && (pit == paragraphs.begin() ||
@@ -271,43 +271,52 @@ TeXOnePar(Buffer const & buf,
                     || boost::prior(pit)->getDepth() < pit->getDepth())))
        {
                if (!lyxrc.language_command_end.empty() &&
-                   previous_language->babel() != doc_language->babel())
+                   prev_par_language->babel() != doc_language->babel())
                {
                        os << from_ascii(subst(lyxrc.language_command_end,
                                "$$lang",
-                               previous_language->babel()))
+                               prev_par_language->babel()))
                           << '\n';
                        texrow.newline();
                }
 
                if (lyxrc.language_command_end.empty() ||
-                   language->babel() != doc_language->babel())
+                   par_language->babel() != doc_language->babel())
                {
                        os << from_ascii(subst(
                                lyxrc.language_command_begin,
                                "$$lang",
-                               language->babel()))
+                               par_language->babel()))
                           << '\n';
                        texrow.newline();
                }
        }
 
-       LyXFont const outerfont =
-               outerFont(std::distance(paragraphs.begin(), pit),
-                         paragraphs);
-       // This must be identical to basefont in Paragraph::simpleTeXOnePar
-       LyXFont basefont = (pit->beginOfBody() > 0) ?
-                       pit->getLabelFont(bparams, outerfont) :
-                       pit->getLayoutFont(bparams, outerfont);
-       Encoding const & outer_encoding(*(outerfont.language()->encoding()));
-       // FIXME we switch from the outer encoding to the encoding of
-       // this paragraph, since I could not figure out the correct
-       // logic to take the encoding of the previous paragraph into
-       // account. This may result in some unneeded encoding changes.
-       if (switchEncoding(os, bparams, outer_encoding,
-                          *(basefont.language()->encoding()))) {
-               os << '\n';
-               texrow.newline();
+       // Switch file encoding if necessary
+       if (bparams.inputenc == "auto") {
+               // Look ahead for future encoding changes.
+               // We try to output them at the beginning of the paragraph,
+               // since the \inputencoding command is not allowed e.g. in
+               // sections.
+               for (pos_type i = 0; i < pit->size(); ++i) {
+                       char_type const c = pit->getChar(i);
+                       if (c < 0x80)
+                               continue;
+                       if (pit->isInset(i))
+                               break;
+                       // All characters before c are in the ASCII range, and
+                       // c is non-ASCII (but no inset), so change the
+                       // encoding to that required by the language of c.
+                       Encoding const * const encoding =
+                               pit->getFontSettings(bparams, i).language()->encoding();
+                       if (switchEncoding(os, bparams, false,
+                                          *(runparams.encoding), *encoding) > 0) {
+                               runparams.encoding = encoding;
+                               os << '\n';
+                               texrow.newline();
+                       }
+                       break;
+               }
        }
 
        // In an inset with unlimited length (all in one row),
@@ -360,6 +369,10 @@ TeXOnePar(Buffer const & buf,
                break;
        }
 
+       LyXFont const outerfont =
+               outerFont(std::distance(paragraphs.begin(), pit),
+                         paragraphs);
+
        // FIXME UNICODE
        os << from_utf8(everypar);
        bool need_par = pit->simpleTeXOnePar(buf, bparams, outerfont,
@@ -434,7 +447,7 @@ TeXOnePar(Buffer const & buf,
        }
 
        if (boost::next(pit) == paragraphs.end()
-           && language->babel() != doc_language->babel()) {
+           && par_language->babel() != doc_language->babel()) {
                // Since \selectlanguage write the language to the aux file,
                // we need to reset the language at the end of footnote or
                // float.
@@ -452,21 +465,15 @@ TeXOnePar(Buffer const & buf,
                        os << from_ascii(subst(
                                lyxrc.language_command_end,
                                "$$lang",
-                               language->babel()));
+                               par_language->babel()));
                pending_newline = true;
        }
 
-       // FIXME we switch from the encoding of this paragraph to the
-       // outer encoding, since I could not figure out the correct logic
-       // to take the encoding of the next paragraph into account.
-       // This may result in some unneeded encoding changes.
-       basefont = pit->getLayoutFont(bparams, outerfont);
-       switchEncoding(os, bparams, *(basefont.language()->encoding()),
-                      outer_encoding);
        if (pending_newline) {
                os << '\n';
                texrow.newline();
        }
+       runparams_in.encoding = runparams.encoding;
 
        // we don't need it for the last paragraph!!!
        // Note from JMarc: we will re-add a \n explicitely in
@@ -476,9 +483,8 @@ TeXOnePar(Buffer const & buf,
                texrow.newline();
        }
 
-       if (boost::next(pit) != paragraphs.end() &&
-           lyxerr.debugging(Debug::LATEX))
-               lyxerr << "TeXOnePar...done " << &*boost::next(pit) << endl;
+       if (boost::next(pit) != paragraphs.end())
+               LYXERR(Debug::LATEX) << "TeXOnePar...done " << &*boost::next(pit) << endl;
 
        return ++pit;
 }
@@ -584,16 +590,21 @@ void latexParagraphs(Buffer const & buf,
 
 
 int switchEncoding(odocstream & os, BufferParams const & bparams,
-                   Encoding const & oldEnc, Encoding const & newEnc)
+                   bool moving_arg, Encoding const & oldEnc,
+                   Encoding const & newEnc)
 {
        // FIXME thailatex does not support the inputenc package, so we
        // ignore switches from/to tis620-0 encoding here. This does of
        // course only work as long as the non-thai text contains ASCII
        // only, but it is the best we can do.
-       if ((bparams.inputenc == "auto" || bparams.inputenc == "default") &&
+       // Since the \inputencoding command does not work inside sections
+       // we ignore the encoding switch also in moving arguments.
+       if (((bparams.inputenc == "auto" && !moving_arg) ||
+            bparams.inputenc == "default") &&
            oldEnc.name() != newEnc.name() &&
+           oldEnc.name() != "ascii" && newEnc.name() != "ascii" &&
            oldEnc.name() != "tis620-0" && newEnc.name() != "tis620-0") {
-               lyxerr[Debug::LATEX] << "Changing LaTeX encoding from "
+               LYXERR(Debug::LATEX) << "Changing LaTeX encoding from "
                                     << oldEnc.name() << " to "
                                     << newEnc.name() << endl;
                os << setEncoding(newEnc.iconvName());