]> git.lyx.org Git - lyx.git/blobdiff - src/tex2lyx/Preamble.cpp
do not hardcode packages loaded by external insets
[lyx.git] / src / tex2lyx / Preamble.cpp
index ad472f89ec75839d315bd721e9fafa252828540a..6ef3aa886bfaaf7f33975f8ef9124f719422fffd 100644 (file)
@@ -57,12 +57,12 @@ namespace {
  * please keep this in sync with known_coded_languages line by line!
  */
 const char * const known_languages[] = {"acadian", "afrikaans", "albanian",
-"american", "arabic", "arabtex", "austrian", "bahasa", "bahasai", "bahasam",
-"basque", "belarusian", "brazil", "brazilian", "breton", "british", "bulgarian",
-"canadian", "canadien", "catalan", "croatian", "czech", "danish", "dutch",
-"english", "esperanto", "estonian", "farsi", "finnish", "francais", "french",
-"frenchb", "frenchle", "frenchpro", "galician", "german", "germanb", "greek",
-"hebrew", "hungarian", "icelandic", "indon", "indonesian", "interlingua",
+"american", "arabic", "arabtex", "australian", "austrian", "bahasa", "bahasai",
+"bahasam", "basque", "belarusian", "brazil", "brazilian", "breton", "british",
+"bulgarian", "canadian", "canadien", "catalan", "croatian", "czech", "danish",
+"dutch", "english", "esperanto", "estonian", "farsi", "finnish", "francais",
+"french", "frenchb", "frenchle", "frenchpro", "galician", "german", "germanb",
+"greek", "hebrew", "hungarian", "icelandic", "indon", "indonesian", "interlingua",
 "irish", "italian", "kazakh", "latin", "latvian", "lithuanian", "lowersorbian",
 "lsorbian", "magyar", "malay", "meyalu", "mongolian", "naustrian", "newzealand",
 "ngerman", "ngermanb", "norsk", "nynorsk", "polutonikogreek", "polish",
@@ -77,14 +77,14 @@ const char * const known_languages[] = {"acadian", "afrikaans", "albanian",
  * please keep this in sync with known_languages line by line!
  */
 const char * const known_coded_languages[] = {"french", "afrikaans", "albanian",
-"american", "arabic_arabi", "arabic_arabtex", "austrian", "bahasa", "bahasa", "bahasam",
-"basque", "belarusian", "brazilian", "brazilian", "breton", "british", "bulgarian",
-"canadian", "canadien", "catalan", "croatian", "czech", "danish", "dutch",
-"english", "esperanto", "estonian", "farsi", "finnish", "french", "french",
-"french", "french", "french", "galician", "german", "german", "greek",
-"hebrew", "magyar", "icelandic", "bahasa", "bahasa", "interlingua",
+"american", "arabic_arabi", "arabic_arabtex", "australian", "austrian", "bahasa", "bahasa",
+"bahasam", "basque", "belarusian", "brazilian", "brazilian", "breton", "british",
+"bulgarian", "canadian", "canadien", "catalan", "croatian", "czech", "danish",
+"dutch", "english", "esperanto", "estonian", "farsi", "finnish", "french",
+"french", "french", "french", "french", "galician", "german", "german",
+"greek", "hebrew", "magyar", "icelandic", "bahasa", "bahasa", "interlingua",
 "irish", "italian", "kazakh", "latin", "latvian", "lithuanian", "lowersorbian",
-"lowersorbian", "magyar", "bahasam", "bahasam", "mongolian", "naustrian", "english",
+"lowersorbian", "magyar", "bahasam", "bahasam", "mongolian", "naustrian", "newzealand",
 "ngerman", "ngerman", "norsk", "nynorsk", "polutonikogreek", "polish",
 "portuguese", "portuguese", "romanian", "russian", "russian", "samin",
 "scottish", "serbian", "serbian-latin", "slovak", "slovene", "spanish",
@@ -93,9 +93,10 @@ const char * const known_coded_languages[] = {"french", "afrikaans", "albanian",
 0};
 
 /// languages with english quotes (.lyx names)
-const char * const known_english_quotes_languages[] = {"american", "bahasa",
-"bahasam", "brazilian", "canadian", "chinese-simplified", "english",
-"esperanto", "hebrew", "irish", "korean", "portuguese", "scottish", "thai", 0};
+const char * const known_english_quotes_languages[] = {"american", "australian",
+"bahasa", "bahasam", "brazilian", "canadian", "chinese-simplified", "english",
+"esperanto", "hebrew", "irish", "korean", "newzealand", "portuguese", "scottish",
+"thai", 0};
 
 /// languages with french quotes (.lyx names)
 const char * const known_french_quotes_languages[] = {"albanian",
@@ -165,6 +166,17 @@ const char * const known_basic_color_codes[] = {"#0000ff", "#000000", "#00ffff",
 const char * const known_if_3arg_commands[] = {"@ifundefined", "IfFileExists",
 0};
 
+/// packages that work only in xetex
+const char * const known_xetex_packages[] = {"arabxetex", "fixlatvian",
+"fontbook", "fontwrap", "mathspec", "philokalia", "polyglossia", "unisugar",
+"xeCJK", "xecolor", "xecyr", "xeindex", "xepersian", "xunicode", 0};
+
+/// packages that are automatically skipped if loaded by LyX
+const char * const known_lyx_packages[] = {"array", "booktabs", "calc",
+"color", "float", "graphicx", "hhline", "ifthen", "longtable", "makeidx",
+"multirow", "nomencl", "pdfpages", "rotfloat", "splitidx", "setspace",
+"subscript", "textcomp", "ulem", "url", "varioref", "verbatim", "wrapfig", 0};
+
 // codes used to remove packages that are loaded automatically by LyX.
 // Syntax: package_beg_sep<name>package_mid_sep<package loading code>package_end_sep
 const char package_beg_sep = '\001';
@@ -313,6 +325,28 @@ void Preamble::suppressDate(bool suppress)
 }
 
 
+void Preamble::registerAuthor(std::string const & name)
+{
+       Author author(from_utf8(name), empty_docstring());
+       author.setUsed(true);
+       authors_.record(author);
+       h_tracking_changes = "true";
+       h_output_changes = "true";
+}
+
+
+Author const & Preamble::getAuthor(std::string const & name) const
+{
+       Author author(from_utf8(name), empty_docstring());
+       for (AuthorList::Authors::const_iterator it = authors_.begin();
+            it != authors_.end(); it++)
+               if (*it == author)
+                       return *it;
+       static Author const dummy;
+       return dummy;
+}
+
+
 void Preamble::add_package(string const & name, vector<string> & options)
 {
        // every package inherits the global options
@@ -383,6 +417,7 @@ Preamble::Preamble() : one_language(true)
        h_html_css_as_file        = "0";
        h_html_math_output        = "0";
        h_inputencoding           = "auto";
+       h_justification           = "true";
        h_language                = "english";
        h_language_package        = "none";
        //h_listings_params;
@@ -421,6 +456,7 @@ Preamble::Preamble() : one_language(true)
        h_tocdepth                = "3";
        h_tracking_changes        = "false";
        h_use_bibtopic            = "false";
+       h_use_indices             = "false";
        h_use_geometry            = "false";
        h_use_amsmath             = "1";
        h_use_default_options     = "false";
@@ -517,6 +553,36 @@ void Preamble::handle_hyperref(vector<string> & options)
 }
 
 
+void Preamble::handle_geometry(vector<string> & options)
+{
+       h_use_geometry = "true";
+       vector<string>::iterator it;
+       // paper orientation
+       if ((it = find(options.begin(), options.end(), "landscape")) != options.end()) {
+               h_paperorientation = "landscape";
+               options.erase(it);
+       }
+       // paper size
+       // keyval version: "paper=letter"
+       string paper = process_keyval_opt(options, "paper");
+       if (!paper.empty())
+               h_papersize = paper + "paper";
+       // alternative version: "letterpaper"
+       handle_opt(options, known_paper_sizes, h_papersize);
+       delete_opt(options, known_paper_sizes);
+       // page margins
+       char const * const * margin = known_paper_margins;
+       for (; *margin; ++margin) {
+               string value = process_keyval_opt(options, *margin);
+               if (!value.empty()) {
+                       int k = margin - known_paper_margins;
+                       string name = known_coded_paper_margins[k];
+                       h_margins += '\\' + name + ' ' + value + '\n';
+               }
+       }
+}
+
+
 void Preamble::handle_package(Parser &p, string const & name,
                               string const & opts, bool in_lyx_preamble)
 {
@@ -524,6 +590,9 @@ void Preamble::handle_package(Parser &p, string const & name,
        add_package(name, options);
        string scale;
 
+       if (is_known(name, known_xetex_packages))
+               xetex = true;
+
        // roman fonts
        if (is_known(name, known_roman_fonts)) {
                h_font_roman = name;
@@ -537,10 +606,10 @@ void Preamble::handle_package(Parser &p, string const & name,
                        h_font_sc = "true";
        }
 
-       if (name == "mathpazo")
+       else if (name == "mathpazo")
                h_font_roman = "palatino";
 
-       if (name == "mathptmx")
+       else if (name == "mathptmx")
                h_font_roman = "times";
 
        // sansserif fonts
@@ -599,7 +668,6 @@ void Preamble::handle_package(Parser &p, string const & name,
                // we need to keep it in the preamble to prevent cases like bug #7861.
                if (!opts.empty()) {
                        // check if more than one option was used - used later for inputenc
-                       // in case inputenc is parsed before babel, set the encoding to auto
                        if (options.begin() != options.end() - 1)
                                one_language = false;
                        // babel takes the last language of the option of its \usepackage
@@ -634,8 +702,8 @@ void Preamble::handle_package(Parser &p, string const & name,
                // inputenc option because otherwise h_inputencoding must be
                // set to "auto" (the default encoding of the document language)
                // Therefore check for the "," character.
-               // It is also only set when there is not more then one babel
-               // language option but this is handled in the routine for babel.
+               // It is also only set when there is not more than one babel
+               // language option.
                if (opts.find(",") == string::npos && one_language == true)
                        h_inputencoding = opts;
                if (!options.empty())
@@ -646,66 +714,31 @@ void Preamble::handle_package(Parser &p, string const & name,
        else if (is_known(name, known_old_language_packages)) {
                // known language packages from the times before babel
                // if they are found and not also babel, they will be used as
-               // cutom language package
+               // custom language package
                h_language_package = "\\usepackage{" + name + "}";
        }
 
-       else if (name == "makeidx")
-               ; // ignore this
-
        else if (name == "prettyref")
-               ; // ignore this
-
-       else if (name == "varioref")
-               ; // ignore this
-
-       else if (name == "verbatim")
-               ; // ignore this
+               ; // ignore this FIXME: Use the package separator mechanism instead
 
-       else if (name == "nomencl")
-               ; // ignore this
-
-       else if (name == "textcomp")
-               ; // ignore this
-
-       else if (name == "url")
-               ; // ignore this
-
-       else if (name == "subscript")
-               ; // ignore this
+       else if (name == "lyxskak") {
+               // ignore this and its options
+               const char * const o[] = {"ps", "mover", 0};
+               delete_opt(options, o);
+       }
 
-       else if (name == "color") {
+       else if (is_known(name, known_lyx_packages) && options.empty()) {
                if (!in_lyx_preamble)
                        h_preamble << package_beg_sep << name
                                   << package_mid_sep << "\\usepackage{"
-                                  << name << '}' << package_end_sep;
+                                  << name << "}\n" << package_end_sep;
        }
 
-       else if (name == "graphicx")
-               ; // ignore this
-
-       else if (name == "setspace")
-               ; // ignore this
-
-#if 0
-       // do not ignore as long as we don't support all commands (e.g. \xout is missing)
-       // and as long as we don't support change tracking
-       else if (name == "ulem")
-               ; // ignore this
-#endif
-
        else if (name == "geometry")
-               ; // Ignore this, the geometry settings are made by the \geometry
-                 // command. This command is handled below.
-
-       else if (name == "rotfloat")
-               ; // ignore this
-
-       else if (name == "wrapfig")
-               ; // ignore this
+               handle_geometry(options);
 
        else if (name == "subfig")
-               ; // ignore this
+               ; // ignore this FIXME: Use the package separator mechanism instead
 
        else if (is_known(name, known_languages))
                h_language = name;
@@ -768,7 +801,7 @@ void Preamble::handle_if(Parser & p, bool in_lyx_preamble)
 }
 
 
-bool Preamble::writeLyXHeader(ostream & os)
+bool Preamble::writeLyXHeader(ostream & os, bool subdoc)
 {
        // translate from babel to LyX names
        h_language = babel2lyx(h_language);
@@ -800,13 +833,18 @@ bool Preamble::writeLyXHeader(ostream & os)
        else if (is_known(h_language, known_english_quotes_languages))
                h_quotes_language = "english";
 
+       if (contains(h_float_placement, "H"))
+               registerAutomaticallyLoadedPackage("float");
+       if (h_spacing != "single" && h_spacing != "default")
+               registerAutomaticallyLoadedPackage("setspace");
+
        // output the LyX file settings
        os << "#LyX file created by tex2lyx " << PACKAGE_VERSION << "\n"
           << "\\lyxformat " << LYX_FORMAT << '\n'
           << "\\begin_document\n"
           << "\\begin_header\n"
           << "\\textclass " << h_textclass << "\n";
-       string const raw = h_preamble.str();
+       string const raw = subdoc ? empty_string() : h_preamble.str();
        if (!raw.empty()) {
                os << "\\begin_preamble\n";
                for (string::size_type i = 0; i < raw.size(); ++i) {
@@ -891,8 +929,10 @@ bool Preamble::writeLyXHeader(ostream & os)
           << "\\use_undertilde " << h_use_undertilde << "\n"
           << "\\cite_engine " << h_cite_engine << "\n"
           << "\\use_bibtopic " << h_use_bibtopic << "\n"
+          << "\\use_indices " << h_use_indices << "\n"
           << "\\paperorientation " << h_paperorientation << '\n'
           << "\\suppress_date " << h_suppress_date << '\n'
+          << "\\justification " << h_justification << '\n'
           << "\\use_refstyle " << h_use_refstyle << '\n';
        if (!h_fontcolor.empty())
                os << "\\fontcolor " << h_fontcolor << '\n';
@@ -921,10 +961,9 @@ bool Preamble::writeLyXHeader(ostream & os)
           << "\\html_math_output " << h_html_math_output << "\n"
           << "\\html_css_as_file " << h_html_css_as_file << "\n"
           << "\\html_be_strict " << h_html_be_strict << "\n"
+          << authors_
           << "\\end_header\n\n"
           << "\\begin_body\n";
-       // clear preamble for subdocuments
-       h_preamble.str("");
        return true;
 }
 
@@ -1014,17 +1053,22 @@ void Preamble::parse(Parser & p, string const & forceclass,
                }
 
                else if (t.cs() == "color") {
+                       string const space =
+                               (p.hasOpt() ? p.getOpt() : string());
                        string argument = p.getArg('{', '}');
                        // check the case that a standard color is used
-                       if (is_known(argument, known_basic_colors)) {
+                       if (space.empty() && is_known(argument, known_basic_colors)) {
                                h_fontcolor = rgbcolor2code(argument);
                                preamble.registerAutomaticallyLoadedPackage("color");
-                       } else if (argument == "document_fontcolor")
+                       } else if (space.empty() && argument == "document_fontcolor")
                                preamble.registerAutomaticallyLoadedPackage("color");
                        // check the case that LyX's document_fontcolor is defined
                        // but not used for \color
                        else {
-                               h_preamble << t.asInput() << '{' << argument << '}';
+                               h_preamble << t.asInput();
+                               if (!space.empty())
+                                       h_preamble << space;
+                               h_preamble << '{' << argument << '}';
                                // the color might already be set because \definecolor
                                // is parsed before this
                                h_fontcolor = "";
@@ -1090,6 +1134,11 @@ void Preamble::parse(Parser & p, string const & forceclass,
                                h_font_default_family = family.erase(0,1);
                        }
 
+                       // remove the lyxdot definition that is re-added by LyX
+                       // if necessary
+                       if (name == "\\lyxdot")
+                               in_lyx_preamble = true;
+
                        // Add the command to the known commands
                        add_known_command(name, opt1, !opt2.empty(), from_utf8(body));
 
@@ -1153,7 +1202,7 @@ void Preamble::parse(Parser & p, string const & forceclass,
                                opts.erase(it);
                        }
                        // paper sizes
-                       // some size options are know to any document classes, other sizes
+                       // some size options are known to any document classes, other sizes
                        // are handled by the \geometry command of the geometry package
                        handle_opt(opts, known_class_paper_sizes, h_papersize);
                        delete_opt(opts, known_class_paper_sizes);
@@ -1273,32 +1322,8 @@ void Preamble::parse(Parser & p, string const & forceclass,
                }
 
                else if (t.cs() == "geometry") {
-                       h_use_geometry = "true";
                        vector<string> opts = split_options(p.getArg('{', '}'));
-                       vector<string>::iterator it;
-                       // paper orientation
-                       if ((it = find(opts.begin(), opts.end(), "landscape")) != opts.end()) {
-                               h_paperorientation = "landscape";
-                               opts.erase(it);
-                       }
-                       // paper size
-                       handle_opt(opts, known_paper_sizes, h_papersize);
-                       delete_opt(opts, known_paper_sizes);
-                       // page margins
-                       char const * const * margin = known_paper_margins;
-                       int k = -1;
-                       for (; *margin; ++margin) {
-                               k += 1;
-                               // search for the "=" in e.g. "lmargin=2cm" to get the value
-                               for(size_t i = 0; i != opts.size(); i++) {
-                                       if (opts.at(i).find(*margin) != string::npos) {
-                                               string::size_type pos = opts.at(i).find("=");
-                                               string value = opts.at(i).substr(pos + 1);
-                                               string name = known_coded_paper_margins[k];
-                                               h_margins += "\\" + name + " " + value + "\n";
-                                       }
-                               }
-                       }
+                       handle_geometry(opts);
                }
 
                else if (t.cs() == "definecolor") {