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 & from, Paragraph & to,
26 BufferParams const & params, pos_type i, pos_type j,
27 Change change = Change(Change::INSERTED))
29 Paragraph::value_type const tmpchar = from.getChar(i);
30 LyXFont tmpfont = from.getFontSettings(params, i);
32 if (tmpchar == Paragraph::META_INSET) {
33 InsetBase * tmpinset = 0;
34 if (from.getInset(i)) {
35 // the inset is not in a paragraph anymore
36 tmpinset = from.insetlist.release(i);
37 from.insetlist.erase(i);
40 if (!to.insetAllowed(tmpinset->lyxCode())) {
45 to.insertInset(j, tmpinset, tmpfont, change);
47 to.insertChar(j, tmpchar, tmpfont, change);
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 Change::Type change = par.lookupChange(i).type;
110 if (moveItem(par, *tmp, bparams, i, j - pos)) {
111 // FIXME: change tracking (MG)
112 tmp->setChange(j - pos, Change(change));
117 for (pos_type i = pos_end; i >= pos; --i)
118 // FIXME: change tracking (MG)
119 par.eraseChar(i, false); // erase without change tracking
123 // Make sure that we keep the language when
124 // breaking paragrpah.
126 LyXFont changed = tmp->getFirstFontSettings(bparams);
127 LyXFont old = par.getFontSettings(bparams, par.size());
128 changed.setLanguage(old.language());
129 tmp->setFont(0, changed);
136 par.params().clear();
137 par.layout(bparams.getLyXTextClass().defaultLayout());
140 // layout stays the same with latex-environments
142 par.layout(tmp->layout());
143 par.setLabelWidthString(tmp->params().labelWidthString());
144 par.params().depth(tmp->params().depth());
147 // subtle, but needed to get empty pars working right
148 if (bparams.trackChanges) {
149 // FIXME: Change tracking (MG)
151 // set 'par' text to INSERTED in CT mode; clear CT info otherwise
152 // else if (!tmp->size())
153 // set 'tmp' text to INSERTED in CT mode; clear CT info otherwise
158 void breakParagraphConservative(BufferParams const & bparams,
159 ParagraphList & pars, pit_type par_offset, pos_type pos)
161 // create a new paragraph
162 Paragraph & tmp = *pars.insert(boost::next(pars.begin(), par_offset + 1),
164 Paragraph & par = pars[par_offset];
166 tmp.makeSameLayout(par);
168 // When can pos > size()?
169 // I guess pos == size() is possible.
170 if (par.size() > pos) {
171 // copy everything behind the break-position to the new
173 pos_type pos_end = par.size() - 1;
175 for (pos_type i = pos, j = pos; i <= pos_end; ++i) {
176 Change::Type change = par.lookupChange(i).type;
177 // FIXME: change tracking (MG)
178 if (moveItem(par, tmp, bparams, i, j - pos, Change(change)))
181 // Move over end-of-par change attr
182 // FIXME: change tracking (MG)
183 tmp.setChange(tmp.size(), Change(par.lookupChange(par.size()).type));
185 // If tracking changes, set all the text that is to be
186 // erased to Type::INSERTED.
187 for (pos_type k = pos_end; k >= pos; --k) {
188 if (bparams.trackChanges)
189 // FIXME: Change tracking (MG)
190 par.setChange(k, Change(Change::INSERTED));
191 // FIXME: change tracking (MG)
192 par.eraseChar(k, false);
198 void mergeParagraph(BufferParams const & bparams,
199 ParagraphList & pars, pit_type par_offset)
201 Paragraph & next = pars[par_offset + 1];
202 Paragraph & par = pars[par_offset];
204 pos_type pos_end = next.size() - 1;
205 pos_type pos_insert = par.size();
207 // What happens is the following. Later on, moveItem() will copy
208 // over characters from the next paragraph to be inserted into this
209 // position. Now, if the first char to be so copied is "red" (i.e.,
210 // marked deleted) and the paragraph break is marked "blue",
211 // insertChar will trigger (eventually, through record(), and see
212 // del() and erase() in changes.C) a "hard" character deletion.
213 // Which doesn't make sense of course at this pos, but the effect is
214 // to shorten the change range to which this para break belongs, by
215 // one. It will (should) remain "orphaned", having no CT info to it,
216 // and check() in changes.C will assert. Setting the para break
217 // forcibly to "black" prevents this scenario. -- MV 13.3.2006
218 // FIXME: change tracking (MG)
219 par.setChange(par.size(), Change(Change::UNCHANGED));
221 Change::Type cr = next.lookupChange(next.size()).type;
222 // ok, now copy the paragraph
223 for (pos_type i = 0, j = 0; i <= pos_end; ++i) {
224 Change::Type change = next.lookupChange(i).type;
225 // FIXME: change tracking (MG)
226 if (moveItem(next, par, bparams, i, pos_insert + j, Change(change)))
229 // Move the change status of "carriage return" over
230 // FIXME: change tracking (MG)
231 par.setChange(par.size(), Change(cr));
233 pars.erase(boost::next(pars.begin(), par_offset + 1));
237 pit_type depthHook(pit_type pit, ParagraphList const & pars, depth_type depth)
239 pit_type newpit = pit;
244 while (newpit != 0 && pars[newpit].getDepth() > depth)
247 if (pars[newpit].getDepth() > depth)
254 pit_type outerHook(pit_type par_offset, ParagraphList const & pars)
256 Paragraph const & par = pars[par_offset];
258 if (par.getDepth() == 0)
260 return depthHook(par_offset, pars, depth_type(par.getDepth() - 1));
264 bool isFirstInSequence(pit_type par_offset, ParagraphList const & pars)
266 Paragraph const & par = pars[par_offset];
268 pit_type dhook_offset = depthHook(par_offset, pars, par.getDepth());
270 Paragraph const & dhook = pars[dhook_offset];
272 return dhook_offset == par_offset
273 || dhook.layout() != par.layout()
274 || dhook.getDepth() != par.getDepth();
278 int getEndLabel(pit_type p, ParagraphList const & pars)
281 depth_type par_depth = pars[p].getDepth();
282 while (pit != pit_type(pars.size())) {
283 LyXLayout_ptr const & layout = pars[pit].layout();
284 int const endlabeltype = layout->endlabeltype;
286 if (endlabeltype != END_LABEL_NO_LABEL) {
287 if (p + 1 == pit_type(pars.size()))
290 depth_type const next_depth =
291 pars[p + 1].getDepth();
292 if (par_depth > next_depth ||
293 (par_depth == next_depth && layout != pars[p + 1].layout()))
299 pit = outerHook(pit, pars);
300 if (pit != pit_type(pars.size()))
301 par_depth = pars[pit].getDepth();
303 return END_LABEL_NO_LABEL;
307 LyXFont const outerFont(pit_type par_offset, ParagraphList const & pars)
309 depth_type par_depth = pars[par_offset].getDepth();
310 LyXFont tmpfont(LyXFont::ALL_INHERIT);
312 // Resolve against environment font information
313 while (par_offset != pit_type(pars.size())
315 && !tmpfont.resolved()) {
316 par_offset = outerHook(par_offset, pars);
317 if (par_offset != pit_type(pars.size())) {
318 tmpfont.realize(pars[par_offset].layout()->font);
319 par_depth = pars[par_offset].getDepth();
327 /// return the number of InsetOptArg in a paragraph
328 int numberOfOptArgs(Paragraph const & par)
332 InsetList::const_iterator it = par.insetlist.begin();
333 InsetList::const_iterator end = par.insetlist.end();
334 for (; it != end ; ++it) {
335 if (it->inset->lyxCode() == InsetBase::OPTARG_CODE)