X-Git-Url: https://git.lyx.org/gitweb/?a=blobdiff_plain;f=src%2Fmathed%2FMacroTable.cpp;h=d23855c511f5bd3fadd26661f81fc2d0b97a9abf;hb=cdc847fd304019a19425a0d5d9d42a556a937097;hp=389f82e9c19393bd46d407839671e4c6804ef36f;hpb=6192345f60bd77c9223c1649956d6814d48ca26b;p=lyx.git diff --git a/src/mathed/MacroTable.cpp b/src/mathed/MacroTable.cpp index 389f82e9c1..d23855c511 100644 --- a/src/mathed/MacroTable.cpp +++ b/src/mathed/MacroTable.cpp @@ -14,6 +14,7 @@ #include "MacroTable.h" #include "MathMacroTemplate.h" #include "MathMacroArgument.h" +#include "MathParser.h" #include "MathStream.h" #include "MathSupport.h" #include "InsetMathNest.h" @@ -24,7 +25,7 @@ #include "Text.h" #include "support/debug.h" - +#include "support/gettext.h" #include "support/lassert.h" #include @@ -40,45 +41,43 @@ namespace lyx { // ///////////////////////////////////////////////////////////////////// -MacroData::MacroData() - : queried_(true), numargs_(0), optionals_(0), lockCount_(0), - redefinition_(false), type_(MacroTypeNewcommand) +MacroData::MacroData(Buffer * buf) + : buffer_(buf), queried_(true), numargs_(0), sym_(0), optionals_(0), + lockCount_(0), redefinition_(false), type_(MacroTypeNewcommand) {} - - -MacroData::MacroData(Buffer const & buf, DocIterator const & pos) - : buffer_(&buf), pos_(pos), queried_(false), numargs_(0), + +MacroData::MacroData(Buffer * buf, DocIterator const & pos) + : buffer_(buf), pos_(pos), queried_(false), numargs_(0), sym_(0), optionals_(0), lockCount_(0), redefinition_(false), type_(MacroTypeNewcommand) { } - - -MacroData::MacroData(MathMacroTemplate const & macro) - : queried_(false), numargs_(0), optionals_(0), lockCount_(0), - redefinition_(false), type_(MacroTypeNewcommand) + + +MacroData::MacroData(Buffer * buf, MathMacroTemplate const & macro) + : buffer_(buf), queried_(false), numargs_(0), sym_(0), optionals_(0), + lockCount_(0), redefinition_(false), type_(MacroTypeNewcommand) { queryData(macro); } -void MacroData::expand(vector const & args, MathData & to) const +bool MacroData::expand(vector const & args, MathData & to) const { updateData(); // Hack. Any inset with a cell would do. - static Buffer buffer(""); - static InsetMathSqrt inset; + InsetMathSqrt inset(const_cast(buffer_)); - // FIXME UNICODE - asArray(display_.empty() ? definition_ : display_, inset.cell(0)); + docstring const & definition(display_.empty() ? definition_ : display_); + asArray(definition, inset.cell(0), Parse::QUIET); //lyxerr << "MathData::expand: args: " << args << endl; - //lyxerr << "MathData::expand: ar: " << inset.cell(0) << endl; - for (DocIterator it = doc_iterator_begin(&buffer, &inset); it; it.forwardChar()) { + //LYXERR0("MathData::expand: ar: " << inset.cell(0)); + for (DocIterator it = doc_iterator_begin(buffer_, &inset); it; it.forwardChar()) { if (!it.nextInset()) continue; - if (it.nextInset()->lyxCode() != MATHMACROARG_CODE) + if (it.nextInset()->lyxCode() != MATH_MACROARG_CODE) continue; //it.cell().erase(it.pos()); //it.cell().insert(it.pos(), it.nextInset()->asInsetMath() @@ -88,8 +87,12 @@ void MacroData::expand(vector const & args, MathData & to) const it.cell().insert(it.pos(), args[n - 1]); } } - //lyxerr << "MathData::expand: res: " << inset.cell(0) << endl; + //LYXERR0("MathData::expand: res: " << inset.cell(0)); to = inset.cell(0); + // If the result is equal to the definition then we either have a + // recursive loop, or the definition did not contain any macro in the + // first place. + return asString(to) != definition; } @@ -100,17 +103,47 @@ size_t MacroData::optionals() const } -vector const & MacroData::defaults() const +vector const & MacroData::defaults() const { updateData(); return defaults_; } +string const MacroData::requires() const +{ + if (sym_) + return sym_->requires; + return string(); +} + + +bool MacroData::hidden() const +{ + if (sym_) + return sym_->hidden; + return false; +} + + +docstring const MacroData::xmlname() const +{ + if (sym_) + return sym_->xmlname; + return docstring(); +} + + +char const * MacroData::MathMLtype() const +{ + return sym_ ? sym_->MathMLtype() : 0; +} + + void MacroData::unlock() const { --lockCount_; - LASSERT(lockCount_ >= 0, /**/); + LASSERT(lockCount_ >= 0, lockCount_ = 0); } @@ -126,7 +159,7 @@ void MacroData::queryData(MathMacroTemplate const & macro) const redefinition_ = macro.redefinition(); type_ = macro.type(); optionals_ = macro.numOptionals(); - + macro.getDefaults(defaults_); } @@ -136,24 +169,24 @@ void MacroData::updateData() const if (queried_) return; - LASSERT(buffer_ != 0, /**/); - + LBUFERR(buffer_); + // Try to fix position DocIterator. Should not do anything in theory. pos_.fixIfBroken(); - + // find macro template Inset * inset = pos_.nextInset(); if (inset == 0 || inset->lyxCode() != MATHMACRO_CODE) { lyxerr << "BUG: No macro template found by MacroData" << endl; return; } - + // query the data from the macro template - queryData(static_cast(*inset)); + queryData(static_cast(*inset)); } - -void MacroData::write(odocstream & os, bool overwriteRedefinition) const + +int MacroData::write(odocstream & os, bool overwriteRedefinition) const { updateData(); @@ -161,14 +194,15 @@ void MacroData::write(odocstream & os, bool overwriteRedefinition) const Inset * inset = pos_.nextInset(); if (inset == 0 || inset->lyxCode() != MATHMACRO_CODE) { lyxerr << "BUG: No macro template found by MacroData" << endl; - return; + return 0; } - + // output template MathMacroTemplate const & tmpl = static_cast(*inset); - WriteStream wi(os, false, true, false); - tmpl.write(wi, overwriteRedefinition); + otexrowstream ots(os); + WriteStream wi(ots, false, true, WriteStream::wsDefault); + return tmpl.write(wi, overwriteRedefinition); } @@ -192,27 +226,36 @@ MacroData const * MacroTable::get(docstring const & name) const } -void MacroTable::insert(docstring const & name, MacroData const & data) +MacroTable::iterator +MacroTable::insert(docstring const & name, MacroData const & data) { //lyxerr << "MacroTable::insert: " << to_utf8(name) << endl; - operator[](name) = data; + iterator it = find(name); + if (it == end()) + it = map::insert( + make_pair(name, data)).first; + else + it->second = data; + return it; } -void MacroTable::insert(docstring const & def, string const & requires) +MacroTable::iterator +MacroTable::insert(Buffer * buf, docstring const & def) { //lyxerr << "MacroTable::insert, def: " << to_utf8(def) << endl; - MathMacroTemplate mac(def); - MacroData data(mac); - data.requires() = requires; - insert(mac.name(), data); + MathMacroTemplate mac(buf); + mac.fromString(def); + MacroData data(buf, mac); + return insert(mac.name(), data); } -void MacroTable::getMacroNames(std::set & names) const +void MacroTable::getMacroNames(std::set & names, bool gethidden) const { for (const_iterator it = begin(); it != end(); ++it) - names.insert(it->first); + if (gethidden || !it->second.hidden()) + names.insert(it->first); } @@ -234,7 +277,7 @@ void MacroTable::dump() // ///////////////////////////////////////////////////////////////////// -MacroContext::MacroContext(Buffer const & buf, DocIterator const & pos) +MacroContext::MacroContext(Buffer const * buf, DocIterator const & pos) : buf_(buf), pos_(pos) { } @@ -242,7 +285,7 @@ MacroContext::MacroContext(Buffer const & buf, DocIterator const & pos) MacroData const * MacroContext::get(docstring const & name) const { - return buf_.getMacro(name, pos_); + return buf_->getMacro(name, pos_); } } // namespace lyx