On Mon, Nov 15, 2010 at 10:38:00PM +0100, Enrico Forestieri wrote:
> On Mon, Nov 15, 2010 at 11:20:34AM -0500, Richard Heck wrote:
> > On 11/13/2010 07:38 PM, Pavel Sanda wrote:
> > >perhaps Richard? - 6733 (LyX allows split environment...)
> > >
> > The only thing that can really be done here now is to try to get rid
> > of that extra line after \end{split}. I don't know why that is
> > there, as I don't know the math code at all. But I think it must be
> > due to changes in InsetMathGrid::write(). I posted a few things in
> > the bug report.
> 
> We already have pendingSpace() and pendingBrace() methods, so we could
> add a pendingEol() method, such that when a newline is desidered, we
> actually don't write it but do pendingEol(true).
> 
> Then, in "WriteStream & operator<<(..." we check for pendingEol and
> actually write it, unless what has to be written already begins with
> a newline. Problem solved, me thinks.

See attached patch. This solves the problem of the extra line after
\end{split}, but the code should be audited for other occurrences
of extra newlines.

I think we could also use this method for properly ending math
environments on a new line (i.e., y=x\] vs y=x\n\]) without fear
of introducing spurious empty lines.

-- 
Enrico
Index: src/mathed/MathStream.cpp
===================================================================
--- src/mathed/MathStream.cpp   (revisione 36309)
+++ src/mathed/MathStream.cpp   (copia locale)
@@ -85,6 +85,12 @@ NormalStream & operator<<(NormalStream &
 
 WriteStream & operator<<(WriteStream & ws, docstring const & s)
 {
+       if (ws.pendingEol() && (s.length() > 0 && s[0] != '\n')) {
+               ws.os() << '\n';
+               ws.addlines(1);
+       }
+       ws.pendingEol(false);
+
        if (ws.pendingBrace()) {
                ws.os() << '}';
                ws.pendingBrace(false);
@@ -113,19 +119,24 @@ WriteStream::WriteStream(odocstream & os
                        Encoding const * encoding)
        : os_(os), fragile_(fragile), firstitem_(false), latex_(latex),
          output_(output), pendingspace_(false), pendingbrace_(false),
-         textmode_(false), locked_(0), ascii_(0), line_(0), encoding_(encoding)
+         pendingeol_(false), textmode_(false), locked_(0), ascii_(0),
+         line_(0), encoding_(encoding)
 {}
 
 
 WriteStream::WriteStream(odocstream & os)
        : os_(os), fragile_(false), firstitem_(false), latex_(false),
          output_(wsDefault), pendingspace_(false), pendingbrace_(false),
-         textmode_(false), locked_(0), ascii_(0), line_(0), encoding_(0)
+         pendingeol_(false), textmode_(false), locked_(0), ascii_(0),
+         line_(0), encoding_(0)
 {}
 
 
 WriteStream::~WriteStream()
 {
+       if (pendingeol_)
+               os_ << '\n';
+
        if (pendingbrace_)
                os_ << '}';
        else if (pendingspace_)
@@ -151,6 +162,12 @@ void WriteStream::pendingBrace(bool brac
 }
 
 
+void WriteStream::pendingEol(bool eol)
+{
+       pendingeol_ = eol;
+}
+
+
 void WriteStream::textMode(bool textmode)
 {
        textmode_ = textmode;
@@ -185,6 +202,12 @@ WriteStream & operator<<(WriteStream & w
 
 WriteStream & operator<<(WriteStream & ws, char const * s)
 {
+       if (ws.pendingEol() && (strlen(s) > 0 && s[0] != '\n')) {
+               ws.os() << '\n';
+               ws.addlines(1);
+       }
+       ws.pendingEol(false);
+
        if (ws.pendingBrace()) {
                ws.os() << '}';
                ws.pendingBrace(false);
@@ -205,6 +228,12 @@ WriteStream & operator<<(WriteStream & w
 
 WriteStream & operator<<(WriteStream & ws, char c)
 {
+       if (ws.pendingEol() && c != '\n') {
+               ws.os() << '\n';
+               ws.addlines(1);
+       }
+       ws.pendingEol(false);
+
        if (ws.pendingBrace()) {
                ws.os() << '}';
                ws.pendingBrace(false);
@@ -226,6 +255,12 @@ WriteStream & operator<<(WriteStream & w
 
 WriteStream & operator<<(WriteStream & ws, int i)
 {
+       if (ws.pendingEol()) {
+               ws.os() << '\n';
+               ws.addlines(1);
+       }
+       ws.pendingEol(false);
+
        if (ws.pendingBrace()) {
                ws.os() << '}';
                ws.pendingBrace(false);
@@ -238,6 +273,12 @@ WriteStream & operator<<(WriteStream & w
 
 WriteStream & operator<<(WriteStream & ws, unsigned int i)
 {
+       if (ws.pendingEol()) {
+               ws.os() << '\n';
+               ws.addlines(1);
+       }
+       ws.pendingEol(false);
+
        if (ws.pendingBrace()) {
                ws.os() << '}';
                ws.pendingBrace(false);
Index: src/mathed/InsetMathSplit.cpp
===================================================================
--- src/mathed/InsetMathSplit.cpp       (revisione 36309)
+++ src/mathed/InsetMathSplit.cpp       (copia locale)
@@ -105,7 +105,8 @@ void InsetMathSplit::write(WriteStream &
        InsetMathGrid::write(ws);
        if (ws.fragile())
                ws << "\\protect";
-       ws << "\\end{" << name_ << "}\n";
+       ws << "\\end{" << name_ << "}";
+       ws.pendingEol(true);
 }
 
 
Index: src/mathed/MathStream.h
===================================================================
--- src/mathed/MathStream.h     (revisione 36309)
+++ src/mathed/MathStream.h     (copia locale)
@@ -67,6 +67,10 @@ public:
        void pendingBrace(bool brace);
        /// tell whether to write the closing brace of \ensuremath
        bool pendingBrace() const { return pendingbrace_; }
+       /// writes newline if next thing doesn't already begin with a newline
+       void pendingEol(bool eol);
+       /// writes newline if next thing doesn't already begin with a newline
+       bool pendingEol() const { return pendingeol_; }
        /// tell whether we are in text mode or not when producing latex code
        void textMode(bool textmode);
        /// tell whether we are in text mode or not when producing latex code
@@ -96,6 +100,8 @@ private:
        bool pendingspace_;
        /// do we have a brace pending?
        bool pendingbrace_;
+       /// do we have a newline pending?
+       bool pendingeol_;
        /// are we in text mode when producing latex code?
        bool textmode_;
        /// are we allowed to switch mode when producing latex code?

Reply via email to