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 // FIXME: change tracking (MG)
127 par.erase(i, false); // erase without change tracking
131 // Make sure that we keep the language when
132 // breaking paragrpah.
134 LyXFont changed = tmp->getFirstFontSettings(bparams);
135 LyXFont old = par.getFontSettings(bparams, par.size());
136 changed.setLanguage(old.language());
137 tmp->setFont(0, changed);
144 par.params().clear();
145 par.layout(bparams.getLyXTextClass().defaultLayout());
148 // layout stays the same with latex-environments
150 par.layout(tmp->layout());
151 par.setLabelWidthString(tmp->params().labelWidthString());
152 par.params().depth(tmp->params().depth());
155 // subtle, but needed to get empty pars working right
156 if (bparams.trackChanges) {
157 // FIXME: Change tracking (MG)
159 // set 'par' text to INSERTED in CT mode; clear CT info otherwise
160 // else if (!tmp->size())
161 // set 'tmp' text to INSERTED in CT mode; clear CT info otherwise
166 void breakParagraphConservative(BufferParams const & bparams,
167 ParagraphList & pars, pit_type par_offset, pos_type pos)
169 // create a new paragraph
170 Paragraph & tmp = *pars.insert(boost::next(pars.begin(), par_offset + 1),
172 Paragraph & par = pars[par_offset];
174 tmp.makeSameLayout(par);
176 // When can pos > size()?
177 // I guess pos == size() is possible.
178 if (par.size() > pos) {
179 // copy everything behind the break-position to the new
181 pos_type pos_end = par.size() - 1;
183 for (pos_type i = pos, j = pos; i <= pos_end; ++i) {
184 Change::Type change = par.lookupChange(i).type;
185 // FIXME: change tracking (MG)
186 if (moveItem(par, tmp, bparams, i, j - pos, Change(change)))
189 // Move over end-of-par change attr
190 // FIXME: change tracking (MG)
191 tmp.setChange(tmp.size(), Change(par.lookupChange(par.size()).type));
193 // If tracking changes, set all the text that is to be
194 // erased to Type::INSERTED.
195 for (pos_type k = pos_end; k >= pos; --k) {
196 if (bparams.trackChanges)
197 // FIXME: Change tracking (MG)
198 par.setChange(k, Change(Change::INSERTED));
199 // FIXME: change tracking (MG)
206 void mergeParagraph(BufferParams const & bparams,
207 ParagraphList & pars, pit_type par_offset)
209 Paragraph & next = pars[par_offset + 1];
210 Paragraph & par = pars[par_offset];
212 pos_type pos_end = next.size() - 1;
213 pos_type pos_insert = par.size();
215 // What happens is the following. Later on, moveItem() will copy
216 // over characters from the next paragraph to be inserted into this
217 // position. Now, if the first char to be so copied is "red" (i.e.,
218 // marked deleted) and the paragraph break is marked "blue",
219 // insertChar will trigger (eventually, through record(), and see
220 // del() and erase() in changes.C) a "hard" character deletion.
221 // Which doesn't make sense of course at this pos, but the effect is
222 // to shorten the change range to which this para break belongs, by
223 // one. It will (should) remain "orphaned", having no CT info to it,
224 // and check() in changes.C will assert. Setting the para break
225 // forcibly to "black" prevents this scenario. -- MV 13.3.2006
226 // FIXME: change tracking (MG)
227 par.setChange(par.size(), Change(Change::UNCHANGED));
229 Change::Type cr = next.lookupChange(next.size()).type;
230 // ok, now copy the paragraph
231 for (pos_type i = 0, j = 0; i <= pos_end; ++i) {
232 Change::Type change = next.lookupChange(i).type;
233 // FIXME: change tracking (MG)
234 if (moveItem(next, par, bparams, i, pos_insert + j, Change(change)))
237 // Move the change status of "carriage return" over
238 // FIXME: change tracking (MG)
239 par.setChange(par.size(), Change(cr));
241 pars.erase(boost::next(pars.begin(), par_offset + 1));
245 pit_type depthHook(pit_type pit,
246 ParagraphList const & pars, Paragraph::depth_type depth)
248 pit_type newpit = pit;
253 while (newpit != 0 && pars[newpit].getDepth() > depth)
256 if (pars[newpit].getDepth() > depth)
263 pit_type outerHook(pit_type par_offset, ParagraphList const & pars)
265 Paragraph const & par = pars[par_offset];
267 if (par.getDepth() == 0)
269 return depthHook(par_offset, pars, Paragraph::depth_type(par.getDepth() - 1));
273 bool isFirstInSequence(pit_type par_offset, ParagraphList const & pars)
275 Paragraph const & par = pars[par_offset];
277 pit_type dhook_offset = depthHook(par_offset, pars, par.getDepth());
279 Paragraph const & dhook = pars[dhook_offset];
281 return dhook_offset == par_offset
282 || dhook.layout() != par.layout()
283 || dhook.getDepth() != par.getDepth();
287 int getEndLabel(pit_type p, ParagraphList const & pars)
290 Paragraph::depth_type par_depth = pars[p].getDepth();
291 while (pit != pit_type(pars.size())) {
292 LyXLayout_ptr const & layout = pars[pit].layout();
293 int const endlabeltype = layout->endlabeltype;
295 if (endlabeltype != END_LABEL_NO_LABEL) {
296 if (p + 1 == pit_type(pars.size()))
299 Paragraph::depth_type const next_depth =
300 pars[p + 1].getDepth();
301 if (par_depth > next_depth ||
302 (par_depth == next_depth && layout != pars[p + 1].layout()))
308 pit = outerHook(pit, pars);
309 if (pit != pit_type(pars.size()))
310 par_depth = pars[pit].getDepth();
312 return END_LABEL_NO_LABEL;
316 LyXFont const outerFont(pit_type par_offset, ParagraphList const & pars)
318 Paragraph::depth_type par_depth = pars[par_offset].getDepth();
319 LyXFont tmpfont(LyXFont::ALL_INHERIT);
321 // Resolve against environment font information
322 while (par_offset != pit_type(pars.size())
324 && !tmpfont.resolved()) {
325 par_offset = outerHook(par_offset, pars);
326 if (par_offset != pit_type(pars.size())) {
327 tmpfont.realize(pars[par_offset].layout()->font);
328 par_depth = pars[par_offset].getDepth();
336 /// return the number of InsetOptArg in a paragraph
337 int numberOfOptArgs(Paragraph const & par)
341 InsetList::const_iterator it = par.insetlist.begin();
342 InsetList::const_iterator end = par.insetlist.end();
343 for (; it != end ; ++it) {
344 if (it->inset->lyxCode() == InsetBase::OPTARG_CODE)