#include <config.h>
+#include "InsetMathSqrt.h"
#include "MacroTable.h"
#include "MathMacroTemplate.h"
#include "MathMacroArgument.h"
+#include "MathStream.h"
#include "MathSupport.h"
-#include "InsetMathSqrt.h"
+#include "InsetMathNest.h"
+
+#include "Buffer.h"
+#include "InsetList.h"
+#include "Text.h"
-#include "debug.h"
+#include "support/debug.h"
#include "DocIterator.h"
#include <boost/assert.hpp>
#include <sstream>
+using namespace std;
namespace lyx {
-using std::endl;
-using std::istringstream;
-using std::map;
-using std::pair;
-using std::string;
-using std::vector;
-using std::size_t;
+/////////////////////////////////////////////////////////////////////
+//
+// MacroData
+//
+/////////////////////////////////////////////////////////////////////
MacroData::MacroData()
- : numargs_(0), lockCount_(0)
+ : queried_(true), numargs_(0), optionals_(0), lockCount_(0),
+ redefinition_(false), type_(MacroTypeNewcommand)
{}
-
-MacroData::MacroData(docstring const & def, int numargs, docstring const & disp, string const & requires)
- : def_(def), numargs_(numargs), disp_(disp), requires_(requires), lockCount_(0)
-{}
+
+
+MacroData::MacroData(Buffer const & buf, DocIterator const & pos)
+ : buffer_(&buf), pos_(pos), queried_(false), numargs_(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)
+{
+ queryData(macro);
+}
void MacroData::expand(vector<MathData> const & args, MathData & to) const
{
+ updateData();
+
InsetMathSqrt inset; // Hack. Any inset with a cell would do.
// FIXME UNICODE
- asArray(disp_.empty() ? def_ : disp_, inset.cell(0));
+ asArray(display_.empty() ? definition_ : display_, inset.cell(0));
//lyxerr << "MathData::expand: args: " << args << endl;
//lyxerr << "MathData::expand: ar: " << inset.cell(0) << endl;
for (DocIterator it = doc_iterator_begin(inset); it; it.forwardChar()) {
if (!it.nextInset())
continue;
- if (it.nextInset()->lyxCode() != Inset::MATHMACROARG_CODE)
+ if (it.nextInset()->lyxCode() != MATHMACROARG_CODE)
continue;
//it.cell().erase(it.pos());
//it.cell().insert(it.pos(), it.nextInset()->asInsetMath()
}
-// The global table.
-MacroTable & MacroTable::globalMacros()
+size_t MacroData::optionals() const
{
- static MacroTable theGlobalMacros;
- return theGlobalMacros;
+ updateData();
+ return optionals_;
+}
+
+
+vector<docstring> const & MacroData::defaults() const
+{
+ updateData();
+ return defaults_;
+}
+
+
+void MacroData::unlock() const
+{
+ --lockCount_;
+ BOOST_ASSERT(lockCount_ >= 0);
+}
+
+
+void MacroData::queryData(MathMacroTemplate const & macro) const
+{
+ if (queried_)
+ return;
+
+ queried_ = true;
+ definition_ = macro.definition();
+ numargs_ = macro.numArgs();
+ display_ = macro.displayDefinition();
+ redefinition_ = macro.redefinition();
+ type_ = macro.type();
+ optionals_ = macro.numOptionals();
+ macro.getDefaults(defaults_);
}
-bool MacroTable::has(docstring const & name) const
+void MacroData::updateData() const
+{
+ if (queried_)
+ return;
+
+ BOOST_ASSERT(buffer_ != 0);
+
+ // 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<MathMacroTemplate const &>(*inset));
+}
+
+
+void MacroData::write(odocstream & os, bool overwriteRedefinition) const
{
- return find(name) != end();
+ updateData();
+
+ // find macro template
+ Inset * inset = pos_.nextInset();
+ if (inset == 0 || inset->lyxCode() != MATHMACRO_CODE) {
+ lyxerr << "BUG: No macro template found by MacroData" << endl;
+ return;
+ }
+
+ // output template
+ MathMacroTemplate const & tmpl
+ = static_cast<MathMacroTemplate const &>(*inset);
+ WriteStream wi(os, false, true);
+ tmpl.write(wi, overwriteRedefinition);
}
-MacroData const & MacroTable::get(docstring const & name) const
+/////////////////////////////////////////////////////////////////////
+//
+// The global table of macros
+//
+/////////////////////////////////////////////////////////////////////
+
+MacroTable & MacroTable::globalMacros()
+{
+ static MacroTable theGlobalMacros;
+ return theGlobalMacros;
+}
+
+
+MacroData const * MacroTable::get(docstring const & name) const
{
const_iterator it = find(name);
- BOOST_ASSERT(it != end());
- return it->second;
+ return it == end() ? 0 : &it->second;
}
{
//lyxerr << "MacroTable::insert, def: " << to_utf8(def) << endl;
MathMacroTemplate mac(def);
- MacroData data = mac.asMacroData();
+ MacroData data(mac);
data.requires() = requires;
insert(mac.name(), data);
}
lyxerr << "\n------------------------------------------" << endl;
for (const_iterator it = begin(); it != end(); ++it)
lyxerr << to_utf8(it->first)
- << " [" << to_utf8(it->second.def()) << "] : "
- << " [" << to_utf8(it->second.disp()) << "] : "
+ << " [" << to_utf8(it->second.definition()) << "] : "
+ << " [" << to_utf8(it->second.display()) << "] : "
<< endl;
lyxerr << "------------------------------------------" << endl;
}
+/////////////////////////////////////////////////////////////////////
+//
+// MacroContext
+//
+/////////////////////////////////////////////////////////////////////
+
+MacroContext::MacroContext(Buffer const & buf, DocIterator const & pos)
+ : buf_(buf), pos_(pos)
+{
+}
+
+
+MacroData const * MacroContext::get(docstring const & name) const
+{
+ return buf_.getMacro(name, pos_);
+}
+
} // namespace lyx