On Tue, Nov 16, 2010 at 09:19:04AM +0100, Jean-Marc Lasgouttes wrote:
> Le 16 nov. 10 à 01:09, Enrico Forestieri a écrit :
> >On Tue, Nov 16, 2010 at 12:01:59AM +0100, Jean-Marc Lasgouttes wrote:
> >>Le 15 nov. 10 à 23:48, Enrico Forestieri a écrit :
> >>>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.
> >
> >The attached patch achieves that goal. All closing \end{mathenv}
> >environments now occur at the beginning of a line without risk of
> >empty lines.
> 
> 
> One comment should be changed below:
> +     /// writes newline if next thing doesn't already begin with a newline
> +     void pendingNewline(bool newline);
> +     /// writes newline if next thing doesn't already begin with a newline
> +     bool pendingNewline() const { return pendingnewline_; }
> 
> It might be worth to define a processNewline(char) method to avoid
> the duplicated
> code like
> +     if (ws.pendingNewline() && c != '\n') {
> +             ws.os() << '\n';
> +             ws.addlines(1);
> +     }
> +     ws.pendingNewline(false);

Revised patch attached.

-- 
Enrico
Index: src/mathed/InsetMathGrid.cpp
===================================================================
--- src/mathed/InsetMathGrid.cpp        (revisione 36309)
+++ src/mathed/InsetMathGrid.cpp        (copia locale)
@@ -1067,8 +1067,7 @@ void InsetMathGrid::write(WriteStream &
                // append newline only if line wasn't completely empty
                // and the formula is not written on a single line
                bool const empty = emptyline && eol.empty();
-               if (!empty && nrows() > 1)
-                       os << "\n";
+               os.pendingNewline(!empty && nrows() > 1);
        }
        // @TODO use end_row instead of nrows() ?
        docstring const s = verboseHLine(rowinfo_[nrows()].lines_);
Index: src/mathed/InsetMathHull.cpp
===================================================================
--- src/mathed/InsetMathHull.cpp        (revisione 36309)
+++ src/mathed/InsetMathHull.cpp        (copia locale)
@@ -708,9 +708,10 @@ void InsetMathHull::header_write(WriteSt
 
        case hullEquation:
                if (n)
-                       os << "\\begin{equation" << star(n) << "}\n";
+                       os << "\\begin{equation" << star(n) << '}';
                else
-                       os << "\\[\n";
+                       os << "\\[";
+               os.pendingNewline(true);
                break;
 
        case hullEqnArray:
@@ -718,18 +719,21 @@ void InsetMathHull::header_write(WriteSt
        case hullFlAlign:
        case hullGather:
        case hullMultline:
-               os << "\\begin{" << hullName(type_) << star(n) << "}\n";
+               os << "\\begin{" << hullName(type_) << star(n) << '}';
+               os.pendingNewline(true);
                break;
 
        case hullAlignAt:
        case hullXAlignAt:
                os << "\\begin{" << hullName(type_) << star(n) << '}'
-                 << '{' << static_cast<unsigned int>((ncols() + 1)/2) << "}\n";
+                 << '{' << static_cast<unsigned int>((ncols() + 1)/2) << '}';
+               os.pendingNewline(true);
                break;
 
        case hullXXAlignAt:
                os << "\\begin{" << hullName(type_) << '}'
-                 << '{' << static_cast<unsigned int>((ncols() + 1)/2) << "}\n";
+                 << '{' << static_cast<unsigned int>((ncols() + 1)/2) << '}';
+               os.pendingNewline(true);
                break;
 
        case hullRegexp:
@@ -738,6 +742,7 @@ void InsetMathHull::header_write(WriteSt
 
        default:
                os << "\\begin{unknown" << star(n) << '}';
+               os.pendingNewline(true);
                break;
        }
 }
@@ -749,7 +754,7 @@ void InsetMathHull::footer_write(WriteSt
 
        switch(type_) {
        case hullNone:
-               os << "\n";
+               os.pendingNewline(true);
                break;
 
        case hullSimple:
@@ -758,9 +763,10 @@ void InsetMathHull::footer_write(WriteSt
 
        case hullEquation:
                if (n)
-                       os << "\\end{equation" << star(n) << "}\n";
+                       os << "\n\\end{equation" << star(n) << "}";
                else
-                       os << "\\]\n";
+                       os << "\n\\]";
+               os.pendingNewline(true);
                break;
 
        case hullEqnArray:
@@ -770,11 +776,13 @@ void InsetMathHull::footer_write(WriteSt
        case hullXAlignAt:
        case hullGather:
        case hullMultline:
-               os << "\\end{" << hullName(type_) << star(n) << "}\n";
+               os << "\n\\end{" << hullName(type_) << star(n) << "}";
+               os.pendingNewline(true);
                break;
 
        case hullXXAlignAt:
-               os << "\\end{" << hullName(type_) << "}\n";
+               os << "\n\\end{" << hullName(type_) << "}";
+               os.pendingNewline(true);
                break;
 
        case hullRegexp:
@@ -782,7 +790,8 @@ void InsetMathHull::footer_write(WriteSt
                break;
 
        default:
-               os << "\\end{unknown" << star(n) << '}';
+               os << "\n\\end{unknown" << star(n) << "}";
+               os.pendingNewline(true);
                break;
        }
 }
Index: src/mathed/MathStream.cpp
===================================================================
--- src/mathed/MathStream.cpp   (revisione 36309)
+++ src/mathed/MathStream.cpp   (copia locale)
@@ -83,8 +83,36 @@ NormalStream & operator<<(NormalStream &
 /////////////////////////////////////////////////////////////////
 
 
+namespace {
+
+void processNewline(WriteStream & ws, docstring const & s)
+{
+       if (ws.pendingNewline() && s.length() > 0) {
+               if (s[0] != '\n') {
+                       ws.os() << '\n';
+                       ws.addlines(1);
+               }
+               ws.pendingNewline(false);
+       }
+}
+
+
+void processNewline(WriteStream & ws, char_type c)
+{
+       if (ws.pendingNewline() && c != '\n') {
+               ws.os() << '\n';
+               ws.addlines(1);
+       }
+       ws.pendingNewline(false);
+}
+
+} // namespace anon
+
+
 WriteStream & operator<<(WriteStream & ws, docstring const & s)
 {
+       processNewline(ws, s);
+
        if (ws.pendingBrace()) {
                ws.os() << '}';
                ws.pendingBrace(false);
@@ -113,19 +141,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)
+         pendingnewline_(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)
+         pendingnewline_(false), textmode_(false), locked_(0), ascii_(0),
+         line_(0), encoding_(0)
 {}
 
 
 WriteStream::~WriteStream()
 {
+       if (pendingnewline_)
+               os_ << '\n';
+
        if (pendingbrace_)
                os_ << '}';
        else if (pendingspace_)
@@ -151,6 +184,12 @@ void WriteStream::pendingBrace(bool brac
 }
 
 
+void WriteStream::pendingNewline(bool newline)
+{
+       pendingnewline_ = newline;
+}
+
+
 void WriteStream::textMode(bool textmode)
 {
        textmode_ = textmode;
@@ -185,26 +224,15 @@ WriteStream & operator<<(WriteStream & w
 
 WriteStream & operator<<(WriteStream & ws, char const * s)
 {
-       if (ws.pendingBrace()) {
-               ws.os() << '}';
-               ws.pendingBrace(false);
-               ws.pendingSpace(false);
-               ws.textMode(true);
-       } else if (ws.pendingSpace() && strlen(s) > 0) {
-               if (isAlphaASCII(s[0]))
-                       ws.os() << ' ';
-               else if (s[0] == ' ' && ws.textMode())
-                       ws.os() << '\\';
-               ws.pendingSpace(false);
-       }
-       ws.os() << s;
-       ws.addlines(int(count(s, s + strlen(s), '\n')));
+       ws << from_utf8(s);
        return ws;
 }
 
 
 WriteStream & operator<<(WriteStream & ws, char c)
 {
+       processNewline(ws, c);
+
        if (ws.pendingBrace()) {
                ws.os() << '}';
                ws.pendingBrace(false);
@@ -226,6 +254,8 @@ WriteStream & operator<<(WriteStream & w
 
 WriteStream & operator<<(WriteStream & ws, int i)
 {
+       processNewline(ws, 0);
+
        if (ws.pendingBrace()) {
                ws.os() << '}';
                ws.pendingBrace(false);
@@ -238,6 +268,8 @@ WriteStream & operator<<(WriteStream & w
 
 WriteStream & operator<<(WriteStream & ws, unsigned int i)
 {
+       processNewline(ws, 0);
+
        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.pendingNewline(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 pendingNewline(bool newline);
+       /// tell whether a newline should be output
+       bool pendingNewline() const { return pendingnewline_; }
        /// 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 pendingnewline_;
        /// 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