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 tmp->setEmptyOrDefaultLayout(bparams.documentClass());
81 // layout stays the same with latex-environments
83 tmp->setLayout(par.layout());
84 tmp->setLabelWidthString(par.params().labelWidthString());
85 tmp->params().depth(par.params().depth());
86 } else if (par.params().depth() > 0) {
87 Paragraph const & hook = pars[outerHook(par_offset, pars)];
88 tmp->setLayout(hook.layout());
89 // not sure the line below is useful
90 tmp->setLabelWidthString(par.params().labelWidthString());
91 tmp->params().depth(hook.params().depth());
94 bool const isempty = (par.allowEmpty() && par.empty());
96 if (!isempty && (par.size() > pos || par.empty())) {
97 tmp->setLayout(par.layout());
98 tmp->params().align(par.params().align());
99 tmp->setLabelWidthString(par.params().labelWidthString());
101 tmp->params().depth(par.params().depth());
102 tmp->params().noindent(par.params().noindent());
104 // move everything behind the break position
105 // to the new paragraph
107 /* Note: if !keepempty, empty() == true, then we reach
108 * here with size() == 0. So pos_end becomes - 1. This
109 * doesn't cause problems because both loops below
110 * enforce pos <= pos_end and 0 <= pos
112 pos_type pos_end = par.size() - 1;
114 for (pos_type i = pos, j = 0; i <= pos_end; ++i) {
115 if (moveItem(par, pos, *tmp, j, bparams)) {
121 // Move over the end-of-par change information
122 tmp->setChange(tmp->size(), par.lookupChange(par.size()));
123 par.setChange(par.size(), Change(bparams.trackChanges ?
124 Change::INSERTED : Change::UNCHANGED));
127 // Make sure that we keep the language when
128 // breaking paragraph.
130 Font changed = tmp->getFirstFontSettings(bparams);
131 Font old = par.getFontSettings(bparams, par.size());
132 changed.setLanguage(old.language());
133 tmp->setFont(0, changed);
140 bool const soa = par.params().startOfAppendix();
141 par.params().clear();
142 // do not lose start of appendix marker (bug 4212)
143 par.params().startOfAppendix(soa);
144 par.setEmptyOrDefaultLayout(bparams.documentClass());
147 // layout stays the same with latex-environments
149 par.setLayout(tmp->layout());
150 par.setLabelWidthString(tmp->params().labelWidthString());
151 par.params().depth(tmp->params().depth());
156 void breakParagraphConservative(BufferParams const & bparams,
157 ParagraphList & pars, pit_type par_offset, pos_type pos)
159 // create a new paragraph
160 Paragraph & tmp = *pars.insert(boost::next(pars.begin(), par_offset + 1),
162 Paragraph & par = pars[par_offset];
164 tmp.makeSameLayout(par);
166 BOOST_ASSERT(pos <= par.size());
168 if (pos < par.size()) {
169 // move everything behind the break position to the new paragraph
170 pos_type pos_end = par.size() - 1;
172 for (pos_type i = pos, j = 0; i <= pos_end; ++i) {
173 if (moveItem(par, pos, tmp, j, bparams)) {
177 // Move over the end-of-par change information
178 tmp.setChange(tmp.size(), par.lookupChange(par.size()));
179 par.setChange(par.size(), Change(bparams.trackChanges ?
180 Change::INSERTED : Change::UNCHANGED));
185 void mergeParagraph(BufferParams const & bparams,
186 ParagraphList & pars, pit_type par_offset)
188 Paragraph & next = pars[par_offset + 1];
189 Paragraph & par = pars[par_offset];
191 pos_type pos_end = next.size() - 1;
192 pos_type pos_insert = par.size();
194 // the imaginary end-of-paragraph character (at par.size()) has to be
195 // marked as unmodified. Otherwise, its change is adopted by the first
196 // character of the next paragraph.
197 if (par.lookupChange(par.size()).type != Change::UNCHANGED) {
198 LYXERR(Debug::CHANGES,
199 "merging par with inserted/deleted end-of-par character");
200 par.setChange(par.size(), Change(Change::UNCHANGED));
203 Change change = next.lookupChange(next.size());
205 // move the content of the second paragraph to the end of the first one
206 for (pos_type i = 0, j = pos_insert; i <= pos_end; ++i) {
207 if (moveItem(next, 0, par, j, bparams)) {
212 // move the change of the end-of-paragraph character
213 par.setChange(par.size(), change);
215 pars.erase(boost::next(pars.begin(), par_offset + 1));
219 pit_type depthHook(pit_type pit, ParagraphList const & pars, depth_type depth)
221 pit_type newpit = pit;
226 while (newpit != 0 && pars[newpit].getDepth() > depth)
229 if (pars[newpit].getDepth() > depth)
236 pit_type outerHook(pit_type par_offset, ParagraphList const & pars)
238 Paragraph const & par = pars[par_offset];
240 if (par.getDepth() == 0)
242 return depthHook(par_offset, pars, depth_type(par.getDepth() - 1));
246 bool isFirstInSequence(pit_type par_offset, ParagraphList const & pars)
248 Paragraph const & par = pars[par_offset];
250 pit_type dhook_offset = depthHook(par_offset, pars, par.getDepth());
252 if (dhook_offset == par_offset)
255 Paragraph const & dhook = pars[dhook_offset];
257 return dhook.layout() != par.layout()
258 || dhook.getDepth() != par.getDepth();
262 int getEndLabel(pit_type p, ParagraphList const & pars)
265 depth_type par_depth = pars[p].getDepth();
266 while (pit != pit_type(pars.size())) {
267 Layout const & layout = pars[pit].layout();
268 int const endlabeltype = layout.endlabeltype;
270 if (endlabeltype != END_LABEL_NO_LABEL) {
271 if (p + 1 == pit_type(pars.size()))
274 depth_type const next_depth =
275 pars[p + 1].getDepth();
276 if (par_depth > next_depth ||
277 (par_depth == next_depth && layout != pars[p + 1].layout()))
283 pit = outerHook(pit, pars);
284 if (pit != pit_type(pars.size()))
285 par_depth = pars[pit].getDepth();
287 return END_LABEL_NO_LABEL;
291 Font const outerFont(pit_type par_offset, ParagraphList const & pars)
293 depth_type par_depth = pars[par_offset].getDepth();
294 FontInfo tmpfont = inherit_font;
296 // Resolve against environment font information
297 while (par_offset != pit_type(pars.size())
299 && !tmpfont.resolved()) {
300 par_offset = outerHook(par_offset, pars);
301 if (par_offset != pit_type(pars.size())) {
302 tmpfont.realize(pars[par_offset].layout().font);
303 par_depth = pars[par_offset].getDepth();
307 return Font(tmpfont);
311 void acceptChanges(ParagraphList & pars, BufferParams const & bparams)
313 pit_type pars_size = static_cast<pit_type>(pars.size());
315 // first, accept changes within each individual paragraph
316 // (do not consider end-of-par)
317 for (pit_type pit = 0; pit < pars_size; ++pit) {
318 if (!pars[pit].empty()) // prevent assertion failure
319 pars[pit].acceptChanges(bparams, 0, pars[pit].size());
322 // next, accept imaginary end-of-par characters
323 for (pit_type pit = 0; pit < pars_size; ++pit) {
324 pos_type pos = pars[pit].size();
326 if (pars[pit].isInserted(pos)) {
327 pars[pit].setChange(pos, Change(Change::UNCHANGED));
328 } else if (pars[pit].isDeleted(pos)) {
329 if (pit == pars_size - 1) {
330 // we cannot remove a par break at the end of the last
331 // paragraph; instead, we mark it unchanged
332 pars[pit].setChange(pos, Change(Change::UNCHANGED));
334 mergeParagraph(bparams, pars, pit);