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 "BufferList.h"
23 #include "DocIterator.h"
24 #include "InsetList.h"
27 #include "support/debug.h"
28 #include "support/FileName.h"
29 #include "support/lassert.h"
34 using namespace lyx::support;
39 /////////////////////////////////////////////////////////////////////
43 /////////////////////////////////////////////////////////////////////
45 MacroData::MacroData()
46 : queried_(true), numargs_(0), optionals_(0), lockCount_(0),
47 redefinition_(false), type_(MacroTypeNewcommand)
52 MacroData::MacroData(Buffer const & buf, DocIterator const & pos)
53 : buffer_(&buf), pos_(pos), queried_(false), numargs_(0),
54 optionals_(0), lockCount_(0), redefinition_(false),
55 type_(MacroTypeNewcommand)
60 MacroData::MacroData(MathMacroTemplate const & macro)
61 : queried_(false), numargs_(0), optionals_(0), lockCount_(0),
62 redefinition_(false), type_(MacroTypeNewcommand)
68 void MacroData::expand(vector<MathData> const & args, MathData & to) const
72 // Hack. Any inset with a cell would do.
73 static Buffer * buffer = 0;
75 buffer = theBufferList().newBuffer(
76 FileName::tempName().absFilename() + "_macrodata.internal");
77 buffer->setUnnamed(true);
79 static InsetMathSqrt inset;
82 asArray(display_.empty() ? definition_ : display_, inset.cell(0));
83 //lyxerr << "MathData::expand: args: " << args << endl;
84 //lyxerr << "MathData::expand: ar: " << inset.cell(0) << endl;
85 for (DocIterator it = doc_iterator_begin(buffer, &inset); it; it.forwardChar()) {
88 if (it.nextInset()->lyxCode() != MATHMACROARG_CODE)
90 //it.cell().erase(it.pos());
91 //it.cell().insert(it.pos(), it.nextInset()->asInsetMath()
92 size_t n = static_cast<MathMacroArgument*>(it.nextInset())->number();
93 if (n <= args.size()) {
94 it.cell().erase(it.pos());
95 it.cell().insert(it.pos(), args[n - 1]);
98 //lyxerr << "MathData::expand: res: " << inset.cell(0) << endl;
103 size_t MacroData::optionals() const
110 vector<docstring> const & MacroData::defaults() const
117 void MacroData::unlock() const
120 LASSERT(lockCount_ >= 0, /**/);
124 void MacroData::queryData(MathMacroTemplate const & macro) const
130 definition_ = macro.definition();
131 numargs_ = macro.numArgs();
132 display_ = macro.displayDefinition();
133 redefinition_ = macro.redefinition();
134 type_ = macro.type();
135 optionals_ = macro.numOptionals();
137 macro.getDefaults(defaults_);
141 void MacroData::updateData() const
146 LASSERT(buffer_ != 0, /**/);
148 // Try to fix position DocIterator. Should not do anything in theory.
151 // find macro template
152 Inset * inset = pos_.nextInset();
153 if (inset == 0 || inset->lyxCode() != MATHMACRO_CODE) {
154 lyxerr << "BUG: No macro template found by MacroData" << endl;
158 // query the data from the macro template
159 queryData(static_cast<MathMacroTemplate const &>(*inset));
163 void MacroData::write(odocstream & os, bool overwriteRedefinition) const
167 // find macro template
168 Inset * inset = pos_.nextInset();
169 if (inset == 0 || inset->lyxCode() != MATHMACRO_CODE) {
170 lyxerr << "BUG: No macro template found by MacroData" << endl;
175 MathMacroTemplate const & tmpl =
176 static_cast<MathMacroTemplate const &>(*inset);
177 WriteStream wi(os, false, true, false);
178 tmpl.write(wi, overwriteRedefinition);
182 /////////////////////////////////////////////////////////////////////
184 // The global table of macros
186 /////////////////////////////////////////////////////////////////////
188 MacroTable & MacroTable::globalMacros()
190 static MacroTable theGlobalMacros;
191 return theGlobalMacros;
195 MacroData const * MacroTable::get(docstring const & name) const
197 const_iterator it = find(name);
198 return it == end() ? 0 : &it->second;
202 void MacroTable::insert(docstring const & name, MacroData const & data)
204 //lyxerr << "MacroTable::insert: " << to_utf8(name) << endl;
205 operator[](name) = data;
209 void MacroTable::insert(docstring const & def, string const & requires)
211 //lyxerr << "MacroTable::insert, def: " << to_utf8(def) << endl;
212 MathMacroTemplate mac(def);
214 data.requires() = requires;
215 insert(mac.name(), data);
219 void MacroTable::getMacroNames(std::set<docstring> & names) const
221 for (const_iterator it = begin(); it != end(); ++it)
222 names.insert(it->first);
226 void MacroTable::dump()
228 lyxerr << "\n------------------------------------------" << endl;
229 for (const_iterator it = begin(); it != end(); ++it)
230 lyxerr << to_utf8(it->first)
231 << " [" << to_utf8(it->second.definition()) << "] : "
232 << " [" << to_utf8(it->second.display()) << "] : "
234 lyxerr << "------------------------------------------" << endl;
238 /////////////////////////////////////////////////////////////////////
242 /////////////////////////////////////////////////////////////////////
244 MacroContext::MacroContext(Buffer const & buf, DocIterator const & pos)
245 : buf_(buf), pos_(pos)
250 MacroData const * MacroContext::get(docstring const & name) const
252 return buf_.getMacro(name, pos_);