diff --git a/src/mathed/Makefile.am b/src/mathed/Makefile.am index ca542e9668..aa18fccd2b 100644 --- a/src/mathed/Makefile.am +++ b/src/mathed/Makefile.am @@ -82,6 +82,8 @@ libmathed_la_SOURCES = \ math_sizeinset.h \ math_spaceinset.C \ math_spaceinset.h \ + math_specialcharinset.C \ + math_specialcharinset.h \ math_sqrtinset.C \ math_sqrtinset.h \ math_stackrelinset.C \ diff --git a/src/mathed/math_charinset.C b/src/mathed/math_charinset.C index bfb6a7c3ab..353f60e9e5 100644 --- a/src/mathed/math_charinset.C +++ b/src/mathed/math_charinset.C @@ -14,21 +14,14 @@ MathCharInset::MathCharInset(char c) - : char_(c), code_(nativeCode(c)), needbs_(false) + : char_(c), code_(nativeCode(c)) { //lyxerr << "creating char '" << char_ << "' with code " << int(code_) << endl; } MathCharInset::MathCharInset(char c, MathTextCodes t) - : char_(c), code_((t == LM_TC_MIN) ? nativeCode(c) : t), needbs_(false) -{ -//lyxerr << "creating char '" << char_ << "' with code " << int(code_) << endl; -} - - -MathCharInset::MathCharInset(char c, MathTextCodes t, bool needbs) - : char_(c), code_((t == LM_TC_MIN) ? nativeCode(c) : t), needbs_(needbs) + : char_(c), code_((t == LM_TC_MIN) ? nativeCode(c) : t) { //lyxerr << "creating char '" << char_ << "' with code " << int(code_) << endl; } @@ -38,8 +31,6 @@ MathTextCodes MathCharInset::nativeCode(char c) const { if (isalpha(c)) return LM_TC_VAR; - if (strchr("#$%{|}", c)) - return LM_TC_SPECIAL; //if (strchr("0123456789;:!|[]().,?+/-*<>=", c) return LM_TC_CONST; } @@ -100,8 +91,6 @@ void MathCharInset::writeTrailer(std::ostream & os) const void MathCharInset::writeRaw(std::ostream & os) const { - if (needbs_) - os << "\\"; os << char_; } @@ -116,8 +105,6 @@ void MathCharInset::write(std::ostream & os, bool) const void MathCharInset::writeNormal(std::ostream & os) const { - if (needbs_) - os << "\\"; os << char_; } diff --git a/src/mathed/math_charinset.h b/src/mathed/math_charinset.h index 1822ed0e9e..37f6029e07 100644 --- a/src/mathed/math_charinset.h +++ b/src/mathed/math_charinset.h @@ -19,8 +19,6 @@ public: /// MathCharInset(char c, MathTextCodes t); /// - MathCharInset(char c, MathTextCodes t, bool needbs); - /// MathInset * clone() const; /// MathTextCodes nativeCode(char c) const; @@ -60,7 +58,5 @@ private: char char_; /// the font to be used on screen MathTextCodes code_; - /// do wee need a backslash when writing LaTeX? - bool needbs_; }; #endif diff --git a/src/mathed/math_cursor.C b/src/mathed/math_cursor.C index 5241a194a5..8b3acf6140 100644 --- a/src/mathed/math_cursor.C +++ b/src/mathed/math_cursor.C @@ -37,6 +37,7 @@ #include "math_matrixinset.h" #include "math_scriptinset.h" #include "math_spaceinset.h" +#include "math_specialcharinset.h" #include "math_parser.h" using std::endl; @@ -1281,6 +1282,11 @@ void MathCursor::interpret(string const & s) return; } + if (lastcode_ != LM_TC_TEX && strchr("#$%{|}", c)) { + insert(new MathSpecialCharInset(c)); + return; + } + if (lastcode_ == LM_TC_TEX) { if (macroName().empty()) { insert(c, LM_TC_TEX); @@ -1299,11 +1305,6 @@ void MathCursor::interpret(string const & s) return; } - if (c == '{' || c == '}') { - niceInsert(new MathCharInset(c, LM_TC_SPECIAL)); - return; - } - if (isalpha(c) && (lastcode_ == LM_TC_GREEK || lastcode_ == LM_TC_GREEK1)) { static char const greek[26] = {'A', 'B', 'X', 0 , 'E', 0 , 0 , 'H', 'I', 0 , diff --git a/src/mathed/math_defs.h b/src/mathed/math_defs.h index b09fb2d06b..3ed2b92ac6 100644 --- a/src/mathed/math_defs.h +++ b/src/mathed/math_defs.h @@ -75,8 +75,6 @@ enum MathTextCodes { LM_TC_TEXTRM, /// Math mode TeX characters ",;:{}" LM_TC_TEX, - /// Special characters "{}&#_%" - LM_TC_SPECIAL, /// Internal code when typing greek LM_TC_GREEK, /// Internal code when typing a single greek character diff --git a/src/mathed/math_factory.C b/src/mathed/math_factory.C index 342f1e50b7..4078da8ed3 100644 --- a/src/mathed/math_factory.C +++ b/src/mathed/math_factory.C @@ -15,6 +15,7 @@ #include "math_notinset.h" #include "math_rootinset.h" #include "math_spaceinset.h" +#include "math_specialcharinset.h" #include "math_sqrtinset.h" #include "math_symbolinset.h" #include "math_stackrelinset.h" @@ -30,6 +31,8 @@ MathInset * createMathInset(latexkeys const * l) return new MathBigopInset(l); case LM_TK_FUNCLIM: return new MathFuncLimInset(l); + case LM_TK_SPECIAL: + return new MathSpecialCharInset(l->id); case LM_TK_SYM: return new MathSymbolInset(l); case LM_TK_STACK: diff --git a/src/mathed/math_parser.C b/src/mathed/math_parser.C index 4eadb7bc99..168a438bdf 100644 --- a/src/mathed/math_parser.C +++ b/src/mathed/math_parser.C @@ -39,6 +39,7 @@ #include "math_rootinset.h" #include "math_sqrtinset.h" #include "math_scriptinset.h" +#include "math_specialcharinset.h" #include "math_sqrtinset.h" #include "debug.h" #include "support.h" @@ -682,18 +683,18 @@ void Parser::parse_into(MathArray & array, unsigned flags, MathTextCodes code) } else if (t.cat() == catBegin) { - array.push_back(new MathCharInset('{', LM_TC_SPECIAL)); + array.push_back(new MathCharInset('{', LM_TC_TEX)); } else if (t.cat() == catEnd) { if (flags & FLAG_BRACE_LAST) return; - array.push_back(new MathCharInset('}', LM_TC_SPECIAL)); + array.push_back(new MathCharInset('}', LM_TC_TEX)); } else if (t.cat() == catAlign) { lyxerr << "found tab unexpectedly, array: '" << array << "'\n"; - array.push_back(new MathCharInset('&', LM_TC_SPECIAL)); + array.push_back(new MathCharInset('&', LM_TC_TEX)); } else if (t.cat() == catSuper) @@ -796,7 +797,7 @@ void Parser::parse_into(MathArray & array, unsigned flags, MathTextCodes code) break; else LM_TK_SPECIAL: - array.push_back(new MathCharInset(ival_, LM_TC_SPECIAL)); + array.push_back(new MathCharInset(ival_, LM_TC_TEX)); break; */ @@ -849,7 +850,7 @@ void Parser::parse_into(MathArray & array, unsigned flags, MathTextCodes code) int pos; for (pos = array.size() - 1; pos >= 0; --pos) { MathInset * q = array.nextInset(pos); - if (q->getChar() == '{' && q->code() == LM_TC_SPECIAL) + if (q->getChar() == '{') break; } if (pos >= 0) { diff --git a/src/mathed/math_specialcharinset.C b/src/mathed/math_specialcharinset.C new file mode 100644 index 0000000000..4fe90e80fc --- /dev/null +++ b/src/mathed/math_specialcharinset.C @@ -0,0 +1,62 @@ +#ifdef __GNUG__ +#pragma implementation +#endif + +#include "math_specialcharinset.h" +#include "support/LOstream.h" +#include "support.h" + + +MathSpecialCharInset::MathSpecialCharInset(char c) + : char_(c) +{} + + +MathInset * MathSpecialCharInset::clone() const +{ + return new MathSpecialCharInset(*this); +} + + +int MathSpecialCharInset::ascent() const +{ + return mathed_char_ascent(LM_TC_CONST, size(), char_); +} + + +int MathSpecialCharInset::descent() const +{ + return mathed_char_descent(LM_TC_CONST, size(), char_); +} + + +int MathSpecialCharInset::width() const +{ + return mathed_char_width(LM_TC_CONST, size(), char_); +} + + +void MathSpecialCharInset::metrics(MathStyles st) const +{ + size_ = st; +} + + +void MathSpecialCharInset::draw(Painter & pain, int x, int y) const +{ + xo(x); + yo(y); + drawChar(pain, LM_TC_CONST, size_, x, y, char_); +} + + +void MathSpecialCharInset::write(std::ostream & os, bool) const +{ + os << "\\" << char_; +} + + +void MathSpecialCharInset::writeNormal(std::ostream & os) const +{ + os << "\\" << char_; +} diff --git a/src/mathed/math_specialcharinset.h b/src/mathed/math_specialcharinset.h new file mode 100644 index 0000000000..6582868f6d --- /dev/null +++ b/src/mathed/math_specialcharinset.h @@ -0,0 +1,41 @@ +// -*- C++ -*- +#ifndef MATH_SPECIALCHARINSET_H +#define MATH_SPECIALCHARINSET_H + +#include "math_inset.h" + +#ifdef __GNUG__ +#pragma interface +#endif + +/** An inset for characters like {, #, and $ that need to be escaped + when written out, but can be inserted by a single keystroke + \author André Pönitz + */ + +class MathSpecialCharInset : public MathInset { +public: + /// + explicit MathSpecialCharInset(char c); + /// + MathInset * clone() const; + /// + void metrics(MathStyles st) const; + /// + void draw(Painter &, int x, int y) const; + /// + void write(std::ostream &, bool fragile) const; + /// + void writeNormal(std::ostream &) const; + /// + int ascent() const; + /// + int descent() const; + /// + int width() const; + +private: + /// the character + char char_; +}; +#endif diff --git a/src/mathed/support.C b/src/mathed/support.C index 87d0af49ba..91528084e2 100644 --- a/src/mathed/support.C +++ b/src/mathed/support.C @@ -165,7 +165,6 @@ LyXFont WhichFont(MathTextCodes type, MathStyles size) f = Math_Fonts[5]; break; - case LM_TC_SPECIAL: //f = Math_Fonts[0]; break; case LM_TC_TEXTRM: case LM_TC_CONST: case LM_TC_TEX: