]> 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 efb8e7760586dfdc0114f0d2b5fc6231c822936f..8ccb9f5eb6e66beeeaed41a3dc19384a6ac1d8e0 100644 (file)
@@ -18,6 +18,7 @@
 #include "Font.h"
 #include "InsetList.h"
 #include "Language.h"
+#include "LaTeXFeatures.h"
 #include "Layout.h"
 #include "LyXRC.h"
 #include "OutputParams.h"
@@ -305,9 +306,11 @@ static TeXEnvironmentData prepareEnvironment(Buffer const & buf,
        data.cjk_nested = false;
        if (data.par_language->encoding()->package() == Encoding::CJK &&
            state->open_encoding_ != CJK && pit->isMultiLingual(bparams)) {
-               if (prev_par_language->encoding()->package() == Encoding::CJK)
-                       os << "\\begin{CJK}{" << from_ascii(data.par_language->encoding()->latexName())
+               if (prev_par_language->encoding()->package() == Encoding::CJK) {
+                       os << "\\begin{CJK}{"
+                          << from_ascii(data.par_language->encoding()->latexName())
                           << "}{" << from_ascii(bparams.fonts_cjk) << "}%\n";
+               }
                state->open_encoding_ = CJK;
                data.cjk_nested = true;
        }
@@ -524,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{}";
+       }
 }
 
 
@@ -679,11 +686,21 @@ void parStartCommand(Paragraph const & par, otexstream & os,
                break;
        case LATEX_ITEM_ENVIRONMENT:
        case LATEX_LIST_ENVIRONMENT:
-               os << "\\" + style.itemcommand();
-               // Item arguments
-               if (!style.itemargs().empty())
-                       latexArgInsets(par, os, runparams, style.itemargs(), "item:");
-               os << " ";
+               if (runparams.for_search) {
+                       os << "\\" + style.itemcommand() << "{" << style.latexname() << "}";
+               }
+               else {
+                       os << "\\" + style.itemcommand();
+                       // Item arguments
+                       if (!style.itemargs().empty())
+                               latexArgInsets(par, os, runparams, style.itemargs(), "item:");
+                       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
@@ -760,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 << '{';
 
@@ -814,10 +836,12 @@ void TeXOnePar(Buffer const & buf,
                            || (priorpar->getDepth() == par.getDepth()
                                    && priorpar->layout() != par.layout()));
        Language const * const prev_language =
-               (priorpar && !priorpar->isPassThru())
-               ? (use_prev_env_language ? state->prev_env_language_
-                                        : priorpar->getParLanguage(bparams))
-               : outer_language;
+               runparams_in.for_search ?
+                       languages.getLanguage("ignore")
+               :(priorpar && !priorpar->isPassThru())
+                       ? (use_prev_env_language ? state->prev_env_language_
+                                               : priorpar->getParLanguage(bparams))
+                       : outer_language;
 
        bool const use_polyglossia = runparams.use_polyglossia;
        string const par_lang = use_polyglossia ?
@@ -842,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 << '{';
        }
 
@@ -849,31 +878,35 @@ void TeXOnePar(Buffer const & buf,
        // Also, if an RTL language is set via environment in polyglossia,
        // only a nested \\text<lang> command will reset the direction for LTR
        // languages (see # 10111).
-       bool const pg_rtl_env =
-               use_polyglossia && outer_language->rightToLeft() && !par_language->rightToLeft();
-       bool const localswitch = text.inset().forceLocalFontSwitch()
+       bool const in_polyglossia_rtl_env =
+               use_polyglossia
+               && runparams.local_font != 0
+               && outer_language->rightToLeft()
+               && !par_language->rightToLeft();
+       bool const localswitch = runparams_in.for_search
+                       || text.inset().forceLocalFontSwitch()
                        || (using_begin_end && text.inset().forcePlainLayout())
-                       || pg_rtl_env;
+                       || in_polyglossia_rtl_env;
        if (localswitch) {
                lang_begin_command = use_polyglossia ?
                            "\\text$$lang$$opts{" : lyxrc.language_command_local;
                lang_end_command = "}";
                lang_command_termination.clear();
        }
-       
+
        bool const localswitch_needed = localswitch && par_lang != outer_lang;
 
        // localswitches need to be closed and reopened at each par
-       if ((par_lang != prev_lang || localswitch_needed)
+       if (runparams_in.for_search || ((par_lang != prev_lang || localswitch_needed)
             // check if we already put language command in TeXEnvironment()
             && !(style.isEnvironment()
                  && (pit == 0 || (priorpar->layout() != par.layout()
                                   && priorpar->getDepth() <= par.getDepth())
-                     || priorpar->getDepth() < par.getDepth()))) {
+                     || priorpar->getDepth() < par.getDepth())))) {
                if (!localswitch
                    && (!using_begin_end || langOpenedAtThisLevel(state))
                    && !lang_end_command.empty()
-                   && prev_lang != outer_lang 
+                   && prev_lang != outer_lang
                    && !prev_lang.empty()
                    && (!using_begin_end || !style.isEnvironment())) {
                        os << from_ascii(subst(lang_end_command,
@@ -930,7 +963,7 @@ void TeXOnePar(Buffer const & buf,
                                        os << "\\L{";
                        }
                        // With CJK, the CJK tag has to be closed first (see below)
-                       if (runparams.encoding->package() != Encoding::CJK
+                       if ((runparams.encoding->package() != Encoding::CJK || runparams.for_search)
                            && (par_lang != openLanguageName(state) || localswitch)
                            && !par_lang.empty()) {
                                string bc = use_polyglossia ?
@@ -982,7 +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) {
-                               os << "\\begin{CJK}{" << from_ascii(par_language->encoding()->latexName())
+                               os << "\\begin{CJK}{"
+                                  << from_ascii(par_language->encoding()->latexName())
                                   << "}{" << from_ascii(bparams.fonts_cjk) << "}%\n";
                                state->open_encoding_ = CJK;
                        }
@@ -1012,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");
@@ -1045,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);
 
@@ -1066,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());
@@ -1154,7 +1202,7 @@ void TeXOnePar(Buffer const & buf,
                        os << '\n';
 
                // when the paragraph uses CJK, the language has to be closed earlier
-               if (font.language()->encoding()->package() != Encoding::CJK) {
+               if ((font.language()->encoding()->package() != Encoding::CJK) || runparams_in.for_search) {
                        if (lang_end_command.empty()) {
                                // If this is a child, we should restore the
                                // master language after the last paragraph.
@@ -1195,7 +1243,7 @@ void TeXOnePar(Buffer const & buf,
                                    || (atSameLastLangSwitchDepth(state)
                                        && state->lang_switch_depth_.size()
                                        && cur_lang != par_lang)
-                                   || pg_rtl_env)
+                                   || in_polyglossia_rtl_env)
                                {
                                        if (using_begin_end && !localswitch)
                                                os << breakln;
@@ -1220,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());
@@ -1241,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
+               && (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!)
@@ -1251,16 +1306,18 @@ void TeXOnePar(Buffer const & buf,
        }
 
        // If this is the last paragraph, close the CJK environment
-       // if necessary. If it's an environment, we'll have to \end that first.
-       if (runparams.isLastPar && !style.isEnvironment()) {
+       // if necessary. If it's an environment or nested in an environment,
+       // we'll have to \end that first.
+       if (runparams.isLastPar && !style.isEnvironment()
+               && par.params().depth() < 1) {
                switch (state->open_encoding_) {
                        case CJK: {
                                // do nothing at the end of child documents
                                if (maintext && buf.masterBuffer() != &buf)
                                        break;
-                               // end of main text
+                               // end of main text: also insert a \clearpage (see #5386)
                                if (maintext) {
-                                       os << "\n\\end{CJK}\n";
+                                       os << "\n\\clearpage\n\\end{CJK}\n";
                                // end of an inset
                                } else
                                        os << "\\end{CJK}";
@@ -1295,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
@@ -1381,13 +1441,19 @@ void latexParagraphs(Buffer const & buf,
        }
 
        // Open a CJK environment at the beginning of the main buffer
-       // if the document's language is a CJK language
-       // (but not in child documents)
+       // 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.encoding().package() == Encoding::CJK) {
-               os << "\\begin{CJK}{" << from_ascii(bparams.encoding().latexName())
-               << "}{" << from_ascii(bparams.fonts_cjk) << "}%\n";
+       if (maintext && !is_child && !bparams.useNonTeXFonts
+           && (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
+                  << "}{" << from_ascii(bparams.fonts_cjk) << "}%\n";
                state->open_encoding_ = CJK;
        }
        // if "auto begin" is switched off, explicitly switch the
@@ -1534,7 +1600,7 @@ void latexParagraphs(Buffer const & buf,
        // If the last paragraph is an environment, we'll have to close
        // CJK at the very end to do proper nesting.
        if (maintext && !is_child && state->open_encoding_ == CJK) {
-               os << "\\end{CJK}\n";
+               os << "\\clearpage\n\\end{CJK}\n";
                state->open_encoding_ = none;
        }
        // Likewise for polyglossia or when using begin/end commands
@@ -1575,11 +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
-       bool const from_to_cjk =
-               (oldEnc.package() == Encoding::CJK && newEnc.package() != Encoding::CJK)
-               || (oldEnc.package() != Encoding::CJK && newEnc.package() == Encoding::CJK);
-       if (!force && !from_to_cjk
+       if (!force
            && ((bparams.inputenc != "auto" && bparams.inputenc != "default") || moving_arg))
                return make_pair(false, 0);
 
@@ -1592,15 +1654,18 @@ pair<bool, int> switchEncoding(odocstream & os, BufferParams const & bparams,
        // This does of course only work in special cases (e.g. switch from
        // tis620-0 to latin1, but the text in latin1 contains ASCII only),
        // but it is the best we can do
-       if (oldEnc.package() == Encoding::none
-               || newEnc.package() == Encoding::none)
+       //
+       // 2019-01-08 Possibly no longer required since tis620-0 is supported
+       // by inputenc (but check special encodings "utf8-plain" and "default").
+       if (oldEnc.package() == Encoding::none || newEnc.package() == Encoding::none)
                return make_pair(false, 0);
 
+       // change encoding
        LYXERR(Debug::LATEX, "Changing LaTeX encoding from "
-               << oldEnc.name() << " to " << newEnc.name());
+                  << oldEnc.name() << " to " << newEnc.name());
        os << setEncoding(newEnc.iconvName());
        if (bparams.inputenc == "default")
-               return make_pair(true, 0);
+         return make_pair(true, 0);
 
        docstring const inputenc_arg(from_ascii(newEnc.latexName()));
        OutputState * state = getOutputState();
@@ -1650,7 +1715,8 @@ pair<bool, int> switchEncoding(odocstream & os, BufferParams const & bparams,
                                os << "\\egroup";
                                count += 7;
                        }
-                       os << "\\begin{CJK}{" << inputenc_arg << "}{"
+                       os << "\\begin{CJK}{"
+                          << from_ascii(newEnc.latexName()) << "}{"
                           << from_ascii(bparams.fonts_cjk) << "}";
                        state->open_encoding_ = CJK;
                        return make_pair(true, count + 15);
@@ -1658,7 +1724,6 @@ pair<bool, int> switchEncoding(odocstream & os, BufferParams const & bparams,
        }
        // Dead code to avoid a warning:
        return make_pair(true, 0);
-
 }
 
 } // namespace lyx