]> git.lyx.org Git - features.git/blobdiff - src/output_latex.cpp
#10285 and #10542 - Add sips as a fallback converter for Mac to provide preview of...
[features.git] / src / output_latex.cpp
index 4e40b6f3c7e46360c4f7bfa5648d2ca953f2c2c3..e2924421438f191c318d3a747d43024a69088ddc 100644 (file)
@@ -107,7 +107,25 @@ bool atSameLastLangSwitchDepth(OutputState const * state)
 
        return state->lang_switch_depth_.size() == 0
                        ? true
-                       : state->lang_switch_depth_.top() == state->nest_level_;
+                       : abs(state->lang_switch_depth_.top()) == state->nest_level_;
+}
+
+
+bool isLocalSwitch(OutputState const * state)
+{
+       // Return true if the language was opened by the \text<lang> command.
+
+       return state->lang_switch_depth_.size()
+               && state->lang_switch_depth_.top() < 0;
+}
+
+
+bool langOpenedAtThisLevel(OutputState const * state)
+{
+       // Return true if the language was opened at the current nesting level.
+
+       return state->lang_switch_depth_.size()
+               && abs(state->lang_switch_depth_.top()) == state->nest_level_;
 }
 
 
@@ -204,10 +222,8 @@ static TeXEnvironmentData prepareEnvironment(Buffer const & buf,
                                prev_par_lang))
                          // the '%' is necessary to prevent unwanted whitespace
                          << "%\n";
-                       if (use_polyglossia) {
-                               state->lang_switch_depth_.pop();
-                               state->open_polyglossia_lang_.pop();
-                       }
+                       if (use_polyglossia)
+                               popPolyglossiaLang();
                }
 
                // If no language was explicitly opened and we are using
@@ -229,10 +245,8 @@ static TeXEnvironmentData prepareEnvironment(Buffer const & buf,
                            os << bc;
                            // the '%' is necessary to prevent unwanted whitespace
                            os << "%\n";
-                           if (use_polyglossia) {
-                                   state->lang_switch_depth_.push(state->nest_level_);
-                                   state->open_polyglossia_lang_.push(par_lang);
-                           }
+                           if (use_polyglossia)
+                                   pushPolyglossiaLang(par_lang);
                }
        }
 
@@ -244,8 +258,10 @@ static TeXEnvironmentData prepareEnvironment(Buffer const & buf,
                data.leftindent_open = true;
        }
 
-       if (style.isEnvironment()) {
+       if (style.isEnvironment())
                state->nest_level_ += 1;
+
+       if (style.isEnvironment() && !style.latexname().empty()) {
                os << "\\begin{" << from_ascii(style.latexname()) << '}';
                if (!style.latexargs().empty()) {
                        OutputParams rp = runparams;
@@ -298,17 +314,21 @@ static void finishEnvironment(otexstream & os, OutputParams const & runparams,
                os << breakln;
                // Close any polyglossia language opened at this nest level
                if (runparams.use_polyglossia) {
-                       OutputState * state = getOutputState();
-                       while (state->lang_switch_depth_.size()
-                              && state->lang_switch_depth_.top() == state->nest_level_) {
-                               os << "\\end{" << openPolyglossiaLang(state)
-                                  << "}%\n";
-                               state->lang_switch_depth_.pop();
-                               state->open_polyglossia_lang_.pop();
+                       while (langOpenedAtThisLevel(state)) {
+                               if (isLocalSwitch(state)) {
+                                       os << "}";
+                               } else {
+                                       os << "\\end{"
+                                          << openPolyglossiaLang(state)
+                                          << "}%\n";
+                               }
+                               popPolyglossiaLang();
                        }
                }
                state->nest_level_ -= 1;
-               os << "\\end{" << from_ascii(data.style->latexname()) << "}\n";
+               string const & name = data.style->latexname();
+               if (!name.empty())
+                       os << "\\end{" << from_ascii(name) << "}\n";
                state->prev_env_language_ = data.par_language;
                if (runparams.encoding != data.prev_encoding) {
                        runparams.encoding = data.prev_encoding;
@@ -486,11 +506,12 @@ void getArgInsets(otexstream & os, OutputParams const & runparams, Layout::LaTeX
 } // namespace anon
 
 
-void pushPolyglossiaLang(string const & lang_name)
+void pushPolyglossiaLang(string const & lang_name, bool localswitch)
 {
        OutputState * state = getOutputState();
 
-       state->lang_switch_depth_.push(state->nest_level_);
+       int nest_level = localswitch ? -state->nest_level_ : state->nest_level_;
+       state->lang_switch_depth_.push(nest_level);
        state->open_polyglossia_lang_.push(lang_name);
 }
 
@@ -504,43 +525,57 @@ void popPolyglossiaLang()
 }
 
 
+namespace {
+
+void addArgInsets(Paragraph const & par, string const & prefix,
+                 Layout::LaTeXArgMap const & latexargs,
+                 map<int, InsetArgument const *> & ilist,
+                 vector<string> & required)
+{
+       for (auto const & table : par.insetList()) {
+               InsetArgument const * arg = table.inset->asInsetArgument();
+               if (!arg)
+                       continue;
+               if (arg->name().empty()) {
+                       LYXERR0("Error: Unnamed argument inset!");
+                       continue;
+               }
+               string const name = prefix.empty() ?
+                       arg->name() : split(arg->name(), ':');
+               // why converting into an integer?
+               unsigned int const nr = convert<unsigned int>(name);
+               if (ilist.find(nr) == ilist.end())
+                       ilist[nr] = arg;
+               Layout::LaTeXArgMap::const_iterator const lit =
+                       latexargs.find(arg->name());
+               if (lit != latexargs.end()) {
+                       Layout::latexarg const & larg = lit->second;
+                       vector<string> req = getVectorFromString(larg.requires);
+                       move(req.begin(), req.end(), back_inserter(required));
+               }
+       }
+}
+
+} // anon namespace
+
+
 void latexArgInsets(Paragraph const & par, otexstream & os,
-       OutputParams const & runparams, Layout::LaTeXArgMap const & latexargs, string const & prefix)
+                    OutputParams const & runparams,
+                    Layout::LaTeXArgMap const & latexargs,
+                    string const & prefix)
 {
        map<int, InsetArgument const *> ilist;
        vector<string> required;
-
-       InsetList::const_iterator it = par.insetList().begin();
-       InsetList::const_iterator end = par.insetList().end();
-       for (; it != end; ++it) {
-               if (it->inset->lyxCode() == ARG_CODE) {
-                       InsetArgument const * ins =
-                               static_cast<InsetArgument const *>(it->inset);
-                       if (ins->name().empty())
-                               LYXERR0("Error: Unnamed argument inset!");
-                       else {
-                               string const name = prefix.empty() ? ins->name() : split(ins->name(), ':');
-                               unsigned int const nr = convert<unsigned int>(name);
-                               ilist[nr] = ins;
-                               Layout::LaTeXArgMap::const_iterator const lit =
-                                               latexargs.find(ins->name());
-                               if (lit != latexargs.end()) {
-                                       Layout::latexarg const & arg = (*lit).second;
-                                       if (!arg.requires.empty()) {
-                                               vector<string> req = getVectorFromString(arg.requires);
-                                               required.insert(required.end(), req.begin(), req.end());
-                                       }
-                               }
-                       }
-               }
-       }
+       addArgInsets(par, prefix, latexargs, ilist, required);
        getArgInsets(os, runparams, latexargs, ilist, required, prefix);
 }
 
 
-void latexArgInsets(ParagraphList const & pars, ParagraphList::const_iterator pit,
-       otexstream & os, OutputParams const & runparams, Layout::LaTeXArgMap const & latexargs,
-       string const & prefix)
+void latexArgInsets(ParagraphList const & pars,
+                    ParagraphList::const_iterator pit,
+                    otexstream & os, OutputParams const & runparams,
+                    Layout::LaTeXArgMap const & latexargs,
+                    string const & prefix)
 {
        map<int, InsetArgument const *> ilist;
        vector<string> required;
@@ -564,39 +599,35 @@ void latexArgInsets(ParagraphList const & pars, ParagraphList::const_iterator pi
        ParagraphList::const_iterator spit = lyx::prev(pit, offset);
 
        for (; spit != pars.end(); ++spit) {
-               if (spit->layout() != current_layout || spit->params().depth() < current_depth)
+               if (spit->layout() != current_layout ||
+                   spit->params().depth() < current_depth)
                        break;
                if (spit->params().depth() > current_depth)
                        continue;
-               InsetList::const_iterator it = spit->insetList().begin();
-               InsetList::const_iterator end = spit->insetList().end();
-               for (; it != end; ++it) {
-                       if (it->inset->lyxCode() == ARG_CODE) {
-                               InsetArgument const * ins =
-                                       static_cast<InsetArgument const *>(it->inset);
-                               if (ins->name().empty())
-                                       LYXERR0("Error: Unnamed argument inset!");
-                               else {
-                                       string const name = prefix.empty() ? ins->name() : split(ins->name(), ':');
-                                       unsigned int const nr = convert<unsigned int>(name);
-                                       if (ilist.find(nr) == ilist.end())
-                                               ilist[nr] = ins;
-                                       Layout::LaTeXArgMap::const_iterator const lit =
-                                                       latexargs.find(ins->name());
-                                       if (lit != latexargs.end()) {
-                                               Layout::latexarg const & arg = (*lit).second;
-                                               if (!arg.requires.empty()) {
-                                                       vector<string> req = getVectorFromString(arg.requires);
-                                                       required.insert(required.end(), req.begin(), req.end());
-                                               }
-                                       }
-                               }
-                       }
-               }
+               addArgInsets(*spit, prefix, latexargs, ilist, required);
+       }
+       getArgInsets(os, runparams, latexargs, ilist, required, prefix);
+}
+
+
+void latexArgInsetsForParent(ParagraphList const & pars, otexstream & os,
+                             OutputParams const & runparams,
+                             Layout::LaTeXArgMap const & latexargs,
+                             string const & prefix)
+{
+       map<int, InsetArgument const *> ilist;
+       vector<string> required;
+
+       for (Paragraph const & par : pars) {
+               if (par.layout().hasArgs())
+                       // The InsetArguments inside this paragraph refer to this paragraph
+                       continue;
+               addArgInsets(par, prefix, latexargs, ilist, required);
        }
        getArgInsets(os, runparams, latexargs, ilist, required, prefix);
 }
 
+
 namespace {
 
 // output the proper paragraph start according to latextype.
@@ -790,10 +821,8 @@ void TeXOnePar(Buffer const & buf,
                                "$$lang",
                                prev_lang))
                           << lang_command_termination;
-                       if (use_polyglossia && !localswitch) {
-                               state->lang_switch_depth_.pop();
-                               state->open_polyglossia_lang_.pop();
-                       }
+                       if (use_polyglossia)
+                               popPolyglossiaLang();
                }
 
                // We need to open a new language if we couldn't close the previous
@@ -850,10 +879,8 @@ void TeXOnePar(Buffer const & buf,
                                          : subst(lang_begin_command, "$$lang", par_lang);
                                os << bc;
                                os << lang_command_termination;
-                               if (use_polyglossia && !localswitch) {
-                                       state->lang_switch_depth_.push(state->nest_level_);
-                                       state->open_polyglossia_lang_.push(par_lang);
-                               }
+                               if (use_polyglossia)
+                                       pushPolyglossiaLang(par_lang, localswitch);
                        }
                }
        }
@@ -912,10 +939,8 @@ void TeXOnePar(Buffer const & buf,
                                                "$$lang",
                                                par_lang))
                                        << lang_command_termination;
-                                       if (use_polyglossia && !localswitch) {
-                                               state->lang_switch_depth_.push(state->nest_level_);
-                                               state->open_polyglossia_lang_.push(par_lang);
-                                       }
+                                       if (use_polyglossia)
+                                               pushPolyglossiaLang(par_lang, localswitch);
                                }
                                runparams.encoding = encoding;
                        }
@@ -1081,11 +1106,8 @@ void TeXOnePar(Buffer const & buf,
                                        os << bc;
                                        pending_newline = !localswitch;
                                        unskip_newline = !localswitch;
-                                       if (use_polyglossia && !localswitch) {
-                                               state->lang_switch_depth_.push(
-                                                               state->nest_level_);
-                                               state->open_polyglossia_lang_.push(current_lang);
-                                       }
+                                       if (use_polyglossia)
+                                               pushPolyglossiaLang(current_lang, localswitch);
                                }
                        } else if (!par_lang.empty()) {
                                // If we are in an environment, we have to close the "outer" language afterwards
@@ -1110,10 +1132,8 @@ void TeXOnePar(Buffer const & buf,
                                                par_lang));
                                        pending_newline = !localswitch;
                                        unskip_newline = !localswitch;
-                                       if (use_polyglossia && !localswitch) {
-                                               state->lang_switch_depth_.pop();
-                                               state->open_polyglossia_lang_.pop();
-                                       }
+                                       if (use_polyglossia)
+                                               popPolyglossiaLang();
                                }
                        }
                }
@@ -1260,9 +1280,19 @@ void latexParagraphs(Buffer const & buf,
                { os << "% LaTeX Output Error\n"; return; } );
 
        BufferParams const & bparams = buf.params();
+       BufferParams const & mparams = buf.masterParams();
 
        bool const maintext = text.isMainText();
        bool const is_child = buf.masterBuffer() != &buf;
+       bool const multibib_child = maintext && is_child
+                       && mparams.multibib == "child";
+
+       if (multibib_child && mparams.useBiblatex())
+               os << "\\newrefsection";
+       else if (multibib_child && mparams.useBibtopic()) {
+               os << "\\begin{btUnit}\n";
+               runparams.openbtUnit = true;
+       }
 
        // Open a CJK environment at the beginning of the main buffer
        // if the document's language is a CJK language
@@ -1291,10 +1321,8 @@ void latexParagraphs(Buffer const & buf,
                          : subst(lang_begin_command, "$$lang", mainlang);
                os << bc;
                os << '\n';
-               if (runparams.use_polyglossia) {
-                       state->lang_switch_depth_.push(state->nest_level_);
-                       state->open_polyglossia_lang_.push(mainlang);
-               }
+               if (runparams.use_polyglossia)
+                       pushPolyglossiaLang(mainlang);
        }
 
        ParagraphList const & paragraphs = text.paragraphs();
@@ -1327,7 +1355,7 @@ void latexParagraphs(Buffer const & buf,
 
                if (layout.intitle) {
                        if (already_title) {
-                               if (!gave_layout_warning) {
+                               if (!gave_layout_warning && !runparams.dryrun) {
                                        gave_layout_warning = true;
                                        frontend::Alert::warning(_("Error in latexParagraphs"),
                                                        bformat(_("You are using at least one "
@@ -1344,7 +1372,7 @@ void latexParagraphs(Buffer const & buf,
                                                        << "}\n";
                                }
                        }
-               } else if (was_title && !already_title) {
+               } else if (was_title && !already_title && !layout.inpreamble) {
                        if (tclass.titletype() == TITLE_ENVIRONMENT) {
                                os << "\\end{" << from_ascii(tclass.titlename())
                                                << "}\n";
@@ -1357,6 +1385,15 @@ void latexParagraphs(Buffer const & buf,
                        was_title = false;
                }
 
+               if (layout.isCommand() && !layout.latexname().empty()
+                   && layout.latexname() == bparams.multibib) {
+                       if (runparams.openbtUnit)
+                               os << "\\end{btUnit}\n";
+                       if (!bparams.useBiblatex()) {
+                               os << '\n' << "\\begin{btUnit}\n";
+                               runparams.openbtUnit = true;
+                       }
+               }
 
                if (!layout.isEnvironment() && par->params().leftIndent().zero()) {
                        // This is a standard top level paragraph, TeX it and continue.
@@ -1389,6 +1426,9 @@ void latexParagraphs(Buffer const & buf,
                }
        }
 
+       if (maintext && !is_child && runparams.openbtUnit)
+               os << "\\end{btUnit}\n";
+
        // if "auto end" is switched off, explicitly close the language at the end
        // but only if the last par is in a babel or polyglossia language
        string const lang_end_command = runparams.use_polyglossia ?
@@ -1399,10 +1439,8 @@ void latexParagraphs(Buffer const & buf,
                                        "$$lang",
                                        mainlang))
                        << '\n';
-               if (runparams.use_polyglossia) {
-                       state->lang_switch_depth_.pop();
-                       state->open_polyglossia_lang_.pop();
-               }
+               if (runparams.use_polyglossia)
+                       popPolyglossiaLang();
        }
 
        // If the last paragraph is an environment, we'll have to close
@@ -1418,10 +1456,8 @@ void latexParagraphs(Buffer const & buf,
                                        "$$lang",
                                        pol_lang))
                   << '\n';
-               if (runparams.use_polyglossia) {
-                       state->lang_switch_depth_.pop();
-                       state->open_polyglossia_lang_.pop();
-               }
+               if (runparams.use_polyglossia)
+                       popPolyglossiaLang();
        }
 
        // reset inherited encoding
@@ -1430,6 +1466,11 @@ void latexParagraphs(Buffer const & buf,
                if (state->cjk_inherited_ == 0)
                        state->open_encoding_ = CJK;
        }
+
+       if (multibib_child && mparams.useBibtopic()) {
+               os << "\\end{btUnit}\n";
+               runparams.openbtUnit = false;
+       }
 }