]> git.lyx.org Git - lyx.git/blobdiff - src/tex2lyx/text.cpp
de.po
[lyx.git] / src / tex2lyx / text.cpp
index 61d7a7b3f5ff2e1f90218ce1515c7b2e017cba90..51b5e931e29dafc528b83611ac51e7ba0e717132 100644 (file)
@@ -528,6 +528,35 @@ string guessQuoteStyle(string in, bool const opening)
 }
 
 
+string const fromPolyglossiaEnvironment(string const s)
+{
+       // Since \arabic is taken by the LaTeX kernel,
+       // the Arabic polyglossia environment is upcased
+       if (s == "Arabic")
+               return "arabic";
+       else
+               return s;
+}
+
+
+string uncapitalize(string const s)
+{
+       docstring in = from_ascii(s);
+       char_type t = lowercase(s[0]);
+       in[0] = t;
+       return to_ascii(in);
+}
+
+
+bool isCapitalized(string const s)
+{
+       docstring in = from_ascii(s);
+       char_type t = uppercase(s[0]);
+       in[0] = t;
+       return to_ascii(in) == s;
+}
+
+
 } // namespace
 
 
@@ -1566,6 +1595,14 @@ void parse_unknown_environment(Parser & p, string const & name, ostream & os,
        if (specialfont)
                parent_context.new_layout_allowed = false;
        output_ert_inset(os, "\\begin{" + name + "}", parent_context);
+       // Try to handle options: Look if we have optional arguments,
+       // and if so, put the brackets in ERT.
+       while (p.hasOpt()) {
+               p.get_token(); // eat '['
+               output_ert_inset(os, "[", parent_context);
+               os << parse_text_snippet(p, FLAG_BRACK_LAST, outer, parent_context);
+               output_ert_inset(os, "]", parent_context);
+       }
        parse_text_snippet(p, os, flags, outer, parent_context);
        output_ert_inset(os, "\\end{" + name + "}", parent_context);
        if (specialfont)
@@ -1602,7 +1639,8 @@ void parse_environment(Parser & p, ostream & os, bool outer,
                        break;
                }
 
-               if (is_known(name, preamble.polyglossia_languages)) {
+               // We need to use fromPolyglossiaEnvironment die to Arabic > arabic
+               if (is_known(fromPolyglossiaEnvironment(name), preamble.polyglossia_languages)) {
                        // We must begin a new paragraph if not already done
                        if (! parent_context.atParagraphStart()) {
                                parent_context.check_end_layout(os);
@@ -1610,7 +1648,8 @@ void parse_environment(Parser & p, ostream & os, bool outer,
                        }
                        // save the language in the context so that it is
                        // handled by parse_text
-                       parent_context.font.language = preamble.polyglossia2lyx(name);
+                       parent_context.font.language =
+                               preamble.polyglossia2lyx(fromPolyglossiaEnvironment(name));
                        parse_text(p, os, FLAG_END, outer, parent_context);
                        // Just in case the environment is empty
                        parent_context.extra_stuff.erase();
@@ -1955,7 +1994,57 @@ void parse_environment(Parser & p, ostream & os, bool outer,
                        break;
                }
 
-               // This is only attempted at landscape environments that consists only
+               // This is only attempted at turn environments that consist only
+               // of a tabular (this is how tables in LyX, modulo longtables, are rotated).
+               // Thus we will fall through in other cases.
+               if (name == "turn") {
+                       // We check if the next thing is a tabular[*|x]
+                       p.pushPosition();
+                       p.getArg('{', '}');// eat turn argument
+                       bool found_end = false;
+                       bool only_table = false;
+                       bool end_table = false;
+                       p.get_token();
+                       p.get_token();
+                       string envname = p.getArg('{', '}');
+                       if (rtrim(envname, "*") == "tabular" || envname == "tabularx") {
+                               // Now we check if the table is the only content
+                               // of the turn environment
+                               string const tenv = envname;
+                               while (!found_end && !end_table && p.good()) {
+                                       envname = p.next_token().cat() == catBegin
+                                                       ? p.getArg('{', '}') : string();
+                                       Token const & t = p.get_token();
+                                       p.skip_spaces();
+                                       end_table = t.asInput() != "\\end"
+                                                       && envname == tenv;
+                                       found_end = t.asInput() == "\\end"
+                                                       && envname == "turn";
+                               }
+                               if (end_table) {
+                                       p.get_token();
+                                       envname = p.getArg('{', '}');
+                                       only_table = p.next_next_token().asInput() == "\\end"
+                                                       && envname == "turn";
+                               }
+                               if (only_table) {
+                                       p.popPosition();
+                                       string const angle = p.getArg('{', '}');
+                                       p.skip_spaces();
+                                       int const save_tablerotation = parent_context.tablerotation;
+                                       parent_context.tablerotation = convert<int>(angle);
+                                       parse_text(p, os, FLAG_END, outer, parent_context);
+                                       parent_context.tablerotation = save_tablerotation;
+                                       p.skip_spaces();
+                                       break;
+                               }
+                               // fall through
+                       }
+                       // fall through
+                       p.popPosition();
+               }
+
+               // This is only attempted at landscape environments that consist only
                // of a longtable (this is how longtables in LyX are rotated by 90 degs).
                // Other landscape environment is handled via the landscape module, thus
                // we will fall through in that case.
@@ -2211,6 +2300,9 @@ void parse_environment(Parser & p, ostream & os, bool outer,
                        }
                        switch (context.layout->latextype) {
                        case  LATEX_LIST_ENVIRONMENT:
+                               context.in_list_preamble =
+                                       !context.layout->listpreamble().empty()
+                                       && p.hasListPreamble(context.layout->itemcommand());
                                context.add_par_extra_stuff("\\labelwidthstring "
                                                            + p.verbatim_item() + '\n');
                                p.skip_spaces();
@@ -2234,11 +2326,20 @@ void parse_environment(Parser & p, ostream & os, bool outer,
                                output_arguments(os, p, outer, false, string(), context,
                                                 context.layout->latexargs());
                        else if (context.layout->latextype == LATEX_ITEM_ENVIRONMENT) {
+                               context.in_list_preamble =
+                                       !context.layout->listpreamble().empty()
+                                       && p.hasListPreamble(context.layout->itemcommand());
                                ostringstream oss;
                                output_arguments(oss, p, outer, false, string(), context,
                                                 context.layout->latexargs());
                                context.list_extra_stuff = oss.str();
                        }
+                       if (context.in_list_preamble) {
+                               // Collect the stuff between \begin and first \item
+                               context.list_preamble =
+                                       parse_text_snippet(p, FLAG_END, outer, context);
+                               context.in_list_preamble = false;
+                       }
                        parse_text(p, os, FLAG_END, outer, context);
                        if (context.layout->latextype == LATEX_ENVIRONMENT)
                                output_arguments(os, p, outer, false, "post", context,
@@ -2818,6 +2919,13 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
                debugToken(cerr, t, flags);
 #endif
 
+               if (context.in_list_preamble
+                   && p.next_token().cs() == context.layout->itemcommand()) {
+                       // We are parsing a list preamble. End before first \item.
+                       flags |= FLAG_LEAVE;
+                       context.in_list_preamble = false;
+               }
+
                if (flags & FLAG_ITEM) {
                        if (t.cat() == catSpace)
                                continue;
@@ -3262,6 +3370,16 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
                        if (context.layout->labeltype != LABEL_MANUAL)
                                output_arguments(os, p, outer, false, "item", context,
                                                 context.layout->itemargs());
+                       if (!context.list_preamble.empty()) {
+                               // We have a list preamble. Output it here.
+                               begin_inset(os, "Argument listpreamble:1");
+                               os << "\nstatus collapsed\n\n"
+                                  << "\\begin_layout Plain Layout\n\n"
+                                  << rtrim(context.list_preamble)
+                                  << "\n\\end_layout";
+                               end_inset(os);
+                               context.list_preamble.clear();
+                       }
                        if (!context.list_extra_stuff.empty()) {
                                os << context.list_extra_stuff;
                                context.list_extra_stuff.clear();
@@ -3447,7 +3565,6 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
                        continue;
                }
 
-
                // Starred section headings
                // Must attempt to parse "Section*" before "Section".
                if ((p.next_token().asInput() == "*") &&
@@ -4120,21 +4237,37 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
                        continue;
                }
 
-               // handle refstyle first to catch \eqref which can also occur
-               // without refstyle. Only recognize these commands if
+               // Handle refstyle first in order to to catch \eqref, because this
+               // can also occur without refstyle. Only recognize these commands if
                // refstyle.sty was found in the preamble (otherwise \eqref
                // and user defined ref commands could be misdetected).
-               if ((where = is_known(t.cs(), known_refstyle_commands))
+               // We uncapitalize the input in order to catch capitalized commands
+               // such as \Eqref.
+               if ((where = is_known(uncapitalize(t.cs()), known_refstyle_commands))
                     && preamble.refstyle()) {
+                       string const cap = isCapitalized(t.cs()) ? "true" : "false";
+                       string plural = "false";
+                       // Catch the plural option [s]
+                       if (p.hasOpt()) {
+                               string const opt = p.getOpt();
+                               if (opt == "[s]")
+                                       plural = "true";
+                               else {
+                                       // LyX does not yet support other optional arguments of ref commands
+                                       output_ert_inset(os, t.asInput() + opt + "{" +
+                                              p.verbatim_item() + '}', context);
+                                       continue;
+                               }
+                       }
                        context.check_layout(os);
                        begin_command_inset(os, "ref", "formatted");
                        os << "reference \"";
                        os << known_refstyle_prefixes[where - known_refstyle_commands]
                           << ":";
-                       os << convert_literate_command_inset_arg(p.verbatim_item())
+                       os << convert_literate_command_inset_arg(p.getArg('{', '}'))
                           << "\"\n";
-                       os << "plural \"false\"\n";
-                       os << "caps \"false\"\n";
+                       os << "plural \"" << plural << "\"\n";
+                       os << "caps \"" << cap << "\"\n";
                        os << "noprefix \"false\"\n";
                        end_inset(os);
                        preamble.registerAutomaticallyLoadedPackage("refstyle");
@@ -4163,8 +4296,8 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
                                        preamble.registerAutomaticallyLoadedPackage("prettyref");
                        } else {
                                // LyX does not yet support optional arguments of ref commands
-                               output_ert_inset(os, t.asInput() + '[' + opt + "]{" +
-                                      p.verbatim_item() + '}', context);
+                               output_ert_inset(os, t.asInput() + opt + "{" +
+                                                p.verbatim_item() + '}', context);
                        }
                        continue;
                }
@@ -5240,6 +5373,14 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
                                } else
                                        os << "encoding " << '"' << enc->name() << '"' << "\n";
                        }
+                       string bibfileencs;
+                       for (auto const & bf : preamble.biblatex_encodings) {
+                               if (!bibfileencs.empty())
+                                       bibfileencs += "\t";
+                               bibfileencs += bf;
+                       }
+                       if (!bibfileencs.empty())
+                               os << "file_encodings " << '"' << bibfileencs << '"' << "\n";
                        end_inset(os);
                        need_commentbib = false;
                        continue;
@@ -5996,8 +6137,18 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
                                p.get_token();  // Eat '*'
                                name += '*';
                        }
-                       if (!parse_command(name, p, os, outer, context))
+                       if (!parse_command(name, p, os, outer, context)) {
                                output_ert_inset(os, name, context);
+                               // Try to handle options of unknown commands:
+                               // Look if we have optional arguments,
+                               // and if so, put the brackets in ERT.
+                               while (p.hasOpt()) {
+                                       p.get_token(); // eat '['
+                                       output_ert_inset(os, "[", context);
+                                       os << parse_text_snippet(p, FLAG_BRACK_LAST, outer, context);
+                                       output_ert_inset(os, "]", context);
+                               }
+                       }
                }
        }
 }
@@ -6095,6 +6246,14 @@ void check_comment_bib(ostream & os, Context & context)
        }
        if (!bibfiles.empty())
                os << "bibfiles " << '"' << bibfiles << '"' << "\n";
+       string bibfileencs;
+       for (auto const & bf : preamble.biblatex_encodings) {
+               if (!bibfileencs.empty())
+                       bibfileencs += "\t";
+               bibfileencs += bf;
+       }
+       if (!bibfileencs.empty())
+               os << "file_encodings " << '"' << bibfileencs << '"' << "\n";
        end_inset(os);// Bibtex
        os << "\\end_layout\n";
        end_inset(os);// Note