- // if we find an optarg, we'll save it for use later.
- InsetText const * arginset = 0;
- InsetList::const_iterator it = par.insetList().begin();
- InsetList::const_iterator end = par.insetList().end();
- for (; it != end; ++it) {
- Inset & inset = *it->inset;
- dit.pos() = it->pos;
- //lyxerr << (void*)&inset << " code: " << inset.lyxCode() << std::endl;
- inset.addToToc(dit, doing_output, utype);
- if (inset.lyxCode() == ARG_CODE)
- arginset = inset.asInsetText();
+ dit.pos() = 0;
+
+ // Custom AddToToc in paragraph layouts (i.e. theorems)
+ if (par.layout().addToToc() && text().isFirstInSequence(pit)) {
+ pit_type end =
+ openAddToTocForParagraph(pit, dit, output_active, backend);
+ addtotoc_stack.push({pit, end});
+ }
+
+ // If we find an InsetArgument that is supposed to provide the TOC caption,
+ // we'll save it for use later.
+ InsetArgument const * arginset = nullptr;
+ for (auto const & table : par.insetList()) {
+ dit.pos() = table.pos;
+ table.inset->addToToc(dit, doing_output, utype, backend);
+ if (InsetArgument const * x = table.inset->asInsetArgument())
+ if (x->isTocCaption())
+ arginset = x;
+ }
+
+ // End custom AddToToc in paragraph layouts
+ while (!addtotoc_stack.empty() && addtotoc_stack.top().second == pit) {
+ // execute the closing function
+ closeAddToTocForParagraph(addtotoc_stack.top().first,
+ addtotoc_stack.top().second, backend);
+ addtotoc_stack.pop();