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.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.makeSameLayout(par);
167 LASSERT(pos <= par.size(), /**/);
169 if (pos < par.size()) {
170 // move everything behind the break position to the new paragraph
171 pos_type pos_end = par.size() - 1;
173 for (pos_type i = pos, j = 0; i <= pos_end; ++i) {
174 if (moveItem(par, pos, tmp, j, bparams)) {
178 // Move over the end-of-par change information
179 tmp.setChange(tmp.size(), par.lookupChange(par.size()));
180 par.setChange(par.size(), Change(bparams.trackChanges ?
181 Change::INSERTED : Change::UNCHANGED));
186 void mergeParagraph(BufferParams const & bparams,
187 ParagraphList & pars, pit_type par_offset)
189 Paragraph & next = pars[par_offset + 1];
190 Paragraph & par = pars[par_offset];
192 pos_type pos_end = next.size() - 1;
193 pos_type pos_insert = par.size();
195 // the imaginary end-of-paragraph character (at par.size()) has to be
196 // marked as unmodified. Otherwise, its change is adopted by the first
197 // character of the next paragraph.
198 if (par.lookupChange(par.size()).type != Change::UNCHANGED) {
199 LYXERR(Debug::CHANGES,
200 "merging par with inserted/deleted end-of-par character");
201 par.setChange(par.size(), Change(Change::UNCHANGED));
204 Change change = next.lookupChange(next.size());
206 // move the content of the second paragraph to the end of the first one
207 for (pos_type i = 0, j = pos_insert; i <= pos_end; ++i) {
208 if (moveItem(next, 0, par, j, bparams)) {
213 // move the change of the end-of-paragraph character
214 par.setChange(par.size(), change);
216 pars.erase(boost::next(pars.begin(), par_offset + 1));
220 pit_type depthHook(pit_type pit, ParagraphList const & pars, depth_type depth)
222 pit_type newpit = pit;
227 while (newpit != 0 && pars[newpit].getDepth() > depth)
230 if (pars[newpit].getDepth() > depth)
237 pit_type outerHook(pit_type par_offset, ParagraphList const & pars)
239 Paragraph const & par = pars[par_offset];
241 if (par.getDepth() == 0)
243 return depthHook(par_offset, pars, depth_type(par.getDepth() - 1));
247 bool isFirstInSequence(pit_type par_offset, ParagraphList const & pars)
249 Paragraph const & par = pars[par_offset];
251 pit_type dhook_offset = depthHook(par_offset, pars, par.getDepth());
253 if (dhook_offset == par_offset)
256 Paragraph const & dhook = pars[dhook_offset];
258 return dhook.layout() != par.layout()
259 || dhook.getDepth() != par.getDepth();
263 int getEndLabel(pit_type p, ParagraphList const & pars)
266 depth_type par_depth = pars[p].getDepth();
267 while (pit != pit_type(pars.size())) {
268 Layout const & layout = pars[pit].layout();
269 int const endlabeltype = layout.endlabeltype;
271 if (endlabeltype != END_LABEL_NO_LABEL) {
272 if (p + 1 == pit_type(pars.size()))
275 depth_type const next_depth =
276 pars[p + 1].getDepth();
277 if (par_depth > next_depth ||
278 (par_depth == next_depth && layout != pars[p + 1].layout()))
284 pit = outerHook(pit, pars);
285 if (pit != pit_type(pars.size()))
286 par_depth = pars[pit].getDepth();
288 return END_LABEL_NO_LABEL;
292 Font const outerFont(pit_type par_offset, ParagraphList const & pars)
294 depth_type par_depth = pars[par_offset].getDepth();
295 FontInfo tmpfont = inherit_font;
297 // Resolve against environment font information
298 while (par_offset != pit_type(pars.size())
300 && !tmpfont.resolved()) {
301 par_offset = outerHook(par_offset, pars);
302 if (par_offset != pit_type(pars.size())) {
303 tmpfont.realize(pars[par_offset].layout().font);
304 par_depth = pars[par_offset].getDepth();
308 return Font(tmpfont);
312 void acceptChanges(ParagraphList & pars, BufferParams const & bparams)
314 pit_type pars_size = static_cast<pit_type>(pars.size());
316 // first, accept changes within each individual paragraph
317 // (do not consider end-of-par)
318 for (pit_type pit = 0; pit < pars_size; ++pit) {
319 if (!pars[pit].empty()) // prevent assertion failure
320 pars[pit].acceptChanges(bparams, 0, pars[pit].size());
323 // next, accept imaginary end-of-par characters
324 for (pit_type pit = 0; pit < pars_size; ++pit) {
325 pos_type pos = pars[pit].size();
327 if (pars[pit].isInserted(pos)) {
328 pars[pit].setChange(pos, Change(Change::UNCHANGED));
329 } else if (pars[pit].isDeleted(pos)) {
330 if (pit == pars_size - 1) {
331 // we cannot remove a par break at the end of the last
332 // paragraph; instead, we mark it unchanged
333 pars[pit].setChange(pos, Change(Change::UNCHANGED));
335 mergeParagraph(bparams, pars, pit);