]> git.lyx.org Git - lyx.git/blobdiff - src/tex2lyx/text.cpp
tex2lyx/text.cpp: a fix and a clarification
[lyx.git] / src / tex2lyx / text.cpp
index 97e4919a8674f6b0ad0ee79cb275264f4963fde9..2b5656c618a2297455c4c92d7c20503bc7c3c2f5 100644 (file)
@@ -24,6 +24,8 @@
 #include "Length.h"
 #include "Preamble.h"
 
+#include "insets/ExternalTemplate.h"
+
 #include "support/lassert.h"
 #include "support/convert.h"
 #include "support/FileName.h"
@@ -567,7 +569,7 @@ void skip_spaces_braces(Parser & p, bool keepws = false)
           should be handled by this function:
           - abc \j{} xyz
           - abc \j {} xyz
-          - abc \j 
+          - abc \j
             {} xyz
           - abc \j %comment
             {} xyz
@@ -611,7 +613,7 @@ void output_command_layout(ostream & os, Parser & p, bool outer,
        while (optargs < context.layout->optargs) {
                eat_whitespace(p, os, context, false);
                if (p.next_token().cat() == catEscape ||
-                   p.next_token().character() != '[') 
+                   p.next_token().character() != '[')
                        break;
                p.get_token(); // eat '['
                begin_inset(os, "Argument\n");
@@ -928,7 +930,7 @@ void parse_box(Parser & p, ostream & os, unsigned outer_flags,
                        // the inner env
                        if (!inner_type.empty() && (inner_flags & FLAG_END))
                                active_environments.pop_back();
-       
+
                        // Ensure that the end of the outer box is parsed correctly:
                        // The opening brace has been eaten by parse_outer_box()
                        if (!outer_type.empty() && (outer_flags & FLAG_ITEM)) {
@@ -1341,6 +1343,30 @@ void parse_environment(Parser & p, ostream & os, bool outer,
                preamble.registerAutomaticallyLoadedPackage("verbatim");
        }
 
+       else if (name == "verbatim") {
+               os << "\n\\begin_layout Verbatim\n";
+               string const s = p.verbatimEnvironment("verbatim");
+               string::const_iterator it2 = s.begin();
+               for (string::const_iterator it = s.begin(), et = s.end(); it != et; ++it) {
+                       if (*it == '\\')
+                               os << "\\backslash ";
+                       else if (*it == '\n') {
+                               it2 = it + 1;
+                               // avoid adding an empty paragraph at the end
+                               // FIXME: if there are 2 consecutive spaces at the end ignore it
+                               // because LyX will re-add a \n
+                               // This hack must be removed once bug 8049 is fixed!
+                               if ((it + 1 != et) && (it + 2 != et || *it2 != '\n'))
+                                       os << "\n\\end_layout\n\\begin_layout Verbatim\n";
+                       } else 
+                               os << *it;
+               }
+               os << "\n\\end_layout\n\n";
+               p.skip_spaces();
+               // reset to Standard layout
+               os << "\n\\begin_layout Standard\n";
+       }
+
        else if (name == "lyxgreyedout") {
                eat_whitespace(p, os, parent_context, false);
                parent_context.check_layout(os);
@@ -1478,7 +1504,7 @@ void parse_environment(Parser & p, ostream & os, bool outer,
                        while (optargs < context.layout->optargs) {
                                eat_whitespace(p, os, context, false);
                                if (p.next_token().cat() == catEscape ||
-                                   p.next_token().character() != '[') 
+                                   p.next_token().character() != '[')
                                        break;
                                p.get_token(); // eat '['
                                if (need_layout) {
@@ -1899,6 +1925,28 @@ void parse_macro(Parser & p, ostream & os, Context & context)
                handle_ert(os, command + ert, context);
 }
 
+
+void registerExternalTemplatePackages(string const & name)
+{
+       external::TemplateManager const & etm = external::TemplateManager::get();
+       external::Template const * const et = etm.getTemplateByName(name);
+       if (!et)
+               return;
+       external::Template::Formats::const_iterator cit = et->formats.end();
+       if (pdflatex)
+               cit = et->formats.find("PDFLaTeX");
+       if (cit == et->formats.end())
+               // If the template has not specified a PDFLaTeX output,
+               // we try the LaTeX format.
+               cit = et->formats.find("LaTeX");
+       if (cit == et->formats.end())
+               return;
+       vector<string>::const_iterator qit = cit->second.requirements.begin();
+       vector<string>::const_iterator qend = cit->second.requirements.end();
+       for (; qit != qend; ++qit)
+               preamble.registerAutomaticallyLoadedPackage(*qit);
+}
+
 } // anonymous namespace
 
 
@@ -1911,7 +1959,7 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
        // Store the latest bibliographystyle and nocite{*} option
        // (needed for bibtex inset)
        string btprint;
-       string bibliographystyle;
+       string bibliographystyle = "default";
        bool const use_natbib = preamble.isPackageUsed("natbib");
        bool const use_jurabib = preamble.isPackageUsed("jurabib");
        string last_env;
@@ -2375,14 +2423,7 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
                                                macro = false;
                                                // register the packages that are automatically reloaded
                                                // by the Gnumeric template
-                                               // Fixme: InsetExternal.cpp should give us that list
-                                               preamble.registerAutomaticallyLoadedPackage("array");
-                                               preamble.registerAutomaticallyLoadedPackage("calc");
-                                               preamble.registerAutomaticallyLoadedPackage("color");
-                                               preamble.registerAutomaticallyLoadedPackage("hhline");
-                                               preamble.registerAutomaticallyLoadedPackage("ifthen");
-                                               preamble.registerAutomaticallyLoadedPackage("longtable");
-                                               preamble.registerAutomaticallyLoadedPackage("multirow");
+                                               registerExternalTemplatePackages("GnumericSpreadsheet");
                                        }
                                }
                        }
@@ -2719,6 +2760,7 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
                        // Warn about invalid options.
                        // Check whether some option was given twice.
                        end_inset(os);
+                       preamble.registerAutomaticallyLoadedPackage("graphicx");
                }
 
                else if (t.cs() == "footnote" ||
@@ -2960,7 +3002,7 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
                        end_inset(os);
                        skip_spaces_braces(p);
                }
-               
+
                else if (t.cs() == "lyxline") {
                        // swallow size argument (it is not used anyway)
                        p.getArg('{', '}');
@@ -3160,12 +3202,16 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
                                btprint = key;
                }
 
-               else if (t.cs() == "index") {
+               else if (t.cs() == "index" ||
+                        (t.cs() == "sindex" && preamble.use_indices() == "true")) {
                        context.check_layout(os);
-                       begin_inset(os, "Index idx\n");
-                       os << "status collapsed\n";
+                       string const kind = (t.cs() == "index") ? "idx" : p.getArg('[', ']');
+                       begin_inset(os, "Index ");
+                       os << kind << "\nstatus collapsed\n";
                        parse_text_in_inset(p, os, FLAG_ITEM, false, context, "Index");
                        end_inset(os);
+                       if (t.cs() == "sindex")
+                               preamble.registerAutomaticallyLoadedPackage("splitidx");
                }
 
                else if (t.cs() == "nomenclature") {
@@ -3182,7 +3228,7 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
                        end_inset(os);
                        preamble.registerAutomaticallyLoadedPackage("nomencl");
                }
-               
+
                else if (t.cs() == "label") {
                        context.check_layout(os);
                        begin_command_inset(os, "label", "label");
@@ -3329,7 +3375,7 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
                else if (t.cs() == "selectlanguage") {
                        context.check_layout(os);
                        // save the language for the case that a
-                       // \foreignlanguage is used 
+                       // \foreignlanguage is used
 
                        context.font.language = babel2lyx(p.verbatim_item());
                        os << "\n\\lang " << context.font.language << "\n";
@@ -3436,12 +3482,12 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
                        handle_ert(os, t.asInput(), context);
 
                // accents (see Table 6 in Comprehensive LaTeX Symbol List)
-               else if (t.cs().size() == 1 
+               else if (t.cs().size() == 1
                         && contains("\"'.=^`bcdHkrtuv~", t.cs())) {
                        context.check_layout(os);
                        // try to see whether the string is in unicodesymbols
                        docstring rem;
-                       string command = t.asInput() + "{" 
+                       string command = t.asInput() + "{"
                                + trimSpaceAndEol(p.verbatim_item())
                                + "}";
                        set<string> req;
@@ -3449,7 +3495,7 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
                                Encodings::TEXT_CMD | Encodings::MATH_CMD, rem, &req);
                        if (!s.empty()) {
                                if (!rem.empty())
-                                       cerr << "When parsing " << command 
+                                       cerr << "When parsing " << command
                                             << ", result is " << to_utf8(s)
                                             << "+" << to_utf8(rem) << endl;
                                os << to_utf8(s);
@@ -3584,6 +3630,7 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
                                begin_inset(os, "External\n");
                                os << "\ttemplate XFig\n"
                                   << "\tfilename " << outname << '\n';
+                               registerExternalTemplatePackages("XFig");
                        } else {
                                begin_command_inset(os, "include", name);
                                os << "preview false\n"
@@ -4006,6 +4053,7 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
                                os << "\tkeepAspectRatio\n";
                        end_inset(os);
                        context.check_layout(os);
+                       registerExternalTemplatePackages("PDFPages");
                }
 
                else if (t.cs() == "loadgame") {
@@ -4037,6 +4085,7 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
                        // after a \loadgame follows a \showboard
                        if (p.get_token().asInput() == "showboard")
                                p.get_token();
+                       registerExternalTemplatePackages("ChessDiagram");
                }
 
                else {
@@ -4049,7 +4098,7 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
                                                                 Encodings::TEXT_CMD, rem, &req);
                        if (!s.empty()) {
                                if (!rem.empty())
-                                       cerr << "When parsing " << t.cs() 
+                                       cerr << "When parsing " << t.cs()
                                             << ", result is " << to_utf8(s)
                                             << "+" << to_utf8(rem) << endl;
                                context.check_layout(os);