From 8e8c214bef6a44ce256305f1c44754a8fd9f9f23 Mon Sep 17 00:00:00 2001 From: Georg Baum Date: Sun, 23 Oct 2011 20:19:49 +0000 Subject: [PATCH] Fix some bugs related to spaces in math: - Interpret argument of LFUN_SPACE_INSERT correctly - Use InsetMathSpace instead of InsetMathSpecialChar for "\ " (bug # 7728) - Use InsetMathSpace instead of InsetMathChar for ~ (bug # 7728). This fixes also the display in LyX (previously a literal ~ was displayed). Using InsetMathSpace enables also the "Insert Formatting" menu entries. No file format change is needed, since the LaTeX export is unchanged. Note that there are still some bugs related to spaces in math: #7746, #7747, #7749, #7842 git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@39947 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/BufferView.cpp | 1 - src/LyXAction.cpp | 1 + src/Paragraph.cpp | 1 - src/ParagraphMetrics.cpp | 1 - src/Text.cpp | 1 - src/Text2.cpp | 1 - src/Text3.cpp | 1 - src/frontends/qt4/GuiHSpace.cpp | 1 + src/mathed/InsetMathNest.cpp | 30 ++++++++++++++-- src/mathed/InsetMathSpace.cpp | 55 +++++++++++++++++------------ src/mathed/InsetMathSpecialChar.cpp | 19 ++-------- src/mathed/MathFactory.cpp | 4 ++- src/mathed/MathParser.cpp | 2 +- src/output_latex.cpp | 1 - 14 files changed, 67 insertions(+), 52 deletions(-) diff --git a/src/BufferView.cpp b/src/BufferView.cpp index c46d7b7d6a..fff47d0559 100644 --- a/src/BufferView.cpp +++ b/src/BufferView.cpp @@ -51,7 +51,6 @@ #include "TextMetrics.h" #include "TexRow.h" #include "TocBackend.h" -#include "VSpace.h" #include "WordLangTuple.h" #include "insets/InsetBibtex.h" diff --git a/src/LyXAction.cpp b/src/LyXAction.cpp index fc19b7064e..6a0bd86c1f 100644 --- a/src/LyXAction.cpp +++ b/src/LyXAction.cpp @@ -478,6 +478,7 @@ void LyXAction::init() * \li Params: : normal, protected, visible, thin, quad, qquad, enspace, enskip, negthinspace, negmedspace, negthickspace, hfill, hfill*, dotfill, hrulefill, hspace, hspace* \n + Only in math mode: med and thick.\n : length for custom spaces (hspace, hspace* for protected) * \li Origin: JSpitzm, 20 May 2003, Mar 17 2008 * \endvar diff --git a/src/Paragraph.cpp b/src/Paragraph.cpp index 130746c1aa..d17540d4ad 100644 --- a/src/Paragraph.cpp +++ b/src/Paragraph.cpp @@ -43,7 +43,6 @@ #include "TextClass.h" #include "TexRow.h" #include "Text.h" -#include "VSpace.h" #include "WordLangTuple.h" #include "WordList.h" diff --git a/src/ParagraphMetrics.cpp b/src/ParagraphMetrics.cpp index 95974f5705..eeccf9de37 100644 --- a/src/ParagraphMetrics.cpp +++ b/src/ParagraphMetrics.cpp @@ -34,7 +34,6 @@ #include "sgml.h" #include "TextClass.h" #include "TexRow.h" -#include "VSpace.h" #include "frontends/FontMetrics.h" diff --git a/src/Text.cpp b/src/Text.cpp index 88a1a46280..3cf1de4904 100644 --- a/src/Text.cpp +++ b/src/Text.cpp @@ -46,7 +46,6 @@ #include "ParIterator.h" #include "TextClass.h" #include "TextMetrics.h" -#include "VSpace.h" #include "WordLangTuple.h" #include "WordList.h" diff --git a/src/Text2.cpp b/src/Text2.cpp index ae82050fce..b7b9f64088 100644 --- a/src/Text2.cpp +++ b/src/Text2.cpp @@ -42,7 +42,6 @@ #include "ParagraphParameters.h" #include "TextClass.h" #include "TextMetrics.h" -#include "VSpace.h" #include "insets/InsetCollapsable.h" diff --git a/src/Text3.cpp b/src/Text3.cpp index ac74ac2948..1aee50488a 100644 --- a/src/Text3.cpp +++ b/src/Text3.cpp @@ -45,7 +45,6 @@ #include "SpellChecker.h" #include "TextClass.h" #include "TextMetrics.h" -#include "VSpace.h" #include "WordLangTuple.h" #include "frontends/Application.h" diff --git a/src/frontends/qt4/GuiHSpace.cpp b/src/frontends/qt4/GuiHSpace.cpp index e2dcfc9961..b00c0b47a0 100644 --- a/src/frontends/qt4/GuiHSpace.cpp +++ b/src/frontends/qt4/GuiHSpace.cpp @@ -42,6 +42,7 @@ GuiHSpace::GuiHSpace(bool math_mode, QWidget * parent) spacingCO->clear(); if (math_mode_) { + spacingCO->addItem(qt_("Interword Space"), toqstr("normal")); spacingCO->addItem(qt_("Thin Space"), toqstr("thinspace")); spacingCO->addItem(qt_("Medium Space"), toqstr("medspace")); spacingCO->addItem(qt_("Thick Space"), toqstr("thickspace")); diff --git a/src/mathed/InsetMathNest.cpp b/src/mathed/InsetMathNest.cpp index 2379e4f8b1..78097f5554 100644 --- a/src/mathed/InsetMathNest.cpp +++ b/src/mathed/InsetMathNest.cpp @@ -1134,10 +1134,34 @@ void InsetMathNest::doDispatch(Cursor & cur, FuncRequest & cmd) break; } - case LFUN_SPACE_INSERT: + case LFUN_SPACE_INSERT: { cur.recordUndoSelection(); - cur.insert(MathAtom(new InsetMathSpace)); + string const name = cmd.getArg(0); + if (name == "normal") + cur.insert(MathAtom(new InsetMathSpace(" ", ""))); + else if (name == "protected") + cur.insert(MathAtom(new InsetMathSpace("~", ""))); + else if (name == "thin" || name == "med" || name == "thick") + cur.insert(MathAtom(new InsetMathSpace(name + "space", ""))); + else if (name == "hfill*") + cur.insert(MathAtom(new InsetMathSpace("hspace*{\\fill}", ""))); + else if (name == "quad" || name == "qquad" || + name == "enspace" || name == "enskip" || + name == "negthinspace" || name == "negmedspace" || + name == "negthickspace" || name == "hfill") + cur.insert(MathAtom(new InsetMathSpace(name, ""))); + else if (name == "hspace" || name == "hspace*") { + string const len = cmd.getArg(1); + if (len.empty() || !isValidLength(len)) { + lyxerr << "LyX function 'space-insert " << name << "' " + "needs a valid length argument." << endl; + break; + } + cur.insert(MathAtom(new InsetMathSpace(name, len))); + } else + cur.insert(MathAtom(new InsetMathSpace)); break; + } case LFUN_MATH_SPACE: cur.recordUndoSelection(); @@ -1420,7 +1444,7 @@ bool InsetMathNest::getStatus(Cursor & cur, FuncRequest const & cmd, case LFUN_SPACE_INSERT: { docstring const & name = cmd.argument(); - if (name == "protected" || name == "normal" || name == "visible") + if (name == "visible") flag.setEnabled(false); break; } diff --git a/src/mathed/InsetMathSpace.cpp b/src/mathed/InsetMathSpace.cpp index 39b848e21c..e53c62e6ee 100644 --- a/src/mathed/InsetMathSpace.cpp +++ b/src/mathed/InsetMathSpace.cpp @@ -42,30 +42,33 @@ struct SpaceInfo { bool negative; bool visible; bool custom; + bool escape; ///< whether a backslash needs to be added for writing }; SpaceInfo space_info[] = { - // name width kind negative visible custom - {"!", 6, InsetSpaceParams::NEGTHIN, true, true, false}, - {"negthinspace", 6, InsetSpaceParams::NEGTHIN, true, true, false}, - {"negmedspace", 8, InsetSpaceParams::NEGMEDIUM, true, true, false}, - {"negthickspace", 10, InsetSpaceParams::NEGTHICK, true, true, false}, - {",", 6, InsetSpaceParams::THIN, false, true, false}, - {"thinspace", 6, InsetSpaceParams::THIN, false, true, false}, - {":", 8, InsetSpaceParams::MEDIUM, false, true, false}, - {"medspace", 8, InsetSpaceParams::MEDIUM, false, true, false}, - {";", 10, InsetSpaceParams::THICK, false, true, false}, - {"thickspace", 10, InsetSpaceParams::THICK, false, true, false}, - {"enskip", 10, InsetSpaceParams::ENSKIP, false, true, false}, - {"enspace", 10, InsetSpaceParams::ENSPACE, false, true, false}, - {"quad", 20, InsetSpaceParams::QUAD, false, true, false}, - {"qquad", 40, InsetSpaceParams::QQUAD, false, true, false}, - {"lyxnegspace", -2, InsetSpaceParams::NEGTHIN, true, false, false}, - {"lyxposspace", 2, InsetSpaceParams::THIN, false, false, false}, - {"hfill", 80, InsetSpaceParams::HFILL, false, true, false}, - {"hspace*{\\fill}", 80, InsetSpaceParams::HFILL_PROTECTED, false, true, false}, - {"hspace*", 0, InsetSpaceParams::CUSTOM_PROTECTED, false, true, true}, - {"hspace", 0, InsetSpaceParams::CUSTOM, false, true, true}, + // name width kind negative visible custom escape + {"!", 6, InsetSpaceParams::NEGTHIN, true, true, false, true}, + {"negthinspace", 6, InsetSpaceParams::NEGTHIN, true, true, false, true}, + {"negmedspace", 8, InsetSpaceParams::NEGMEDIUM, true, true, false, true}, + {"negthickspace", 10, InsetSpaceParams::NEGTHICK, true, true, false, true}, + {",", 6, InsetSpaceParams::THIN, false, true, false, true}, + {"thinspace", 6, InsetSpaceParams::THIN, false, true, false, true}, + {":", 8, InsetSpaceParams::MEDIUM, false, true, false, true}, + {"medspace", 8, InsetSpaceParams::MEDIUM, false, true, false, true}, + {";", 10, InsetSpaceParams::THICK, false, true, false, true}, + {"thickspace", 10, InsetSpaceParams::THICK, false, true, false, true}, + {"enskip", 10, InsetSpaceParams::ENSKIP, false, true, false, true}, + {"enspace", 10, InsetSpaceParams::ENSPACE, false, true, false, true}, + {"quad", 20, InsetSpaceParams::QUAD, false, true, false, true}, + {"qquad", 40, InsetSpaceParams::QQUAD, false, true, false, true}, + {"lyxnegspace", -2, InsetSpaceParams::NEGTHIN, true, false, false, true}, + {"lyxposspace", 2, InsetSpaceParams::THIN, false, false, false, true}, + {"hfill", 80, InsetSpaceParams::HFILL, false, true, false, true}, + {"hspace*{\\fill}", 80, InsetSpaceParams::HFILL_PROTECTED, false, true, false, true}, + {"hspace*", 0, InsetSpaceParams::CUSTOM_PROTECTED,false, true, true, true}, + {"hspace", 0, InsetSpaceParams::CUSTOM, false, true, true, true}, + {" ", 10, InsetSpaceParams::NORMAL, false, true, false, true}, + {"~", 10, InsetSpaceParams::PROTECTED, false, true, false, false}, }; int const nSpace = sizeof(space_info)/sizeof(SpaceInfo); @@ -246,6 +249,10 @@ void InsetMathSpace::htmlize(HtmlStream & ms) const << from_ascii(" ") << ETag("span"); break; } + case InsetSpaceParams::NORMAL: + case InsetSpaceParams::PROTECTED: + ms << from_ascii(" "); + break; default: break; } @@ -261,10 +268,12 @@ void InsetMathSpace::normalize(NormalStream & os) const void InsetMathSpace::write(WriteStream & os) const { // no MathEnsurer - all kinds work in text and math mode - os << '\\' << space_info[space_].name.c_str(); + if (space_info[space_].escape) + os << '\\'; + os << space_info[space_].name.c_str(); if (space_info[space_].custom) os << '{' << length_.asLatexString().c_str() << '}'; - else + else if (space_info[space_].escape && space_info[space_].name != " ") os.pendingSpace(true); } diff --git a/src/mathed/InsetMathSpecialChar.cpp b/src/mathed/InsetMathSpecialChar.cpp index a1f9dd40e9..94d0514b9e 100644 --- a/src/mathed/InsetMathSpecialChar.cpp +++ b/src/mathed/InsetMathSpecialChar.cpp @@ -55,11 +55,7 @@ Inset * InsetMathSpecialChar::clone() const void InsetMathSpecialChar::metrics(MetricsInfo & mi, Dimension & dim) const { - if (char_ == ' ') { - dim.asc = 4; - dim.des = 0; - dim.wid = 10; - } else if (mi.base.fontname == "mathnormal") { + if (mi.base.fontname == "mathnormal") { ShapeChanger dummy(mi.base.font, UP_SHAPE); dim = theFontMetrics(mi.base.font).dimension(char_); } else { @@ -72,18 +68,7 @@ void InsetMathSpecialChar::metrics(MetricsInfo & mi, Dimension & dim) const void InsetMathSpecialChar::draw(PainterInfo & pi, int x, int y) const { - if (char_ == ' ') { - int xp[4]; - int yp[4]; - int w = 10; - - xp[0] = ++x; yp[0] = y - 3; - xp[1] = x; yp[1] = y; - xp[2] = x + w - 2; yp[2] = y; - xp[3] = x + w - 2; yp[3] = y - 3; - - pi.pain.lines(xp, yp, 4, Color_special); - } else if (pi.base.fontname == "mathnormal") { + if (pi.base.fontname == "mathnormal") { ShapeChanger dummy(pi.base.font, UP_SHAPE); pi.draw(x, y, char_); } else { diff --git a/src/mathed/MathFactory.cpp b/src/mathed/MathFactory.cpp index 678a868eec..4c5c01a653 100644 --- a/src/mathed/MathFactory.cpp +++ b/src/mathed/MathFactory.cpp @@ -240,7 +240,7 @@ bool isSpecialChar(docstring const & name) char_type const c = name.at(0); return c == '{' || c == '}' || c == '&' || c == '$' || - c == '#' || c == '%' || c == '_' || c == ' '; + c == '#' || c == '%' || c == '_'; } @@ -505,6 +505,8 @@ MathAtom createInsetMath(docstring const & s, Buffer * buf) return MathAtom(new InsetMathEnsureMath(buf)); if (isSpecialChar(s)) return MathAtom(new InsetMathSpecialChar(s)); + if (s == " ") + return MathAtom(new InsetMathSpace(" ", "")); if (s == "regexp") return MathAtom(new InsetMathHull(buf, hullRegexp)); diff --git a/src/mathed/MathParser.cpp b/src/mathed/MathParser.cpp index d5724fdb89..be3df70d1e 100644 --- a/src/mathed/MathParser.cpp +++ b/src/mathed/MathParser.cpp @@ -914,7 +914,7 @@ bool Parser::parse1(InsetMathGrid & grid, unsigned flags, } else if (t.cat() == catActive) - cell->push_back(MathAtom(new InsetMathChar(t.character()))); + cell->push_back(MathAtom(new InsetMathSpace(string(1, t.character()), ""))); else if (t.cat() == catBegin) { MathData ar; diff --git a/src/output_latex.cpp b/src/output_latex.cpp index 0b3580875d..b29e7fde60 100644 --- a/src/output_latex.cpp +++ b/src/output_latex.cpp @@ -25,7 +25,6 @@ #include "ParagraphParameters.h" #include "TextClass.h" #include "TexRow.h" -#include "VSpace.h" #include "insets/InsetBibitem.h" #include "insets/InsetArgument.h" -- 2.39.5