#include "MathMacroTemplate.h"
#include "DocIterator.h"
+#include "LaTeXFeatures.h"
#include "InsetMathBrace.h"
#include "InsetMathChar.h"
#include "InsetMathSqrt.h"
#include "BufferView.h"
#include "Color.h"
#include "Cursor.h"
-#include "support/debug.h"
#include "DispatchResult.h"
#include "FuncRequest.h"
#include "FuncStatus.h"
-#include "support/gettext.h"
#include "Lexer.h"
#include "Undo.h"
-#include "frontends/FontMetrics.h"
#include "frontends/Painter.h"
#include "support/convert.h"
+#include "support/debug.h"
+#include "support/gettext.h"
#include "support/docstream.h"
#include "support/lstrings.h"
-#include "support/debug.h"
-
#include <sstream>
using namespace std;
InsetLabelBox::InsetLabelBox(MathAtom const & atom, docstring label,
- MathMacroTemplate const & parent, bool frame)
-: InsetMathNest(1), parent_(parent), label_(label), frame_(frame)
+ MathMacroTemplate const & parent, bool frame)
+ : InsetMathNest(1), parent_(parent), label_(label), frame_(frame)
{
cell(0).insert(0, atom);
}
InsetLabelBox::InsetLabelBox(docstring label,
MathMacroTemplate const & parent, bool frame)
-: InsetMathNest(1), parent_(parent), label_(label), frame_(frame)
+ : InsetMathNest(1), parent_(parent), label_(label), frame_(frame)
{
}
if (!parent_.premetrics())
dim.des += maxasc + maxdes + 1;
}
-
- setDimCache(mi, dim);
}
dim.wid = 0;
dim.asc = 0;
dim.des = 0;
- setDimCache(mi, dim);
}
}
}
+///////////////////////////////////////////////////////////////////////
+class InsetColoredCell : public InsetMathNest {
+public:
+ ///
+ InsetColoredCell(ColorCode min, ColorCode max);
+ ///
+ InsetColoredCell(ColorCode min, ColorCode max, MathAtom const & atom);
+ ///
+ void draw(PainterInfo &, int x, int y) const;
+ ///
+ void metrics(MetricsInfo & mi, Dimension & dim) const;
+
+protected:
+ ///
+ Inset * clone() const;
+ ///
+ ColorCode min_;
+ ///
+ ColorCode max_;
+};
+
+
+InsetColoredCell::InsetColoredCell(ColorCode min, ColorCode max)
+ : InsetMathNest(1), min_(min), max_(max)
+{
+}
+
+
+InsetColoredCell::InsetColoredCell(ColorCode min, ColorCode max, MathAtom const & atom)
+ : InsetMathNest(1), min_(min), max_(max)
+{
+ cell(0).insert(0, atom);
+}
+
+
+Inset * InsetColoredCell::clone() const
+{
+ return new InsetColoredCell(*this);
+}
+
+
+void InsetColoredCell::metrics(MetricsInfo & mi, Dimension & dim) const
+{
+ cell(0).metrics(mi, dim);
+}
+
+
+void InsetColoredCell::draw(PainterInfo & pi, int x, int y) const
+{
+ pi.pain.enterMonochromeMode(min_, max_);
+ cell(0).draw(pi, x, y);
+ pi.pain.leaveMonochromeMode();
+}
+
+
///////////////////////////////////////////////////////////////////////
class InsetNameWrapper : public InsetMathWrapper {
MathMacroTemplate::MathMacroTemplate()
- : InsetMathNest(3), numargs_(0), optionals_(0),
+ : InsetMathNest(3), numargs_(0), argsInLook_(0), optionals_(0),
type_(MacroTypeNewcommand), lookOutdated_(true)
{
initMath();
int optionals, MacroType type,
vector<MathData> const & optionalValues,
MathData const & def, MathData const & display)
- : InsetMathNest(optionals + 3), numargs_(numargs),
+ : InsetMathNest(optionals + 3), numargs_(numargs), argsInLook_(0),
optionals_(optionals), optionalValues_(optionalValues),
type_(type), lookOutdated_(true)
{
Inset * MathMacroTemplate::clone() const
{
- return new MathMacroTemplate(*this);
+ MathMacroTemplate * inset = new MathMacroTemplate(*this);
+ // the parent pointers of the proxy insets above will point to
+ // to the old template. Hence, the look must be updated.
+ inset->updateLook();
+ return inset;
}
}
-void MathMacroTemplate::createLook() const
+void MathMacroTemplate::createLook(int args) const
{
look_.clear();
+ argsInLook_ = args;
// \foo
look_.push_back(MathAtom(new InsetLabelBox(_("Name"), *this, false)));
int i = 0;
if (optionals_ > 0) {
look_.push_back(MathAtom(new InsetLabelBox(_("optional"), *this, false)));
- MathData & optData = look_[look_.size() - 1].nucleus()->cell(0);
-
+
for (; i < optionals_; ++i) {
- optData.push_back(MathAtom(new InsetMathChar('[')));
- optData.push_back(MathAtom(new InsetMathWrapper(&cell(1 + i))));
- optData.push_back(MathAtom(new InsetMathChar(']')));
+ MathData * optData = &look_[look_.size() - 1].nucleus()->cell(0);
+
+ // color it red, if it is to be remove when the cursor leaves
+ if (optionals_ > argsInLook_) {
+ optData->push_back(MathAtom(
+ new InsetColoredCell(Color_mathbg, Color_mathmacrooldarg)));
+ optData = &(*optData)[0].nucleus()->cell(0);
+ }
+
+ optData->push_back(MathAtom(new InsetMathChar('[')));
+ optData->push_back(MathAtom(new InsetMathWrapper(&cell(1 + i))));
+ optData->push_back(MathAtom(new InsetMathChar(']')));
}
}
for (; i < numargs_; ++i) {
MathData arg;
arg.push_back(MathAtom(new MathMacroArgument(i + 1)));
- look_.push_back(MathAtom(new InsetMathBrace(arg)));
+ if (i >= argsInLook_) {
+ look_.push_back(MathAtom(new InsetColoredCell(
+ Color_mathbg, Color_mathmacrooldarg,
+ MathAtom(new InsetMathBrace(arg)))));
+ } else
+ look_.push_back(MathAtom(new InsetMathBrace(arg)));
}
-
+ for (; i < argsInLook_; ++i) {
+ MathData arg;
+ arg.push_back(MathAtom(new MathMacroArgument(i + 1)));
+ look_.push_back(MathAtom(new InsetColoredCell(
+ Color_mathbg, Color_mathmacronewarg,
+ MathAtom(new InsetMathBrace(arg)))));
+ }
+
// :=
look_.push_back(MathAtom(new InsetMathChar(':')));
look_.push_back(MathAtom(new InsetMathChar('=')));
}
// update look?
- if (lookOutdated_) {
+ int argsInDef = maxArgumentInDefinition();
+ if (lookOutdated_ || argsInDef != argsInLook_) {
lookOutdated_ = false;
- createLook();
+ createLook(argsInDef);
}
/// metrics for inset contents
}
-void MathMacroTemplate::edit(Cursor & cur, bool left)
+void MathMacroTemplate::edit(Cursor & cur, bool front, EntryDirection entry_from)
{
updateLook();
- cur.updateFlags(Update::Force);
- InsetMathNest::edit(cur, left);
+ cur.updateFlags(Update::SinglePar);
+ InsetMathNest::edit(cur, front, entry_from);
}
-bool MathMacroTemplate::notifyCursorLeaves(Cursor & cur)
+bool MathMacroTemplate::notifyCursorLeaves(Cursor const & old, Cursor & cur)
{
+ // find this in cursor old
+ Cursor insetCur = old;
+ int scriptSlice = insetCur.find(this);
+ BOOST_ASSERT(scriptSlice != -1);
+ insetCur.cutOff(scriptSlice);
+
+ commitEditChanges(insetCur);
updateLook();
cur.updateFlags(Update::Force);
- return InsetMathNest::notifyCursorLeaves(cur);
+ return InsetMathNest::notifyCursorLeaves(old, cur);
}
}
+int MathMacroTemplate::maxArgumentInDefinition() const
+{
+ int maxArg = 0;
+ MathMacroTemplate * nonConst = const_cast<MathMacroTemplate *>(this);
+ DocIterator it = doc_iterator_begin(*nonConst);
+ it.idx() = defIdx();
+ for (; it; it.forwardChar()) {
+ if (!it.nextInset())
+ continue;
+ if (it.nextInset()->lyxCode() != MATHMACROARG_CODE)
+ continue;
+ MathMacroArgument * arg = static_cast<MathMacroArgument*>(it.nextInset());
+ maxArg = std::max(int(arg->number()), maxArg);
+ }
+ return maxArg;
+}
+
+
+void MathMacroTemplate::insertMissingArguments(int maxArg)
+{
+ bool found[9] = { false, false, false, false, false, false, false, false, false };
+ idx_type idx = cell(displayIdx()).empty() ? defIdx() : displayIdx();
+
+ // search for #n macros arguments
+ DocIterator it = doc_iterator_begin(*this);
+ it.idx() = idx;
+ for (; it && it[0].idx() == idx; it.forwardChar()) {
+ if (!it.nextInset())
+ continue;
+ if (it.nextInset()->lyxCode() != MATHMACROARG_CODE)
+ continue;
+ MathMacroArgument * arg = static_cast<MathMacroArgument*>(it.nextInset());
+ found[arg->number() - 1] = true;
+ }
+
+ // add missing ones
+ for (int i = 0; i < maxArg; ++i) {
+ if (found[i])
+ continue;
+
+ cell(idx).push_back(MathAtom(new MathMacroArgument(i + 1)));
+ }
+}
+
+
+void MathMacroTemplate::changeArity(Cursor & cur, int newNumArg)
+{
+ // remove parameter which do not appear anymore in the definition
+ for (int i = numargs_; i > newNumArg; --i)
+ removeParameter(cur, numargs_ - 1, false);
+
+ // add missing parameter
+ for (int i = numargs_; i < newNumArg; ++i)
+ insertParameter(cur, numargs_, false, false);
+}
+
+
+void MathMacroTemplate::commitEditChanges(Cursor & cur)
+{
+ int argsInDef = maxArgumentInDefinition();
+ if (argsInDef != numargs_) {
+ cur.recordUndoFullDocument();
+ changeArity(cur, argsInDef);
+ }
+ insertMissingArguments(argsInDef);
+}
+
+
// FIXME: factorize those functions here with a functional style, maybe using Boost's function
// objects?
}
-void MathMacroTemplate::insertParameter(Cursor & cur, int pos, bool greedy)
+void MathMacroTemplate::insertParameter(Cursor & cur, int pos, bool greedy, bool addarg)
{
if (pos <= numargs_ && pos >= optionals_ && numargs_ < 9) {
++numargs_;
- shiftArguments(pos, 1);
-
+
// append example #n
- cell(defIdx()).push_back(MathAtom(new MathMacroArgument(pos + 1)));
- if (!cell(displayIdx()).empty())
- cell(displayIdx()).push_back(MathAtom(new MathMacroArgument(pos + 1)));
+ if (addarg) {
+ shiftArguments(pos, 1);
+
+ cell(defIdx()).push_back(MathAtom(new MathMacroArgument(pos + 1)));
+ if (!cell(displayIdx()).empty())
+ cell(displayIdx()).push_back(MathAtom(new MathMacroArgument(pos + 1)));
+ }
if (!greedy) {
Cursor dit = cur;
if (numargs_ > 0 && optionals_ > 0) {
--optionals_;
- // store default value for later if the use changes his mind
+ // store default value for later if the user changes his mind
optionalValues_[optionals_] = cell(optIdx(optionals_));
cells_.erase(cells_.begin() + optIdx(optionals_));
case LFUN_MATH_MACRO_ADD_PARAM:
if (numargs_ < 9) {
+ commitEditChanges(cur);
cur.recordUndoFullDocument();
size_t pos = numargs_;
if (arg.size() != 0)
case LFUN_MATH_MACRO_REMOVE_PARAM:
if (numargs_ > 0) {
+ commitEditChanges(cur);
cur.recordUndoFullDocument();
size_t pos = numargs_ - 1;
if (arg.size() != 0)
case LFUN_MATH_MACRO_APPEND_GREEDY_PARAM:
if (numargs_ < 9) {
+ commitEditChanges(cur);
cur.recordUndoFullDocument();
insertParameter(cur, numargs_, true);
}
case LFUN_MATH_MACRO_REMOVE_GREEDY_PARAM:
if (numargs_ > 0) {
+ commitEditChanges(cur);
cur.recordUndoFullDocument();
removeParameter(cur, numargs_ - 1, true);
}
break;
case LFUN_MATH_MACRO_MAKE_OPTIONAL:
+ commitEditChanges(cur);
cur.recordUndoFullDocument();
makeOptional(cur);
break;
case LFUN_MATH_MACRO_MAKE_NONOPTIONAL:
+ commitEditChanges(cur);
cur.recordUndoFullDocument();
makeNonOptional(cur);
break;
case LFUN_MATH_MACRO_ADD_OPTIONAL_PARAM:
if (numargs_ < 9) {
+ commitEditChanges(cur);
cur.recordUndoFullDocument();
insertParameter(cur, optionals_);
makeOptional(cur);
case LFUN_MATH_MACRO_REMOVE_OPTIONAL_PARAM:
if (optionals_ > 0) {
+ commitEditChanges(cur);
cur.recordUndoFullDocument();
removeParameter(cur, optionals_ - 1);
} break;
case LFUN_MATH_MACRO_ADD_GREEDY_OPTIONAL_PARAM:
if (numargs_ == optionals_) {
+ commitEditChanges(cur);
cur.recordUndoFullDocument();
insertParameter(cur, 0, true);
makeOptional(cur);
}
-void MathMacroTemplate::read(Buffer const &, Lexer & lex)
+void MathMacroTemplate::read(Lexer & lex)
{
MathData ar;
mathed_parse_cell(ar, lex.getStream());
}
-void MathMacroTemplate::write(Buffer const &, ostream & os) const
+void MathMacroTemplate::write(ostream & os) const
{
odocstringstream oss;
WriteStream wi(oss, false, false);
void MathMacroTemplate::write(WriteStream & os, bool overwriteRedefinition) const
{
- if (type_ == MacroTypeDef) {
- os << "\\def\\" << name().c_str();
- for (int i = 1; i <= numargs_; ++i)
- os << '#' << i;
- } else {
- // newcommand or renewcommand
+ // newcommand or renewcommand
+ if (os.latex() && optionals_ > 1)
+ os << "\\newlyxcommand";
+ else {
if (redefinition_ && !overwriteRedefinition)
os << "\\renewcommand";
else
os << "\\newcommand";
- os << "{\\" << name().c_str() << '}';
- if (numargs_ > 0)
- os << '[' << numargs_ << ']';
-
- // optional values
- if (os.latex()) {
- // in latex only one optional possible, simulate the others
- if (optionals_ >= 1) {
- docstring optValue = asString(cell(optIdx(0)));
- if (optValue.find(']') != docstring::npos)
- os << "[{" << cell(optIdx(0)) << "}]";
- else
- os << "[" << cell(optIdx(0)) << "]";
- }
- } else {
- // in lyx we handle all optionals as real optionals
- for (int i = 0; i < optionals_; ++i) {
- docstring optValue = asString(cell(optIdx(i)));
- if (optValue.find(']') != docstring::npos)
- os << "[{" << cell(optIdx(i)) << "}]";
- else
- os << "[" << cell(optIdx(i)) << "]";
- }
- }
+ }
+ os << "{\\" << name().c_str() << '}';
+ if (numargs_ > 0)
+ os << '[' << numargs_ << ']';
+
+ // optional values
+ for (int i = 0; i < optionals_; ++i) {
+ docstring optValue = asString(cell(optIdx(i)));
+ if (optValue.find(']') != docstring::npos)
+ os << "[{" << cell(optIdx(i)) << "}]";
+ else
+ os << "[" << cell(optIdx(i)) << "]";
}
os << "{" << cell(defIdx()) << "}";
}
-int MathMacroTemplate::plaintext(Buffer const & buf, odocstream & os,
+int MathMacroTemplate::plaintext(odocstream & os,
OutputParams const &) const
{
- static docstring const str = '[' + buf.B_("math macro") + ']';
+ static docstring const str = '[' + buffer().B_("math macro") + ']';
os << str;
return str.size();
// valid characters?
for (size_t i = 0; i < n.size(); ++i) {
- if (!(n[i] >= 'a' && n[i] <= 'z') &&
- !(n[i] >= 'A' && n[i] <= 'Z'))
+ if (!(n[i] >= 'a' && n[i] <= 'z')
+ && !(n[i] >= 'A' && n[i] <= 'Z')
+ && n[i] != '*')
return false;
}
return data.size() > 0;
}
+
+void MathMacroTemplate::validate(LaTeXFeatures & features) const
+{
+ if (optionals_ > 1) {
+ features.require("newlyxcommand");
+ }
+}
void MathMacroTemplate::getDefaults(vector<docstring> & defaults) const
{