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 char_type const tmpChar = fromPar.getChar(fromPos);
40 Font const tmpFont = fromPar.getFontSettings(params, fromPos);
41 Change const tmpChange = fromPar.lookupChange(fromPos);
43 if (fromPar.isInset(fromPos)) {
45 if (fromPar.getInset(fromPos)) {
46 // the inset is not in the paragraph any more
47 tmpInset = fromPar.releaseInset(fromPos);
50 if (!toPar.inInset().insetAllowed(tmpInset->lyxCode())) {
55 toPar.insertInset(toPos, tmpInset, tmpFont, tmpChange);
57 fromPar.eraseChar(fromPos, false);
58 toPar.insertChar(toPos, tmpChar, tmpFont, tmpChange);
65 void breakParagraph(BufferParams const & bparams,
66 ParagraphList & pars, pit_type par_offset, pos_type pos,
69 // create a new paragraph, and insert into the list
70 ParagraphList::iterator tmp =
71 pars.insert(boost::next(pars.begin(), par_offset + 1),
74 Paragraph & par = pars[par_offset];
76 // remember to set the inset_owner
77 tmp->setInsetOwner(&par.inInset());
78 // without doing that we get a crash when typing <Return> at the
80 tmp->setPlainOrDefaultLayout(bparams.documentClass());
82 // layout stays the same with latex-environments
84 tmp->setLayout(par.layout());
85 tmp->setLabelWidthString(par.params().labelWidthString());
86 tmp->params().depth(par.params().depth());
87 } else if (par.params().depth() > 0) {
88 Paragraph const & hook = pars[outerHook(par_offset, pars)];
89 tmp->setLayout(hook.layout());
90 // not sure the line below is useful
91 tmp->setLabelWidthString(par.params().labelWidthString());
92 tmp->params().depth(hook.params().depth());
95 bool const isempty = (par.allowEmpty() && par.empty());
97 if (!isempty && (par.size() > pos || par.empty())) {
98 tmp->setLayout(par.layout());
99 tmp->params().align(par.params().align());
100 tmp->setLabelWidthString(par.params().labelWidthString());
102 tmp->params().depth(par.params().depth());
103 tmp->params().noindent(par.params().noindent());
105 // move everything behind the break position
106 // to the new paragraph
108 /* Note: if !keepempty, empty() == true, then we reach
109 * here with size() == 0. So pos_end becomes - 1. This
110 * doesn't cause problems because both loops below
111 * enforce pos <= pos_end and 0 <= pos
113 pos_type pos_end = par.size() - 1;
115 for (pos_type i = pos, j = 0; i <= pos_end; ++i) {
116 if (moveItem(par, pos, *tmp, j, bparams)) {
122 // Move over the end-of-par change information
123 tmp->setChange(tmp->size(), par.lookupChange(par.size()));
124 par.setChange(par.size(), Change(bparams.trackChanges ?
125 Change::INSERTED : Change::UNCHANGED));
128 // Make sure that we keep the language when
129 // breaking paragraph.
131 Font changed = tmp->getFirstFontSettings(bparams);
132 Font old = par.getFontSettings(bparams, par.size());
133 changed.setLanguage(old.language());
134 tmp->setFont(0, changed);
141 bool const soa = par.params().startOfAppendix();
142 par.params().clear();
143 // do not lose start of appendix marker (bug 4212)
144 par.params().startOfAppendix(soa);
145 par.setPlainOrDefaultLayout(bparams.documentClass());
148 // layout stays the same with latex-environments
150 par.setLayout(tmp->layout());
151 par.setLabelWidthString(tmp->params().labelWidthString());
152 par.params().depth(tmp->params().depth());
157 void breakParagraphConservative(BufferParams const & bparams,
158 ParagraphList & pars, pit_type par_offset, pos_type pos)
160 // create a new paragraph
161 Paragraph & tmp = *pars.insert(boost::next(pars.begin(), par_offset + 1),
163 Paragraph & par = pars[par_offset];
165 tmp.setInsetOwner(&par.inInset());
166 tmp.makeSameLayout(par);
168 LASSERT(pos <= par.size(), /**/);
170 if (pos < par.size()) {
171 // move everything behind the break position to the new paragraph
172 pos_type pos_end = par.size() - 1;
174 for (pos_type i = pos, j = 0; i <= pos_end; ++i) {
175 if (moveItem(par, pos, tmp, j, bparams)) {
179 // Move over the end-of-par change information
180 tmp.setChange(tmp.size(), par.lookupChange(par.size()));
181 par.setChange(par.size(), Change(bparams.trackChanges ?
182 Change::INSERTED : Change::UNCHANGED));
187 void mergeParagraph(BufferParams const & bparams,
188 ParagraphList & pars, pit_type par_offset)
190 Paragraph & next = pars[par_offset + 1];
191 Paragraph & par = pars[par_offset];
193 pos_type pos_end = next.size() - 1;
194 pos_type pos_insert = par.size();
196 // the imaginary end-of-paragraph character (at par.size()) has to be
197 // marked as unmodified. Otherwise, its change is adopted by the first
198 // character of the next paragraph.
199 if (par.lookupChange(par.size()).type != Change::UNCHANGED) {
200 LYXERR(Debug::CHANGES,
201 "merging par with inserted/deleted end-of-par character");
202 par.setChange(par.size(), Change(Change::UNCHANGED));
205 Change change = next.lookupChange(next.size());
207 // move the content of the second paragraph to the end of the first one
208 for (pos_type i = 0, j = pos_insert; i <= pos_end; ++i) {
209 if (moveItem(next, 0, par, j, bparams)) {
214 // move the change of the end-of-paragraph character
215 par.setChange(par.size(), change);
217 pars.erase(boost::next(pars.begin(), par_offset + 1));
221 pit_type depthHook(pit_type pit, ParagraphList const & pars, depth_type depth)
223 pit_type newpit = pit;
228 while (newpit != 0 && pars[newpit].getDepth() > depth)
231 if (pars[newpit].getDepth() > depth)
238 pit_type outerHook(pit_type par_offset, ParagraphList const & pars)
240 Paragraph const & par = pars[par_offset];
242 if (par.getDepth() == 0)
244 return depthHook(par_offset, pars, depth_type(par.getDepth() - 1));
248 bool isFirstInSequence(pit_type par_offset, ParagraphList const & pars)
250 Paragraph const & par = pars[par_offset];
252 pit_type dhook_offset = depthHook(par_offset, pars, par.getDepth());
254 if (dhook_offset == par_offset)
257 Paragraph const & dhook = pars[dhook_offset];
259 return dhook.layout() != par.layout()
260 || dhook.getDepth() != par.getDepth();
264 int getEndLabel(pit_type p, ParagraphList const & pars)
267 depth_type par_depth = pars[p].getDepth();
268 while (pit != pit_type(pars.size())) {
269 Layout const & layout = pars[pit].layout();
270 int const endlabeltype = layout.endlabeltype;
272 if (endlabeltype != END_LABEL_NO_LABEL) {
273 if (p + 1 == pit_type(pars.size()))
276 depth_type const next_depth =
277 pars[p + 1].getDepth();
278 if (par_depth > next_depth ||
279 (par_depth == next_depth && layout != pars[p + 1].layout()))
285 pit = outerHook(pit, pars);
286 if (pit != pit_type(pars.size()))
287 par_depth = pars[pit].getDepth();
289 return END_LABEL_NO_LABEL;
293 Font const outerFont(pit_type par_offset, ParagraphList const & pars)
295 depth_type par_depth = pars[par_offset].getDepth();
296 FontInfo tmpfont = inherit_font;
298 // Resolve against environment font information
299 while (par_offset != pit_type(pars.size())
301 && !tmpfont.resolved()) {
302 par_offset = outerHook(par_offset, pars);
303 if (par_offset != pit_type(pars.size())) {
304 tmpfont.realize(pars[par_offset].layout().font);
305 par_depth = pars[par_offset].getDepth();
309 return Font(tmpfont);
313 void acceptChanges(ParagraphList & pars, BufferParams const & bparams)
315 pit_type pars_size = static_cast<pit_type>(pars.size());
317 // first, accept changes within each individual paragraph
318 // (do not consider end-of-par)
319 for (pit_type pit = 0; pit < pars_size; ++pit) {
320 if (!pars[pit].empty()) // prevent assertion failure
321 pars[pit].acceptChanges(bparams, 0, pars[pit].size());
324 // next, accept imaginary end-of-par characters
325 for (pit_type pit = 0; pit < pars_size; ++pit) {
326 pos_type pos = pars[pit].size();
328 if (pars[pit].isInserted(pos)) {
329 pars[pit].setChange(pos, Change(Change::UNCHANGED));
330 } else if (pars[pit].isDeleted(pos)) {
331 if (pit == pars_size - 1) {
332 // we cannot remove a par break at the end of the last
333 // paragraph; instead, we mark it unchanged
334 pars[pit].setChange(pos, Change(Change::UNCHANGED));
336 mergeParagraph(bparams, pars, pit);