- if (style == next_layout
- // no blank lines before environments!
- || !next_layout.isEnvironment()
- // unless there's a depth change
- // FIXME What we really want to do here is put every \begin and \end
- // tag on a new line (which was not the case with nested environments).
- // But in the present state of play, we don't have access to the
- // information whether the current TeX row is empty or not.
- // For some ideas about how to fix this, see this thread:
- // http://www.mail-archive.com/lyx-devel@lists.lyx.org/msg145787.html
- || nextpar->params().depth() != par.params().depth()) {
- os << '\n';
+ if (!text.inset().getLayout().parbreakIgnored() && !merged_par)
+ // Make sure to start a new line
+ os << breakln;
+ // A newline '\n' is always output before a command,
+ // so avoid doubling it.
+ if (!next_layout.isCommand()) {
+ // Here we now try to avoid spurious empty lines by
+ // outputting a paragraph break only if: (case 1) the
+ // paragraph style allows parbreaks and no \begin, \end
+ // or \item tags are going to follow (i.e., if the next
+ // isn't the first or the current isn't the last
+ // paragraph of an environment or itemize) and the
+ // depth and alignment of the following paragraph is
+ // unchanged, or (case 2) the following is a
+ // non-environment paragraph whose depth is increased
+ // but whose alignment is unchanged, or (case 3) the
+ // paragraph is not an environment and the next one is a
+ // non-itemize-like env at lower depth, or (case 4) the
+ // paragraph is a command not followed by an environment
+ // and the alignment of the current and next paragraph
+ // is unchanged, or (case 5) the current alignment is
+ // changed and a standard paragraph follows.
+ DocumentClass const & tclass = bparams.documentClass();
+ if ((style == next_layout
+ && !style.parbreak_is_newline
+ && !text.inset().getLayout().parbreakIsNewline()
+ && !text.inset().getLayout().parbreakIgnored()
+ && style.latextype != LATEX_ITEM_ENVIRONMENT
+ && style.latextype != LATEX_LIST_ENVIRONMENT
+ && style.align == par.getAlign(bparams)
+ && nextpar->getDepth() == par.getDepth()
+ && nextpar->getAlign(bparams) == par.getAlign(bparams))
+ || (!next_layout.isEnvironment()
+ && nextpar->getDepth() > par.getDepth()
+ && nextpar->getAlign(bparams) == next_layout.align)
+ || (!style.isEnvironment()
+ && next_layout.latextype == LATEX_ENVIRONMENT
+ && nextpar->getDepth() < par.getDepth())
+ || (style.isCommand()
+ && !next_layout.isEnvironment()
+ && style.align == par.getAlign(bparams)
+ && next_layout.align == nextpar->getAlign(bparams))
+ || (style.align != par.getAlign(bparams)
+ && tclass.isDefaultLayout(next_layout))) {
+ // and omit paragraph break if it has been deleted with ct
+ // and changes are not shown in output
+ if (!merged_par)
+ os << '\n';
+ }