]> git.lyx.org Git - lyx.git/blobdiff - src/output_latex.cpp
Fix broken layout file syntax
[lyx.git] / src / output_latex.cpp
index 0b3580875d57223f2675723cd28d5d063b2bb5c8..e680cc7aeb1bfee54a0510ca5a98a52485050981 100644 (file)
 #include "ParagraphParameters.h"
 #include "TextClass.h"
 #include "TexRow.h"
-#include "VSpace.h"
 
 #include "insets/InsetBibitem.h"
 #include "insets/InsetArgument.h"
 
 #include "support/lassert.h"
+#include "support/convert.h"
 #include "support/debug.h"
 #include "support/lstrings.h"
 
+#include <algorithm>
 #include <boost/next_prior.hpp>
 #include <list>
 
@@ -46,9 +47,9 @@ namespace lyx {
 namespace {
 
 enum OpenEncoding {
-               none,
-               inputenc,
-               CJK
+       none,
+       inputenc,
+       CJK
 };
 
 static int open_encoding_ = none;
@@ -110,16 +111,16 @@ static TeXEnvironmentData prepareEnvironment(Buffer const & buf,
                                         : priorpit->getParLanguage(bparams))
                : doc_language;
 
-       bool const use_pg = runparams.use_polyglossia;
-       string const par_lang = use_pg ?
+       bool const use_polyglossia = runparams.use_polyglossia;
+       string const par_lang = use_polyglossia ?
                getPolyglossiaEnvName(data.par_language) : data.par_language->babel();
-       string const prev_par_lang = use_pg ?
+       string const prev_par_lang = use_polyglossia ?
                getPolyglossiaEnvName(prev_par_language) : prev_par_language->babel();
-       string const doc_lang = use_pg ?
+       string const doc_lang = use_polyglossia ?
                getPolyglossiaEnvName(doc_language) : doc_language->babel();
-       string const lang_begin_command = use_pg ?
+       string const lang_begin_command = use_polyglossia ?
                "\\begin{$$lang}" : lyxrc.language_command_begin;
-       string const lang_end_command = use_pg ?
+       string const lang_end_command = use_polyglossia ?
                "\\end{$$lang}" : lyxrc.language_command_end;
 
        if (par_lang != prev_par_lang) {
@@ -141,7 +142,7 @@ static TeXEnvironmentData prepareEnvironment(Buffer const & buf,
                                lang_begin_command,
                                "$$lang",
                                par_lang));
-                       if (runparams.use_polyglossia
+                       if (use_polyglossia
                            && !data.par_language->polyglossiaOpts().empty())
                                        os << "["
                                           << from_ascii(data.par_language->polyglossiaOpts())
@@ -161,8 +162,8 @@ static TeXEnvironmentData prepareEnvironment(Buffer const & buf,
 
        if (style.isEnvironment()) {
                os << "\\begin{" << from_ascii(style.latexname()) << '}';
-               if (style.optargs != 0 || style.reqargs != 0)
-                       latexArgInsets(*pit, os, runparams, style.reqargs, style.optargs);
+               if (!style.latexargs().empty())
+                   latexArgInsets(*pit, os, runparams, style.latexargs());
                if (style.latextype == LATEX_LIST_ENVIRONMENT) {
                        os << '{'
                           << pit->params().labelWidthString()
@@ -240,7 +241,6 @@ 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);
 
                // check first if this is an higher depth paragraph.
@@ -277,7 +277,6 @@ void TeXEnvironment(Buffer const & buf, Text const & text,
                        // be two for Standard paragraphs that are depth-increment'ed to be
                        // output correctly. However, tables can also be paragraphs so
                        // don't adjust them.
-                       // 
 
                        // FIXME (Lgb): Will it ever harm to have one '\n' too
                        // many? i.e. that we sometimes will have
@@ -314,53 +313,85 @@ void TeXEnvironment(Buffer const & buf, Text const & text,
 
 
 void latexArgInsets(Paragraph const & par, otexstream & os,
-       OutputParams const & runparams, unsigned int reqargs,
-       unsigned int optargs)
+       OutputParams const & runparams, Layout::LaTeXArgMap const & latexargs)
 {
-       unsigned int totalargs = reqargs + optargs;
-       list<InsetArgument const *> ilist;
+       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) {
-                       if (ilist.size() >= totalargs) {
-                               LYXERR0("WARNING: Found extra argument inset.");
-                               continue;
-                       }
                        InsetArgument const * ins =
                                static_cast<InsetArgument const *>(it->inset);
-                       ilist.push_back(ins);
+                       if (ins->name().empty())
+                               LYXERR0("Error: Unnamed argument inset!");
+                       else {
+                               unsigned int const nr = convert<unsigned int>(ins->name());
+                               ilist[nr] = ins;
+                               Layout::LaTeXArgMap::const_iterator const lit =
+                                               latexargs.find(nr);
+                               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());
+                                       }
+                               }
+                       }
                }
        }
 
-       if (!reqargs && ilist.size() == 0)
+       unsigned int const argnr = latexargs.size();
+       if (argnr == 0)
                return;
 
-       bool const have_optional_args = ilist.size() > reqargs;
-       if (have_optional_args) {
-               unsigned int todo = ilist.size() - reqargs;
-               for (unsigned int i = 0; i < todo; ++i) {
-                       InsetArgument const * ins = ilist.front();
-                       ilist.pop_front();
-                       ins->latexArgument(os, runparams, true);
+       for (unsigned int i = 1; i <= argnr; ++i) {
+               map<int, InsetArgument const *>::const_iterator lit = ilist.find(i);
+               bool inserted = false;
+               if (lit != ilist.end()) {
+                       InsetArgument const * ins = (*lit).second;
+                       if (ins) {
+                               Layout::LaTeXArgMap::const_iterator const lait =
+                                               latexargs.find(convert<unsigned int>(ins->name()));
+                               if (lait != latexargs.end()) {
+                                       Layout::latexarg arg = (*lait).second;
+                                       docstring ldelim = arg.mandatory ?
+                                                       from_ascii("{") : from_ascii("[");
+                                       docstring rdelim = arg.mandatory ?
+                                                       from_ascii("}") : from_ascii("]");
+                                       if (!arg.ldelim.empty())
+                                               ldelim = arg.ldelim;
+                                       if (!arg.rdelim.empty())
+                                               rdelim = arg.rdelim;
+                                       ins->latexArgument(os, runparams, ldelim, rdelim);
+                                       inserted = true;
+                               }
+                       }
                }
-       }
-
-       // we should now have no more insets than there are required
-       // arguments.
-       LASSERT(ilist.size() <= reqargs, /* */);
-       if (!reqargs)
-               return;
-
-       for (unsigned int i = 0; i < reqargs; ++i) {
-               if (ilist.empty())
-                       // a required argument wasn't given, so we output {}
-                       os << "{}";
-               else {
-                       InsetArgument const * ins = ilist.front();
-                       ilist.pop_front();
-                       ins->latexArgument(os, runparams, false);
+               if (!inserted) {
+                       Layout::LaTeXArgMap::const_iterator lait = latexargs.begin();
+                       Layout::LaTeXArgMap::const_iterator const laend = latexargs.end();
+                       for (; lait != laend; ++lait) {
+                               if ((*lait).first == i) {
+                                       Layout::latexarg arg = (*lait).second;
+                                       if (arg.mandatory) {
+                                               docstring ldelim = arg.ldelim.empty() ?
+                                                               from_ascii("{") : arg.ldelim;
+                                               docstring rdelim = arg.rdelim.empty() ?
+                                                               from_ascii("}") : arg.rdelim;
+                                               os << ldelim << rdelim;
+                                       } else if (find(required.begin(), required.end(),
+                                                  convert<string>((*lait).first)) != required.end()) {
+                                               docstring ldelim = arg.ldelim.empty() ?
+                                                               from_ascii("[") : arg.ldelim;
+                                               docstring rdelim = arg.rdelim.empty() ?
+                                                               from_ascii("]") : arg.rdelim;
+                                               os << ldelim << rdelim;
+                                       } else
+                                               break;
+                               }
+                       }
                }
        }
 }
@@ -376,8 +407,8 @@ void parStartCommand(Paragraph const & par, otexstream & os,
                os << '\\' << from_ascii(style.latexname());
 
                // Separate handling of optional argument inset.
-               if (style.optargs != 0 || style.reqargs != 0)
-                       latexArgInsets(par, os, runparams, style.reqargs, style.optargs);
+               if (!style.latexargs().empty())
+                       latexArgInsets(par, os, runparams, style.latexargs());
                else
                        os << from_ascii(style.latexparam());
                break;
@@ -508,18 +539,18 @@ void TeXOnePar(Buffer const & buf,
                : outer_language;
 
 
-       bool const use_pg = runparams.use_polyglossia;
-       string const par_lang = use_pg ?
+       bool const use_polyglossia = runparams.use_polyglossia;
+       string const par_lang = use_polyglossia ?
                getPolyglossiaEnvName(par_language): par_language->babel();
-       string const prev_lang = use_pg ?
+       string const prev_lang = use_polyglossia ?
                getPolyglossiaEnvName(prev_language) : prev_language->babel();
-       string const doc_lang = use_pg ?
+       string const doc_lang = use_polyglossia ?
                getPolyglossiaEnvName(doc_language) : doc_language->babel();
-       string const outer_lang = use_pg ?
+       string const outer_lang = use_polyglossia ?
                getPolyglossiaEnvName(outer_language) : outer_language->babel();
-       string const lang_begin_command = use_pg ?
+       string const lang_begin_command = use_polyglossia ?
                "\\begin{$$lang}" : lyxrc.language_command_begin;
-       string const lang_end_command = use_pg ?
+       string const lang_end_command = use_polyglossia ?
                "\\end{$$lang}" : lyxrc.language_command_end;
 
        if (par_lang != prev_lang
@@ -552,7 +583,7 @@ void TeXOnePar(Buffer const & buf,
                        // language paragraph should appear within an \L or \R (in addition
                        // to, outside of, the normal language switch commands).
                        // This behavior is not correct for ArabTeX, though.
-                       if (!runparams.use_polyglossia
+                       if (!use_polyglossia
                            // not for ArabTeX
                                && par_language->lang() != "arabic_arabtex"
                                && outer_language->lang() != "arabic_arabtex"
@@ -584,12 +615,13 @@ 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
+                           && !par_lang.empty()) {
                                os << from_ascii(subst(
                                        lang_begin_command,
                                        "$$lang",
                                        par_lang));
-                               if (runparams.use_polyglossia
+                               if (use_polyglossia
                                    && !par_language->polyglossiaOpts().empty())
                                                os << "["
                                                  << from_ascii(par_language->polyglossiaOpts())
@@ -645,7 +677,8 @@ void TeXOnePar(Buffer const & buf,
                                        os << "%\n";
                                }
                                // With CJK, the CJK tag had to be closed first (see above)
-                               if (runparams.encoding->package() == Encoding::CJK) {
+                               if (runparams.encoding->package() == Encoding::CJK
+                                   && !par_lang.empty()) {
                                        os << from_ascii(subst(
                                                lang_begin_command,
                                                "$$lang",
@@ -755,7 +788,7 @@ void TeXOnePar(Buffer const & buf,
        // Closing the language is needed for the last paragraph; it is also
        // needed if we're within an \L or \R that we may have opened above (not
        // necessarily in this paragraph) and are about to close.
-       bool closing_rtl_ltr_environment = !runparams.use_polyglossia
+       bool closing_rtl_ltr_environment = !use_polyglossia
                // not for ArabTeX
                && (par_language->lang() != "arabic_arabtex"
                    && outer_language->lang() != "arabic_arabtex")
@@ -767,7 +800,9 @@ void TeXOnePar(Buffer const & buf,
                   || (runparams.isLastPar && par_language->babel() != outer_language->babel()));
 
        if (closing_rtl_ltr_environment
-               || (runparams.isLastPar && par_language->babel() != outer_language->babel())) {
+           || (runparams.isLastPar
+               && ((!use_polyglossia && par_language->babel() != outer_language->babel())
+                   || (use_polyglossia && par_language->polyglossia() != outer_language->polyglossia())))) {
                // Since \selectlanguage write the language to the aux file,
                // we need to reset the language at the end of footnote or
                // float.
@@ -784,7 +819,7 @@ void TeXOnePar(Buffer const & buf,
                                        (runparams.isLastPar && runparams.master_language)
                                                ? runparams.master_language
                                                : outer_language;
-                               string const current_lang = runparams.use_polyglossia
+                               string const current_lang = use_polyglossia
                                        ? getPolyglossiaEnvName(current_language)
                                        : current_language->babel();
                                if (!current_lang.empty()) {