+ // next paragraph
+ it.pit()++;
+ it.pos() = 0;
+ }
+}
+
+
+void Buffer::updateBlockMacros(DocIterator & it, DocIterator & scope) const
+{
+ Paragraph & par = it.paragraph();
+
+ // set scope for macros in this paragraph:
+ // * either the "old" outer scope
+ // * or the scope ending after the environment
+ if (par.layout()->isEnvironment()) {
+ // find end of environment block,
+ DocIterator envEnd = it;
+ pit_type n = it.lastpit() + 1;
+ depth_type depth = par.params().depth();
+ Length const & length = par.params().leftIndent();
+ // looping through the paragraph, basically until
+ // the layout changes or the depth gets smaller.
+ // (the logic of output_latex.cpp's TeXEnvironment)
+ do {
+ envEnd.pit()++;
+ if (envEnd.pit() == n)
+ break;
+ } while (par.layout() == envEnd.paragraph().layout()
+ || depth < envEnd.paragraph().params().depth()
+ || length != envEnd.paragraph().params().leftIndent());
+
+ // collect macros from environment block
+ updateEnvironmentMacros(it, envEnd.pit() - 1, envEnd);
+ } else {
+ // collect macros from paragraph
+ updateEnvironmentMacros(it, it.pit(), scope);
+ }
+}
+
+
+void Buffer::updateInsetMacros(DocIterator & it, DocIterator & scope) const
+{
+ // look for macros in each paragraph
+ pit_type n = it.lastpit() + 1;
+ while (it.pit() < n)
+ updateBlockMacros(it, scope);
+}
+
+
+void Buffer::updateMacros() const
+{
+ if (d->macro_lock)
+ return;
+
+ LYXERR(Debug::DEBUG, "updateMacro of " << d->filename.onlyFileName());
+
+ // start with empty table
+ d->macros.clear();
+ d->children_positions.clear();
+ d->position_to_children.clear();
+
+ // Iterate over buffer, starting with first paragraph
+ // The scope must be bigger than any lookup DocIterator
+ // later. For the global lookup, lastpit+1 is used, hence
+ // we use lastpit+2 here.
+ DocIterator it = par_iterator_begin();
+ DocIterator outerScope = it;
+ outerScope.pit() = outerScope.lastpit() + 2;
+ updateInsetMacros(it, outerScope);
+}
+
+
+void Buffer::updateMacroInstances() const
+{
+ LYXERR(Debug::DEBUG, "updateMacroInstances for " << d->filename.onlyFileName());
+ ParIterator it = par_iterator_begin();
+ ParIterator end = par_iterator_end();
+ for (; it != end; it.forwardPos()) {
+ // look for MathData cells in InsetMathNest insets
+ Inset * inset = it.nextInset();
+ if (!inset)
+ continue;
+
+ InsetMath * minset = inset->asInsetMath();
+ if (!minset)
+ continue;
+
+ // update macro in all cells of the InsetMathNest
+ DocIterator::idx_type n = minset->nargs();
+ MacroContext mc = MacroContext(*this, it);
+ for (DocIterator::idx_type i = 0; i < n; ++i) {
+ MathData & data = minset->cell(i);
+ data.updateMacros(0, mc);