> I couldn't resist.

I bet this would be the case ;-)

> Attached a patch that implements the glue length for texted. Maybe you can do
> the file format/lyx2lyx stuff.

The lyx2lyx part was more complicated than I thought. However, attached is the 
patch to go in.

regards Uwe
Index: lib/lyx2lyx/lyx_2_0.py
===================================================================
--- lib/lyx2lyx/lyx_2_0.py	(revision 30715)
+++ lib/lyx2lyx/lyx_2_0.py	(working copy)
@@ -182,43 +182,138 @@
     i = string.find("text%")
     if i > -1:
         percent = True
-        value = string[:i]
-        value = str(float(value)/100)
-        return "True," + value + "\\textwidth"
+        minus = string.rfind("-", 0, i)
+        plus = string.rfind("+", 0, i)
+        if plus == -1 and minus == -1:
+            value = string[:i]
+            value = str(float(value)/100)
+            end = string[i+5:]
+            string = value + "\\textwidth" + end
+        if plus > minus:
+            value = string[plus+1:i]
+            value = str(float(value)/100)
+            begin = string[:plus+1]
+            end = string[i+5:]
+            string = begin + value + "\\textwidth" + end
+        if plus < minus:
+            value = string[minus+1:i]
+            value = str(float(value)/100)
+            begin = string[:minus+1]
+            string = begin + value + "\\textwidth"
     i = string.find("col%")
     if i > -1:
         percent = True
-        value = string[:i]
-        value = str(float(value)/100)
-        return "True," + value + "\\columnwidth"
+        minus = string.rfind("-", 0, i)
+        plus = string.rfind("+", 0, i)
+        if plus == -1 and minus == -1:
+            value = string[:i]
+            value = str(float(value)/100)
+            end = string[i+4:]
+            string = value + "\\columnwidth" + end
+        if plus > minus:
+            value = string[plus+1:i]
+            value = str(float(value)/100)
+            begin = string[:plus+1]
+            end = string[i+4:]
+            string = begin + value + "\\columnwidth" + end
+        if plus < minus:
+            value = string[minus+1:i]
+            value = str(float(value)/100)
+            begin = string[:minus+1]
+            string = begin + value + "\\columnwidth"
     i = string.find("page%")
     if i > -1:
         percent = True
-        value = string[:i]
-        value = str(float(value)/100)
-        return "True," + value + "\\paperwidth"
+        minus = string.rfind("-", 0, i)
+        plus = string.rfind("+", 0, i)
+        if plus == -1 and minus == -1:
+            value = string[:i]
+            value = str(float(value)/100)
+            end = string[i+5:]
+            sstring = value + "\\paperwidth" + end
+        if plus > minus:
+            value = string[plus+1:i]
+            value = str(float(value)/100)
+            begin = string[:plus+1]
+            end = string[i+5:]
+            string = begin + value + "\\paperwidth" + end
+        if plus < minus:
+            value = string[minus+1:i]
+            value = str(float(value)/100)
+            begin = string[:minus+1]
+            string = begin + value + "\\paperwidth"
     i = string.find("line%")
     if i > -1:
         percent = True
-        value = string[:i]
-        value = str(float(value)/100)
-        return "True," + value + "\\linewidth"
+        minus = string.rfind("-", 0, i)
+        plus = string.rfind("+", 0, i)
+        if plus == -1 and minus == -1:
+            value = string[:i]
+            value = str(float(value)/100)
+            end = string[i+5:]
+            string = value + "\\linewidth" + end
+        if plus > minus:
+            value = string[plus+1:i]
+            value = str(float(value)/100)
+            begin = string[:plus+1]
+            end = string[i+5:]
+            string = begin + value + "\\linewidth" + end
+        if plus < minus:
+            value = string[minus+1:i]
+            value = str(float(value)/100)
+            begin = string[:minus+1]
+            string = begin + value + "\\linewidth"
     i = string.find("theight%")
     if i > -1:
         percent = True
-        value = string[:i]
-        value = str(float(value)/100)
-        return "True," + value + "\\textheight"
+        minus = string.rfind("-", 0, i)
+        plus = string.rfind("+", 0, i)
+        if plus == -1 and minus == -1:
+            value = string[:i]
+            value = str(float(value)/100)
+            end = string[i+8:]
+            string = value + "\\textheight" + end
+        if plus > minus:
+            value = string[plus+1:i]
+            value = str(float(value)/100)
+            begin = string[:plus+1]
+            end = string[i+8:]
+            string = begin + value + "\\textheight" + end
+        if plus < minus:
+            value = string[minus+1:i]
+            value = str(float(value)/100)
+            begin = string[:minus+1]
+            string = begin + value + "\\textheight"
     i = string.find("pheight%")
     if i > -1:
         percent = True
-        value = string[:i]
-        value = str(float(value)/100)
-        return "True," + value + "\\paperheight"
+        minus = string.rfind("-", 0, i)
+        plus = string.rfind("+", 0, i)
+        if plus == -1 and minus == -1:
+            value = string[:i]
+            value = str(float(value)/100)
+            end = string[i+8:]
+            string = value + "\\paperheight" + end
+        if plus > minus:
+            value = string[plus+1:i]
+            document.warning("value_plus: " + value)
+            value = str(float(value)/100)
+            begin = string[:plus+1]
+            end = string[i+8:]
+            string = begin + value + "\\paperheight" + end
+        if plus < minus:
+            value = string[minus+1:i]
+            value = str(float(value)/100)
+            begin = string[:minus+1]
+            string = begin + value + "\\paperheight"
     if percent ==  False:
-        return "False," +  string
+        return "False," + string
+    else:
+        string = string.replace("+", " plus ")
+        string = string.replace("-", " minus ")
+        return "True," + string
+        
 
-
 ####################################################################
 
 
@@ -945,6 +1040,53 @@
       j = i
 
 
+def revert_hspace_glue_lengths(document):
+    " Revert HSpace glue lengths to ERT "
+    i = 0
+    j = 0
+    while True:
+      i = find_token(document.body, "\\begin_inset space \hspace{}", i)
+      if i == -1:
+          j = find_token(document.body, "\\begin_inset space \hspace*{}", j)
+          if j == -1:
+              break
+          else:
+              star = True
+              i = j
+      else:
+          star = False
+      # only revert when a custom length was set and when
+      # it used a percent length
+      o = document.body[i+1].find("\\length")
+      if o == -1:
+          document.warning("Error: Cannot find lenght for \\hspace!")
+          break
+      # search for the beginning of the value via the space
+      k = document.body[i+1].find(" ")
+      length = document.body[i+1][k+1:]
+      # check if the length contains a plus or minus
+      l = length.find("+")
+      if l == -1:
+          l = length.find("-")
+          if l == -1:
+              break
+      # handle percent lengths
+      length = latex_length(length)
+      # latex_length returns "bool,length"
+      m = length.find(",")
+      percent = length[:m]
+      length = length[m+1:]
+      # revert the HSpace inset to ERT
+      if percent == "True":
+          if star == True:
+              subst = [put_cmd_in_ert("\\hspace*{" + length + "}")]
+          else:
+              subst = [put_cmd_in_ert("\\hspace{" + length + "}")]
+          document.body[i:i+3] = subst
+      i = i + 2
+      j = i
+
+
 ##
 # Conversion hub
 #
@@ -971,10 +1113,12 @@
            [364, []],
            [365, []],
            [366, []],
-           [367, []]
+           [367, []],
+           [368, []]
           ]
 
-revert =  [[366, [revert_percent_vspace_lengths, revert_percent_hspace_lengths]],
+revert =  [[367, [revert_hspace_glue_lengths]],
+           [366, [revert_percent_vspace_lengths, revert_percent_hspace_lengths]],
            [365, [revert_percent_skip_lengths]],
            [364, [revert_paragraph_indentation]],
            [363, [revert_branch_filename]],
Index: src/Buffer.cpp
===================================================================
--- src/Buffer.cpp	(revision 30715)
+++ src/Buffer.cpp	(working copy)
@@ -127,7 +127,7 @@
 
 // Do not remove the comment below, so we get merge conflict in
 // independent branches. Instead add your own.
-int const LYX_FORMAT = 367; // uwestoehr: percent lengths for the VSpace dialog
+int const LYX_FORMAT = 368; // spitz, uwestoehr: glue lengths for the HSpace dialog
 
 typedef map<string, bool> DepClean;
 typedef map<docstring, pair<InsetLabel const *, Buffer::References> > RefCache;
Index: src/factory.cpp
===================================================================
--- src/factory.cpp	(revision 30715)
+++ src/factory.cpp	(working copy)
@@ -387,22 +387,22 @@
 			else if (name == "hrulefill")
 				isp.kind = InsetSpaceParams::HRULEFILL;
 			else if (name == "hspace") {
-				if (len.empty() || !isValidLength(len)) {
+				if (len.empty() || !isValidGlueLength(len)) {
 					lyxerr << "LyX function 'space-insert hspace' "
 					       << "needs a valid length argument." << endl;
 					break;
 				}
 				isp.kind = InsetSpaceParams::CUSTOM;
-				isp.length = Length(len);
+				isp.length = GlueLength(len);
 			}
 			else if (name == "hspace*") {
-				if (len.empty() || !isValidLength(len)) {
+				if (len.empty() || !isValidGlueLength(len)) {
 					lyxerr << "LyX function 'space-insert hspace*' "
 					       << "needs a valid length argument." << endl;
 					break;
 				}
 				isp.kind = InsetSpaceParams::CUSTOM_PROTECTED;
-				isp.length = Length(len);
+				isp.length = GlueLength(len);
 			}
 			else {
 				lyxerr << "Wrong argument for LyX function 'space-insert'." << endl;
Index: src/frontends/qt4/GuiHSpace.cpp
===================================================================
--- src/frontends/qt4/GuiHSpace.cpp	(revision 30715)
+++ src/frontends/qt4/GuiHSpace.cpp	(working copy)
@@ -57,7 +57,10 @@
 	connect(fillPatternCO, SIGNAL(activated(int)),
 		this, SLOT(patternChanged()));
 
-	valueLE->setValidator(unsignedLengthValidator(valueLE));
+	if (params_.math)
+		valueLE->setValidator(unsignedLengthValidator(valueLE));
+	else
+		valueLE->setValidator(unsignedGlueLengthValidator(valueLE));
 
 	// Manage the ok, apply, restore and cancel/close buttons
 	bc().setPolicy(ButtonPolicy::OkApplyCancelReadOnlyPolicy);
@@ -245,9 +248,10 @@
 	keep->setChecked(protect);
 
 	Length::UNIT const default_unit = Length::defaultUnit();
-	if (item == (params.math ? 9 : 7))
-		lengthToWidgets(value, unit, params.length, default_unit);
-	else
+	if (item == (params.math ? 9 : 7)) {
+		string length = params.length.asString();
+		lengthToWidgets(value, unit, length, default_unit);
+	} else
 		lengthToWidgets(value, unit, "", default_unit);
 }
 
@@ -269,7 +273,7 @@
 		case 8: params.kind = InsetSpaceParams::QQUAD;     break;
 		case 9:
 			params.kind = InsetSpaceParams::CUSTOM;
-			params.length = Length(widgetsToLength(value, unit));
+			params.length = GlueLength(widgetsToLength(value, unit));
 			break;
 		}
 		return params;
@@ -323,7 +327,7 @@
 				params.kind = InsetSpaceParams::CUSTOM_PROTECTED;
 			else
 				params.kind = InsetSpaceParams::CUSTOM;
-			params.length = Length(widgetsToLength(value, unit));
+			params.length = GlueLength(widgetsToLength(value, unit));
 			break;
 	}
 	return params;
Index: src/insets/InsetSpace.cpp
===================================================================
--- src/insets/InsetSpace.cpp	(revision 30715)
+++ src/insets/InsetSpace.cpp	(working copy)
@@ -53,7 +53,7 @@
 }
 
 
-Length InsetSpace::length() const
+GlueLength InsetSpace::length() const
 {
 	return params_.length;
 }
@@ -130,12 +130,14 @@
 		message = _("Horizontal Fill (Down Brace)");
 		break;
 	case InsetSpaceParams::CUSTOM:
+		// FIXME unicode
 		message = support::bformat(_("Horizontal Space (%1$s)"),
-				params_.length.asDocstring());
+				from_ascii(params_.length.asString()));
 		break;
 	case InsetSpaceParams::CUSTOM_PROTECTED:
+		// FIXME unicode
 		message = support::bformat(_("Protected Horizontal Space (%1$s)"),
-				params_.length.asDocstring());
+				from_ascii(params_.length.asString()));
 		break;
 	}
 	return message;
@@ -238,7 +240,7 @@
 		case InsetSpaceParams::CUSTOM:
 		case InsetSpaceParams::CUSTOM_PROTECTED: {
 			int const w = 
-				params_.length.inPixels(mi.base.textwidth,
+				params_.length.len().inPixels(mi.base.textwidth,
 							fm.width(char_type('M')));
 			int const minw = (w < 0) ? 3 * arrow_size : 4;
 			dim.wid = max(minw, abs(w));
@@ -264,7 +266,7 @@
 {
 	Dimension const dim = dimension(*pi.base.bv);
 
-	if (isStretchableSpace() || params_.length.value() < 0) {
+	if (isStretchableSpace() || params_.length.len().value() < 0) {
 		int const asc = theFontMetrics(pi.base.font).ascent('M');
 		int const desc = theFontMetrics(pi.base.font).descent('M');
 		// Pixel height divisible by 2 for prettier fill graphics:
@@ -450,7 +452,7 @@
 		break;
 	}
 	
-	if (!length.empty())
+	if (!length.len().empty())
 		os << "\n\\length " << length.asString();
 }
 
Index: src/insets/InsetSpace.h
===================================================================
--- src/insets/InsetSpace.h	(revision 30715)
+++ src/insets/InsetSpace.h	(working copy)
@@ -81,7 +81,7 @@
 	///
 	Kind kind;
 	///
-	Length length;
+	GlueLength length;
 	/**
 	 * Whether these params are to be used in mathed.
 	 * This determines the set of valid kinds.
@@ -110,7 +110,7 @@
 	///
 	static std::string params2string(InsetSpaceParams const &);
 	///
-	Length length() const;
+	GlueLength length() const;
 
 	///
 	docstring toolTip(BufferView const & bv, int x, int y) const;
Index: src/mathed/InsetMathSpace.cpp
===================================================================
--- src/mathed/InsetMathSpace.cpp	(revision 30715)
+++ src/mathed/InsetMathSpace.cpp	(working copy)
@@ -214,7 +214,7 @@
 	LASSERT(space_info[space_].visible, /**/);
 	InsetSpaceParams isp(true);
 	isp.kind = space_info[space_].kind;
-	isp.length = length_;
+	isp.length = GlueLength(length_);
 	return InsetSpace::params2string(isp);
 }
 

Reply via email to