]> git.lyx.org Git - lyx.git/blobdiff - src/mathed/MathMacro.cpp
* src/mathed/InsetMathHull.cpp:
[lyx.git] / src / mathed / MathMacro.cpp
index 784c725de3b8c37d60191e438cf1597b8d67472f..ca3ec2298dd5ef28de987e93d728c980267671bb 100644 (file)
 #include <config.h>
 
 #include "MathMacro.h"
-#include "MathSupport.h"
+
+#include "MathCompletionList.h"
 #include "MathExtern.h"
 #include "MathStream.h"
+#include "MathSupport.h"
 
 #include "Buffer.h"
 #include "BufferView.h"
@@ -31,6 +33,8 @@
 #include "frontends/Painter.h"
 
 #include "support/debug.h"
+#include "support/lassert.h"
+#include "support/textutils.h"
 
 #include <ostream>
 #include <vector>
@@ -129,10 +133,11 @@ Inset * MathMacro::clone() const
 
 docstring MathMacro::name() const
 {
-       if (displayMode_ == DISPLAY_UNFOLDED)
+       if (displayMode_ == DISPLAY_UNFOLDED
+           && (name_.size() > 1 || (name_[0] != '_' && name_[0] != '^')))
                return asString(cell(0));
-       else
-               return name_;
+
+       return name_;
 }
 
 
@@ -192,7 +197,7 @@ void MathMacro::metrics(MetricsInfo & mi, Dimension & dim) const
                   && editing_[mi.base.bv]) {
                // Macro will be edited in a old-style list mode here:
 
-               BOOST_ASSERT(macro_ != 0);
+               LASSERT(macro_ != 0, /**/);
                Dimension fontDim;
                FontInfo labelFont = sane_font;
                math_font_max_dim(labelFont, fontDim.asc, fontDim.des);
@@ -229,7 +234,7 @@ void MathMacro::metrics(MetricsInfo & mi, Dimension & dim) const
                dim.wid += 2;
                metricsMarkers2(dim);
        } else {
-               BOOST_ASSERT(macro_ != 0);
+               LASSERT(macro_ != 0, /**/);
 
                // metrics are computed here for the cells,
                // in the proxy we will then use the dim from the cache
@@ -333,18 +338,14 @@ void MathMacro::draw(PainterInfo & pi, int x, int y) const
        int expy = y;
 
        if (displayMode_ == DISPLAY_INIT || displayMode_ == DISPLAY_INTERACTIVE_INIT) {         
-               PainterInfo pi2(pi.base.bv, pi.pain);
-               pi2.base.font.setColor(macro_ ? Color_latex : Color_error);
-               //pi2.base.style = LM_ST_TEXT;
-               pi2.pain.text(x, y, from_ascii("\\") + name(), pi2.base.font);
+               FontSetChanger dummy(pi.base, "lyxtex");
+               pi.pain.text(x, y, from_ascii("\\") + name(), pi.base.font);
        } else if (displayMode_ == DISPLAY_UNFOLDED) {
-               PainterInfo pi2(pi.base.bv, pi.pain);
-               pi2.base.font.setColor(macro_ ? Color_latex : Color_error);
-               //pi2.base.style = LM_ST_TEXT;
-               pi2.pain.text(x, y, from_ascii("\\"), pi2.base.font);
-               x += mathed_string_width(pi2.base.font, from_ascii("\\")) + 1;
-               cell(0).draw(pi2, x, y);
-               drawMarkers(pi2, expx, expy);
+               FontSetChanger dummy(pi.base, "lyxtex");
+               pi.pain.text(x, y, from_ascii("\\"), pi.base.font);
+               x += mathed_string_width(pi.base.font, from_ascii("\\")) + 1;
+               cell(0).draw(pi, x, y);
+               drawMarkers(pi, expx, expy);
        } else if (lyxrc.macro_edit_style == LyXRC::MACRO_EDIT_LIST
                   && editing_[pi.base.bv]) {
                // Macro will be edited in a old-style list mode here:
@@ -515,6 +516,12 @@ void MathMacro::validate(LaTeXFeatures & features) const
 
        if (name() == "binom" || name() == "mathcircumflex")
                features.require(to_utf8(name()));
+       
+       // validate the cells and the definition
+       if (displayMode() == DISPLAY_NORMAL) {
+               definition_.validate(features);
+               InsetMathNest::validate(features);
+       }
 }
 
 
@@ -538,7 +545,7 @@ Inset * MathMacro::editXY(Cursor & cur, int x, int y)
 
 void MathMacro::removeArgument(Inset::pos_type pos) {
        if (displayMode_ == DISPLAY_NORMAL) {
-               BOOST_ASSERT(size_t(pos) < cells_.size());
+               LASSERT(size_t(pos) < cells_.size(), /**/);
                cells_.erase(cells_.begin() + pos);
                if (size_t(pos) < attachedArgsNum_)
                        --attachedArgsNum_;
@@ -553,7 +560,7 @@ void MathMacro::removeArgument(Inset::pos_type pos) {
 
 void MathMacro::insertArgument(Inset::pos_type pos) {
        if (displayMode_ == DISPLAY_NORMAL) {
-               BOOST_ASSERT(size_t(pos) <= cells_.size());
+               LASSERT(size_t(pos) <= cells_.size(), /**/);
                cells_.insert(cells_.begin() + pos, MathData());
                if (size_t(pos) < attachedArgsNum_)
                        ++attachedArgsNum_;
@@ -567,7 +574,7 @@ void MathMacro::insertArgument(Inset::pos_type pos) {
 
 void MathMacro::detachArguments(vector<MathData> & args, bool strip)
 {
-       BOOST_ASSERT(displayMode_ == DISPLAY_NORMAL);   
+       LASSERT(displayMode_ == DISPLAY_NORMAL, /**/);  
        args = cells_;
 
        // strip off empty cells, but not more than arity-attachedArgsNum_
@@ -588,7 +595,7 @@ void MathMacro::detachArguments(vector<MathData> & args, bool strip)
 
 void MathMacro::attachArguments(vector<MathData> const & args, size_t arity, int optionals)
 {
-       BOOST_ASSERT(displayMode_ == DISPLAY_NORMAL);
+       LASSERT(displayMode_ == DISPLAY_NORMAL, /**/);
        cells_ = args;
        attachedArgsNum_ = args.size();
        cells_.resize(arity);
@@ -646,15 +653,18 @@ bool MathMacro::folded() const
 
 void MathMacro::write(WriteStream & os) const
 {
+       MathEnsurer ensurer(os, macro_ != 0, true);
+
        // non-normal mode
        if (displayMode_ != DISPLAY_NORMAL) {
-               os << "\\" << name() << " ";
-               os.pendingSpace(true);
+               os << "\\" << name();
+               if (name().size() != 1 || isAlphaASCII(name()[0]))
+                       os.pendingSpace(true);
                return;
        }
 
        // normal mode
-       BOOST_ASSERT(macro_);
+       LASSERT(macro_, /**/);
 
        // optional arguments make macros fragile
        if (optionals_ > 0 && os.fragile())
@@ -770,7 +780,7 @@ bool MathMacro::automaticPopupCompletion() const
 }
 
 
-Inset::CompletionList const * 
+CompletionList const * 
 MathMacro::createCompletionList(Cursor const & cur) const
 {
        if (displayMode() != DISPLAY_UNFOLDED)