]> git.lyx.org Git - features.git/blobdiff - src/output_latex.cpp
Move labels and insets out of moving arguments
[features.git] / src / output_latex.cpp
index e6e628a5c24f19c6f1aeb076f2d660bf47c6bed6..8ccb9f5eb6e66beeeaed41a3dc19384a6ac1d8e0 100644 (file)
@@ -307,11 +307,8 @@ static TeXEnvironmentData prepareEnvironment(Buffer const & buf,
        if (data.par_language->encoding()->package() == Encoding::CJK &&
            state->open_encoding_ != CJK && pit->isMultiLingual(bparams)) {
                if (prev_par_language->encoding()->package() == Encoding::CJK) {
-                       docstring const cjkenc = (bparams.encoding().name() == "utf8-cjk"
-                                                 && LaTeXFeatures::isAvailable("CJKutf8")) ?
-                                                       from_ascii("UTF8")
-                                                     : from_ascii(data.par_language->encoding()->latexName());
-                       os << "\\begin{CJK}{" << cjkenc
+                       os << "\\begin{CJK}{"
+                          << from_ascii(data.par_language->encoding()->latexName())
                           << "}{" << from_ascii(bparams.fonts_cjk) << "}%\n";
                }
                state->open_encoding_ = CJK;
@@ -530,6 +527,10 @@ void getArgInsets(otexstream & os, OutputParams const & runparams, Layout::LaTeX
                        }
                }
        }
+       if (runparams.for_search) {
+               // Mark end of arguments for findadv() only
+               os << "\\endarguments{}";
+       }
 }
 
 
@@ -696,6 +697,11 @@ void parStartCommand(Paragraph const & par, otexstream & os,
                        os << " ";
                }
                break;
+       case LATEX_ENVIRONMENT:
+               if (runparams.for_search) {
+                       os << "\\latexenvironment{" << style.latexname() << "}{";
+               }
+               break;
        case LATEX_BIB_ENVIRONMENT:
                // ignore this, the inset will write itself
                break;
@@ -771,6 +777,11 @@ void TeXOnePar(Buffer const & buf,
        if (style.pass_thru) {
                Font const outerfont = text.outerFont(pit);
                parStartCommand(par, os, runparams, style);
+               if (style.isCommand() && style.needprotect)
+                       // Due to the moving argument, some fragile
+                       // commands (labels, index entries)
+                       // are output after this command (#2154)
+                       runparams.postpone_fragile_stuff = true;
                if (intitle_command)
                        os << '{';
 
@@ -855,6 +866,11 @@ void TeXOnePar(Buffer const & buf,
        // (see #10849); thus open the command here.
        if (intitle_command) {
                parStartCommand(par, os, runparams, style);
+               if (style.isCommand() && style.needprotect)
+                       // Due to the moving argument, some fragile
+                       // commands (labels, index entries)
+                       // are output after this command (#2154)
+                       runparams.postpone_fragile_stuff = true;
                os << '{';
        }
 
@@ -999,11 +1015,8 @@ void TeXOnePar(Buffer const & buf,
                        // context (nesting issue).
                        if (par_language->encoding()->package() == Encoding::CJK
                                && state->open_encoding_ != CJK && state->cjk_inherited_ == 0) {
-                               docstring const cjkenc = (bparams.encoding().name() == "utf8-cjk"
-                                                         && LaTeXFeatures::isAvailable("CJKutf8")) ?
-                                                               from_ascii("UTF8")
-                                                             : from_ascii(par_language->encoding()->latexName());
-                               os << "\\begin{CJK}{" << cjkenc
+                               os << "\\begin{CJK}{"
+                                  << from_ascii(par_language->encoding()->latexName())
                                   << "}{" << from_ascii(bparams.fonts_cjk) << "}%\n";
                                state->open_encoding_ = CJK;
                        }
@@ -1033,6 +1046,8 @@ void TeXOnePar(Buffer const & buf,
        }
 
        runparams.moving_arg |= style.needprotect;
+       if (style.needmboxprotect)
+               ++runparams.inulemcmd;
        Encoding const * const prev_encoding = runparams.encoding;
 
        bool const useSetSpace = bparams.documentClass().provides("SetSpace");
@@ -1066,8 +1081,14 @@ void TeXOnePar(Buffer const & buf,
 
        // For InTitle commands, we already started the command before
        // the language switch
-       if (!intitle_command)
+       if (!intitle_command) {
                parStartCommand(par, os, runparams, style);
+               if (style.isCommand() && style.needprotect)
+                       // Due to the moving argument, some fragile
+                       // commands (labels, index entries)
+                       // are output after this command (#2154)
+                       runparams.postpone_fragile_stuff = true;
+       }
 
        Font const outerfont = text.outerFont(pit);
 
@@ -1087,6 +1108,12 @@ void TeXOnePar(Buffer const & buf,
                        os << '}';
                        if (!style.postcommandargs().empty())
                                latexArgInsets(par, os, runparams, style.postcommandargs(), "post:");
+                       if (!runparams.post_macro.empty()) {
+                               // Output the stored fragile commands (labels, indices etc.)
+                               // that need to be output after the command with moving argument.
+                               os << runparams.post_macro;
+                               runparams.post_macro.clear();
+                       }
                        if (runparams.encoding != prev_encoding) {
                                runparams.encoding = prev_encoding;
                                os << setEncoding(prev_encoding->iconvName());
@@ -1241,6 +1268,12 @@ void TeXOnePar(Buffer const & buf,
                        os << '}';
                        if (!style.postcommandargs().empty())
                                latexArgInsets(par, os, runparams, style.postcommandargs(), "post:");
+                       if (!runparams.post_macro.empty()) {
+                               // Output the stored fragile commands (labels, indices etc.)
+                               // that need to be output after the command with moving argument.
+                               os << runparams.post_macro;
+                               runparams.post_macro.clear();
+                       }
                        if (runparams.encoding != prev_encoding) {
                                runparams.encoding = prev_encoding;
                                os << setEncoding(prev_encoding->iconvName());
@@ -1262,7 +1295,8 @@ void TeXOnePar(Buffer const & buf,
        // if this is a CJK-paragraph and the next isn't, close CJK
        // also if the next paragraph is a multilingual environment (because of nesting)
        if (nextpar
-               && (state->open_encoding_ == CJK && bparams.encoding().iconvName() != "UTF-8")
+               && (state->open_encoding_ == CJK && bparams.encoding().iconvName() != "UTF-8"
+                       && bparams.encoding().package() != Encoding::CJK )
                && (nextpar_language->encoding()->package() != Encoding::CJK
                   || (nextpar->layout().isEnvironment() && nextpar->isMultiLingual(bparams)))
                // inbetween environments, CJK has to be closed later (nesting!)
@@ -1318,6 +1352,9 @@ void TeXOnePar(Buffer const & buf,
        else
                runparams_in.encoding = runparams.encoding;
 
+       // Also pass the post_macros upstream
+       runparams_in.post_macro = runparams.post_macro;
+
 
        // we don't need a newline for the last paragraph!!!
        // Note from JMarc: we will re-add a \n explicitly in
@@ -1404,24 +1441,15 @@ void latexParagraphs(Buffer const & buf,
        }
 
        // Open a CJK environment at the beginning of the main buffer
-       // (but not in child documents or documents using system fonts)
-       // if the document's language requires CJK,
-       // if a secondary language requires CJK and the encoding is utf8,
-       // or if the document encoding is utf8-cjk:
+       // if the document's main encoding requires the CJK package
+       // or the document encoding is utf8 and the CJK package is required
+       // (but not in child documents or documents using system fonts):
        OutputState * state = getOutputState();
        if (maintext && !is_child && !bparams.useNonTeXFonts
-           && ((bparams.language->encoding()->package() == Encoding::CJK
-                        && (bparams.encoding().iconvName() != "UTF-8"
-                                || bparams.encoding().name() == "utf8-cjk"
-                                || bparams.encoding().name() == "utf8"))
-                       || (bparams.encoding().name() == "utf8-cjk"
-                               && LaTeXFeatures::isAvailable("CJKutf8"))
-                       // FIXME: test for secondary language requiring CJK
-                       // || (LaTeXFeatures::mustProvide("CJK"))
-                       //     && bparams.encoding().name() == "utf8"
-                       //         && LaTeXFeatures::isAvailable("CJKutf8"))
-                       // error: cannot call member function ‘bool lyx::LaTeXFeatures::mustProvide(const string&) const’ without object 
-                  )) {
+           && (bparams.encoding().package() == Encoding::CJK
+                       || (bparams.encoding().name() == "utf8"
+                               && runparams.use_CJK))
+          ) {
                docstring const cjkenc = bparams.encoding().iconvName() == "UTF-8"
                                                                 ? from_ascii("UTF8") : from_ascii(bparams.encoding().latexName());
                os << "\\begin{CJK}{" << cjkenc
@@ -1613,15 +1641,7 @@ pair<bool, int> switchEncoding(odocstream & os, BufferParams const & bparams,
 
        Encoding const & oldEnc = *runparams.encoding;
        bool moving_arg = runparams.moving_arg;
-       // If we switch from/to CJK, we need to switch anyway, despite custom inputenc,
-       // except if we use CJKutf8 or explicitely set inputenc to a CJK encoding
-       bool const from_to_cjk =
-               ((oldEnc.package() == Encoding::CJK && newEnc.package() != Encoding::CJK)
-                || (oldEnc.package() != Encoding::CJK && newEnc.package() == Encoding::CJK))
-               && ((bparams.encoding().name() != "utf8-cjk" && bparams.encoding().name() != "utf8")
-                       || !LaTeXFeatures::isAvailable("CJKutf8"))
-               && bparams.encoding().package() != Encoding::CJK;
-       if (!force && !from_to_cjk
+       if (!force
            && ((bparams.inputenc != "auto" && bparams.inputenc != "default") || moving_arg))
                return make_pair(false, 0);
 
@@ -1640,14 +1660,12 @@ pair<bool, int> switchEncoding(odocstream & os, BufferParams const & bparams,
        if (oldEnc.package() == Encoding::none || newEnc.package() == Encoding::none)
                return make_pair(false, 0);
 
-       // change encoding (not required with UTF8)
-       if (bparams.encoding().iconvName() != "UTF-8") {
-               LYXERR(Debug::LATEX, "Changing LaTeX encoding from "
-                          << oldEnc.name() << " to " << newEnc.name());
-               os << setEncoding(newEnc.iconvName());
-               if (bparams.inputenc == "default")
-                 return make_pair(true, 0);
-       }
+       // change encoding
+       LYXERR(Debug::LATEX, "Changing LaTeX encoding from "
+                  << oldEnc.name() << " to " << newEnc.name());
+       os << setEncoding(newEnc.iconvName());
+       if (bparams.inputenc == "default")
+         return make_pair(true, 0);
 
        docstring const inputenc_arg(from_ascii(newEnc.latexName()));
        OutputState * state = getOutputState();
@@ -1697,10 +1715,8 @@ pair<bool, int> switchEncoding(odocstream & os, BufferParams const & bparams,
                                os << "\\egroup";
                                count += 7;
                        }
-                       docstring const cjkenc = (bparams.encoding().iconvName() == "UTF-8"
-                                                                         && LaTeXFeatures::isAvailable("CJKutf8"))
-                                                                         ? from_ascii("UTF8") : from_ascii(newEnc.latexName());
-                       os << "\\begin{CJK}{" << cjkenc << "}{"
+                       os << "\\begin{CJK}{"
+                          << from_ascii(newEnc.latexName()) << "}{"
                           << from_ascii(bparams.fonts_cjk) << "}";
                        state->open_encoding_ = CJK;
                        return make_pair(true, count + 15);