Jürgen Spitzmüller wrote:

> If there are changes in
> tex2lyx that do not follow this rule (which ones?), they have to be
> reverted.

I checked the situation, and there are several changes that corrupt the file
format:

- hspace
- href
- newcommandx
- nocite

I hope, I haven't missed anything.

The applied patch restores a sane file format (247). I'll commit shortly.
The patch will be attached to bug 4379, so the changes can be reapplied
once tex2lyx is able to produce the appropriate file format.

Jürgen

Index: src/tex2lyx/tex2lyx.cpp
===================================================================
--- src/tex2lyx/tex2lyx.cpp	(Revision 24556)
+++ src/tex2lyx/tex2lyx.cpp	(Arbeitskopie)
@@ -108,7 +108,7 @@
 
 
 void add_known_command(string const & command, string const & o1,
-	unsigned optionalsNum)
+		       bool o2)
 {
 	// We have to handle the following cases:
 	// definition                      o1    o2    invocation result
@@ -117,14 +117,14 @@
 	// \newcommand{\foo}[1][]{bar #1}  "[1]" true  \foo       bar
 	// \newcommand{\foo}[1][]{bar #1}  "[1]" true  \foo[x]    bar x
 	// \newcommand{\foo}[1][x]{bar #1} "[1]" true  \foo[x]    bar x
-	// and the same with \newlyxcommand
 	unsigned int nargs = 0;
 	vector<ArgumentType> arguments;
 	string const opt1 = rtrim(ltrim(o1, "["), "]");
 	if (isStrUnsignedInt(opt1)) {
 		// The command has arguments
 		nargs = convert<unsigned int>(opt1);
-		for (unsigned int i = 0; i < optionalsNum; ++i) {
+		if (nargs > 0 && o2) {
+			// The first argument is optional
 			arguments.push_back(optional);
 			--nargs;
 		}
Index: src/tex2lyx/tex2lyx.h
===================================================================
--- src/tex2lyx/tex2lyx.h	(Revision 24556)
+++ src/tex2lyx/tex2lyx.h	(Arbeitskopie)
@@ -79,11 +79,11 @@
 /*!
  * Adds the command \p command to the list of known commands.
  * \param o1 first optional parameter to the latex command \newcommand
- * (with brackets), or the empty string if there were no optional argument.
- * \param optionalsNum is the number of optional parameters
+ * (with brackets), or the empty string if there were no optional arguments.
+ * \param o2 wether \newcommand had a second optional parameter
  */
 void add_known_command(std::string const & command, std::string const & o1,
-	unsigned optionalsNum);
+		       bool o2);
 
 // Access to environment stack
 extern std::vector<std::string> active_environments;
Index: src/tex2lyx/text.cpp
===================================================================
--- src/tex2lyx/text.cpp	(Revision 24556)
+++ src/tex2lyx/text.cpp	(Arbeitskopie)
@@ -89,7 +89,7 @@
 }
 
 
-char const * const known_latex_commands[] = { "ref", "cite", "nocite", "label",
+char const * const known_latex_commands[] = { "ref", "cite", "label",
  "index", "printindex", "pageref", "url", "vref", "vpageref", "prettyref",
  "eqref", 0 };
 
@@ -1458,12 +1458,8 @@
 			os << "\\bibitem ";
 			os << p.getOpt();
 			os << '{' << p.verbatim_item() << '}' << "\n";
-		} 
-		
-		else if(t.cs() == "global") {
-			// skip global which can appear in front of e.g. "def"
 		}
-		
+
 		else if (t.cs() == "def") {
 			context.check_layout(os);
 			eat_whitespace(p, os, context, false);
@@ -2300,32 +2296,6 @@
 			skip_braces(p); // eat {}
 		}
 
-		else if (t.cs() == "href") {
-			context.check_layout(os);
-			begin_inset(os, "CommandInset ");
-			os << t.cs() << "\n";
-			os << "LatexCommand " << t.cs() << "\n";
-			bool erase = false;
-			size_t pos;
-			// the first argument is "type:target", "type:" is optional
-			// the second argument the name
-			string href_target = subst(p.verbatim_item(), "\n", " ");
-			string href_name = subst(p.verbatim_item(), "\n", " ");
-			string href_type;
-			// serach for the ":" to divide type from target
-			if ((pos = href_target.find(":", 0)) != string::npos){
-				href_type = href_target;
-				href_type.erase(pos + 1, href_type.length());
-				href_target.erase(0, pos + 1);
-			    erase = true;											
-			}
-			os << "name " << '"' << href_name << '"' << "\n";
-			os << "target " << '"' << href_target << '"' << "\n";
-			if(erase)
-				os << "type " << '"' << href_type << '"' << "\n";
-			end_inset(os);
-		}
-
 		else if (t.cs() == "input" || t.cs() == "include"
 			 || t.cs() == "verbatiminput") {
 			string name = '\\' + t.cs();
@@ -2452,8 +2422,7 @@
 
 		else if (t.cs() == "newcommand" ||
 			 t.cs() == "providecommand" ||
-			 t.cs() == "renewcommand" ||
-			 t.cs() == "newlyxcommand") {
+			 t.cs() == "renewcommand") {
 			// these could be handled by parse_command(), but
 			// we need to call add_known_command() here.
 			string name = t.asInput();
@@ -2464,18 +2433,11 @@
 			}
 			string const command = p.verbatim_item();
 			string const opt1 = p.getOpt();
-			string optionals;
-			unsigned optionalsNum = 0;
-			while (true) {
-				string const opt = p.getFullOpt();
-				if (opt.empty())
-					break;
-				optionalsNum++;
-				optionals += opt;
-			}
-			add_known_command(command, opt1, optionalsNum);
-			string const ert = name + '{' + command + '}' + opt1
-				+ optionals + '{' + p.verbatim_item() + '}';
+			string const opt2 = p.getFullOpt();
+			add_known_command(command, opt1, !opt2.empty());
+			string const ert = name + '{' + command + '}' +
+					   opt1 + opt2 +
+					   '{' + p.verbatim_item() + '}';
 
 			context.check_layout(os);
 			begin_inset(os, "FormulaMacro");
@@ -2483,103 +2445,6 @@
 			end_inset(os);
 		}
 		
-		else if (t.cs() == "newcommandx" ||
-			 t.cs() == "renewcommandx") {
-			// \newcommandx{\foo}[2][usedefault, addprefix=\global,1=default]{#1,#2}
-
-			// get command name
-			string command;
-			if (p.next_token().cat() == catBegin)
-				command = p.verbatim_item();
-			else 
-				command = "\\" + p.get_token().cs();
-			
-			// get arity, we do not check that it fits to the given
-			// optional parameters here.
-			string const opt1 = p.getOpt();
-			
-			// get options and default values for optional parameters
-			std::vector<string> optionalValues;
-			int optionalsNum = 0;
-			if (p.next_token().character() == '[') {
-				// skip '['
-				p.get_token();
-				
-				// handle 'opt=value' options, separated by ','.
-				eat_whitespace(p, os, context, false);
-				while (p.next_token().character() != ']' && p.good()) {
-					char_type nextc = p.next_token().character();
-					if (nextc >= '1' && nextc <= '9') {
-						// optional value -> get parameter number
-						int n = p.getChar() - '0';
-
-						// skip '='
-						if (p.next_token().character() != '=') {
-							cerr << "'=' expected after numeral option of \\newcommandx" << std::endl;
-							// try to find ] or ,
-							while (p.next_token().character() != ','
-							       && p.next_token().character() != ']')
-								p.get_token();
-							continue;
-						} else
-							p.get_token();
-						
-						// get value
-						optionalValues.resize(max(size_t(n), optionalValues.size()));
-						optionalValues[n - 1].clear();
-						while (p.next_token().character() != ']'
-						       && p.next_token().character() != ',')
-							optionalValues[n - 1] += p.verbatim_item();
-						optionalsNum = max(n, optionalsNum);
-					} else if (p.next_token().cat() == catLetter) {
-						// we in fact ignore every non-optional
-						// parameters
-						
-						// get option name
-						docstring opt;
-						while (p.next_token().cat() == catLetter)
-							opt += p.getChar();
-						
-						// value?
-						eat_whitespace(p, os, context, false);
-						if (p.next_token().character() == '=') {
-							p.get_token();
-							while (p.next_token().character() != ']'
-							       && p.next_token().character() != ',')
-								p.verbatim_item();
-						}
-					} else
-						return;
-					
-					// skip komma
-					eat_whitespace(p, os, context, false);
-					if (p.next_token().character() == ',') {
-						p.getChar();
-						eat_whitespace(p, os, context, false);
-					} else if (p.next_token().character() != ']')
-						continue;
-				}
-				
-				// skip ']'
-				p.get_token();
-			}
-			
-			// concat the default values to the optionals string
-			string optionals;
-			for (unsigned i = 0; i < optionalValues.size(); ++i)
-				optionals += "[" + optionalValues[i] + "]";
-			
-			// register and output command
-			add_known_command(command, opt1, optionalsNum);
-			string const ert = "\\newcommand{" + command + '}' + opt1
-			+ optionals + '{' + p.verbatim_item() + '}';
-			
-			context.check_layout(os);
-			begin_inset(os, "FormulaMacro");
-			os << "\n" << ert;
-			end_inset(os);
-		}
-
 		else if (t.cs() == "vspace") {
 			bool starred = false;
 			if (p.next_token().asInput() == "*") {
@@ -2632,6 +2497,7 @@
 					}
 				}
 			}
+
 			if (known_unit || known_vspace) {
 				// Literal length or known variable
 				context.check_layout(os);
@@ -2659,79 +2525,6 @@
 			}
 		}
 
-		else if (t.cs() == "hspace") {
-			bool starred = false;
-			if (p.next_token().asInput() == "*") {
-				p.get_token();
-				starred = true;
-			}
-			string const length = p.verbatim_item();
-			string unit;
-			string valstring;
-			bool valid = splitLatexLength(length, valstring, unit);
-			bool known_unit = false;
-			bool fill = false;
-			double value;
-			if (valid) {
-				istringstream iss(valstring);
-				iss >> value;
-				if (value == 1.0)
-					if (unit == "\\fill") {
-						known_unit = true;
-						fill = true;
-					}
-				switch (unitFromString(unit)) {
-				case Length::SP:
-				case Length::PT:
-				case Length::BP:
-				case Length::DD:
-				case Length::MM:
-				case Length::PC:
-				case Length::CC:
-				case Length::CM:
-				case Length::IN:
-				case Length::EX:
-				case Length::EM:
-				case Length::MU:
-					known_unit = true;
-					break;
-				default:
-					break;
-				}
-			}
-			if (known_unit) {
-				// Literal length or known variable
-				context.check_layout(os);
-				begin_inset(os, "Space ");
-				if (known_unit) {
-					os << "\\hspace";
-					if (starred)
-						os << '*';
-					if (fill)
-						os << "{" + unit + "}";
-					else {
-						os << "{}\n";
-						os << "\\length " << value << unit;
-					}
-				}				
-				end_inset(os);
-			} else {
-				// LyX can't handle other length variables in Inset HSpace
-				string name = t.asInput();
-				if (starred)
-					name += '*';
-				if (valid) {
-					if (value == 1.0)
-						handle_ert(os, name + '{' + unit + '}', context);
-					else if (value == -1.0)
-						handle_ert(os, name + "{-" + unit + '}', context);
-					else
-						handle_ert(os, name + '{' + valstring + unit + '}', context);
-				} else
-					handle_ert(os, name + '{' + length + '}', context);
-			}
-		}
-
 		else {
 			//cerr << "#: " << t << " mode: " << mode << endl;
 			// heuristic: read up to next non-nested space
Index: src/tex2lyx/preamble.cpp
===================================================================
--- src/tex2lyx/preamble.cpp	(Revision 24556)
+++ src/tex2lyx/preamble.cpp	(Arbeitskopie)
@@ -486,10 +486,8 @@
 			p.setCatCode('@', catOther);
 		}
 
-		else if (t.cs() == "newcommand" 
-			 || t.cs() == "renewcommand"
-			 || t.cs() == "providecommand"
-			 || t.cs() == "newlyxcommand") {
+		else if (t.cs() == "newcommand" || t.cs() == "renewcommand"
+			    || t.cs() == "providecommand") {
 			bool star = false;
 			if (p.next_token().character() == '*') {
 				p.get_token();

Reply via email to