From be82f8869594a9602ae695114c3d3a72663e3860 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Uwe=20St=C3=B6hr?= Date: Tue, 11 Aug 2009 00:07:08 +0000 Subject: [PATCH] tex2lyx/preamble.cpp: revert r30103 and r30448 as discussed git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@30974 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/tex2lyx/preamble.cpp | 77 ++++++++++++++-------------------------- 1 file changed, 27 insertions(+), 50 deletions(-) diff --git a/src/tex2lyx/preamble.cpp b/src/tex2lyx/preamble.cpp index 24e24df8b7..99cad01760 100644 --- a/src/tex2lyx/preamble.cpp +++ b/src/tex2lyx/preamble.cpp @@ -104,29 +104,6 @@ const char * const known_coded_paper_margins[] = { "leftmargin", "topmargin", "rightmargin", "bottommargin", "headheight", "headsep", "footskip", "columnsep", 0}; -const char * const known_lyx_commands[] = { "binom", "cedilla", "cyrtext", -"dacute", "dgrave", "docedilla", "doogonek", "dosubhat", "dosubring", -"dosubtilde", "greektext", "guillemotleft", "guillemotright", "guilsinglleft", -"guilsinglright", "LyX", "lyxadded", "lyxaddress", "lyxarrow", "lyxcode", -"lyxdeleted", "lyxdot", "lyxgreyedout", "lyxline", "lyxlist", "lyxmathsym", -"LyXParagraphLeftIndent", "lyxrightaddress", "makenomenclature", -"mathcircumflex", "noun", "ogonek", "printnomenclature", "quotedblbase", -"quotesinglbase", "rcap", "subhat", "subring", "subtilde", "tabularnewline", -"textcyr", "textgreek", 0}; - -const char * const known_lyx_comments[] = { -"%% Binom macro for standard LaTeX users\n", -"%% For printing a cirumflex inside a formula\n", -"%% Because html converters don't know tabularnewline\n", -"%% The greyedout annotation environment\n", -"%% A simple dot to overcome graphicx limitations\n", -"%% Change tracking with ulem\n", -"% the following is useful when we have the old nomencl.sty package\n", -"%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% LyX specific LaTeX commands.\n", -"%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% User specified LaTeX commands.\n", -"%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Textclass specific LaTeX commands.\n", -0}; - // default settings ostringstream h_preamble; string h_textclass = "article"; @@ -275,7 +252,8 @@ string const scale_as_percentage(string const & scale) } -void handle_package(Parser &p, string const & name, string const & opts) +void handle_package(Parser &p, string const & name, string const & opts, + bool in_lyx_preamble) { vector options = split_options(opts); add_package(name, options); @@ -391,7 +369,7 @@ void handle_package(Parser &p, string const & name, string const & opts) else if (name == "varioref") ; // ignore this - else if (name == "verbatim") + else if (name == "verbatim") ; // ignore this else if (name == "nomencl") @@ -457,10 +435,7 @@ void handle_package(Parser &p, string const & name, string const & opts) else if (name == "jurabib") h_cite_engine = "jurabib"; - else if (name == "babel") - ; // ignore this - - else { + else if (!in_lyx_preamble) { if (options.empty()) h_preamble << "\\usepackage{" << name << "}"; else { @@ -537,7 +512,8 @@ void parse_preamble(Parser & p, ostream & os, // initialize fixed types special_columns['D'] = 3; bool is_full_document = false; - bool lyx_specific_preamble = false; + bool is_lyx_file = false; + bool in_lyx_preamble = false; // determine whether this is a full document or a fragment for inclusion while (p.good()) { @@ -560,7 +536,8 @@ void parse_preamble(Parser & p, ostream & os, // // cat codes // - if ((t.cat() == catLetter || + if (!in_lyx_preamble && + (t.cat() == catLetter || t.cat() == catSuper || t.cat() == catSub || t.cat() == catOther || @@ -572,13 +549,17 @@ void parse_preamble(Parser & p, ostream & os, t.cat() == catParameter)) h_preamble << t.character(); - else if (t.cat() == catSpace || t.cat() == catNewline) + else if (!in_lyx_preamble && + (t.cat() == catSpace || t.cat() == catNewline)) h_preamble << t.asInput(); else if (t.cat() == catComment) { // regex to parse comments (currently not used) - //static regex const islyxfile("%% LyX .* created this file"); + static regex const islyxfile("%% LyX .* created this file"); + static regex const usercommands("User specified LaTeX commands"); + string const comment = t.asInput(); + // magically switch encoding default if it looks like XeLaTeX static string const magicXeLaTeX = "% This document must be compiled with XeLaTeX "; @@ -589,8 +570,13 @@ void parse_preamble(Parser & p, ostream & os, h_inputencoding = "utf8"; } smatch sub; - // don't output LyX specific comments - if (!is_known(comment, known_lyx_comments)) + if (regex_search(comment, sub, islyxfile)) { + is_lyx_file = true; + in_lyx_preamble = true; + } else if (is_lyx_file + && regex_search(comment, sub, usercommands)) + in_lyx_preamble = false; + else if (!in_lyx_preamble) h_preamble << t.asInput(); } @@ -636,17 +622,8 @@ void parse_preamble(Parser & p, ostream & os, // remove leading "\" h_font_default_family = family.erase(0,1); } - // LyX specific commands that will automatically be set by LyX - string lyx_command = name; - // remove the leading "\" - lyx_command.erase(0,1); - lyx_specific_preamble = false; - // allow redefinitions of LyX specific commands - if (is_known(lyx_command, known_lyx_commands) - && (t.cs() != "renewcommand")) - lyx_specific_preamble = true; // only non-lyxspecific stuff - if (!lyx_specific_preamble) { + if (!in_lyx_preamble) { ostringstream ss; ss << '\\' << t.cs(); if (star) @@ -738,7 +715,8 @@ void parse_preamble(Parser & p, ostream & os, vector::const_iterator it = vecnames.begin(); vector::const_iterator end = vecnames.end(); for (; it != end; ++it) - handle_package(p, trim(*it), options); + handle_package(p, trim(*it), options, + in_lyx_preamble); } else if (t.cs() == "inputencoding") { @@ -750,13 +728,12 @@ void parse_preamble(Parser & p, ostream & os, else if (t.cs() == "newenvironment") { string const name = p.getArg('{', '}'); ostringstream ss; - // only non LyX specific stuff is output ss << "\\newenvironment{" << name << "}"; ss << p.getOpt(); ss << p.getOpt(); ss << '{' << p.verbatim_item() << '}'; ss << '{' << p.verbatim_item() << '}'; - if (!is_known(name, known_lyx_commands)) + if (!in_lyx_preamble) h_preamble << ss.str(); } @@ -764,7 +741,7 @@ void parse_preamble(Parser & p, ostream & os, string name = p.get_token().cs(); while (p.next_token().cat() != catBegin) name += p.get_token().asString(); - if (!is_known(name, known_lyx_commands)) + if (!in_lyx_preamble) h_preamble << "\\def\\" << name << '{' << p.verbatim_item() << "}"; } @@ -872,7 +849,7 @@ void parse_preamble(Parser & p, ostream & os, } } - else if (!t.cs().empty()) + else if (!t.cs().empty() && !in_lyx_preamble) h_preamble << '\\' << t.cs(); // remove the whitespace -- 2.39.5