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"
23 #include "DocIterator.h"
25 #include <boost/assert.hpp>
33 using std::istringstream;
41 MacroData::MacroData()
42 : numargs_(0), lockCount_(0), redefinition_(false)
46 MacroData::MacroData(docstring const & definition, std::vector<docstring> const & defaults,
47 int numargs, int optionals, docstring const & display, string const & requires)
48 : definition_(definition), numargs_(numargs), display_(display),
49 requires_(requires), lockCount_(0), redefinition_(false), optionals_(optionals),
52 defaults_.resize(optionals);
56 void MacroData::expand(vector<MathData> const & args, MathData & to) const
58 InsetMathSqrt inset; // Hack. Any inset with a cell would do.
60 asArray(display_.empty() ? definition_ : display_, inset.cell(0));
61 //lyxerr << "MathData::expand: args: " << args << endl;
62 //lyxerr << "MathData::expand: ar: " << inset.cell(0) << endl;
63 for (DocIterator it = doc_iterator_begin(inset); it; it.forwardChar()) {
66 if (it.nextInset()->lyxCode() != MATHMACROARG_CODE)
68 //it.cell().erase(it.pos());
69 //it.cell().insert(it.pos(), it.nextInset()->asInsetMath()
70 size_t n = static_cast<MathMacroArgument*>(it.nextInset())->number();
71 if (n <= args.size()) {
72 it.cell().erase(it.pos());
73 it.cell().insert(it.pos(), args[n - 1]);
76 //lyxerr << "MathData::expand: res: " << inset.cell(0) << endl;
81 int MacroData::optionals() const
87 std::vector<docstring> const & MacroData::defaults() const
94 MacroTable & MacroTable::globalMacros()
96 static MacroTable theGlobalMacros;
97 return theGlobalMacros;
101 bool MacroTable::has(docstring const & name) const
103 return find(name) != end();
107 MacroData const & MacroTable::get(docstring const & name) const
109 const_iterator it = find(name);
110 BOOST_ASSERT(it != end());
115 void MacroTable::insert(docstring const & name, MacroData const & data)
117 //lyxerr << "MacroTable::insert: " << to_utf8(name) << endl;
118 operator[](name) = data;
122 void MacroTable::insert(docstring const & def, string const & requires)
124 //lyxerr << "MacroTable::insert, def: " << to_utf8(def) << endl;
125 MathMacroTemplate mac(def);
126 MacroData data = mac.asMacroData();
127 data.requires() = requires;
128 insert(mac.name(), data);
132 void MacroTable::dump()
134 lyxerr << "\n------------------------------------------" << endl;
135 for (const_iterator it = begin(); it != end(); ++it)
136 lyxerr << to_utf8(it->first)
137 << " [" << to_utf8(it->second.definition()) << "] : "
138 << " [" << to_utf8(it->second.display()) << "] : "
140 lyxerr << "------------------------------------------" << endl;
144 MacroContext::MacroContext(Buffer const & buf, Paragraph const & par)
145 : buf_(buf), par_(par)
150 bool MacroContext::has(docstring const & name) const
152 // check if it's a local macro
153 if (macros_.has(name))
156 // otherwise ask the buffer
157 return buf_.hasMacro(name, par_);
161 MacroData const & MacroContext::get(docstring const & name) const
163 // check if it's a local macro
164 if (macros_.has(name))
165 return macros_.get(name);
167 // ask the buffer for its macros
168 return buf_.getMacro(name, par_);
172 void MacroContext::insert(docstring const & name, MacroData const & data)
174 macros_.insert(name, data);