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 bool moveItem(Paragraph & from, Paragraph & to,
28 BufferParams const & params, pos_type i, pos_type j,
29 Change change = Change(Change::INSERTED));
31 bool moveItem(Paragraph & from, Paragraph & to,
32 BufferParams const & params, pos_type i, pos_type j,
35 Paragraph::value_type const tmpchar = from.getChar(i);
36 LyXFont tmpfont = from.getFontSettings(params, i);
38 if (tmpchar == Paragraph::META_INSET) {
39 InsetBase * tmpinset = 0;
40 if (from.getInset(i)) {
41 // the inset is not in a paragraph anymore
42 tmpinset = from.insetlist.release(i);
43 from.insetlist.erase(i);
46 if (!to.insetAllowed(tmpinset->lyxCode())) {
51 to.insertInset(j, tmpinset, tmpfont, change);
53 to.insertChar(j, tmpchar, tmpfont, change);
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 // this is an idea for a more userfriendly layout handling, I will
81 // see what the users say
83 // layout stays the same with latex-environments
85 tmp->layout(par.layout());
86 tmp->setLabelWidthString(par.params().labelWidthString());
87 tmp->params().depth(par.params().depth());
88 } else if (par.params().depth() > 0) {
89 Paragraph const & hook = pars[outerHook(par_offset, pars)];
90 tmp->layout(hook.layout());
91 // not sure the line below is useful
92 tmp->setLabelWidthString(par.params().labelWidthString());
93 tmp->params().depth(hook.params().depth());
96 bool const isempty = (par.allowEmpty() && par.empty());
98 if (!isempty && (par.size() > pos || par.empty() || flag == 2)) {
99 tmp->layout(par.layout());
100 tmp->params().align(par.params().align());
101 tmp->setLabelWidthString(par.params().labelWidthString());
103 tmp->params().depth(par.params().depth());
104 tmp->params().noindent(par.params().noindent());
106 // copy everything behind the break-position
107 // to the new paragraph
109 /* Note: if !keepempty, empty() == true, then we reach
110 * here with size() == 0. So pos_end becomes - 1. This
111 * doesn't cause problems because both loops below
112 * enforce pos <= pos_end and 0 <= pos
114 pos_type pos_end = par.size() - 1;
116 for (pos_type i = pos, j = pos; i <= pos_end; ++i) {
117 Change::Type change = par.lookupChange(i).type;
118 if (moveItem(par, *tmp, bparams, i, j - pos)) {
119 tmp->setChange(j - pos, change);
124 for (pos_type i = pos_end; i >= pos; --i)
129 // Make sure that we keep the language when
130 // breaking paragrpah.
132 LyXFont changed = tmp->getFirstFontSettings(bparams);
133 LyXFont old = par.getFontSettings(bparams, par.size());
134 changed.setLanguage(old.language());
135 tmp->setFont(0, changed);
142 par.params().clear();
143 par.layout(bparams.getLyXTextClass().defaultLayout());
146 // layout stays the same with latex-environments
148 par.layout(tmp->layout());
149 par.setLabelWidthString(tmp->params().labelWidthString());
150 par.params().depth(tmp->params().depth());
153 // subtle, but needed to get empty pars working right
154 if (bparams.trackChanges) {
155 // FIXME: Change tracking (MG)
157 // set 'par' text to INSERTED in CT mode; clear CT info otherwise
158 // else if (!tmp->size())
159 // set 'tmp' text to INSERTED in CT mode; clear CT info otherwise
164 void breakParagraphConservative(BufferParams const & bparams,
165 ParagraphList & pars, pit_type par_offset, pos_type pos)
167 // create a new paragraph
168 Paragraph & tmp = *pars.insert(boost::next(pars.begin(), par_offset + 1),
170 Paragraph & par = pars[par_offset];
172 tmp.makeSameLayout(par);
174 // When can pos > size()?
175 // I guess pos == size() is possible.
176 if (par.size() > pos) {
177 // copy everything behind the break-position to the new
179 pos_type pos_end = par.size() - 1;
181 for (pos_type i = pos, j = pos; i <= pos_end; ++i) {
182 Change::Type change = par.lookupChange(i).type;
183 if (moveItem(par, tmp, bparams, i, j - pos, change))
186 // Move over end-of-par change attr
187 tmp.setChange(tmp.size(), par.lookupChange(par.size()).type);
189 // If tracking changes, set all the text that is to be
190 // erased to Type::INSERTED.
191 for (pos_type k = pos_end; k >= pos; --k) {
192 if (bparams.trackChanges)
193 // FIXME: Change tracking (MG)
194 par.setChange(k, Change::INSERTED);
201 void mergeParagraph(BufferParams const & bparams,
202 ParagraphList & pars, pit_type par_offset)
204 Paragraph & next = pars[par_offset + 1];
205 Paragraph & par = pars[par_offset];
207 pos_type pos_end = next.size() - 1;
208 pos_type pos_insert = par.size();
210 // What happens is the following. Later on, moveItem() will copy
211 // over characters from the next paragraph to be inserted into this
212 // position. Now, if the first char to be so copied is "red" (i.e.,
213 // marked deleted) and the paragraph break is marked "blue",
214 // insertChar will trigger (eventually, through record(), and see
215 // del() and erase() in changes.C) a "hard" character deletion.
216 // Which doesn't make sense of course at this pos, but the effect is
217 // to shorten the change range to which this para break belongs, by
218 // one. It will (should) remain "orphaned", having no CT info to it,
219 // and check() in changes.C will assert. Setting the para break
220 // forcibly to "black" prevents this scenario. -- MV 13.3.2006
221 par.setChange(par.size(), Change::UNCHANGED);
223 Change::Type cr = next.lookupChange(next.size()).type;
224 // ok, now copy the paragraph
225 for (pos_type i = 0, j = 0; i <= pos_end; ++i) {
226 Change::Type change = next.lookupChange(i).type;
227 if (moveItem(next, par, bparams, i, pos_insert + j, change))
230 // Move the change status of "carriage return" over
231 par.setChange(par.size(), cr);
233 pars.erase(boost::next(pars.begin(), par_offset + 1));
237 pit_type depthHook(pit_type pit,
238 ParagraphList const & pars, Paragraph::depth_type depth)
240 pit_type newpit = pit;
245 while (newpit != 0 && pars[newpit].getDepth() > depth)
248 if (pars[newpit].getDepth() > depth)
255 pit_type outerHook(pit_type par_offset, ParagraphList const & pars)
257 Paragraph const & par = pars[par_offset];
259 if (par.getDepth() == 0)
261 return depthHook(par_offset, pars, Paragraph::depth_type(par.getDepth() - 1));
265 bool isFirstInSequence(pit_type par_offset, ParagraphList const & pars)
267 Paragraph const & par = pars[par_offset];
269 pit_type dhook_offset = depthHook(par_offset, pars, par.getDepth());
271 Paragraph const & dhook = pars[dhook_offset];
273 return dhook_offset == par_offset
274 || dhook.layout() != par.layout()
275 || dhook.getDepth() != par.getDepth();
279 int getEndLabel(pit_type p, ParagraphList const & pars)
282 Paragraph::depth_type par_depth = pars[p].getDepth();
283 while (pit != pit_type(pars.size())) {
284 LyXLayout_ptr const & layout = pars[pit].layout();
285 int const endlabeltype = layout->endlabeltype;
287 if (endlabeltype != END_LABEL_NO_LABEL) {
288 if (p + 1 == pit_type(pars.size()))
291 Paragraph::depth_type const next_depth =
292 pars[p + 1].getDepth();
293 if (par_depth > next_depth ||
294 (par_depth == next_depth && layout != pars[p + 1].layout()))
300 pit = outerHook(pit, pars);
301 if (pit != pit_type(pars.size()))
302 par_depth = pars[pit].getDepth();
304 return END_LABEL_NO_LABEL;
308 LyXFont const outerFont(pit_type par_offset, ParagraphList const & pars)
310 Paragraph::depth_type par_depth = pars[par_offset].getDepth();
311 LyXFont tmpfont(LyXFont::ALL_INHERIT);
313 // Resolve against environment font information
314 while (par_offset != pit_type(pars.size())
316 && !tmpfont.resolved()) {
317 par_offset = outerHook(par_offset, pars);
318 if (par_offset != pit_type(pars.size())) {
319 tmpfont.realize(pars[par_offset].layout()->font);
320 par_depth = pars[par_offset].getDepth();
328 /// return the number of InsetOptArg in a paragraph
329 int numberOfOptArgs(Paragraph const & par)
333 InsetList::const_iterator it = par.insetlist.begin();
334 InsetList::const_iterator end = par.insetlist.end();
335 for (; it != end ; ++it) {
336 if (it->inset->lyxCode() == InsetBase::OPTARG_CODE)