- buffer->listMacroNames(macros);
- MacroNameSet::iterator it = macros.begin();
- MacroNameSet::iterator end = macros.end();
- docstring macro_preamble;
- for (; it != end; ++it) {
- MacroData const * data = buffer->getMacro(*it, pos, true);
- if (data) {
- odocstringstream macro_def;
- data->write(macro_def, false);
- docstring const md = macro_def.str();
- macro_def << endl;
- bool is_newcomm = prefixIs(md, from_ascii("\\newcomm"));
- // assure that \newcommand defs are only added once
- if (!is_newcomm || !preview_->hasMacroDef(md, *buffer)) {
- if (is_newcomm)
- preview_->addMacroDef(md, *buffer);
- macro_preamble.append(macro_def.str());
- }
- }
+ pos.buffer()->listMacroNames(macros);
+
+ // look for math insets and collect definitions for the used macros
+ MacroNameSet defs;
+ DocIterator dit = doc_iterator_begin(pos.buffer(), this);
+ DocIterator const dend = doc_iterator_end(pos.buffer(), this);
+ if (!dit.nextInset())
+ dit.forwardInset();
+ for (; dit != dend; dit.forwardInset()) {
+ InsetMath * im = dit.nextInset()->asInsetMath();
+ InsetMathHull * hull = im ? im->asHullInset() : 0;
+ if (!hull)
+ continue;
+ for (idx_type idx = 0; idx < hull->nargs(); ++idx)
+ hull->usedMacros(hull->cell(idx), pos, macros, defs);