]> git.lyx.org Git - lyx.git/blobdiff - src/output_latex.cpp
Amend 3093789e for cmake build
[lyx.git] / src / output_latex.cpp
index 04d1e1789f04201c006a3ecaa541b1a1d3e2e6e6..8f8459ffa724baa01dff408732f284dc3d956403 100644 (file)
@@ -19,8 +19,6 @@
 #include "Font.h"
 #include "InsetList.h"
 #include "Language.h"
-#include "LaTeXFeatures.h"
-#include "Layout.h"
 #include "LyXRC.h"
 #include "OutputParams.h"
 #include "Paragraph.h"
@@ -37,7 +35,6 @@
 #include "support/convert.h"
 #include "support/debug.h"
 #include "support/lstrings.h"
-#include "support/lyxalgo.h"
 #include "support/textutils.h"
 #include "support/gettext.h"
 
@@ -63,16 +60,16 @@ enum OpenEncoding {
 
 struct OutputState
 {
-       OutputState() : open_encoding_(none), cjk_inherited_(0),
-               prev_env_language_(nullptr), nest_level_(0)
+       OutputState() : prev_env_language_(nullptr), open_encoding_(none),
+               cjk_inherited_(0), nest_level_(0)
        {
        }
-       OpenEncoding open_encoding_;
-       int cjk_inherited_;
        Language const * prev_env_language_;
-       int nest_level_;
        stack<int> lang_switch_depth_;          // Both are always empty when
        stack<string> open_polyglossia_lang_;   // not using polyglossia
+       OpenEncoding open_encoding_;
+       int cjk_inherited_;
+       int nest_level_;
 };
 
 
@@ -109,7 +106,7 @@ bool atSameLastLangSwitchDepth(OutputState const * state)
        // commands. Instead, return always true when using babel with
        // only a begin command.
 
-       return state->lang_switch_depth_.size() == 0
+       return state->lang_switch_depth_.empty()
                        ? true
                        : abs(state->lang_switch_depth_.top()) == state->nest_level_;
 }
@@ -119,7 +116,7 @@ bool isLocalSwitch(OutputState const * state)
 {
        // Return true if the language was opened by a local command switch.
 
-       return state->lang_switch_depth_.size()
+       return !state->lang_switch_depth_.empty()
                && state->lang_switch_depth_.top() < 0;
 }
 
@@ -128,7 +125,7 @@ bool langOpenedAtThisLevel(OutputState const * state)
 {
        // Return true if the language was opened at the current nesting level.
 
-       return state->lang_switch_depth_.size()
+       return !state->lang_switch_depth_.empty()
                && abs(state->lang_switch_depth_.top()) == state->nest_level_;
 }
 
@@ -164,10 +161,10 @@ string const getPolyglossiaBegin(string const & lang_begin_command,
 
 struct TeXEnvironmentData
 {
-       bool cjk_nested;
        Layout const * style;
        Language const * par_language;
        Encoding const * prev_encoding;
+       bool cjk_nested;
        bool leftindent_open;
 };
 
@@ -241,7 +238,7 @@ static TeXEnvironmentData prepareEnvironment(Buffer const & buf,
                // polyglossia or begin/end commands, then the current
                // language is the document language.
                string const & cur_lang = using_begin_end
-                                         && state->lang_switch_depth_.size()
+                                         && !state->lang_switch_depth_.empty()
                                                  ? openLanguageName(state)
                                                  : doc_lang;
 
@@ -383,7 +380,7 @@ void TeXEnvironment(Buffer const & buf, Text const & text,
                    pit_type & pit, otexstream & os)
 {
        ParagraphList const & paragraphs = text.paragraphs();
-       ParagraphList::const_iterator ipar = paragraphs.constIterator(pit);
+       ParagraphList::const_iterator ipar = paragraphs.iterator_at(pit);
        LYXERR(Debug::LATEX, "TeXEnvironment for paragraph " << pit);
 
        Layout const & current_layout = ipar->layout();
@@ -393,7 +390,7 @@ void TeXEnvironment(Buffer const & buf, Text const & text,
        // This is for debugging purpose at the end.
        pit_type const par_begin = pit;
        for (; pit < runparams.par_end; ++pit) {
-               ParagraphList::const_iterator par = paragraphs.constIterator(pit);
+               ParagraphList::const_iterator par = paragraphs.iterator_at(pit);
 
                // check first if this is an higher depth paragraph.
                bool go_out = (par->params().depth() < current_depth);
@@ -437,12 +434,12 @@ void TeXEnvironment(Buffer const & buf, Text const & text,
                // Do not output empty environments if the whole paragraph has
                // been deleted with ct and changes are not output.
                if (size_t(pit + 1) < paragraphs.size()) {
-                       ParagraphList::const_iterator nextpar = paragraphs.constIterator(pit + 1);
+                       ParagraphList::const_iterator nextpar = paragraphs.iterator_at(pit + 1);
                        Paragraph const & cpar = paragraphs.at(pit);
                        if ((par->layout() != nextpar->layout()
                             || par->params().depth() == nextpar->params().depth()
                             || par->params().leftIndent() == nextpar->params().leftIndent())
-                           && !runparams.for_search && cpar.size() > 0
+                           && !runparams.for_search && !cpar.empty()
                            && cpar.isDeleted(0, cpar.size()) && !buf.params().output_changes) {
                                if (!buf.params().output_changes && !cpar.parEndChange().deleted())
                                        os << '\n' << '\n';
@@ -464,9 +461,9 @@ void TeXEnvironment(Buffer const & buf, Text const & text,
 
 
 void getArgInsets(otexstream & os, OutputParams const & runparams, Layout::LaTeXArgMap const & latexargs,
-                 map<int, lyx::InsetArgument const *> ilist, vector<string> required, string const & prefix)
+                 map<size_t, lyx::InsetArgument const *> ilist, vector<string> required, string const & prefix)
 {
-       unsigned int const argnr = latexargs.size();
+       size_t const argnr = latexargs.size();
        if (argnr == 0)
                return;
 
@@ -474,22 +471,22 @@ void getArgInsets(otexstream & os, OutputParams const & runparams, Layout::LaTeX
        // other arguments, consider this.
        for (auto const & larg : latexargs) {
                Layout::latexarg const & arg = larg.second;
-               if ((!arg.presetarg.empty() || !arg.defaultarg.empty()) && !arg.requires.empty()) {
-                               vector<string> req = getVectorFromString(arg.requires);
+               if ((!arg.presetarg.empty() || !arg.defaultarg.empty()) && !arg.required.empty()) {
+                               vector<string> req = getVectorFromString(arg.required);
                                required.insert(required.end(), req.begin(), req.end());
                        }
        }
 
-       for (unsigned int i = 1; i <= argnr; ++i) {
-               map<int, InsetArgument const *>::const_iterator lit = ilist.find(i);
+       for (size_t i = 1; i <= argnr; ++i) {
+               map<size_t, InsetArgument const *>::const_iterator lit = ilist.find(i);
                bool inserted = false;
                if (lit != ilist.end()) {
-                       InsetArgument const * ins = (*lit).second;
+                       InsetArgument const * ins = lit->second;
                        if (ins) {
                                Layout::LaTeXArgMap::const_iterator const lait =
                                                latexargs.find(ins->name());
                                if (lait != latexargs.end()) {
-                                       Layout::latexarg arg = (*lait).second;
+                                       Layout::latexarg arg = lait->second;
                                        docstring ldelim;
                                        docstring rdelim;
                                        if (!arg.nodelims) {
@@ -588,7 +585,7 @@ namespace {
 
 void addArgInsets(Paragraph const & par, string const & prefix,
                  Layout::LaTeXArgMap const & latexargs,
-                 map<int, InsetArgument const *> & ilist,
+                 map<size_t, InsetArgument const *> & ilist,
                  vector<string> & required)
 {
        for (auto const & table : par.insetList()) {
@@ -601,15 +598,13 @@ void addArgInsets(Paragraph const & par, string const & prefix,
                }
                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;
+               size_t const nr = convert<size_t>(name);
+               ilist.insert({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);
+                       vector<string> req = getVectorFromString(larg.required);
                        move(req.begin(), req.end(), back_inserter(required));
                }
        }
@@ -623,7 +618,7 @@ void latexArgInsets(Paragraph const & par, otexstream & os,
                     Layout::LaTeXArgMap const & latexargs,
                     string const & prefix)
 {
-       map<int, InsetArgument const *> ilist;
+       map<size_t, InsetArgument const *> ilist;
        vector<string> required;
        addArgInsets(par, prefix, latexargs, ilist, required);
        getArgInsets(os, runparams, latexargs, ilist, required, prefix);
@@ -636,18 +631,18 @@ void latexArgInsets(ParagraphList const & pars,
                     Layout::LaTeXArgMap const & latexargs,
                     string const & prefix)
 {
-       map<int, InsetArgument const *> ilist;
+       map<size_t, InsetArgument const *> ilist;
        vector<string> required;
 
        depth_type const current_depth = pit->params().depth();
        Layout const current_layout = pit->layout();
 
        // get the first paragraph in sequence with this layout and depth
-       pit_type offset = 0;
+       ptrdiff_t offset = 0;
        while (true) {
-               if (lyx::prev(pit, offset) == pars.begin())
+               if (prev(pit, offset) == pars.begin())
                        break;
-               ParagraphList::const_iterator priorpit = lyx::prev(pit, offset + 1);
+               ParagraphList::const_iterator priorpit = prev(pit, offset + 1);
                if (priorpit->layout() == current_layout
                    && priorpit->params().depth() == current_depth)
                        ++offset;
@@ -655,8 +650,7 @@ void latexArgInsets(ParagraphList const & pars,
                        break;
        }
 
-       ParagraphList::const_iterator spit = lyx::prev(pit, offset);
-
+       ParagraphList::const_iterator spit = prev(pit, offset);
        for (; spit != pars.end(); ++spit) {
                if (spit->layout() != current_layout ||
                    spit->params().depth() < current_depth)
@@ -674,7 +668,7 @@ void latexArgInsetsForParent(ParagraphList const & pars, otexstream & os,
                              Layout::LaTeXArgMap const & latexargs,
                              string const & prefix)
 {
-       map<int, InsetArgument const *> ilist;
+       map<size_t, InsetArgument const *> ilist;
        vector<string> required;
 
        for (Paragraph const & par : pars) {
@@ -761,7 +755,7 @@ void TeXOnePar(Buffer const & buf,
        // Do not output empty commands if the whole paragraph has
        // been deleted with ct and changes are not output.
        if (!runparams_in.for_search && style.latextype != LATEX_ENVIRONMENT
-           && par.size() > 0 && par.isDeleted(0, par.size()) && !bparams.output_changes)
+           && !par.empty() && par.isDeleted(0, par.size()) && !bparams.output_changes)
                return;
 
        LYXERR(Debug::LATEX, "TeXOnePar for paragraph " << pit << " ptr " << &par << " '"
@@ -1312,7 +1306,7 @@ void TeXOnePar(Buffer const & buf,
                                                && nextpar
                                                && style != nextpar->layout())))
                                    || (atSameLastLangSwitchDepth(state)
-                                       && state->lang_switch_depth_.size()
+                                       && !state->lang_switch_depth_.empty()
                                        && cur_lang != par_lang)
                                    || in_polyglossia_rtl_env)
                                {
@@ -1355,7 +1349,7 @@ void TeXOnePar(Buffer const & buf,
        }
 
        bool const last_was_separator =
-               par.size() > 0 && par.isEnvSeparator(par.size() - 1);
+               !par.empty() && par.isEnvSeparator(par.size() - 1);
 
        // Signify added/deleted par break in output if show changes in output
        if (nextpar && !os.afterParbreak() && !last_was_separator
@@ -1442,6 +1436,9 @@ void TeXOnePar(Buffer const & buf,
 
        // Also pass the post_macros upstream
        runparams_in.post_macro = runparams.post_macro;
+       // These need to be passed upstream as well
+       runparams_in.need_maketitle = runparams.need_maketitle;
+       runparams_in.have_maketitle = runparams.have_maketitle;
 
 
        // we don't need a newline for the last paragraph!!!
@@ -1586,18 +1583,19 @@ void latexParagraphs(Buffer const & buf,
                // The full doc will be exported but it is easier to just rely on
                // runparams range parameters that will be passed TeXEnvironment.
                runparams.par_begin = 0;
-               runparams.par_end = paragraphs.size();
+               runparams.par_end = static_cast<int>(paragraphs.size());
        }
 
        pit_type pit = runparams.par_begin;
        // lastpit is for the language check after the loop.
        pit_type lastpit = pit;
-       // variables used in the loop:
        DocumentClass const & tclass = bparams.documentClass();
 
+       // Did we already warn about inTitle layout mixing? (we only warn once)
+       bool gave_layout_warning = false;
        for (; pit < runparams.par_end; ++pit) {
                lastpit = pit;
-               ParagraphList::const_iterator par = paragraphs.constIterator(pit);
+               ParagraphList::const_iterator par = paragraphs.iterator_at(pit);
 
                // FIXME This check should not be needed. We should
                // perhaps issue an error if it is.
@@ -1605,9 +1603,9 @@ void latexParagraphs(Buffer const & buf,
                                tclass.plainLayout() : par->layout();
 
                if (layout.intitle) {
-                       if (runparams.already_title) {
-                               if (!runparams.gave_layout_warning && !runparams.dryrun) {
-                                       runparams.gave_layout_warning = true;
+                       if (runparams.have_maketitle) {
+                               if (!gave_layout_warning && !runparams.dryrun) {
+                                       gave_layout_warning = true;
                                        frontend::Alert::warning(_("Error in latexParagraphs"),
                                                        bformat(_("You are using at least one "
                                                          "layout (%1$s) intended for the title, "
@@ -1615,16 +1613,16 @@ void latexParagraphs(Buffer const & buf,
                                                          "could lead to missing or incorrect output."
                                                          ), layout.name()));
                                }
-                       } else if (!runparams.issued_title_cmd) {
-                               runparams.issued_title_cmd = true;
+                       } else if (!runparams.need_maketitle) {
+                               runparams.need_maketitle = true;
                                if (tclass.titletype() == TITLE_ENVIRONMENT) {
                                        os << "\\begin{"
                                                        << from_ascii(tclass.titlename())
                                                        << "}\n";
                                }
                        }
-               } else if (runparams.issued_title_cmd &&
-                                  !runparams.already_title && !layout.inpreamble) {
+               } else if (runparams.need_maketitle && !runparams.have_maketitle
+                          && !layout.inpreamble && !text.inset().isInTitle()) {
                        if (tclass.titletype() == TITLE_ENVIRONMENT) {
                                os << "\\end{" << from_ascii(tclass.titlename())
                                                << "}\n";
@@ -1633,8 +1631,8 @@ void latexParagraphs(Buffer const & buf,
                                os << "\\" << from_ascii(tclass.titlename())
                                                << "\n";
                        }
-                       runparams.already_title = true;
-                       runparams.issued_title_cmd = false;
+                       runparams.have_maketitle = true;
+                       runparams.need_maketitle = false;
                }
 
                if (layout.isCommand() && !layout.latexname().empty()
@@ -1657,14 +1655,14 @@ void latexParagraphs(Buffer const & buf,
                // Do not output empty environments if the whole paragraph has
                // been deleted with ct and changes are not output.
                if (size_t(pit + 1) < paragraphs.size()) {
-                       ParagraphList::const_iterator nextpar = paragraphs.constIterator(pit + 1);
+                       ParagraphList::const_iterator nextpar = paragraphs.iterator_at(pit + 1);
                        Paragraph const & cpar = paragraphs.at(pit);
                        if ((par->layout() != nextpar->layout()
                             || par->params().depth() == nextpar->params().depth()
                             || par->params().leftIndent() == nextpar->params().leftIndent())
-                           && !runparams.for_search && cpar.size() > 0
+                           && !runparams.for_search && !cpar.empty()
                            && cpar.isDeleted(0, cpar.size()) && !bparams.output_changes) {
-                               if (!bparams.output_changes && !cpar.parEndChange().deleted())
+                               if (!cpar.parEndChange().deleted())
                                        os << '\n' << '\n';
                                continue;
                        }
@@ -1686,20 +1684,17 @@ void latexParagraphs(Buffer const & buf,
        }
 
        // It might be that we only have a title in this document.
-       // But if we're in a branch, this is not the end of
+       // But if we're in an inset, this is not the end of
        // the document. (There may be some other checks of this
        // kind that are needed.)
-       if (runparams.issued_title_cmd &&
-                       !runparams.already_title && !runparams.inbranch) {
+       if (runparams.need_maketitle && !runparams.have_maketitle && maintext) {
                if (tclass.titletype() == TITLE_ENVIRONMENT) {
                        os << "\\end{" << from_ascii(tclass.titlename())
                           << "}\n";
-                       runparams.issued_title_cmd = false;
                } else {
                        os << "\\" << from_ascii(tclass.titlename())
                           << "\n";
                }
-               runparams.already_title = true;
        }
 
        if (maintext && !is_child && runparams.openbtUnit)
@@ -1814,7 +1809,7 @@ pair<bool, int> switchEncoding(odocstream & os, BufferParams const & bparams,
                        // shouldn't ever reach here (see above) but avoids warning.
                        return make_pair(true, 0);
                case Encoding::inputenc: {
-                       int count = inputenc_arg.length();
+                       size_t count = inputenc_arg.length();
                        if (oldEnc.package() == Encoding::CJK &&
                            state->open_encoding_ == CJK) {
                                os << "\\end{CJK}";
@@ -1842,7 +1837,7 @@ pair<bool, int> switchEncoding(odocstream & os, BufferParams const & bparams,
                        return make_pair(true, count + 16);
                }
                case Encoding::CJK: {
-                       int count = inputenc_arg.length();
+                       size_t count = inputenc_arg.length();
                        if (oldEnc.package() == Encoding::CJK &&
                            state->open_encoding_ == CJK) {
                                os << "\\end{CJK}";