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 // FIXME: change tracking (MG)
120 tmp->setChange(j - pos, Change(change));
125 for (pos_type i = pos_end; i >= pos; --i)
126 par.erase(i, false); // erase without change tracking
130 // Make sure that we keep the language when
131 // breaking paragrpah.
133 LyXFont changed = tmp->getFirstFontSettings(bparams);
134 LyXFont old = par.getFontSettings(bparams, par.size());
135 changed.setLanguage(old.language());
136 tmp->setFont(0, changed);
143 par.params().clear();
144 par.layout(bparams.getLyXTextClass().defaultLayout());
147 // layout stays the same with latex-environments
149 par.layout(tmp->layout());
150 par.setLabelWidthString(tmp->params().labelWidthString());
151 par.params().depth(tmp->params().depth());
154 // subtle, but needed to get empty pars working right
155 if (bparams.trackChanges) {
156 // FIXME: Change tracking (MG)
158 // set 'par' text to INSERTED in CT mode; clear CT info otherwise
159 // else if (!tmp->size())
160 // set 'tmp' text to INSERTED in CT mode; clear CT info otherwise
165 void breakParagraphConservative(BufferParams const & bparams,
166 ParagraphList & pars, pit_type par_offset, pos_type pos)
168 // create a new paragraph
169 Paragraph & tmp = *pars.insert(boost::next(pars.begin(), par_offset + 1),
171 Paragraph & par = pars[par_offset];
173 tmp.makeSameLayout(par);
175 // When can pos > size()?
176 // I guess pos == size() is possible.
177 if (par.size() > pos) {
178 // copy everything behind the break-position to the new
180 pos_type pos_end = par.size() - 1;
182 for (pos_type i = pos, j = pos; i <= pos_end; ++i) {
183 Change::Type change = par.lookupChange(i).type;
184 // FIXME: change tracking (MG)
185 if (moveItem(par, tmp, bparams, i, j - pos, Change(change)))
188 // Move over end-of-par change attr
189 // FIXME: change tracking (MG)
190 tmp.setChange(tmp.size(), Change(par.lookupChange(par.size()).type));
192 // If tracking changes, set all the text that is to be
193 // erased to Type::INSERTED.
194 for (pos_type k = pos_end; k >= pos; --k) {
195 if (bparams.trackChanges)
196 // FIXME: Change tracking (MG)
197 par.setChange(k, Change(Change::INSERTED));
198 // FIXME: change tracking (MG)
205 void mergeParagraph(BufferParams const & bparams,
206 ParagraphList & pars, pit_type par_offset)
208 Paragraph & next = pars[par_offset + 1];
209 Paragraph & par = pars[par_offset];
211 pos_type pos_end = next.size() - 1;
212 pos_type pos_insert = par.size();
214 // What happens is the following. Later on, moveItem() will copy
215 // over characters from the next paragraph to be inserted into this
216 // position. Now, if the first char to be so copied is "red" (i.e.,
217 // marked deleted) and the paragraph break is marked "blue",
218 // insertChar will trigger (eventually, through record(), and see
219 // del() and erase() in changes.C) a "hard" character deletion.
220 // Which doesn't make sense of course at this pos, but the effect is
221 // to shorten the change range to which this para break belongs, by
222 // one. It will (should) remain "orphaned", having no CT info to it,
223 // and check() in changes.C will assert. Setting the para break
224 // forcibly to "black" prevents this scenario. -- MV 13.3.2006
225 // FIXME: change tracking (MG)
226 par.setChange(par.size(), Change(Change::UNCHANGED));
228 Change::Type cr = next.lookupChange(next.size()).type;
229 // ok, now copy the paragraph
230 for (pos_type i = 0, j = 0; i <= pos_end; ++i) {
231 Change::Type change = next.lookupChange(i).type;
232 // FIXME: change tracking (MG)
233 if (moveItem(next, par, bparams, i, pos_insert + j, Change(change)))
236 // Move the change status of "carriage return" over
237 // FIXME: change tracking (MG)
238 par.setChange(par.size(), Change(cr));
240 pars.erase(boost::next(pars.begin(), par_offset + 1));
244 pit_type depthHook(pit_type pit,
245 ParagraphList const & pars, Paragraph::depth_type depth)
247 pit_type newpit = pit;
252 while (newpit != 0 && pars[newpit].getDepth() > depth)
255 if (pars[newpit].getDepth() > depth)
262 pit_type outerHook(pit_type par_offset, ParagraphList const & pars)
264 Paragraph const & par = pars[par_offset];
266 if (par.getDepth() == 0)
268 return depthHook(par_offset, pars, Paragraph::depth_type(par.getDepth() - 1));
272 bool isFirstInSequence(pit_type par_offset, ParagraphList const & pars)
274 Paragraph const & par = pars[par_offset];
276 pit_type dhook_offset = depthHook(par_offset, pars, par.getDepth());
278 Paragraph const & dhook = pars[dhook_offset];
280 return dhook_offset == par_offset
281 || dhook.layout() != par.layout()
282 || dhook.getDepth() != par.getDepth();
286 int getEndLabel(pit_type p, ParagraphList const & pars)
289 Paragraph::depth_type par_depth = pars[p].getDepth();
290 while (pit != pit_type(pars.size())) {
291 LyXLayout_ptr const & layout = pars[pit].layout();
292 int const endlabeltype = layout->endlabeltype;
294 if (endlabeltype != END_LABEL_NO_LABEL) {
295 if (p + 1 == pit_type(pars.size()))
298 Paragraph::depth_type const next_depth =
299 pars[p + 1].getDepth();
300 if (par_depth > next_depth ||
301 (par_depth == next_depth && layout != pars[p + 1].layout()))
307 pit = outerHook(pit, pars);
308 if (pit != pit_type(pars.size()))
309 par_depth = pars[pit].getDepth();
311 return END_LABEL_NO_LABEL;
315 LyXFont const outerFont(pit_type par_offset, ParagraphList const & pars)
317 Paragraph::depth_type par_depth = pars[par_offset].getDepth();
318 LyXFont tmpfont(LyXFont::ALL_INHERIT);
320 // Resolve against environment font information
321 while (par_offset != pit_type(pars.size())
323 && !tmpfont.resolved()) {
324 par_offset = outerHook(par_offset, pars);
325 if (par_offset != pit_type(pars.size())) {
326 tmpfont.realize(pars[par_offset].layout()->font);
327 par_depth = pars[par_offset].getDepth();
335 /// return the number of InsetOptArg in a paragraph
336 int numberOfOptArgs(Paragraph const & par)
340 InsetList::const_iterator it = par.insetlist.begin();
341 InsetList::const_iterator end = par.insetlist.end();
342 for (; it != end ; ++it) {
343 if (it->inset->lyxCode() == InsetBase::OPTARG_CODE)