2 * \file paragraph_funcs.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Lars Gullik Bjønnes
8 * Full author contact details are available in file CREDITS.
13 #include "paragraph_funcs.h"
15 #include "BufferParams.h"
17 #include "InsetList.h"
19 #include "Paragraph.h"
20 #include "ParagraphParameters.h"
22 #include "TextClass.h"
24 #include "support/debug.h"
26 #include <boost/next_prior.hpp>
32 static bool moveItem(Paragraph & fromPar, pos_type fromPos,
33 Paragraph & toPar, pos_type toPos, BufferParams const & params)
35 // Note: moveItem() does not honour change tracking!
36 // Therefore, it should only be used for breaking and merging paragraphs
38 char_type const tmpChar = fromPar.getChar(fromPos);
39 Font const tmpFont = fromPar.getFontSettings(params, fromPos);
40 Change const tmpChange = fromPar.lookupChange(fromPos);
42 if (fromPar.isInset(fromPos)) {
44 if (fromPar.getInset(fromPos)) {
45 // the inset is not in the paragraph any more
46 tmpInset = fromPar.releaseInset(fromPos);
49 if (!toPar.insetAllowed(tmpInset->lyxCode())) {
54 toPar.insertInset(toPos, tmpInset, tmpFont, tmpChange);
56 fromPar.eraseChar(fromPos, false);
57 toPar.insertChar(toPos, tmpChar, tmpFont, tmpChange);
64 void breakParagraph(BufferParams const & bparams,
65 ParagraphList & pars, pit_type par_offset, pos_type pos,
68 // create a new paragraph, and insert into the list
69 ParagraphList::iterator tmp =
70 pars.insert(boost::next(pars.begin(), par_offset + 1),
73 Paragraph & par = pars[par_offset];
75 // remember to set the inset_owner
76 tmp->setInsetOwner(par.inInset());
77 // without doing that we get a crash when typing <Return> at the
79 if (par.useEmptyLayout())
80 tmp->layout(bparams.getTextClass().emptyLayout());
82 tmp->layout(bparams.getTextClass().defaultLayout());
84 // layout stays the same with latex-environments
86 tmp->layout(par.layout());
87 tmp->setLabelWidthString(par.params().labelWidthString());
88 tmp->params().depth(par.params().depth());
89 } else if (par.params().depth() > 0) {
90 Paragraph const & hook = pars[outerHook(par_offset, pars)];
91 tmp->layout(hook.layout());
92 // not sure the line below is useful
93 tmp->setLabelWidthString(par.params().labelWidthString());
94 tmp->params().depth(hook.params().depth());
97 bool const isempty = (par.allowEmpty() && par.empty());
99 if (!isempty && (par.size() > pos || par.empty())) {
100 tmp->layout(par.layout());
101 tmp->params().align(par.params().align());
102 tmp->setLabelWidthString(par.params().labelWidthString());
104 tmp->params().depth(par.params().depth());
105 tmp->params().noindent(par.params().noindent());
107 // move everything behind the break position
108 // to the new paragraph
110 /* Note: if !keepempty, empty() == true, then we reach
111 * here with size() == 0. So pos_end becomes - 1. This
112 * doesn't cause problems because both loops below
113 * enforce pos <= pos_end and 0 <= pos
115 pos_type pos_end = par.size() - 1;
117 for (pos_type i = pos, j = 0; i <= pos_end; ++i) {
118 if (moveItem(par, pos, *tmp, j, bparams)) {
124 // Move over the end-of-par change information
125 tmp->setChange(tmp->size(), par.lookupChange(par.size()));
126 par.setChange(par.size(), Change(bparams.trackChanges ?
127 Change::INSERTED : Change::UNCHANGED));
130 // Make sure that we keep the language when
131 // breaking paragraph.
133 Font changed = tmp->getFirstFontSettings(bparams);
134 Font old = par.getFontSettings(bparams, par.size());
135 changed.setLanguage(old.language());
136 tmp->setFont(0, changed);
143 bool const soa = par.params().startOfAppendix();
144 par.params().clear();
145 // do not lose start of appendix marker (bug 4212)
146 par.params().startOfAppendix(soa);
147 par.layout(bparams.getTextClass().defaultLayout());
150 // layout stays the same with latex-environments
152 par.layout(tmp->layout());
153 par.setLabelWidthString(tmp->params().labelWidthString());
154 par.params().depth(tmp->params().depth());
159 void breakParagraphConservative(BufferParams const & bparams,
160 ParagraphList & pars, pit_type par_offset, pos_type pos)
162 // create a new paragraph
163 Paragraph & tmp = *pars.insert(boost::next(pars.begin(), par_offset + 1),
165 Paragraph & par = pars[par_offset];
167 tmp.makeSameLayout(par);
169 BOOST_ASSERT(pos <= par.size());
171 if (pos < par.size()) {
172 // move everything behind the break position to the new paragraph
173 pos_type pos_end = par.size() - 1;
175 for (pos_type i = pos, j = 0; i <= pos_end; ++i) {
176 if (moveItem(par, pos, tmp, j, bparams)) {
180 // Move over the end-of-par change information
181 tmp.setChange(tmp.size(), par.lookupChange(par.size()));
182 par.setChange(par.size(), Change(bparams.trackChanges ?
183 Change::INSERTED : Change::UNCHANGED));
188 void mergeParagraph(BufferParams const & bparams,
189 ParagraphList & pars, pit_type par_offset)
191 Paragraph & next = pars[par_offset + 1];
192 Paragraph & par = pars[par_offset];
194 pos_type pos_end = next.size() - 1;
195 pos_type pos_insert = par.size();
197 // the imaginary end-of-paragraph character (at par.size()) has to be
198 // marked as unmodified. Otherwise, its change is adopted by the first
199 // character of the next paragraph.
200 if (par.lookupChange(par.size()).type != Change::UNCHANGED) {
201 LYXERR(Debug::CHANGES,
202 "merging par with inserted/deleted end-of-par character");
203 par.setChange(par.size(), Change(Change::UNCHANGED));
206 Change change = next.lookupChange(next.size());
208 // move the content of the second paragraph to the end of the first one
209 for (pos_type i = 0, j = pos_insert; i <= pos_end; ++i) {
210 if (moveItem(next, 0, par, j, bparams)) {
215 // move the change of the end-of-paragraph character
216 par.setChange(par.size(), change);
218 pars.erase(boost::next(pars.begin(), par_offset + 1));
222 pit_type depthHook(pit_type pit, ParagraphList const & pars, depth_type depth)
224 pit_type newpit = pit;
229 while (newpit != 0 && pars[newpit].getDepth() > depth)
232 if (pars[newpit].getDepth() > depth)
239 pit_type outerHook(pit_type par_offset, ParagraphList const & pars)
241 Paragraph const & par = pars[par_offset];
243 if (par.getDepth() == 0)
245 return depthHook(par_offset, pars, depth_type(par.getDepth() - 1));
249 bool isFirstInSequence(pit_type par_offset, ParagraphList const & pars)
251 Paragraph const & par = pars[par_offset];
253 pit_type dhook_offset = depthHook(par_offset, pars, par.getDepth());
255 if (dhook_offset == par_offset)
258 Paragraph const & dhook = pars[dhook_offset];
260 return dhook.layout() != par.layout()
261 || dhook.getDepth() != par.getDepth();
265 int getEndLabel(pit_type p, ParagraphList const & pars)
268 depth_type par_depth = pars[p].getDepth();
269 while (pit != pit_type(pars.size())) {
270 LayoutPtr const & layout = pars[pit].layout();
271 int const endlabeltype = layout->endlabeltype;
273 if (endlabeltype != END_LABEL_NO_LABEL) {
274 if (p + 1 == pit_type(pars.size()))
277 depth_type const next_depth =
278 pars[p + 1].getDepth();
279 if (par_depth > next_depth ||
280 (par_depth == next_depth && layout != pars[p + 1].layout()))
286 pit = outerHook(pit, pars);
287 if (pit != pit_type(pars.size()))
288 par_depth = pars[pit].getDepth();
290 return END_LABEL_NO_LABEL;
294 Font const outerFont(pit_type par_offset, ParagraphList const & pars)
296 depth_type par_depth = pars[par_offset].getDepth();
297 FontInfo tmpfont = inherit_font;
299 // Resolve against environment font information
300 while (par_offset != pit_type(pars.size())
302 && !tmpfont.resolved()) {
303 par_offset = outerHook(par_offset, pars);
304 if (par_offset != pit_type(pars.size())) {
305 tmpfont.realize(pars[par_offset].layout()->font);
306 par_depth = pars[par_offset].getDepth();
310 return Font(tmpfont);
314 void acceptChanges(ParagraphList & pars, BufferParams const & bparams)
316 pit_type pars_size = static_cast<pit_type>(pars.size());
318 // first, accept changes within each individual paragraph
319 // (do not consider end-of-par)
320 for (pit_type pit = 0; pit < pars_size; ++pit) {
321 if (!pars[pit].empty()) // prevent assertion failure
322 pars[pit].acceptChanges(bparams, 0, pars[pit].size());
325 // next, accept imaginary end-of-par characters
326 for (pit_type pit = 0; pit < pars_size; ++pit) {
327 pos_type pos = pars[pit].size();
329 if (pars[pit].isInserted(pos)) {
330 pars[pit].setChange(pos, Change(Change::UNCHANGED));
331 } else if (pars[pit].isDeleted(pos)) {
332 if (pit == pars_size - 1) {
333 // we cannot remove a par break at the end of the last
334 // paragraph; instead, we mark it unchanged
335 pars[pit].setChange(pos, Change(Change::UNCHANGED));
337 mergeParagraph(bparams, pars, pit);