void MathInset::write(WriteStream & os) const
{
- os << '\\' << name().c_str() << ' ';
+ os << '\\' << name().c_str();
+ os.pendingSpace(true);
}
using std::strlen;
+WriteStream::WriteStream(ostream & os, bool fragile, bool latex)
+ : os_(os), fragile_(fragile), firstitem_(false), latex_(latex),
+ pendingspace_(false), line_(0)
+{}
+
+
+WriteStream::WriteStream(ostream & os)
+ : os_(os), fragile_(false), firstitem_(false), latex_(false),
+ pendingspace_(false), line_(0)
+{}
+
+
+void WriteStream::addlines(unsigned int n)
+{
+ line_ += n;
+}
+
+
+WriteStream & operator<<(WriteStream & ws, MathAtom const & at)
+{
+ at->write(ws);
+ return ws;
+}
+
+
+WriteStream & operator<<(WriteStream & ws, MathArray const & ar)
+{
+ write(ar, ws);
+ return ws;
+}
+
+
+WriteStream & operator<<(WriteStream & ws, char const * s)
+{
+ ws.os() << s;
+ ws.addlines(int(lyx::count(s, s + strlen(s), '\n')));
+ return ws;
+}
+
+
+WriteStream & operator<<(WriteStream & ws, char c)
+{
+ if (ws.pendingSpace()) {
+ if (isalpha(c))
+ ws.os() << ' ';
+ ws.pendingSpace(false);
+ }
+ ws.os() << c;
+ if (c == '\n')
+ ws.addlines(1);
+ return ws;
+}
+
+
+WriteStream & operator<<(WriteStream & ws, int i)
+{
+ ws.os() << i;
+ return ws;
+}
+
+
+WriteStream & operator<<(WriteStream & ws, unsigned int i)
+{
+ ws.os() << i;
+ return ws;
+}
+
+
+//////////////////////////////////////////////////////////////////////
+
+
MathMLStream::MathMLStream(ostream & os)
: os_(os), tab_(0), line_(0), lastchar_(0)
{}
//////////////////////////////////////////////////////////////////////
-
-WriteStream::WriteStream(ostream & os, bool fragile, bool latex)
- : os_(os), fragile_(fragile), latex_(latex), firstitem_(false), line_(0)
-{}
-
-
-WriteStream::WriteStream(ostream & os)
- : os_(os), fragile_(false), latex_(false), firstitem_(false), line_(0)
-{}
-
-
-void WriteStream::addlines(unsigned int n)
-{
- line_ += n;
-}
-
-
-WriteStream & operator<<(WriteStream & ws, MathAtom const & at)
-{
- at->write(ws);
- return ws;
-}
-
-
-WriteStream & operator<<(WriteStream & ws, MathArray const & ar)
-{
- write(ar, ws);
- return ws;
-}
-
-
-WriteStream & operator<<(WriteStream & ws, char const * s)
-{
- ws.os() << s;
- ws.addlines(int(lyx::count(s, s + strlen(s), '\n')));
- return ws;
-}
-
-
-WriteStream & operator<<(WriteStream & ws, char c)
-{
- ws.os() << c;
- if (c == '\n')
- ws.addlines(1);
- return ws;
-}
-
-
-WriteStream & operator<<(WriteStream & ws, int i)
-{
- ws.os() << i;
- return ws;
-}
-
-
-WriteStream & operator<<(WriteStream & ws, unsigned int i)
-{
- ws.os() << i;
- return ws;
-}
class MathInset;
class MathAtom;
+//
+// LaTeX/LyX
+//
+
+class WriteStream {
+public:
+ ///
+ WriteStream(std::ostream & os, bool fragile, bool latex);
+ ///
+ explicit WriteStream(std::ostream & os_);
+ ///
+ int line() const { return line_; }
+ ///
+ bool fragile() const { return fragile_; }
+ ///
+ bool latex() const { return latex_; }
+ ///
+ std::ostream & os() { return os_; }
+ ///
+ bool & firstitem() { return firstitem_; }
+ ///
+ void addlines(unsigned int);
+ /// writes space if next thing is isalpha()
+ void pendingSpace(bool how) { pendingspace_ = how; }
+ /// writes space if next thing is isalpha()
+ bool pendingSpace() const { return pendingspace_; }
+private:
+ ///
+ std::ostream & os_;
+ /// do we have to write \\protect sometimes
+ bool fragile_;
+ /// are we at the beginning of an MathArray?
+ bool firstitem_;
+ /// are we writing to .tex?
+ int latex_;
+ /// do we have a space pending?
+ bool pendingspace_;
+ ///
+ int line_;
+};
+
+///
+WriteStream & operator<<(WriteStream &, MathAtom const &);
+///
+WriteStream & operator<<(WriteStream &, MathArray const &);
+///
+WriteStream & operator<<(WriteStream &, char const *);
+///
+WriteStream & operator<<(WriteStream &, char);
+///
+WriteStream & operator<<(WriteStream &, int);
+///
+WriteStream & operator<<(WriteStream &, unsigned int);
+
+
//
// MathML
-//
-// LaTeX/LyX
-//
-
-class WriteStream {
-public:
- ///
- WriteStream(std::ostream & os, bool fragile, bool latex);
- ///
- explicit WriteStream(std::ostream & os_);
- ///
- int line() const { return line_; }
- ///
- bool fragile() const { return fragile_; }
- ///
- bool latex() const { return latex_; }
- ///
- std::ostream & os() { return os_; }
- ///
- bool & firstitem() { return firstitem_; }
- ///
- void addlines(unsigned int);
-private:
- ///
- std::ostream & os_;
- ///
- bool fragile_;
- /// are we writing to .tex?
- int latex_;
- /// are we at the beginning of an MathArray?
- bool firstitem_;
- ///
- int line_;
-};
-
-///
-WriteStream & operator<<(WriteStream &, MathAtom const &);
-///
-WriteStream & operator<<(WriteStream &, MathArray const &);
-///
-WriteStream & operator<<(WriteStream &, char const *);
-///
-WriteStream & operator<<(WriteStream &, char);
-///
-WriteStream & operator<<(WriteStream &, int);
-///
-WriteStream & operator<<(WriteStream &, unsigned int);
-
#endif
os << '\\' << name().c_str();
for (unsigned i = 0; i < nargs(); ++i)
os << '{' << cell(i) << '}';
- if (lock_ && !os.latex())
- os << "\\lyxlock ";
if (nargs() == 0)
- os << ' ';
+ os.pendingSpace(true);
+ if (lock_ && !os.latex()) {
+ os << "\\lyxlock";
+ os.pendingSpace(true);
+ }
}
else if (t.cat() == catLetter)
cell->push_back(MathAtom(new MathCharInset(t.character())));
- else if (t.cat() == catSpace && mode != MathInset::MATH_MODE)
- cell->push_back(MathAtom(new MathCharInset(t.character())));
+ else if (t.cat() == catSpace && mode != MathInset::MATH_MODE) {
+ if (cell->empty() || cell->back()->getChar() != ' ')
+ cell->push_back(MathAtom(new MathCharInset(t.character())));
+ }
else if (t.cat() == catNewline && mode != MathInset::MATH_MODE)
cell->push_back(MathAtom(new MathCharInset(t.character())));
void MathSpaceInset::write(WriteStream & os) const
{
- if (space_ >= 0 && space_ < nSpace)
- os << '\\' << latex_mathspace[space_] << ' ';
+ if (space_ >= 0 && space_ < nSpace) {
+ os << '\\' << latex_mathspace[space_];
+ os.pendingSpace(true);
+ }
}
void MathSymbolInset::write(WriteStream & os) const
{
- os << '\\' << name() << ' ';
+ os << '\\' << name();
+ os.pendingSpace(true);
}