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"
16 #include "bufferparams.h"
24 #include "outputparams.h"
25 #include "paragraph_pimpl.h"
26 #include "pariterator.h"
31 #include "support/filetools.h"
32 #include "support/lstrings.h"
33 #include "support/lyxlib.h"
41 using lyx::support::ascii_lowercase;
42 using lyx::support::bformat;
43 using lyx::support::compare_ascii_no_case;
44 using lyx::support::compare_no_case;
45 using lyx::support::contains;
46 using lyx::support::split;
47 using lyx::support::subst;
53 using std::istringstream;
60 bool moveItem(Paragraph & from, Paragraph & to,
61 BufferParams const & params, pos_type i, pos_type j,
62 Change change = Change(Change::INSERTED));
64 bool moveItem(Paragraph & from, Paragraph & to,
65 BufferParams const & params, pos_type i, pos_type j,
68 Paragraph::value_type const tmpchar = from.getChar(i);
69 LyXFont tmpfont = from.getFontSettings(params, i);
71 if (tmpchar == Paragraph::META_INSET) {
72 InsetBase * tmpinset = 0;
73 if (from.getInset(i)) {
74 // the inset is not in a paragraph anymore
75 tmpinset = from.insetlist.release(i);
76 from.insetlist.erase(i);
79 if (!to.insetAllowed(tmpinset->lyxCode())) {
84 to.insertInset(j, tmpinset, tmpfont, change);
86 to.insertChar(j, tmpchar, tmpfont, change);
94 void breakParagraph(BufferParams const & bparams,
95 ParagraphList & pars, pit_type par_offset, pos_type pos, int flag)
97 // create a new paragraph, and insert into the list
98 ParagraphList::iterator tmp =
99 pars.insert(boost::next(pars.begin(), par_offset + 1),
102 Paragraph & par = pars[par_offset];
104 // we will invalidate the row cache
107 // without doing that we get a crash when typing <Return> at the
108 // end of a paragraph
109 tmp->layout(bparams.getLyXTextClass().defaultLayout());
110 // remember to set the inset_owner
111 tmp->setInsetOwner(par.inInset());
113 if (bparams.tracking_changes)
116 // this is an idea for a more userfriendly layout handling, I will
117 // see what the users say
119 // layout stays the same with latex-environments
121 tmp->layout(par.layout());
122 tmp->setLabelWidthString(par.params().labelWidthString());
123 tmp->params().depth(par.params().depth());
124 } else if (par.params().depth() > 0) {
125 Paragraph const & hook = pars[outerHook(par_offset, pars)];
126 tmp->layout(hook.layout());
127 // not sure the line below is useful
128 tmp->setLabelWidthString(par.params().labelWidthString());
129 tmp->params().depth(hook.params().depth());
132 bool const isempty = (par.allowEmpty() && par.empty());
134 if (!isempty && (par.size() > pos || par.empty() || flag == 2)) {
135 tmp->layout(par.layout());
136 tmp->params().align(par.params().align());
137 tmp->setLabelWidthString(par.params().labelWidthString());
139 tmp->params().depth(par.params().depth());
140 tmp->params().noindent(par.params().noindent());
142 // copy everything behind the break-position
143 // to the new paragraph
145 /* Note: if !keepempty, empty() == true, then we reach
146 * here with size() == 0. So pos_end becomes - 1. This
147 * doesn't cause problems because both loops below
148 * enforce pos <= pos_end and 0 <= pos
150 pos_type pos_end = par.size() - 1;
152 for (pos_type i = pos, j = pos; i <= pos_end; ++i) {
153 Change::Type change = par.lookupChange(i).type;
154 if (moveItem(par, *tmp, bparams, i, j - pos)) {
155 tmp->setChange(j - pos, change);
160 for (pos_type i = pos_end; i >= pos; --i)
165 // Make sure that we keep the language when
166 // breaking paragrpah.
168 LyXFont changed = tmp->getFirstFontSettings(bparams);
169 LyXFont old = par.getFontSettings(bparams, par.size());
170 changed.setLanguage(old.language());
171 tmp->setFont(0, changed);
178 par.params().clear();
179 par.layout(bparams.getLyXTextClass().defaultLayout());
182 // layout stays the same with latex-environments
184 par.layout(tmp->layout());
185 par.setLabelWidthString(tmp->params().labelWidthString());
186 par.params().depth(tmp->params().depth());
189 // subtle, but needed to get empty pars working right
190 if (bparams.tracking_changes) {
193 } else if (!tmp->size()) {
200 void breakParagraphConservative(BufferParams const & bparams,
201 ParagraphList & pars, pit_type par_offset, pos_type pos)
203 // create a new paragraph
204 Paragraph & tmp = *pars.insert(boost::next(pars.begin(), par_offset + 1),
206 Paragraph & par = pars[par_offset];
208 if (bparams.tracking_changes)
211 tmp.makeSameLayout(par);
213 // When can pos > size()?
214 // I guess pos == size() is possible.
215 if (par.size() > pos) {
216 // copy everything behind the break-position to the new
218 pos_type pos_end = par.size() - 1;
220 for (pos_type i = pos, j = pos; i <= pos_end; ++i) {
221 Change::Type change = par.lookupChange(i).type;
222 if (moveItem(par, tmp, bparams, i, j - pos, change))
225 // Move over end-of-par change attr
226 tmp.setChange(tmp.size(), par.lookupChange(par.size()).type);
228 // If tracking changes, set all the text that is to be
229 // erased to Type::INSERTED.
230 for (pos_type k = pos_end; k >= pos; --k) {
231 if (bparams.tracking_changes)
232 par.setChange(k, Change::INSERTED);
239 void mergeParagraph(BufferParams const & bparams,
240 ParagraphList & pars, pit_type par_offset)
242 Paragraph & next = pars[par_offset + 1];
243 Paragraph & par = pars[par_offset];
245 pos_type pos_end = next.size() - 1;
246 pos_type pos_insert = par.size();
248 // What happens is the following. Later on, moveItem() will copy
249 // over characters from the next paragraph to be inserted into this
250 // position. Now, if the first char to be so copied is "red" (i.e.,
251 // marked deleted) and the paragraph break is marked "blue",
252 // insertChar will trigger (eventually, through record(), and see
253 // del() and erase() in changes.C) a "hard" character deletion.
254 // Which doesn't make sense of course at this pos, but the effect is
255 // to shorten the change range to which this para break belongs, by
256 // one. It will (should) remain "orphaned", having no CT info to it,
257 // and check() in changes.C will assert. Setting the para break
258 // forcibly to "black" prevents this scenario. -- MV 13.3.2006
259 par.setChange(par.size(), Change::UNCHANGED);
261 Change::Type cr = next.lookupChange(next.size()).type;
262 // ok, now copy the paragraph
263 for (pos_type i = 0, j = 0; i <= pos_end; ++i) {
264 Change::Type change = next.lookupChange(i).type;
265 if (moveItem(next, par, bparams, i, pos_insert + j, change))
268 // Move the change status of "carriage return" over
269 par.setChange(par.size(), cr);
271 pars.erase(boost::next(pars.begin(), par_offset + 1));
275 pit_type depthHook(pit_type pit,
276 ParagraphList const & pars, Paragraph::depth_type depth)
278 pit_type newpit = pit;
283 while (newpit != 0 && pars[newpit].getDepth() > depth)
286 if (pars[newpit].getDepth() > depth)
293 pit_type outerHook(pit_type par_offset, ParagraphList const & pars)
295 Paragraph const & par = pars[par_offset];
297 if (par.getDepth() == 0)
299 return depthHook(par_offset, pars, Paragraph::depth_type(par.getDepth() - 1));
303 bool isFirstInSequence(pit_type par_offset, ParagraphList const & pars)
305 Paragraph const & par = pars[par_offset];
307 pit_type dhook_offset = depthHook(par_offset, pars, par.getDepth());
309 Paragraph const & dhook = pars[dhook_offset];
311 return dhook_offset == par_offset
312 || dhook.layout() != par.layout()
313 || dhook.getDepth() != par.getDepth();
317 int getEndLabel(pit_type p, ParagraphList const & pars)
320 Paragraph::depth_type par_depth = pars[p].getDepth();
321 while (pit != pit_type(pars.size())) {
322 LyXLayout_ptr const & layout = pars[pit].layout();
323 int const endlabeltype = layout->endlabeltype;
325 if (endlabeltype != END_LABEL_NO_LABEL) {
326 if (p + 1 == pit_type(pars.size()))
329 Paragraph::depth_type const next_depth =
330 pars[p + 1].getDepth();
331 if (par_depth > next_depth ||
332 (par_depth == next_depth && layout != pars[p + 1].layout()))
338 pit = outerHook(pit, pars);
339 if (pit != pit_type(pars.size()))
340 par_depth = pars[pit].getDepth();
342 return END_LABEL_NO_LABEL;
346 LyXFont const outerFont(pit_type par_offset, ParagraphList const & pars)
348 Paragraph::depth_type par_depth = pars[par_offset].getDepth();
349 LyXFont tmpfont(LyXFont::ALL_INHERIT);
351 // Resolve against environment font information
352 while (par_offset != pit_type(pars.size())
354 && !tmpfont.resolved()) {
355 par_offset = outerHook(par_offset, pars);
356 if (par_offset != pit_type(pars.size())) {
357 tmpfont.realize(pars[par_offset].layout()->font);
358 par_depth = pars[par_offset].getDepth();
366 /// return the number of InsetOptArg in a paragraph
367 int numberOfOptArgs(Paragraph const & par)
371 InsetList::const_iterator it = par.insetlist.begin();
372 InsetList::const_iterator end = par.insetlist.end();
373 for (; it != end ; ++it) {
374 if (it->inset->lyxCode() == InsetBase::OPTARG_CODE)