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"
18 #include "Paragraph.h"
19 #include "ParagraphParameters.h"
28 static bool moveItem(Paragraph & fromPar, pos_type fromPos,
29 Paragraph & toPar, pos_type toPos, BufferParams const & params)
31 // Note: moveItem() does not honour change tracking!
32 // Therefore, it should only be used for breaking and merging paragraphs
34 Paragraph::value_type const tmpChar = fromPar.getChar(fromPos);
35 Font const tmpFont = fromPar.getFontSettings(params, fromPos);
36 Change const tmpChange = fromPar.lookupChange(fromPos);
38 if (tmpChar == Paragraph::META_INSET) {
40 if (fromPar.getInset(fromPos)) {
41 // the inset is not in the paragraph any more
42 tmpInset = fromPar.insetlist.release(fromPos);
45 fromPar.eraseChar(fromPos, false);
47 if (!toPar.insetAllowed(tmpInset->lyxCode())) {
52 toPar.insertInset(toPos, tmpInset, tmpFont, tmpChange);
54 fromPar.eraseChar(fromPos, false);
55 toPar.insertChar(toPos, tmpChar, tmpFont, tmpChange);
62 void breakParagraph(BufferParams const & bparams,
63 ParagraphList & pars, pit_type par_offset, pos_type pos, int flag)
65 // create a new paragraph, and insert into the list
66 ParagraphList::iterator tmp =
67 pars.insert(boost::next(pars.begin(), par_offset + 1),
70 Paragraph & par = pars[par_offset];
72 // without doing that we get a crash when typing <Return> at the
74 tmp->layout(bparams.getTextClass().defaultLayout());
75 // remember to set the inset_owner
76 tmp->setInsetOwner(par.inInset());
78 // layout stays the same with latex-environments
80 tmp->layout(par.layout());
81 tmp->setLabelWidthString(par.params().labelWidthString());
82 tmp->params().depth(par.params().depth());
83 } else if (par.params().depth() > 0) {
84 Paragraph const & hook = pars[outerHook(par_offset, pars)];
85 tmp->layout(hook.layout());
86 // not sure the line below is useful
87 tmp->setLabelWidthString(par.params().labelWidthString());
88 tmp->params().depth(hook.params().depth());
91 bool const isempty = (par.allowEmpty() && par.empty());
93 if (!isempty && (par.size() > pos || par.empty() || flag == 2)) {
94 tmp->layout(par.layout());
95 tmp->params().align(par.params().align());
96 tmp->setLabelWidthString(par.params().labelWidthString());
98 tmp->params().depth(par.params().depth());
99 tmp->params().noindent(par.params().noindent());
101 // move everything behind the break position
102 // to the new paragraph
104 /* Note: if !keepempty, empty() == true, then we reach
105 * here with size() == 0. So pos_end becomes - 1. This
106 * doesn't cause problems because both loops below
107 * enforce pos <= pos_end and 0 <= pos
109 pos_type pos_end = par.size() - 1;
111 for (pos_type i = pos, j = 0; i <= pos_end; ++i) {
112 if (moveItem(par, pos, *tmp, j, bparams)) {
118 // Move over the end-of-par change information
119 tmp->setChange(tmp->size(), par.lookupChange(par.size()));
120 par.setChange(par.size(), Change(bparams.trackChanges ?
121 Change::INSERTED : Change::UNCHANGED));
124 // Make sure that we keep the language when
125 // breaking paragraph.
127 Font changed = tmp->getFirstFontSettings(bparams);
128 Font old = par.getFontSettings(bparams, par.size());
129 changed.setLanguage(old.language());
130 tmp->setFont(0, changed);
137 bool const soa = par.params().startOfAppendix();
138 par.params().clear();
139 // do not lose start of appendix marker (bug 4212)
140 par.params().startOfAppendix(soa);
141 par.layout(bparams.getTextClass().defaultLayout());
144 // layout stays the same with latex-environments
146 par.layout(tmp->layout());
147 par.setLabelWidthString(tmp->params().labelWidthString());
148 par.params().depth(tmp->params().depth());
153 void breakParagraphConservative(BufferParams const & bparams,
154 ParagraphList & pars, pit_type par_offset, pos_type pos)
156 // create a new paragraph
157 Paragraph & tmp = *pars.insert(boost::next(pars.begin(), par_offset + 1),
159 Paragraph & par = pars[par_offset];
161 tmp.makeSameLayout(par);
163 BOOST_ASSERT(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" << endl;
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 LayoutPtr 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 Font tmpfont(Font::ALL_INHERIT);
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();
308 /// return the number of InsetOptArg in a paragraph
309 int numberOfOptArgs(Paragraph const & par)
313 InsetList::const_iterator it = par.insetlist.begin();
314 InsetList::const_iterator end = par.insetlist.end();
315 for (; it != end ; ++it) {
316 if (it->inset->lyxCode() == Inset::OPTARG_CODE)
323 void acceptChanges(ParagraphList & pars, BufferParams const & bparams)
325 pit_type pars_size = static_cast<pit_type>(pars.size());
327 // first, accept changes within each individual paragraph
328 // (do not consider end-of-par)
329 for (pit_type pit = 0; pit < pars_size; ++pit) {
330 if (!pars[pit].empty()) // prevent assertion failure
331 pars[pit].acceptChanges(bparams, 0, pars[pit].size());
334 // next, accept imaginary end-of-par characters
335 for (pit_type pit = 0; pit < pars_size; ++pit) {
336 pos_type pos = pars[pit].size();
338 if (pars[pit].isInserted(pos)) {
339 pars[pit].setChange(pos, Change(Change::UNCHANGED));
340 } else if (pars[pit].isDeleted(pos)) {
341 if (pit == pars_size - 1) {
342 // we cannot remove a par break at the end of the last
343 // paragraph; instead, we mark it unchanged
344 pars[pit].setChange(pos, Change(Change::UNCHANGED));
346 mergeParagraph(bparams, pars, pit);