]> git.lyx.org Git - lyx.git/blobdiff - src/output_latex.cpp
Fix broken layout file syntax
[lyx.git] / src / output_latex.cpp
index 3178ac599f7b3f99635621dda826bef624187cf8..e680cc7aeb1bfee54a0510ca5a98a52485050981 100644 (file)
 #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>
 
@@ -45,9 +47,9 @@ namespace lyx {
 namespace {
 
 enum OpenEncoding {
-               none,
-               inputenc,
-               CJK
+       none,
+       inputenc,
+       CJK
 };
 
 static int open_encoding_ = none;
@@ -160,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()
@@ -311,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.empty())
+       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;
+                               }
+                       }
                }
        }
 }
@@ -373,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;
@@ -581,7 +615,8 @@ 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",
@@ -642,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",
@@ -764,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.