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 // this is an idea for a more userfriendly layout handling, I will
114 // see what the users say
116 // layout stays the same with latex-environments
118 tmp->layout(par.layout());
119 tmp->setLabelWidthString(par.params().labelWidthString());
120 tmp->params().depth(par.params().depth());
121 } else if (par.params().depth() > 0) {
122 Paragraph const & hook = pars[outerHook(par_offset, pars)];
123 tmp->layout(hook.layout());
124 // not sure the line below is useful
125 tmp->setLabelWidthString(par.params().labelWidthString());
126 tmp->params().depth(hook.params().depth());
129 bool const isempty = (par.allowEmpty() && par.empty());
131 if (!isempty && (par.size() > pos || par.empty() || flag == 2)) {
132 tmp->layout(par.layout());
133 tmp->params().align(par.params().align());
134 tmp->setLabelWidthString(par.params().labelWidthString());
136 tmp->params().depth(par.params().depth());
137 tmp->params().noindent(par.params().noindent());
139 // copy everything behind the break-position
140 // to the new paragraph
142 /* Note: if !keepempty, empty() == true, then we reach
143 * here with size() == 0. So pos_end becomes - 1. This
144 * doesn't cause problems because both loops below
145 * enforce pos <= pos_end and 0 <= pos
147 pos_type pos_end = par.size() - 1;
149 for (pos_type i = pos, j = pos; i <= pos_end; ++i) {
150 Change::Type change = par.lookupChange(i).type;
151 if (moveItem(par, *tmp, bparams, i, j - pos)) {
152 tmp->setChange(j - pos, change);
157 for (pos_type i = pos_end; i >= pos; --i)
162 // Make sure that we keep the language when
163 // breaking paragrpah.
165 LyXFont changed = tmp->getFirstFontSettings(bparams);
166 LyXFont old = par.getFontSettings(bparams, par.size());
167 changed.setLanguage(old.language());
168 tmp->setFont(0, changed);
175 par.params().clear();
176 par.layout(bparams.getLyXTextClass().defaultLayout());
179 // layout stays the same with latex-environments
181 par.layout(tmp->layout());
182 par.setLabelWidthString(tmp->params().labelWidthString());
183 par.params().depth(tmp->params().depth());
186 // subtle, but needed to get empty pars working right
187 if (bparams.trackChanges) {
188 // FIXME: Change tracking (MG)
190 // set 'par' text to INSERTED in CT mode; clear CT info otherwise
191 // else if (!tmp->size())
192 // set 'tmp' text to INSERTED in CT mode; clear CT info otherwise
197 void breakParagraphConservative(BufferParams const & bparams,
198 ParagraphList & pars, pit_type par_offset, pos_type pos)
200 // create a new paragraph
201 Paragraph & tmp = *pars.insert(boost::next(pars.begin(), par_offset + 1),
203 Paragraph & par = pars[par_offset];
205 tmp.makeSameLayout(par);
207 // When can pos > size()?
208 // I guess pos == size() is possible.
209 if (par.size() > pos) {
210 // copy everything behind the break-position to the new
212 pos_type pos_end = par.size() - 1;
214 for (pos_type i = pos, j = pos; i <= pos_end; ++i) {
215 Change::Type change = par.lookupChange(i).type;
216 if (moveItem(par, tmp, bparams, i, j - pos, change))
219 // Move over end-of-par change attr
220 tmp.setChange(tmp.size(), par.lookupChange(par.size()).type);
222 // If tracking changes, set all the text that is to be
223 // erased to Type::INSERTED.
224 for (pos_type k = pos_end; k >= pos; --k) {
225 if (bparams.trackChanges)
226 // FIXME: Change tracking (MG)
227 par.setChange(k, Change::INSERTED);
234 void mergeParagraph(BufferParams const & bparams,
235 ParagraphList & pars, pit_type par_offset)
237 Paragraph & next = pars[par_offset + 1];
238 Paragraph & par = pars[par_offset];
240 pos_type pos_end = next.size() - 1;
241 pos_type pos_insert = par.size();
243 // What happens is the following. Later on, moveItem() will copy
244 // over characters from the next paragraph to be inserted into this
245 // position. Now, if the first char to be so copied is "red" (i.e.,
246 // marked deleted) and the paragraph break is marked "blue",
247 // insertChar will trigger (eventually, through record(), and see
248 // del() and erase() in changes.C) a "hard" character deletion.
249 // Which doesn't make sense of course at this pos, but the effect is
250 // to shorten the change range to which this para break belongs, by
251 // one. It will (should) remain "orphaned", having no CT info to it,
252 // and check() in changes.C will assert. Setting the para break
253 // forcibly to "black" prevents this scenario. -- MV 13.3.2006
254 par.setChange(par.size(), Change::UNCHANGED);
256 Change::Type cr = next.lookupChange(next.size()).type;
257 // ok, now copy the paragraph
258 for (pos_type i = 0, j = 0; i <= pos_end; ++i) {
259 Change::Type change = next.lookupChange(i).type;
260 if (moveItem(next, par, bparams, i, pos_insert + j, change))
263 // Move the change status of "carriage return" over
264 par.setChange(par.size(), cr);
266 pars.erase(boost::next(pars.begin(), par_offset + 1));
270 pit_type depthHook(pit_type pit,
271 ParagraphList const & pars, Paragraph::depth_type depth)
273 pit_type newpit = pit;
278 while (newpit != 0 && pars[newpit].getDepth() > depth)
281 if (pars[newpit].getDepth() > depth)
288 pit_type outerHook(pit_type par_offset, ParagraphList const & pars)
290 Paragraph const & par = pars[par_offset];
292 if (par.getDepth() == 0)
294 return depthHook(par_offset, pars, Paragraph::depth_type(par.getDepth() - 1));
298 bool isFirstInSequence(pit_type par_offset, ParagraphList const & pars)
300 Paragraph const & par = pars[par_offset];
302 pit_type dhook_offset = depthHook(par_offset, pars, par.getDepth());
304 Paragraph const & dhook = pars[dhook_offset];
306 return dhook_offset == par_offset
307 || dhook.layout() != par.layout()
308 || dhook.getDepth() != par.getDepth();
312 int getEndLabel(pit_type p, ParagraphList const & pars)
315 Paragraph::depth_type par_depth = pars[p].getDepth();
316 while (pit != pit_type(pars.size())) {
317 LyXLayout_ptr const & layout = pars[pit].layout();
318 int const endlabeltype = layout->endlabeltype;
320 if (endlabeltype != END_LABEL_NO_LABEL) {
321 if (p + 1 == pit_type(pars.size()))
324 Paragraph::depth_type const next_depth =
325 pars[p + 1].getDepth();
326 if (par_depth > next_depth ||
327 (par_depth == next_depth && layout != pars[p + 1].layout()))
333 pit = outerHook(pit, pars);
334 if (pit != pit_type(pars.size()))
335 par_depth = pars[pit].getDepth();
337 return END_LABEL_NO_LABEL;
341 LyXFont const outerFont(pit_type par_offset, ParagraphList const & pars)
343 Paragraph::depth_type par_depth = pars[par_offset].getDepth();
344 LyXFont tmpfont(LyXFont::ALL_INHERIT);
346 // Resolve against environment font information
347 while (par_offset != pit_type(pars.size())
349 && !tmpfont.resolved()) {
350 par_offset = outerHook(par_offset, pars);
351 if (par_offset != pit_type(pars.size())) {
352 tmpfont.realize(pars[par_offset].layout()->font);
353 par_depth = pars[par_offset].getDepth();
361 /// return the number of InsetOptArg in a paragraph
362 int numberOfOptArgs(Paragraph const & par)
366 InsetList::const_iterator it = par.insetlist.begin();
367 InsetList::const_iterator end = par.insetlist.end();
368 for (; it != end ; ++it) {
369 if (it->inset->lyxCode() == InsetBase::OPTARG_CODE)