- docstring const snippet = str.str();
- preview_->addPreview(snippet, *pos.buffer());
+
+ // collect macros at this position
+ MacroNameSet macros;
+ 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);
+ }
+ MacroNameSet::iterator it = defs.begin();
+ MacroNameSet::iterator end = defs.end();
+ docstring macro_preamble;
+ for (; it != end; ++it)
+ macro_preamble.append(*it);
+
+ docstring const snippet = macro_preamble + str.str();
+ preview_->addPreview(snippet, *pos.buffer());