3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
13 #include "MacroTable.h"
14 #include "MathMacroTemplate.h"
15 #include "MathMacroArgument.h"
16 #include "MathSupport.h"
17 #include "InsetMathSqrt.h"
19 #include "InsetMathNest.h"
22 #include "support/debug.h"
23 #include "DocIterator.h"
25 #include <boost/assert.hpp>
34 /////////////////////////////////////////////////////////////////////
38 /////////////////////////////////////////////////////////////////////
40 MacroData::MacroData()
41 : numargs_(0), lockCount_(0), redefinition_(false)
45 MacroData::MacroData(docstring const & definition,
46 vector<docstring> const & defaults,
47 int numargs, int optionals, docstring const & display,
48 string const & requires)
49 : definition_(definition), numargs_(numargs), display_(display),
50 requires_(requires), lockCount_(0), redefinition_(false),
51 optionals_(optionals), defaults_(defaults)
53 defaults_.resize(optionals);
57 void MacroData::expand(vector<MathData> const & args, MathData & to) const
59 InsetMathSqrt inset; // Hack. Any inset with a cell would do.
61 asArray(display_.empty() ? definition_ : display_, inset.cell(0));
62 //lyxerr << "MathData::expand: args: " << args << endl;
63 //lyxerr << "MathData::expand: ar: " << inset.cell(0) << endl;
64 for (DocIterator it = doc_iterator_begin(inset); it; it.forwardChar()) {
67 if (it.nextInset()->lyxCode() != MATHMACROARG_CODE)
69 //it.cell().erase(it.pos());
70 //it.cell().insert(it.pos(), it.nextInset()->asInsetMath()
71 size_t n = static_cast<MathMacroArgument*>(it.nextInset())->number();
72 if (n <= args.size()) {
73 it.cell().erase(it.pos());
74 it.cell().insert(it.pos(), args[n - 1]);
77 //lyxerr << "MathData::expand: res: " << inset.cell(0) << endl;
82 size_t MacroData::optionals() const
88 vector<docstring> const & MacroData::defaults() const
94 void MacroData::unlock() const
97 BOOST_ASSERT(lockCount_ >= 0);
101 /////////////////////////////////////////////////////////////////////
103 // The global table of macros
105 /////////////////////////////////////////////////////////////////////
107 MacroTable & MacroTable::globalMacros()
109 static MacroTable theGlobalMacros;
110 return theGlobalMacros;
114 bool MacroTable::has(docstring const & name) const
116 return find(name) != end();
120 MacroData const & MacroTable::get(docstring const & name) const
122 const_iterator it = find(name);
123 BOOST_ASSERT(it != end());
128 void MacroTable::insert(docstring const & name, MacroData const & data)
130 //lyxerr << "MacroTable::insert: " << to_utf8(name) << endl;
131 operator[](name) = data;
135 void MacroTable::insert(docstring const & def, string const & requires)
137 //lyxerr << "MacroTable::insert, def: " << to_utf8(def) << endl;
138 MathMacroTemplate mac(def);
139 MacroData data = mac.asMacroData();
140 data.requires() = requires;
141 insert(mac.name(), data);
145 void MacroTable::dump()
147 lyxerr << "\n------------------------------------------" << endl;
148 for (const_iterator it = begin(); it != end(); ++it)
149 lyxerr << to_utf8(it->first)
150 << " [" << to_utf8(it->second.definition()) << "] : "
151 << " [" << to_utf8(it->second.display()) << "] : "
153 lyxerr << "------------------------------------------" << endl;
157 MacroContext::MacroContext(Buffer const & buf, Paragraph const & par)
158 : buf_(buf), par_(par)
163 bool MacroContext::has(docstring const & name) const
165 // check if it's a local macro
166 if (macros_.has(name))
169 // otherwise ask the buffer
170 return buf_.hasMacro(name, par_);
174 MacroData const & MacroContext::get(docstring const & name) const
176 // check if it's a local macro
177 if (macros_.has(name))
178 return macros_.get(name);
180 // ask the buffer for its macros
181 return buf_.getMacro(name, par_);
185 void MacroContext::insert(docstring const & name, MacroData const & data)
187 macros_.insert(name, data);