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 "support/lassert.h"
27 #include <boost/next_prior.hpp>
33 static bool moveItem(Paragraph & fromPar, pos_type fromPos,
34 Paragraph & toPar, pos_type toPos, BufferParams const & params)
36 // Note: moveItem() does not honour change tracking!
37 // Therefore, it should only be used for breaking and merging paragraphs
39 // We need a copy here because the character at fromPos is going to be erased.
40 Font const tmpFont = fromPar.getFontSettings(params, fromPos);
41 Change const tmpChange = fromPar.lookupChange(fromPos);
43 if (Inset * tmpInset = fromPar.getInset(fromPos)) {
44 fromPar.releaseInset(fromPos);
45 // The inset is not in fromPar any more.
46 if (!toPar.insertInset(toPos, tmpInset, tmpFont, tmpChange)) {
53 char_type const tmpChar = fromPar.getChar(fromPos);
54 fromPar.eraseChar(fromPos, false);
55 toPar.insertChar(toPos, tmpChar, tmpFont, tmpChange);
60 void breakParagraph(BufferParams const & bparams,
61 ParagraphList & pars, pit_type par_offset, pos_type pos,
64 // create a new paragraph, and insert into the list
65 ParagraphList::iterator tmp =
66 pars.insert(boost::next(pars.begin(), par_offset + 1),
69 Paragraph & par = pars[par_offset];
71 // remember to set the inset_owner
72 tmp->setInsetOwner(&par.inInset());
73 // without doing that we get a crash when typing <Return> at the
75 tmp->setPlainOrDefaultLayout(bparams.documentClass());
77 // layout stays the same with latex-environments
79 tmp->setLayout(par.layout());
80 tmp->setLabelWidthString(par.params().labelWidthString());
81 tmp->params().depth(par.params().depth());
82 } else if (par.params().depth() > 0) {
83 Paragraph const & hook = pars[outerHook(par_offset, pars)];
84 tmp->setLayout(hook.layout());
85 // not sure the line below is useful
86 tmp->setLabelWidthString(par.params().labelWidthString());
87 tmp->params().depth(hook.params().depth());
90 bool const isempty = (par.allowEmpty() && par.empty());
92 if (!isempty && (par.size() > pos || par.empty())) {
93 tmp->setLayout(par.layout());
94 tmp->params().align(par.params().align());
95 tmp->setLabelWidthString(par.params().labelWidthString());
97 tmp->params().depth(par.params().depth());
98 tmp->params().noindent(par.params().noindent());
100 // move everything behind the break position
101 // to the new paragraph
103 /* Note: if !keepempty, empty() == true, then we reach
104 * here with size() == 0. So pos_end becomes - 1. This
105 * doesn't cause problems because both loops below
106 * enforce pos <= pos_end and 0 <= pos
108 pos_type pos_end = par.size() - 1;
110 for (pos_type i = pos, j = 0; i <= pos_end; ++i) {
111 if (moveItem(par, pos, *tmp, j, bparams)) {
117 // Move over the end-of-par change information
118 tmp->setChange(tmp->size(), par.lookupChange(par.size()));
119 par.setChange(par.size(), Change(bparams.trackChanges ?
120 Change::INSERTED : Change::UNCHANGED));
123 // Make sure that we keep the language when
124 // breaking paragraph.
126 Font changed = tmp->getFirstFontSettings(bparams);
127 Font const & old = par.getFontSettings(bparams, par.size());
128 changed.setLanguage(old.language());
129 tmp->setFont(0, changed);
136 bool const soa = par.params().startOfAppendix();
137 par.params().clear();
138 // do not lose start of appendix marker (bug 4212)
139 par.params().startOfAppendix(soa);
140 par.setPlainOrDefaultLayout(bparams.documentClass());
143 // layout stays the same with latex-environments
145 par.setLayout(tmp->layout());
146 par.setLabelWidthString(tmp->params().labelWidthString());
147 par.params().depth(tmp->params().depth());
152 void breakParagraphConservative(BufferParams const & bparams,
153 ParagraphList & pars, pit_type par_offset, pos_type pos)
155 // create a new paragraph
156 Paragraph & tmp = *pars.insert(boost::next(pars.begin(), par_offset + 1),
158 Paragraph & par = pars[par_offset];
160 tmp.setInsetOwner(&par.inInset());
161 tmp.makeSameLayout(par);
163 LASSERT(pos <= par.size(), /**/);
165 if (pos < par.size()) {
166 // move everything behind the break position to the new paragraph
167 pos_type pos_end = par.size() - 1;
169 for (pos_type i = pos, j = 0; i <= pos_end; ++i) {
170 if (moveItem(par, pos, tmp, j, bparams)) {
174 // Move over the end-of-par change information
175 tmp.setChange(tmp.size(), par.lookupChange(par.size()));
176 par.setChange(par.size(), Change(bparams.trackChanges ?
177 Change::INSERTED : Change::UNCHANGED));
182 void mergeParagraph(BufferParams const & bparams,
183 ParagraphList & pars, pit_type par_offset)
185 Paragraph & next = pars[par_offset + 1];
186 Paragraph & par = pars[par_offset];
188 pos_type pos_end = next.size() - 1;
189 pos_type pos_insert = par.size();
191 // the imaginary end-of-paragraph character (at par.size()) has to be
192 // marked as unmodified. Otherwise, its change is adopted by the first
193 // character of the next paragraph.
194 if (par.lookupChange(par.size()).type != Change::UNCHANGED) {
195 LYXERR(Debug::CHANGES,
196 "merging par with inserted/deleted end-of-par character");
197 par.setChange(par.size(), Change(Change::UNCHANGED));
200 Change change = next.lookupChange(next.size());
202 // move the content of the second paragraph to the end of the first one
203 for (pos_type i = 0, j = pos_insert; i <= pos_end; ++i) {
204 if (moveItem(next, 0, par, j, bparams)) {
209 // move the change of the end-of-paragraph character
210 par.setChange(par.size(), change);
212 pars.erase(boost::next(pars.begin(), par_offset + 1));
216 pit_type depthHook(pit_type pit, ParagraphList const & pars, depth_type depth)
218 pit_type newpit = pit;
223 while (newpit != 0 && pars[newpit].getDepth() > depth)
226 if (pars[newpit].getDepth() > depth)
233 pit_type outerHook(pit_type par_offset, ParagraphList const & pars)
235 Paragraph const & par = pars[par_offset];
237 if (par.getDepth() == 0)
239 return depthHook(par_offset, pars, depth_type(par.getDepth() - 1));
243 bool isFirstInSequence(pit_type par_offset, ParagraphList const & pars)
245 Paragraph const & par = pars[par_offset];
247 pit_type dhook_offset = depthHook(par_offset, pars, par.getDepth());
249 if (dhook_offset == par_offset)
252 Paragraph const & dhook = pars[dhook_offset];
254 return dhook.layout() != par.layout()
255 || dhook.getDepth() != par.getDepth();
259 int getEndLabel(pit_type p, ParagraphList const & pars)
262 depth_type par_depth = pars[p].getDepth();
263 while (pit != pit_type(pars.size())) {
264 Layout const & layout = pars[pit].layout();
265 int const endlabeltype = layout.endlabeltype;
267 if (endlabeltype != END_LABEL_NO_LABEL) {
268 if (p + 1 == pit_type(pars.size()))
271 depth_type const next_depth =
272 pars[p + 1].getDepth();
273 if (par_depth > next_depth ||
274 (par_depth == next_depth && layout != pars[p + 1].layout()))
280 pit = outerHook(pit, pars);
281 if (pit != pit_type(pars.size()))
282 par_depth = pars[pit].getDepth();
284 return END_LABEL_NO_LABEL;
288 Font const outerFont(pit_type par_offset, ParagraphList const & pars)
290 depth_type par_depth = pars[par_offset].getDepth();
291 FontInfo tmpfont = inherit_font;
293 // Resolve against environment font information
294 while (par_offset != pit_type(pars.size())
296 && !tmpfont.resolved()) {
297 par_offset = outerHook(par_offset, pars);
298 if (par_offset != pit_type(pars.size())) {
299 tmpfont.realize(pars[par_offset].layout().font);
300 par_depth = pars[par_offset].getDepth();
304 return Font(tmpfont);
308 void acceptChanges(ParagraphList & pars, BufferParams const & bparams)
310 pit_type pars_size = static_cast<pit_type>(pars.size());
312 // first, accept changes within each individual paragraph
313 // (do not consider end-of-par)
314 for (pit_type pit = 0; pit < pars_size; ++pit) {
315 if (!pars[pit].empty()) // prevent assertion failure
316 pars[pit].acceptChanges(bparams, 0, pars[pit].size());
319 // next, accept imaginary end-of-par characters
320 for (pit_type pit = 0; pit < pars_size; ++pit) {
321 pos_type pos = pars[pit].size();
323 if (pars[pit].isInserted(pos)) {
324 pars[pit].setChange(pos, Change(Change::UNCHANGED));
325 } else if (pars[pit].isDeleted(pos)) {
326 if (pit == pars_size - 1) {
327 // we cannot remove a par break at the end of the last
328 // paragraph; instead, we mark it unchanged
329 pars[pit].setChange(pos, Change(Change::UNCHANGED));
331 mergeParagraph(bparams, pars, pit);