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"
25 static bool moveItem(Paragraph & fromPar, pos_type fromPos,
26 Paragraph & toPar, pos_type toPos, BufferParams const & params)
28 Paragraph::value_type const tmpChar = fromPar.getChar(fromPos);
29 LyXFont const tmpFont = fromPar.getFontSettings(params, fromPos);
30 Change const tmpChange = fromPar.lookupChange(fromPos);
32 if (tmpChar == Paragraph::META_INSET) {
33 InsetBase * tmpInset = 0;
34 if (fromPar.getInset(fromPos)) {
35 // the inset is not in a paragraph anymore
36 tmpInset = fromPar.insetlist.release(fromPos);
37 fromPar.insetlist.erase(fromPos);
40 if (!toPar.insetAllowed(tmpInset->lyxCode())) {
45 toPar.insertInset(toPos, tmpInset, tmpFont, tmpChange);
47 toPar.insertChar(toPos, tmpChar, tmpFont, tmpChange);
53 void breakParagraph(BufferParams const & bparams,
54 ParagraphList & pars, pit_type par_offset, pos_type pos, int flag)
56 // create a new paragraph, and insert into the list
57 ParagraphList::iterator tmp =
58 pars.insert(boost::next(pars.begin(), par_offset + 1),
61 Paragraph & par = pars[par_offset];
63 // we will invalidate the row cache
66 // without doing that we get a crash when typing <Return> at the
68 tmp->layout(bparams.getLyXTextClass().defaultLayout());
69 // remember to set the inset_owner
70 tmp->setInsetOwner(par.inInset());
72 // this is an idea for a more userfriendly layout handling, I will
73 // see what the users say
75 // layout stays the same with latex-environments
77 tmp->layout(par.layout());
78 tmp->setLabelWidthString(par.params().labelWidthString());
79 tmp->params().depth(par.params().depth());
80 } else if (par.params().depth() > 0) {
81 Paragraph const & hook = pars[outerHook(par_offset, pars)];
82 tmp->layout(hook.layout());
83 // not sure the line below is useful
84 tmp->setLabelWidthString(par.params().labelWidthString());
85 tmp->params().depth(hook.params().depth());
88 bool const isempty = (par.allowEmpty() && par.empty());
90 if (!isempty && (par.size() > pos || par.empty() || flag == 2)) {
91 tmp->layout(par.layout());
92 tmp->params().align(par.params().align());
93 tmp->setLabelWidthString(par.params().labelWidthString());
95 tmp->params().depth(par.params().depth());
96 tmp->params().noindent(par.params().noindent());
98 // copy everything behind the break-position
99 // to the new paragraph
101 /* Note: if !keepempty, empty() == true, then we reach
102 * here with size() == 0. So pos_end becomes - 1. This
103 * doesn't cause problems because both loops below
104 * enforce pos <= pos_end and 0 <= pos
106 pos_type pos_end = par.size() - 1;
108 for (pos_type i = pos, j = pos; i <= pos_end; ++i) {
109 if (moveItem(par, i, *tmp, j - pos, bparams)) {
114 for (pos_type i = pos_end; i >= pos; --i)
115 // FIXME: change tracking (MG)
116 par.eraseChar(i, false); // erase without change tracking
120 // Make sure that we keep the language when
121 // breaking paragrpah.
123 LyXFont changed = tmp->getFirstFontSettings(bparams);
124 LyXFont old = par.getFontSettings(bparams, par.size());
125 changed.setLanguage(old.language());
126 tmp->setFont(0, changed);
133 par.params().clear();
134 par.layout(bparams.getLyXTextClass().defaultLayout());
137 // layout stays the same with latex-environments
139 par.layout(tmp->layout());
140 par.setLabelWidthString(tmp->params().labelWidthString());
141 par.params().depth(tmp->params().depth());
144 // subtle, but needed to get empty pars working right
145 if (bparams.trackChanges) {
146 // FIXME: Change tracking (MG)
148 // set 'par' text to INSERTED in CT mode; clear CT info otherwise
149 // else if (!tmp->size())
150 // set 'tmp' text to INSERTED in CT mode; clear CT info otherwise
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 // When can pos > size()?
166 // I guess pos == size() is possible.
167 if (par.size() > pos) {
168 // copy everything behind the break-position to the new
170 pos_type pos_end = par.size() - 1;
172 for (pos_type i = pos, j = pos; i <= pos_end; ++i) {
173 if (moveItem(par, i, tmp, j - pos, bparams)) {
177 // Move over end-of-par change attr
178 // FIXME: change tracking (MG)
179 tmp.setChange(tmp.size(), Change(par.lookupChange(par.size()).type));
181 // If tracking changes, set all the text that is to be
182 // erased to Type::INSERTED.
183 for (pos_type k = pos_end; k >= pos; --k) {
184 if (bparams.trackChanges)
185 // FIXME: Change tracking (MG)
186 par.setChange(k, Change(Change::INSERTED));
187 // FIXME: change tracking (MG)
188 par.eraseChar(k, false);
194 void mergeParagraph(BufferParams const & bparams,
195 ParagraphList & pars, pit_type par_offset)
197 Paragraph & next = pars[par_offset + 1];
198 Paragraph & par = pars[par_offset];
200 pos_type pos_end = next.size() - 1;
201 pos_type pos_insert = par.size();
203 // What happens is the following. Later on, moveItem() will copy
204 // over characters from the next paragraph to be inserted into this
205 // position. Now, if the first char to be so copied is "red" (i.e.,
206 // marked deleted) and the paragraph break is marked "blue",
207 // insertChar will trigger (eventually, through record(), and see
208 // del() and erase() in changes.C) a "hard" character deletion.
209 // Which doesn't make sense of course at this pos, but the effect is
210 // to shorten the change range to which this para break belongs, by
211 // one. It will (should) remain "orphaned", having no CT info to it,
212 // and check() in changes.C will assert. Setting the para break
213 // forcibly to "black" prevents this scenario. -- MV 13.3.2006
214 // FIXME: change tracking (MG)
215 par.setChange(par.size(), Change(Change::UNCHANGED));
217 Change::Type cr = next.lookupChange(next.size()).type;
218 // ok, now copy the paragraph
219 for (pos_type i = 0, j = 0; i <= pos_end; ++i) {
220 if (moveItem(next, i, par, pos_insert + j, bparams)) {
224 // Move the change status of "carriage return" over
225 // FIXME: change tracking (MG)
226 par.setChange(par.size(), Change(cr));
228 pars.erase(boost::next(pars.begin(), par_offset + 1));
232 pit_type depthHook(pit_type pit, ParagraphList const & pars, depth_type depth)
234 pit_type newpit = pit;
239 while (newpit != 0 && pars[newpit].getDepth() > depth)
242 if (pars[newpit].getDepth() > depth)
249 pit_type outerHook(pit_type par_offset, ParagraphList const & pars)
251 Paragraph const & par = pars[par_offset];
253 if (par.getDepth() == 0)
255 return depthHook(par_offset, pars, depth_type(par.getDepth() - 1));
259 bool isFirstInSequence(pit_type par_offset, ParagraphList const & pars)
261 Paragraph const & par = pars[par_offset];
263 pit_type dhook_offset = depthHook(par_offset, pars, par.getDepth());
265 Paragraph const & dhook = pars[dhook_offset];
267 return dhook_offset == par_offset
268 || dhook.layout() != par.layout()
269 || dhook.getDepth() != par.getDepth();
273 int getEndLabel(pit_type p, ParagraphList const & pars)
276 depth_type par_depth = pars[p].getDepth();
277 while (pit != pit_type(pars.size())) {
278 LyXLayout_ptr const & layout = pars[pit].layout();
279 int const endlabeltype = layout->endlabeltype;
281 if (endlabeltype != END_LABEL_NO_LABEL) {
282 if (p + 1 == pit_type(pars.size()))
285 depth_type const next_depth =
286 pars[p + 1].getDepth();
287 if (par_depth > next_depth ||
288 (par_depth == next_depth && layout != pars[p + 1].layout()))
294 pit = outerHook(pit, pars);
295 if (pit != pit_type(pars.size()))
296 par_depth = pars[pit].getDepth();
298 return END_LABEL_NO_LABEL;
302 LyXFont const outerFont(pit_type par_offset, ParagraphList const & pars)
304 depth_type par_depth = pars[par_offset].getDepth();
305 LyXFont tmpfont(LyXFont::ALL_INHERIT);
307 // Resolve against environment font information
308 while (par_offset != pit_type(pars.size())
310 && !tmpfont.resolved()) {
311 par_offset = outerHook(par_offset, pars);
312 if (par_offset != pit_type(pars.size())) {
313 tmpfont.realize(pars[par_offset].layout()->font);
314 par_depth = pars[par_offset].getDepth();
322 /// return the number of InsetOptArg in a paragraph
323 int numberOfOptArgs(Paragraph const & par)
327 InsetList::const_iterator it = par.insetlist.begin();
328 InsetList::const_iterator end = par.insetlist.end();
329 for (; it != end ; ++it) {
330 if (it->inset->lyxCode() == InsetBase::OPTARG_CODE)