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"
19 #include "Paragraph.h"
20 #include "ParagraphParameters.h"
29 static bool moveItem(Paragraph & fromPar, pos_type fromPos,
30 Paragraph & toPar, pos_type toPos, BufferParams const & params)
32 // Note: moveItem() does not honour change tracking!
33 // Therefore, it should only be used for breaking and merging paragraphs
35 Paragraph::value_type const tmpChar = fromPar.getChar(fromPos);
36 Font const tmpFont = fromPar.getFontSettings(params, fromPos);
37 Change const tmpChange = fromPar.lookupChange(fromPos);
39 if (tmpChar == Paragraph::META_INSET) {
41 if (fromPar.getInset(fromPos)) {
42 // the inset is not in the paragraph any more
43 tmpInset = fromPar.insetlist.release(fromPos);
46 fromPar.eraseChar(fromPos, false);
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, int flag)
66 // create a new paragraph, and insert into the list
67 ParagraphList::iterator tmp =
68 pars.insert(boost::next(pars.begin(), par_offset + 1),
71 Paragraph & par = pars[par_offset];
73 // without doing that we get a crash when typing <Return> at the
75 tmp->layout(bparams.getTextClass().defaultLayout());
76 // remember to set the inset_owner
77 tmp->setInsetOwner(par.inInset());
79 // layout stays the same with latex-environments
81 tmp->layout(par.layout());
82 tmp->setLabelWidthString(par.params().labelWidthString());
83 tmp->params().depth(par.params().depth());
84 } else if (par.params().depth() > 0) {
85 Paragraph const & hook = pars[outerHook(par_offset, pars)];
86 tmp->layout(hook.layout());
87 // not sure the line below is useful
88 tmp->setLabelWidthString(par.params().labelWidthString());
89 tmp->params().depth(hook.params().depth());
92 bool const isempty = (par.allowEmpty() && par.empty());
94 if (!isempty && (par.size() > pos || par.empty() || flag == 2)) {
95 tmp->layout(par.layout());
96 tmp->params().align(par.params().align());
97 tmp->setLabelWidthString(par.params().labelWidthString());
99 tmp->params().depth(par.params().depth());
100 tmp->params().noindent(par.params().noindent());
102 // move everything behind the break position
103 // to the new paragraph
105 /* Note: if !keepempty, empty() == true, then we reach
106 * here with size() == 0. So pos_end becomes - 1. This
107 * doesn't cause problems because both loops below
108 * enforce pos <= pos_end and 0 <= pos
110 pos_type pos_end = par.size() - 1;
112 for (pos_type i = pos, j = 0; i <= pos_end; ++i) {
113 if (moveItem(par, pos, *tmp, j, bparams)) {
119 // Move over the end-of-par change information
120 tmp->setChange(tmp->size(), par.lookupChange(par.size()));
121 par.setChange(par.size(), Change(bparams.trackChanges ?
122 Change::INSERTED : Change::UNCHANGED));
125 // Make sure that we keep the language when
126 // breaking paragraph.
128 Font changed = tmp->getFirstFontSettings(bparams);
129 Font old = par.getFontSettings(bparams, par.size());
130 changed.setLanguage(old.language());
131 tmp->setFont(0, changed);
138 bool const soa = par.params().startOfAppendix();
139 par.params().clear();
140 // do not lose start of appendix marker (bug 4212)
141 par.params().startOfAppendix(soa);
142 par.layout(bparams.getTextClass().defaultLayout());
145 // layout stays the same with latex-environments
147 par.layout(tmp->layout());
148 par.setLabelWidthString(tmp->params().labelWidthString());
149 par.params().depth(tmp->params().depth());
154 void breakParagraphConservative(BufferParams const & bparams,
155 ParagraphList & pars, pit_type par_offset, pos_type pos)
157 // create a new paragraph
158 Paragraph & tmp = *pars.insert(boost::next(pars.begin(), par_offset + 1),
160 Paragraph & par = pars[par_offset];
162 tmp.makeSameLayout(par);
164 BOOST_ASSERT(pos <= par.size());
166 if (pos < par.size()) {
167 // move everything behind the break position to the new paragraph
168 pos_type pos_end = par.size() - 1;
170 for (pos_type i = pos, j = 0; i <= pos_end; ++i) {
171 if (moveItem(par, pos, tmp, j, bparams)) {
175 // Move over the end-of-par change information
176 tmp.setChange(tmp.size(), par.lookupChange(par.size()));
177 par.setChange(par.size(), Change(bparams.trackChanges ?
178 Change::INSERTED : Change::UNCHANGED));
183 void mergeParagraph(BufferParams const & bparams,
184 ParagraphList & pars, pit_type par_offset)
186 Paragraph & next = pars[par_offset + 1];
187 Paragraph & par = pars[par_offset];
189 pos_type pos_end = next.size() - 1;
190 pos_type pos_insert = par.size();
192 // the imaginary end-of-paragraph character (at par.size()) has to be
193 // marked as unmodified. Otherwise, its change is adopted by the first
194 // character of the next paragraph.
195 if (par.lookupChange(par.size()).type != Change::UNCHANGED) {
196 LYXERR(Debug::CHANGES) <<
197 "merging par with inserted/deleted end-of-par character" << endl;
198 par.setChange(par.size(), Change(Change::UNCHANGED));
201 Change change = next.lookupChange(next.size());
203 // move the content of the second paragraph to the end of the first one
204 for (pos_type i = 0, j = pos_insert; i <= pos_end; ++i) {
205 if (moveItem(next, 0, par, j, bparams)) {
210 // move the change of the end-of-paragraph character
211 par.setChange(par.size(), change);
213 pars.erase(boost::next(pars.begin(), par_offset + 1));
217 pit_type depthHook(pit_type pit, ParagraphList const & pars, depth_type depth)
219 pit_type newpit = pit;
224 while (newpit != 0 && pars[newpit].getDepth() > depth)
227 if (pars[newpit].getDepth() > depth)
234 pit_type outerHook(pit_type par_offset, ParagraphList const & pars)
236 Paragraph const & par = pars[par_offset];
238 if (par.getDepth() == 0)
240 return depthHook(par_offset, pars, depth_type(par.getDepth() - 1));
244 bool isFirstInSequence(pit_type par_offset, ParagraphList const & pars)
246 Paragraph const & par = pars[par_offset];
248 pit_type dhook_offset = depthHook(par_offset, pars, par.getDepth());
250 if (dhook_offset == par_offset)
253 Paragraph const & dhook = pars[dhook_offset];
255 return dhook.layout() != par.layout()
256 || dhook.getDepth() != par.getDepth();
260 int getEndLabel(pit_type p, ParagraphList const & pars)
263 depth_type par_depth = pars[p].getDepth();
264 while (pit != pit_type(pars.size())) {
265 LayoutPtr const & layout = pars[pit].layout();
266 int const endlabeltype = layout->endlabeltype;
268 if (endlabeltype != END_LABEL_NO_LABEL) {
269 if (p + 1 == pit_type(pars.size()))
272 depth_type const next_depth =
273 pars[p + 1].getDepth();
274 if (par_depth > next_depth ||
275 (par_depth == next_depth && layout != pars[p + 1].layout()))
281 pit = outerHook(pit, pars);
282 if (pit != pit_type(pars.size()))
283 par_depth = pars[pit].getDepth();
285 return END_LABEL_NO_LABEL;
289 Font const outerFont(pit_type par_offset, ParagraphList const & pars)
291 depth_type par_depth = pars[par_offset].getDepth();
292 Font tmpfont(Font::ALL_INHERIT);
294 // Resolve against environment font information
295 while (par_offset != pit_type(pars.size())
297 && !tmpfont.resolved()) {
298 par_offset = outerHook(par_offset, pars);
299 if (par_offset != pit_type(pars.size())) {
300 tmpfont.realize(pars[par_offset].layout()->font);
301 par_depth = pars[par_offset].getDepth();
309 /// return the number of InsetOptArg in a paragraph
310 int numberOfOptArgs(Paragraph const & par)
314 InsetList::const_iterator it = par.insetlist.begin();
315 InsetList::const_iterator end = par.insetlist.end();
316 for (; it != end ; ++it) {
317 if (it->inset->lyxCode() == Inset::OPTARG_CODE)
324 void acceptChanges(ParagraphList & pars, BufferParams const & bparams)
326 pit_type pars_size = static_cast<pit_type>(pars.size());
328 // first, accept changes within each individual paragraph
329 // (do not consider end-of-par)
330 for (pit_type pit = 0; pit < pars_size; ++pit) {
331 if (!pars[pit].empty()) // prevent assertion failure
332 pars[pit].acceptChanges(bparams, 0, pars[pit].size());
335 // next, accept imaginary end-of-par characters
336 for (pit_type pit = 0; pit < pars_size; ++pit) {
337 pos_type pos = pars[pit].size();
339 if (pars[pit].isInserted(pos)) {
340 pars[pit].setChange(pos, Change(Change::UNCHANGED));
341 } else if (pars[pit].isDeleted(pos)) {
342 if (pit == pars_size - 1) {
343 // we cannot remove a par break at the end of the last
344 // paragraph; instead, we mark it unchanged
345 pars[pit].setChange(pos, Change(Change::UNCHANGED));
347 mergeParagraph(bparams, pars, pit);