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 "InsetMathSqrt.h"
14 #include "MacroTable.h"
15 #include "MathMacroTemplate.h"
16 #include "MathMacroArgument.h"
17 #include "MathStream.h"
18 #include "MathSupport.h"
19 #include "InsetMathNest.h"
22 #include "DocIterator.h"
23 #include "InsetList.h"
26 #include "support/debug.h"
28 #include <boost/assert.hpp>
37 /////////////////////////////////////////////////////////////////////
41 /////////////////////////////////////////////////////////////////////
43 MacroData::MacroData()
44 : queried_(true), numargs_(0), optionals_(0), lockCount_(0),
45 redefinition_(false), type_(MacroTypeNewcommand)
50 MacroData::MacroData(Buffer const & buf, DocIterator const & pos)
51 : buffer_(&buf), pos_(pos), queried_(false), numargs_(0),
52 optionals_(0), lockCount_(0), redefinition_(false),
53 type_(MacroTypeNewcommand)
58 MacroData::MacroData(MathMacroTemplate const & macro)
59 : queried_(false), numargs_(0), optionals_(0), lockCount_(0),
60 redefinition_(false), type_(MacroTypeNewcommand)
66 void MacroData::expand(vector<MathData> const & args, MathData & to) const
70 InsetMathSqrt inset; // Hack. Any inset with a cell would do.
72 asArray(display_.empty() ? definition_ : display_, inset.cell(0));
73 //lyxerr << "MathData::expand: args: " << args << endl;
74 //lyxerr << "MathData::expand: ar: " << inset.cell(0) << endl;
75 for (DocIterator it = doc_iterator_begin(inset); it; it.forwardChar()) {
78 if (it.nextInset()->lyxCode() != MATHMACROARG_CODE)
80 //it.cell().erase(it.pos());
81 //it.cell().insert(it.pos(), it.nextInset()->asInsetMath()
82 size_t n = static_cast<MathMacroArgument*>(it.nextInset())->number();
83 if (n <= args.size()) {
84 it.cell().erase(it.pos());
85 it.cell().insert(it.pos(), args[n - 1]);
88 //lyxerr << "MathData::expand: res: " << inset.cell(0) << endl;
93 size_t MacroData::optionals() const
100 vector<docstring> const & MacroData::defaults() const
107 void MacroData::unlock() const
110 BOOST_ASSERT(lockCount_ >= 0);
114 void MacroData::queryData(MathMacroTemplate const & macro) const
120 definition_ = macro.definition();
121 numargs_ = macro.numArgs();
122 display_ = macro.displayDefinition();
123 redefinition_ = macro.redefinition();
124 type_ = macro.type();
125 optionals_ = macro.numOptionals();
126 macro.getDefaults(defaults_);
130 void MacroData::updateData() const
135 BOOST_ASSERT(buffer_ != 0);
137 // Try to fix position DocIterator. Should not do anything in theory.
140 // find macro template
141 Inset * inset = pos_.nextInset();
142 if (inset == 0 || inset->lyxCode() != MATHMACRO_CODE) {
143 lyxerr << "BUG: No macro template found by MacroData" << endl;
147 // query the data from the macro template
148 queryData(static_cast<MathMacroTemplate const &>(*inset));
152 void MacroData::write(odocstream & os, bool overwriteRedefinition) const
156 // find macro template
157 Inset * inset = pos_.nextInset();
158 if (inset == 0 || inset->lyxCode() != MATHMACRO_CODE) {
159 lyxerr << "BUG: No macro template found by MacroData" << endl;
164 MathMacroTemplate const & tmpl
165 = static_cast<MathMacroTemplate const &>(*inset);
166 WriteStream wi(os, false, true);
167 tmpl.write(wi, overwriteRedefinition);
171 /////////////////////////////////////////////////////////////////////
173 // The global table of macros
175 /////////////////////////////////////////////////////////////////////
177 MacroTable & MacroTable::globalMacros()
179 static MacroTable theGlobalMacros;
180 return theGlobalMacros;
184 MacroData const * MacroTable::get(docstring const & name) const
186 const_iterator it = find(name);
187 return it == end() ? 0 : &it->second;
191 void MacroTable::insert(docstring const & name, MacroData const & data)
193 //lyxerr << "MacroTable::insert: " << to_utf8(name) << endl;
194 operator[](name) = data;
198 void MacroTable::insert(docstring const & def, string const & requires)
200 //lyxerr << "MacroTable::insert, def: " << to_utf8(def) << endl;
201 MathMacroTemplate mac(def);
203 data.requires() = requires;
204 insert(mac.name(), data);
208 void MacroTable::dump()
210 lyxerr << "\n------------------------------------------" << endl;
211 for (const_iterator it = begin(); it != end(); ++it)
212 lyxerr << to_utf8(it->first)
213 << " [" << to_utf8(it->second.definition()) << "] : "
214 << " [" << to_utf8(it->second.display()) << "] : "
216 lyxerr << "------------------------------------------" << endl;
220 /////////////////////////////////////////////////////////////////////
224 /////////////////////////////////////////////////////////////////////
226 MacroContext::MacroContext(Buffer const & buf, DocIterator const & pos)
227 : buf_(buf), pos_(pos)
232 MacroData const * MacroContext::get(docstring const & name) const
234 return buf_.getMacro(name, pos_);