void Buffer::Impl::updateMacros(DocIterator & it, DocIterator & scope)
{
- pit_type const lastpit = it.lastpit();
-
- // look for macros in each paragraph
- while (it.pit() <= lastpit) {
- Paragraph & par = it.paragraph();
-
- // iterate over the insets of the current paragraph
- for (auto const & insit : par.insetList()) {
- it.pos() = insit.pos;
-
- if (InsetText const * itext = insit.inset->asInsetText()) {
- // collect macros in inset
- it.push_back(CursorSlice(*insit.inset));
- if (itext->producesOutput()) {
- // the simple case
- updateMacros(it, scope);
- } else {
- // We don't want macros declared in notes, comments, etc,
- // to affect anything outside them.
- // New scope which ends just behind the inset
- DocIterator new_scope = it;
- ++new_scope.pos();
- updateMacros(it, new_scope);
- }
- it.pop_back();
- continue;
- }
-
- if (insit.inset->asInsetTabular()) {
- CursorSlice slice(*insit.inset);
- size_t const numcells = slice.nargs();
- for (; slice.idx() < numcells; slice.forwardIdx()) {
- it.push_back(slice);
- updateMacros(it, scope);
- it.pop_back();
- }
- continue;
- }
-
- // is it an external file?
- if (insit.inset->lyxCode() == INCLUDE_CODE) {
- // get buffer of external file
- InsetInclude const & incinset =
- static_cast<InsetInclude const &>(*insit.inset);
- macro_lock = true;
- Buffer * child = incinset.loadIfNeeded();
- macro_lock = false;
- if (!child)
- continue;
-
- // register its position, but only when it is
- // included first in the buffer
- children_positions.insert({child, it});
-
- // register child with its scope
- position_to_children[it] = Impl::ScopeBuffer(scope, child);
- continue;
- }
-
- InsetMath * im = insit.inset->asInsetMath();
- if (doing_export && im) {
- InsetMathHull * hull = im->asHullInset();
- if (hull)
- hull->recordLocation(it);
- }
-
- if (insit.inset->lyxCode() != MATHMACRO_CODE)
- continue;
-
- // get macro data
- InsetMathMacroTemplate & macroTemplate =
- *insit.inset->asInsetMath()->asMacroTemplate();
- MacroContext mc(owner_, it);
- macroTemplate.updateToContext(mc);
-
- // valid?
- bool valid = macroTemplate.validMacro();
- // FIXME: Should be fixNameAndCheckIfValid() in fact,
- // then the BufferView's cursor will be invalid in
- // some cases which leads to crashes.
- if (!valid)
- continue;
-
- // register macro
- macro_table.addMacroDefinition(macroTemplate.name(), it, scope,
- MacroData(const_cast<Buffer *>(owner_), it));
- }
-
- // next paragraph
- it.pit()++;
- it.pos() = 0;
- }
+ owner_->inset().updateMacros(it, scope);
}