try to suppress unneeded spaces when writing

git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@5069 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
André Pönitz 2002-08-22 10:04:11 +00:00
parent 6c94685d36
commit 528a1a42d4
7 changed files with 143 additions and 117 deletions

View File

@ -209,7 +209,8 @@ void MathInset::drawT(TextPainter &, int, int) const
void MathInset::write(WriteStream & os) const void MathInset::write(WriteStream & os) const
{ {
os << '\\' << name().c_str() << ' '; os << '\\' << name().c_str();
os.pendingSpace(true);
} }

View File

@ -11,6 +11,77 @@ using std::ostream;
using std::strlen; 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) MathMLStream::MathMLStream(ostream & os)
: os_(os), tab_(0), line_(0), lastchar_(0) : os_(os), tab_(0), line_(0), lastchar_(0)
{} {}
@ -228,63 +299,3 @@ NormalStream & operator<<(NormalStream & ns, int i)
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
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;
}

View File

@ -14,6 +14,61 @@ class MathArray;
class MathInset; class MathInset;
class MathAtom; 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 // MathML
@ -189,52 +244,4 @@ OctaveStream & operator<<(OctaveStream &, int);
//
// 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 #endif

View File

@ -293,10 +293,12 @@ void MathNestInset::write(WriteStream & os) const
os << '\\' << name().c_str(); os << '\\' << name().c_str();
for (unsigned i = 0; i < nargs(); ++i) for (unsigned i = 0; i < nargs(); ++i)
os << '{' << cell(i) << '}'; os << '{' << cell(i) << '}';
if (lock_ && !os.latex())
os << "\\lyxlock ";
if (nargs() == 0) if (nargs() == 0)
os << ' '; os.pendingSpace(true);
if (lock_ && !os.latex()) {
os << "\\lyxlock";
os.pendingSpace(true);
}
} }

View File

@ -648,8 +648,10 @@ void Parser::parse1(MathGridInset & grid, unsigned flags,
else if (t.cat() == catLetter) else if (t.cat() == catLetter)
cell->push_back(MathAtom(new MathCharInset(t.character()))); cell->push_back(MathAtom(new MathCharInset(t.character())));
else if (t.cat() == catSpace && mode != MathInset::MATH_MODE) else if (t.cat() == catSpace && mode != MathInset::MATH_MODE) {
cell->push_back(MathAtom(new MathCharInset(t.character()))); if (cell->empty() || cell->back()->getChar() != ' ')
cell->push_back(MathAtom(new MathCharInset(t.character())));
}
else if (t.cat() == catNewline && mode != MathInset::MATH_MODE) else if (t.cat() == catNewline && mode != MathInset::MATH_MODE)
cell->push_back(MathAtom(new MathCharInset(t.character()))); cell->push_back(MathAtom(new MathCharInset(t.character())));

View File

@ -111,6 +111,8 @@ void MathSpaceInset::normalize(NormalStream & os) const
void MathSpaceInset::write(WriteStream & os) const void MathSpaceInset::write(WriteStream & os) const
{ {
if (space_ >= 0 && space_ < nSpace) if (space_ >= 0 && space_ < nSpace) {
os << '\\' << latex_mathspace[space_] << ' '; os << '\\' << latex_mathspace[space_];
os.pendingSpace(true);
}
} }

View File

@ -170,7 +170,8 @@ void MathSymbolInset::octavize(OctaveStream & os) const
void MathSymbolInset::write(WriteStream & os) const void MathSymbolInset::write(WriteStream & os) const
{ {
os << '\\' << name() << ' '; os << '\\' << name();
os.pendingSpace(true);
} }