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 "support/debug.h"
18 #include "InsetList.h"
20 #include "Paragraph.h"
21 #include "ParagraphParameters.h"
23 #include "TextClass.h"
25 #include <boost/next_prior.hpp>
31 static bool moveItem(Paragraph & fromPar, pos_type fromPos,
32 Paragraph & toPar, pos_type toPos, BufferParams const & params)
34 // Note: moveItem() does not honour change tracking!
35 // Therefore, it should only be used for breaking and merging paragraphs
37 char_type const tmpChar = fromPar.getChar(fromPos);
38 Font const tmpFont = fromPar.getFontSettings(params, fromPos);
39 Change const tmpChange = fromPar.lookupChange(fromPos);
41 if (fromPar.isInset(fromPos)) {
43 if (fromPar.getInset(fromPos)) {
44 // the inset is not in the paragraph any more
45 tmpInset = fromPar.releaseInset(fromPos);
48 if (!toPar.insetAllowed(tmpInset->lyxCode())) {
53 toPar.insertInset(toPos, tmpInset, tmpFont, tmpChange);
55 fromPar.eraseChar(fromPos, false);
56 toPar.insertChar(toPos, tmpChar, tmpFont, tmpChange);
63 void breakParagraph(BufferParams const & bparams,
64 ParagraphList & pars, pit_type par_offset, pos_type pos,
67 // create a new paragraph, and insert into the list
68 ParagraphList::iterator tmp =
69 pars.insert(boost::next(pars.begin(), par_offset + 1),
72 Paragraph & par = pars[par_offset];
74 // without doing that we get a crash when typing <Return> at the
76 tmp->layout(bparams.getTextClass().defaultLayout());
77 // remember to set the inset_owner
78 tmp->setInsetOwner(par.inInset());
80 // layout stays the same with latex-environments
82 tmp->layout(par.layout());
83 tmp->setLabelWidthString(par.params().labelWidthString());
84 tmp->params().depth(par.params().depth());
85 } else if (par.params().depth() > 0) {
86 Paragraph const & hook = pars[outerHook(par_offset, pars)];
87 tmp->layout(hook.layout());
88 // not sure the line below is useful
89 tmp->setLabelWidthString(par.params().labelWidthString());
90 tmp->params().depth(hook.params().depth());
93 bool const isempty = (par.allowEmpty() && par.empty());
95 if (!isempty && (par.size() > pos || par.empty())) {
96 tmp->layout(par.layout());
97 tmp->params().align(par.params().align());
98 tmp->setLabelWidthString(par.params().labelWidthString());
100 tmp->params().depth(par.params().depth());
101 tmp->params().noindent(par.params().noindent());
103 // move everything behind the break position
104 // to the new paragraph
106 /* Note: if !keepempty, empty() == true, then we reach
107 * here with size() == 0. So pos_end becomes - 1. This
108 * doesn't cause problems because both loops below
109 * enforce pos <= pos_end and 0 <= pos
111 pos_type pos_end = par.size() - 1;
113 for (pos_type i = pos, j = 0; i <= pos_end; ++i) {
114 if (moveItem(par, pos, *tmp, j, bparams)) {
120 // Move over the end-of-par change information
121 tmp->setChange(tmp->size(), par.lookupChange(par.size()));
122 par.setChange(par.size(), Change(bparams.trackChanges ?
123 Change::INSERTED : Change::UNCHANGED));
126 // Make sure that we keep the language when
127 // breaking paragraph.
129 Font changed = tmp->getFirstFontSettings(bparams);
130 Font old = par.getFontSettings(bparams, par.size());
131 changed.setLanguage(old.language());
132 tmp->setFont(0, changed);
139 bool const soa = par.params().startOfAppendix();
140 par.params().clear();
141 // do not lose start of appendix marker (bug 4212)
142 par.params().startOfAppendix(soa);
143 par.layout(bparams.getTextClass().defaultLayout());
146 // layout stays the same with latex-environments
148 par.layout(tmp->layout());
149 par.setLabelWidthString(tmp->params().labelWidthString());
150 par.params().depth(tmp->params().depth());
155 void breakParagraphConservative(BufferParams const & bparams,
156 ParagraphList & pars, pit_type par_offset, pos_type pos)
158 // create a new paragraph
159 Paragraph & tmp = *pars.insert(boost::next(pars.begin(), par_offset + 1),
161 Paragraph & par = pars[par_offset];
163 tmp.makeSameLayout(par);
165 BOOST_ASSERT(pos <= par.size());
167 if (pos < par.size()) {
168 // move everything behind the break position to the new paragraph
169 pos_type pos_end = par.size() - 1;
171 for (pos_type i = pos, j = 0; i <= pos_end; ++i) {
172 if (moveItem(par, pos, tmp, j, bparams)) {
176 // Move over the end-of-par change information
177 tmp.setChange(tmp.size(), par.lookupChange(par.size()));
178 par.setChange(par.size(), Change(bparams.trackChanges ?
179 Change::INSERTED : Change::UNCHANGED));
184 void mergeParagraph(BufferParams const & bparams,
185 ParagraphList & pars, pit_type par_offset)
187 Paragraph & next = pars[par_offset + 1];
188 Paragraph & par = pars[par_offset];
190 pos_type pos_end = next.size() - 1;
191 pos_type pos_insert = par.size();
193 // the imaginary end-of-paragraph character (at par.size()) has to be
194 // marked as unmodified. Otherwise, its change is adopted by the first
195 // character of the next paragraph.
196 if (par.lookupChange(par.size()).type != Change::UNCHANGED) {
197 LYXERR(Debug::CHANGES,
198 "merging par with inserted/deleted end-of-par character");
199 par.setChange(par.size(), Change(Change::UNCHANGED));
202 Change change = next.lookupChange(next.size());
204 // move the content of the second paragraph to the end of the first one
205 for (pos_type i = 0, j = pos_insert; i <= pos_end; ++i) {
206 if (moveItem(next, 0, par, j, bparams)) {
211 // move the change of the end-of-paragraph character
212 par.setChange(par.size(), change);
214 pars.erase(boost::next(pars.begin(), par_offset + 1));
218 pit_type depthHook(pit_type pit, ParagraphList const & pars, depth_type depth)
220 pit_type newpit = pit;
225 while (newpit != 0 && pars[newpit].getDepth() > depth)
228 if (pars[newpit].getDepth() > depth)
235 pit_type outerHook(pit_type par_offset, ParagraphList const & pars)
237 Paragraph const & par = pars[par_offset];
239 if (par.getDepth() == 0)
241 return depthHook(par_offset, pars, depth_type(par.getDepth() - 1));
245 bool isFirstInSequence(pit_type par_offset, ParagraphList const & pars)
247 Paragraph const & par = pars[par_offset];
249 pit_type dhook_offset = depthHook(par_offset, pars, par.getDepth());
251 if (dhook_offset == par_offset)
254 Paragraph const & dhook = pars[dhook_offset];
256 return dhook.layout() != par.layout()
257 || dhook.getDepth() != par.getDepth();
261 int getEndLabel(pit_type p, ParagraphList const & pars)
264 depth_type par_depth = pars[p].getDepth();
265 while (pit != pit_type(pars.size())) {
266 LayoutPtr const & layout = pars[pit].layout();
267 int const endlabeltype = layout->endlabeltype;
269 if (endlabeltype != END_LABEL_NO_LABEL) {
270 if (p + 1 == pit_type(pars.size()))
273 depth_type const next_depth =
274 pars[p + 1].getDepth();
275 if (par_depth > next_depth ||
276 (par_depth == next_depth && layout != pars[p + 1].layout()))
282 pit = outerHook(pit, pars);
283 if (pit != pit_type(pars.size()))
284 par_depth = pars[pit].getDepth();
286 return END_LABEL_NO_LABEL;
290 Font const outerFont(pit_type par_offset, ParagraphList const & pars)
292 depth_type par_depth = pars[par_offset].getDepth();
293 FontInfo tmpfont = inherit_font;
295 // Resolve against environment font information
296 while (par_offset != pit_type(pars.size())
298 && !tmpfont.resolved()) {
299 par_offset = outerHook(par_offset, pars);
300 if (par_offset != pit_type(pars.size())) {
301 tmpfont.realize(pars[par_offset].layout()->font);
302 par_depth = pars[par_offset].getDepth();
306 return Font(tmpfont);
310 void acceptChanges(ParagraphList & pars, BufferParams const & bparams)
312 pit_type pars_size = static_cast<pit_type>(pars.size());
314 // first, accept changes within each individual paragraph
315 // (do not consider end-of-par)
316 for (pit_type pit = 0; pit < pars_size; ++pit) {
317 if (!pars[pit].empty()) // prevent assertion failure
318 pars[pit].acceptChanges(bparams, 0, pars[pit].size());
321 // next, accept imaginary end-of-par characters
322 for (pit_type pit = 0; pit < pars_size; ++pit) {
323 pos_type pos = pars[pit].size();
325 if (pars[pit].isInserted(pos)) {
326 pars[pit].setChange(pos, Change(Change::UNCHANGED));
327 } else if (pars[pit].isDeleted(pos)) {
328 if (pit == pars_size - 1) {
329 // we cannot remove a par break at the end of the last
330 // paragraph; instead, we mark it unchanged
331 pars[pit].setChange(pos, Change(Change::UNCHANGED));
333 mergeParagraph(bparams, pars, pit);