- ParagraphList::const_iterator outpit = priorpit;
- ParagraphList::const_iterator refpit = priorpit;
-
- // We go back until the depth kept increasing or the current layout
- // matches the reference one. When the depth decreases, we should stop
- // only if we started from inside the enclosing environment, otherwise
- // we should continue to go back.
-
- bool topped = false;
- depth_type const refdepth =
- priorpit->getDepth() ? priorpit->getDepth() - 1 : 0;
- while (outpit != pars_begin
- && (outpit->hasSameLayout(*refpit)
- || outpit->getDepth() > pit->getDepth())) {
- outpit = boost::prior(outpit);
- if (outpit->getDepth() == pit->getDepth()
- && outpit->layout() == pit->layout()
- && (priorpit->getAlign() != priorpit->layout().align
- || priorpit->getDepth() > pit->getDepth())) {
- // After skipping either a nested or non-default
- // aligned environment, we ended at an environment with
- // same layout and nesting depth as the current one.
- break;
- }
- if (outpit->getDepth() == refdepth) {
- if (outpit->getDepth() >= pit->getDepth()
- && (priorpit->getDepth() > pit->getDepth()
- || outpit->layout() == refpit->layout())) {
- // The prior environment was nested in
- // another one, or was enclosing some other
- // one, or we are still in it. In any case,
- // we still have to go back.
- refpit = outpit;
- } else {
- topped = true;
- break;
- }
- }
- }
- ParagraphList::const_iterator const inpit =
- topped ? refpit : boost::next(outpit);
- // Check if we stopped at the first paragraph,
- // but we should still go back.
- topped = !topped && (outpit == pars_begin
- && (outpit->hasSameLayout(*refpit)
- || outpit->getDepth() > pit->getDepth())
- && !(outpit->getDepth() == pit->getDepth()
- && outpit->layout() == pit->layout()
- && (priorpit->getAlign() != priorpit->layout().align
- || priorpit->getDepth() > pit->getDepth())));
- Language const * const outenv_language =
- topped ? bparams.language
- : outpit->getParLanguage(bparams);
- Language const * const inenv_language =
- topped ? outpit->getParLanguage(bparams)
- : inpit->getParLanguage(bparams);
- if (outenv_language->babel() != inenv_language->babel())
- return inenv_language;
- return outenv_language;
-}