}
bool the_end = readBody(lex);
+ //lyxerr << "removing " << MacroTable::localMacros().size()
+ // << " temporary macro entries" << endl;
+ //MacroTable::localMacros().clear();
params().setPaperStuff();
#ifdef WITH_WARNINGS
void Buffer::insertMacro(string const & name, MacroData const & data)
{
+ MacroTable::globalMacros().insert(name, data);
pimpl_->macros.insert(name, data);
}
if (macro && macro->getInsetName() == name)
lyxerr << "can't enter recursive macro" << endl;
- plainInsert(createMathInset(name));
- if (buffer().hasMacro(name)) {
- MacroData const & tmpl = buffer().getMacro(name);
- for (int i = 0; i < tmpl.numargs(); ++i)
- cell().insert(pos(), MathAtom(new MathBraceInset));
- }
+ niceInsert(createMathInset(name));
}
}
inset->read(buf, lex);
+
+#warning hack..
+ if (inset->lyxCode() == InsetBase::MATHMACRO_CODE) {
+ MathMacroTemplate const * tmpl =
+ static_cast<MathMacroTemplate*>(inset.get());
+ MacroTable::globalMacros().insert
+ (tmpl->name(), tmpl->asMacroData());
+ lyxerr << "creating local macro " << tmpl->name() << endl;
+ }
}
return inset.release();
dim_.wid = 0;
Dimension d;
- BufferView & bv = *mi.base.bv;
- Buffer const & buf = *bv.buffer();
+ //BufferView & bv = *mi.base.bv;
+ //Buffer const & buf = *bv.buffer();
for (size_t i = 0, n = size(); i != n; ++i) {
MathAtom const & at = operator[](i);
+#if 0
MathMacro const * mac = at->asMacro();
if (mac && buf.hasMacro(mac->name())) {
MacroData const & tmpl = buf.getMacro(mac->name());
continue;
}
}
+#endif
at->metrics(mi, d);
dim_ += d;
}
|| x >= pi.pain.paperWidth())
return;
- BufferView & bv = *pi.base.bv;
- Buffer const & buf = *bv.buffer();
+ //BufferView & bv = *pi.base.bv;
for (size_t i = 0, n = size(); i != n; ++i) {
MathAtom const & at = operator[](i);
+#if 0
+ Buffer const & buf = *bv.buffer();
// special macro handling
MathMacro const * mac = at->asMacro();
if (mac && buf.hasMacro(mac->name())) {
continue;
}
}
+#endif
at->drawSelection(pi, x, y);
at->draw(pi, x, y);
x += at->width();
if (s == "dfrac")
return MathAtom(new MathDfracInset);
- return MathAtom(new MathMacro(s));
+ if (MacroTable::globalMacros().has(s))
+ return MathAtom(new MathMacro(s,
+ MacroTable::globalMacros().get(s).numargs()));
+ //if (MacroTable::localMacros().has(s))
+ // return MathAtom(new MathMacro(s,
+ // MacroTable::localMacros().get(s).numargs()));
+
+ return MathAtom(new MathUnknownInset(s));
}
using std::endl;
-
-MathMacro::MathMacro(string const & name)
- : name_(name)
+MathMacro::MathMacro(string const & name, int numargs)
+ : MathNestInset(numargs), name_(name)
{}
}
-void MathMacro::setExpansion(MathArray const & exp, MathArray const & arg) const
-{
- expanded_ = exp;
- args_ = arg;
-}
-
-
void MathMacro::metrics(MetricsInfo & mi, Dimension & dim) const
{
- LyXFont font = mi.base.font;
- augmentFont(font, "lyxtex");
- mathed_string_dim(font, "\\" + name(), dim);
- dim_ = dim;
-}
-
-
-void MathMacro::metricsExpanded(MetricsInfo & mi, Dimension & dim) const
-{
- args_.metrics(mi);
- expanded_.metrics(mi, dim);
- dim.wid -= args_.size() ? args_.width() : 0;
+ if (!MacroTable::globalMacros().has(name())) {
+ mathed_string_dim(mi.base.font, "Unknown: " + name(), dim);
+ } else if (editing(mi.base.bv)) {
+ asArray(MacroTable::globalMacros().get(name()).def(), tmpl_);
+ LyXFont font = mi.base.font;
+ augmentFont(font, "lyxtex");
+ tmpl_.metrics(mi, dim);
+ dim.wid += mathed_string_width(font, name()) + 10;
+ int ww = mathed_string_width(font, "#1: ");
+ for (idx_type i = 0; i < nargs(); ++i) {
+ MathArray const & c = cell(i);
+ c.metrics(mi);
+ dim.wid = max(dim.wid, c.width() + ww);
+ dim.des += c.height() + 10;
+ }
+ } else {
+ MacroTable::globalMacros().get(name()).expand(cells_, expanded_);
+ expanded_.metrics(mi, dim);
+ }
+ metricsMarkers2(dim);
dim_ = dim;
}
void MathMacro::draw(PainterInfo & pi, int x, int y) const
{
- LyXFont font = pi.base.font;
- augmentFont(font, "lyxtex");
- drawStr(pi, font, x, y, "\\" + name());
- setPosCache(pi, x, y);
-}
-
-
-void MathMacro::drawExpanded(PainterInfo & pi, int x, int y) const
-{
- expanded_.draw(pi, x, y);
+ if (!MacroTable::globalMacros().has(name())) {
+ drawStrRed(pi, x, y, "Unknown: " + name());
+ } else if (editing(pi.base.bv)) {
+ LyXFont font = pi.base.font;
+ augmentFont(font, "lyxtex");
+ int h = y - dim_.ascent() + 2 + tmpl_.ascent();
+ drawStr(pi, font, x + 3, h, name());
+ int const w = mathed_string_width(font, name());
+ tmpl_.draw(pi, x + w + 12, h);
+ h += tmpl_.descent();
+ Dimension ldim;
+ mathed_string_dim(font, "#1: ", ldim);
+ for (idx_type i = 0; i < nargs(); ++i) {
+ MathArray const & c = cell(i);
+ h += max(c.ascent(), ldim.asc) + 5;
+ c.draw(pi, x + ldim.wid, h);
+ char str[] = "#1:";
+ str[1] += static_cast<char>(i);
+ drawStr(pi, font, x + 3, h, str);
+ h += max(c.descent(), ldim.des) + 5;
+ }
+ } else {
+ expanded_.draw(pi, x, y);
+ }
drawMarkers2(pi, x, y);
}
-int MathMacro::widthExpanded() const
-{
- return expanded_.width();
-}
-
-
void MathMacro::validate(LaTeXFeatures & features) const
{
if (name() == "binom" || name() == "mathcircumflex")
void MathMacro::updateExpansion() const
{
-#warning FIXME
- //expand();
//expanded_.substitute(*this);
}
#include "math_nestinset.h"
#include "math_data.h"
+#include "math_nestinset.h"
+#include "math_macrotable.h"
/// This class contains the data for a macro.
-class MathMacro : public MathDimInset {
+class MathMacro : public MathNestInset {
public:
/// A macro can be built from an existing template
- explicit MathMacro(std::string const &);
+ MathMacro(std::string const & name, int numargs);
///
std::auto_ptr<InsetBase> clone() const;
///
- MathMacro * asMacro() { return this; }
- ///
- MathMacro const * asMacro() const { return this; }
- ///
void draw(PainterInfo & pi, int x, int y) const;
///
void drawExpanded(PainterInfo & pi, int x, int y) const;
///
void metrics(MetricsInfo & mi, Dimension & dim) const;
///
- void metricsExpanded(MetricsInfo & mi, Dimension & dim) const;
- ///
- int widthExpanded() const;
- ///
std::string name() const;
///
void setExpansion(MathArray const & exp, MathArray const & args) const;
private:
///
void updateExpansion() const;
-
///
+ void expand() const;
+
+ /// name of macro
std::string name_;
- ///
+ /// the unexpanded macro defintition
+ mutable MathArray tmpl_;
+ /// the matcro substituted with our args
mutable MathArray expanded_;
- ///
- mutable MathArray args_;
};
{}
-void MacroData::expand(MathArray const & args, MathArray & to) const
+void MacroData::expand(vector<MathArray> const & args, MathArray & to) const
{
MathSqrtInset inset; // Hack. Any inset with a cell would do.
asArray(disp_.empty() ? def_ : disp_, inset.cell(0));
//it.cell().insert(it.pos(), it.nextInset()->asMathInset()
int n = static_cast<MathMacroArgument*>(it.nextInset())->number();
if (n <= args.size()) {
- if (args[n - 1]->asBraceInset()) {
- it.cell().erase(it.pos());
- it.cell().insert(it.pos(), args[n - 1]->cell(0));
- } else {
- it.cell()[it.pos()] = args[n - 1];
- }
+ it.cell().erase(it.pos());
+ it.cell().insert(it.pos(), args[n - 1]);
}
}
//lyxerr << "MathData::expand: res: " << inset.cell(0) << endl;
}
+// The local table.
+//MacroTable & MacroTable::localMacros()
+//{
+// static MacroTable theLocalMacros;
+// return theLocalMacros;
+//}
+
+
bool MacroTable::has(string const & name) const
{
- return macros_.find(name) != macros_.end();
+ return find(name) != end();
}
MacroData const & MacroTable::get(string const & name) const
{
- table_type::const_iterator it = macros_.find(name);
- BOOST_ASSERT(it != macros_.end());
+ const_iterator it = find(name);
+ BOOST_ASSERT(it != end());
return it->second;
}
void MacroTable::insert(string const & name, MacroData const & data)
{
- lyxerr << "MathMacroTable::insert: " << name << endl;
- macros_[name] = data;
+ //lyxerr << "MacroTable::insert: " << name << endl;
+ operator[](name) = data;
}
void MacroTable::insert(string const & def)
{
- //lyxerr << "MathMacroTable::insert, def: " << def << endl;
+ //lyxerr << "MacroTable::insert, def: " << def << endl;
istringstream is(def);
MathMacroTemplate mac(is);
insert(mac.name(), mac.asMacroData());
void MacroTable::dump()
{
lyxerr << "\n------------------------------------------" << endl;
- table_type::const_iterator it;
- for (it = macros_.begin(); it != macros_.end(); ++it)
+ for (const_iterator it = begin(); it != end(); ++it)
lyxerr << it->first
<< " [" << it->second.def() << "] : "
<< " [" << it->second.disp() << "] : "
class MathArray;
+
///
class MacroData {
public:
///
int numargs() const { return numargs_; }
/// replace #1,#2,... by given MathAtom 0,1,..
- void expand(MathArray const & from, MathArray & to) const;
+ void expand(std::vector<MathArray> const & from, MathArray & to) const;
private:
///
// either because they implement a feature of standard LaTeX or some
// hack to display certain contents nicely.
-class MacroTable {
+class MacroTable : public std::map<std::string, MacroData>
+{
public:
/// Parse full "\def..." or "\newcommand..." or ...
void insert(std::string const & definition);
/// the global list
static MacroTable & globalMacros();
-
-private:
- ///
- typedef std::map<std::string, MacroData> table_type;
-
- ///
- table_type macros_;
+ /// the local list hack
+ //static MacroTable & localMacros();
};
#endif
}
-void MathMacroTemplate::edit(LCursor & cur, bool left)
+void MathMacroTemplate::edit(LCursor & cur, bool)
{
lyxerr << "MathMacroTemplate: edit left/right" << endl;
cur.push(*this);
class Parser {
-
public:
///
typedef MathInset::mode_type mode_type;
os << name_;
}
+
void MathUnknownInset::mathematica(MathematicaStream & os) const
{
os << name_;