]> git.lyx.org Git - lyx.git/blobdiff - src/tex2lyx/Preamble.cpp
Compile fix after change 4b64aaf, replace missing constructor for C-string with docst...
[lyx.git] / src / tex2lyx / Preamble.cpp
index 01587cc02dfbbe0995cde64168ed7cca9e472129..a52927f2a2b43812494dba4db4bad095682c0dc1 100644 (file)
@@ -185,9 +185,6 @@ const char * const known_lyx_packages[] = {"amsbsy", "amsmath", "amssymb",
 "rotfloat", "splitidx", "setspace", "subscript", "textcomp", "tipa", "tipx",
 "tone", "ulem", "url", "varioref", "verbatim", "wrapfig", "xcolor", "xunicode", 0};
 
-// used for the handling of \newindex
-int index_number = 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';
@@ -460,7 +457,8 @@ string remove_braces(string const & value)
 
 
 Preamble::Preamble() : one_language(true), explicit_babel(false),
-       title_layout_found(false), h_font_cjk_set(false)
+       title_layout_found(false), index_number(0), h_font_cjk_set(false),
+       h_use_microtype(false)
 {
        //h_backgroundcolor;
        //h_boxbgcolor;
@@ -536,6 +534,7 @@ Preamble::Preamble() : one_language(true), explicit_babel(false),
        h_secnumdepth             = "3";
        h_shortcut[0]             = "idx";
        h_spacing                 = "single";
+       h_save_transient_properties = "true";
        h_suppress_date           = "false";
        h_textclass               = "article";
        h_tocdepth                = "3";
@@ -545,6 +544,7 @@ Preamble::Preamble() : one_language(true), explicit_babel(false),
        h_use_geometry            = "false";
        h_use_default_options     = "false";
        h_use_hyperref            = "false";
+       h_use_microtype           = false;
        h_use_refstyle            = false;
        h_use_packages["amsmath"]    = "1";
        h_use_packages["amssymb"]    = "0";
@@ -674,7 +674,8 @@ void Preamble::handle_geometry(vector<string> & options)
 
 
 void Preamble::handle_package(Parser &p, string const & name,
-                              string const & opts, bool in_lyx_preamble)
+                              string const & opts, bool in_lyx_preamble,
+                              bool detectEncoding)
 {
        vector<string> options = split_options(opts);
        add_package(name, options);
@@ -810,7 +811,7 @@ void Preamble::handle_package(Parser &p, string const & name,
        if (is_known(name, known_roman_fonts) || is_known(name, known_sans_fonts)
                ||      is_known(name, known_typewriter_fonts) || is_known(name, known_math_fonts))
                ;
-
+       //"On". See the enum Package in BufferParams.h if you thought that "2" should have been "42"
        else if (name == "amsmath" || name == "amssymb" || name == "cancel" ||
                 name == "esint" || name == "mhchem" || name == "mathdots" ||
                 name == "mathtools" || name == "stackrel" ||
@@ -911,9 +912,11 @@ void Preamble::handle_package(Parser &p, string const & name,
                        string const encoding = options.back();
                        Encoding const * const enc = encodings.fromLaTeXName(
                                encoding, Encoding::inputenc, true);
-                       if (!enc)
-                               cerr << "Unknown encoding " << encoding << ". Ignoring." << std::endl;
-                       else {
+                       if (!enc) {
+                               if (!detectEncoding)
+                                       cerr << "Unknown encoding " << encoding
+                                            << ". Ignoring." << std::endl;
+                       } else {
                                if (!enc->unsafe() && options.size() == 1 && one_language == true)
                                        h_inputencoding = enc->name();
                                p.setEncoding(enc->iconvName());
@@ -1012,6 +1015,13 @@ void Preamble::handle_package(Parser &p, string const & name,
                                h_options += ',' + join(options, ",");
                }
        }
+       else if (name == "microtype") {
+               //we internally support only microtype without params
+               if (options.empty())
+                       h_use_microtype = true;
+               else
+                       h_preamble << "\\usepackage[" << opts << "]{microtype}";
+       }
 
        else if (!in_lyx_preamble) {
                if (options.empty())
@@ -1028,7 +1038,7 @@ void Preamble::handle_package(Parser &p, string const & name,
        }
 
        // We need to do something with the options...
-       if (!options.empty())
+       if (!options.empty() && !detectEncoding)
                cerr << "Ignoring options '" << join(options, ",")
                     << "' of package " << name << '.' << endl;
 
@@ -1102,6 +1112,7 @@ bool Preamble::writeLyXHeader(ostream & os, bool subdoc, string const & outfiled
           << "\\lyxformat " << LYX_FORMAT << '\n'
           << "\\begin_document\n"
           << "\\begin_header\n"
+          << "\\save_transient_properties " << h_save_transient_properties << "\n"
           << "\\origin " << origin << "\n"
           << "\\textclass " << h_textclass << "\n";
        string const raw = subdoc ? empty_string() : h_preamble.str();
@@ -1160,7 +1171,8 @@ bool Preamble::writeLyXHeader(ostream & os, bool subdoc, string const & outfiled
           << ' ' << h_font_tt_scale[1] << '\n';
        if (!h_font_cjk.empty())
                os << "\\font_cjk " << h_font_cjk << '\n';
-       os << "\\graphics " << h_graphics << '\n'
+       os << "\\use_microtype " << h_use_microtype << '\n'
+          << "\\graphics " << h_graphics << '\n'
           << "\\default_output_format " << h_default_output_format << "\n"
           << "\\output_sync " << h_output_sync << "\n";
        if (h_output_sync == "1")
@@ -1261,6 +1273,13 @@ void Preamble::parse(Parser & p, string const & forceclass,
 {
        // initialize fixed types
        special_columns_['D'] = 3;
+       parse(p, forceclass, false, tc);
+}
+
+
+void Preamble::parse(Parser & p, string const & forceclass,
+                     bool detectEncoding, TeX2LyXDocClass & tc)
+{
        bool is_full_document = false;
        bool is_lyx_file = false;
        bool in_lyx_preamble = false;
@@ -1276,11 +1295,19 @@ void Preamble::parse(Parser & p, string const & forceclass,
        }
        p.reset();
 
+       if (detectEncoding && !is_full_document)
+               return;
+
        while (is_full_document && p.good()) {
+               if (detectEncoding && h_inputencoding != "auto" &&
+                   h_inputencoding != "default")
+                       return;
+
                Token const & t = p.get_token();
 
 #ifdef FILEDEBUG
-               cerr << "t: " << t << "\n";
+               if (!detectEncoding)
+                       cerr << "t: " << t << '\n';
 #endif
 
                //
@@ -1315,7 +1342,8 @@ void Preamble::parse(Parser & p, string const & forceclass,
                        if (comment.size() > magicXeLaTeX.size()
                                  && comment.substr(0, magicXeLaTeX.size()) == magicXeLaTeX
                                  && h_inputencoding == "auto") {
-                               cerr << "XeLaTeX comment found, switching to UTF8\n";
+                               if (!detectEncoding)
+                                       cerr << "XeLaTeX comment found, switching to UTF8\n";
                                h_inputencoding = "utf8";
                        }
                        smatch sub;
@@ -1411,9 +1439,9 @@ void Preamble::parse(Parser & p, string const & forceclass,
                        // check the case that a standard color is used
                        if (space.empty() && is_known(argument, known_basic_colors)) {
                                h_fontcolor = rgbcolor2code(argument);
-                               preamble.registerAutomaticallyLoadedPackage("color");
+                               registerAutomaticallyLoadedPackage("color");
                        } else if (space.empty() && argument == "document_fontcolor")
-                               preamble.registerAutomaticallyLoadedPackage("color");
+                               registerAutomaticallyLoadedPackage("color");
                        // check the case that LyX's document_fontcolor is defined
                        // but not used for \color
                        else {
@@ -1433,7 +1461,7 @@ void Preamble::parse(Parser & p, string const & forceclass,
                        if (is_known(argument, known_basic_colors)) {
                                h_backgroundcolor = rgbcolor2code(argument);
                        } else if (argument == "page_backgroundcolor")
-                               preamble.registerAutomaticallyLoadedPackage("color");
+                               registerAutomaticallyLoadedPackage("color");
                        // check the case that LyX's page_backgroundcolor is defined
                        // but not used for \pagecolor
                        else {
@@ -1658,16 +1686,18 @@ void Preamble::parse(Parser & p, string const & forceclass,
                        vector<string>::const_iterator end = vecnames.end();
                        for (; it != end; ++it)
                                handle_package(p, trimSpaceAndEol(*it), options,
-                                              in_lyx_preamble);
+                                              in_lyx_preamble, detectEncoding);
                }
 
                else if (t.cs() == "inputencoding") {
                        string const encoding = p.getArg('{','}');
                        Encoding const * const enc = encodings.fromLaTeXName(
                                encoding, Encoding::inputenc, true);
-                       if (!enc)
-                               cerr << "Unknown encoding " << encoding << ". Ignoring." << std::endl;
-                       else {
+                       if (!enc) {
+                               if (!detectEncoding)
+                                       cerr << "Unknown encoding " << encoding
+                                            << ". Ignoring." << std::endl;
+                       } else {
                                if (!enc->unsafe())
                                        h_inputencoding = enc->name();
                                p.setEncoding(enc->iconvName());
@@ -1950,6 +1980,16 @@ void Preamble::parse(Parser & p, string const & forceclass,
 }
 
 
+string Preamble::parseEncoding(Parser & p, string const & forceclass)
+{
+       TeX2LyXDocClass dummy;
+       parse(p, forceclass, true, dummy);
+       if (h_inputencoding != "auto" && h_inputencoding != "default")
+               return h_inputencoding;
+       return "";
+}
+
+
 string babel2lyx(string const & language)
 {
        char const * const * where = is_known(language, known_languages);