2 * \file paragraph_funcs.C
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 "paragraph_pimpl.h"
27 static bool moveItem(Paragraph & fromPar, pos_type fromPos,
28 Paragraph & toPar, pos_type toPos, BufferParams const & params)
30 // Note: moveItem() does not honour change tracking!
31 // Therefore, it should only be used for breaking and merging paragraphs
33 Paragraph::value_type const tmpChar = fromPar.getChar(fromPos);
34 LyXFont const tmpFont = fromPar.getFontSettings(params, fromPos);
35 Change const tmpChange = fromPar.lookupChange(fromPos);
37 if (tmpChar == Paragraph::META_INSET) {
38 InsetBase * tmpInset = 0;
39 if (fromPar.getInset(fromPos)) {
40 // the inset is not in the paragraph any more
41 tmpInset = fromPar.insetlist.release(fromPos);
44 fromPar.eraseChar(fromPos, false);
46 if (!toPar.insetAllowed(tmpInset->lyxCode())) {
51 toPar.insertInset(toPos, tmpInset, tmpFont, tmpChange);
53 fromPar.eraseChar(fromPos, false);
54 toPar.insertChar(toPos, tmpChar, tmpFont, tmpChange);
61 void breakParagraph(BufferParams const & bparams,
62 ParagraphList & pars, pit_type par_offset, pos_type pos, int flag)
64 // create a new paragraph, and insert into the list
65 ParagraphList::iterator tmp =
66 pars.insert(boost::next(pars.begin(), par_offset + 1),
69 Paragraph & par = pars[par_offset];
71 // we will invalidate the row cache
74 // without doing that we get a crash when typing <Return> at the
76 tmp->layout(bparams.getLyXTextClass().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() || flag == 2)) {
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 LyXFont changed = tmp->getFirstFontSettings(bparams);
130 LyXFont old = par.getFontSettings(bparams, par.size());
131 changed.setLanguage(old.language());
132 tmp->setFont(0, changed);
139 par.params().clear();
140 par.layout(bparams.getLyXTextClass().defaultLayout());
143 // layout stays the same with latex-environments
145 par.layout(tmp->layout());
146 par.setLabelWidthString(tmp->params().labelWidthString());
147 par.params().depth(tmp->params().depth());
152 void breakParagraphConservative(BufferParams const & bparams,
153 ParagraphList & pars, pit_type par_offset, pos_type pos)
155 // create a new paragraph
156 Paragraph & tmp = *pars.insert(boost::next(pars.begin(), par_offset + 1),
158 Paragraph & par = pars[par_offset];
160 tmp.makeSameLayout(par);
162 BOOST_ASSERT(pos <= par.size());
164 if (pos < par.size()) {
165 // move everything behind the break position to the new paragraph
166 pos_type pos_end = par.size() - 1;
168 for (pos_type i = pos, j = 0; i <= pos_end; ++i) {
169 if (moveItem(par, pos, tmp, j, bparams)) {
173 // Move over the end-of-par change information
174 tmp.setChange(tmp.size(), par.lookupChange(par.size()));
175 par.setChange(par.size(), Change(bparams.trackChanges ?
176 Change::INSERTED : Change::UNCHANGED));
181 void mergeParagraph(BufferParams const & bparams,
182 ParagraphList & pars, pit_type par_offset)
184 Paragraph & next = pars[par_offset + 1];
185 Paragraph & par = pars[par_offset];
187 pos_type pos_end = next.size() - 1;
188 pos_type pos_insert = par.size();
190 // the imaginary end-of-paragraph character (at par.size()) has to be
191 // marked as unmodified. Otherwise, its change is adopted by the first
192 // character of the next paragraph.
193 if (par.lookupChange(par.size()).type != Change::UNCHANGED) {
194 lyxerr[Debug::CHANGES] <<
195 "merging par with inserted/deleted end-of-par character" << endl;
196 par.setChange(par.size(), Change(Change::UNCHANGED));
199 Change change = next.lookupChange(next.size());
201 // move the content of the second paragraph to the end of the first one
202 for (pos_type i = 0, j = pos_insert; i <= pos_end; ++i) {
203 if (moveItem(next, 0, par, j, bparams)) {
208 // move the change of the end-of-paragraph character
209 par.setChange(par.size(), change);
211 pars.erase(boost::next(pars.begin(), par_offset + 1));
215 pit_type depthHook(pit_type pit, ParagraphList const & pars, depth_type depth)
217 pit_type newpit = pit;
222 while (newpit != 0 && pars[newpit].getDepth() > depth)
225 if (pars[newpit].getDepth() > depth)
232 pit_type outerHook(pit_type par_offset, ParagraphList const & pars)
234 Paragraph const & par = pars[par_offset];
236 if (par.getDepth() == 0)
238 return depthHook(par_offset, pars, depth_type(par.getDepth() - 1));
242 bool isFirstInSequence(pit_type par_offset, ParagraphList const & pars)
244 Paragraph const & par = pars[par_offset];
246 pit_type dhook_offset = depthHook(par_offset, pars, par.getDepth());
248 Paragraph const & dhook = pars[dhook_offset];
250 return dhook_offset == par_offset
251 || dhook.layout() != par.layout()
252 || dhook.getDepth() != par.getDepth();
256 int getEndLabel(pit_type p, ParagraphList const & pars)
259 depth_type par_depth = pars[p].getDepth();
260 while (pit != pit_type(pars.size())) {
261 LyXLayout_ptr const & layout = pars[pit].layout();
262 int const endlabeltype = layout->endlabeltype;
264 if (endlabeltype != END_LABEL_NO_LABEL) {
265 if (p + 1 == pit_type(pars.size()))
268 depth_type const next_depth =
269 pars[p + 1].getDepth();
270 if (par_depth > next_depth ||
271 (par_depth == next_depth && layout != pars[p + 1].layout()))
277 pit = outerHook(pit, pars);
278 if (pit != pit_type(pars.size()))
279 par_depth = pars[pit].getDepth();
281 return END_LABEL_NO_LABEL;
285 LyXFont const outerFont(pit_type par_offset, ParagraphList const & pars)
287 depth_type par_depth = pars[par_offset].getDepth();
288 LyXFont tmpfont(LyXFont::ALL_INHERIT);
290 // Resolve against environment font information
291 while (par_offset != pit_type(pars.size())
293 && !tmpfont.resolved()) {
294 par_offset = outerHook(par_offset, pars);
295 if (par_offset != pit_type(pars.size())) {
296 tmpfont.realize(pars[par_offset].layout()->font);
297 par_depth = pars[par_offset].getDepth();
305 /// return the number of InsetOptArg in a paragraph
306 int numberOfOptArgs(Paragraph const & par)
310 InsetList::const_iterator it = par.insetlist.begin();
311 InsetList::const_iterator end = par.insetlist.end();
312 for (; it != end ; ++it) {
313 if (it->inset->lyxCode() == InsetBase::OPTARG_CODE)