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"
23 #include "outputparams.h"
24 #include "paragraph_pimpl.h"
25 #include "pariterator.h"
30 #include "support/filetools.h"
31 #include "support/lstrings.h"
32 #include "support/lyxlib.h"
33 #include "support/std_sstream.h"
40 using lyx::support::ascii_lowercase;
41 using lyx::support::atoi;
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)
63 char const tmpchar = from.getChar(i);
64 LyXFont tmpfont = from.getFontSettings(params, i);
66 if (tmpchar == Paragraph::META_INSET) {
67 InsetBase * tmpinset = 0;
68 if (from.getInset(i)) {
69 // the inset is not in a paragraph anymore
70 tmpinset = from.insetlist.release(i);
71 from.insetlist.erase(i);
74 if (!to.insetAllowed(tmpinset->lyxCode())) {
79 to.insertInset(j, tmpinset, tmpfont);
81 if (!to.checkInsertChar(tmpfont))
83 to.insertChar(j, tmpchar, tmpfont);
91 void breakParagraph(BufferParams const & bparams,
92 ParagraphList & pars, par_type par, pos_type pos, int flag)
94 // create a new paragraph, and insert into the list
95 ParagraphList::iterator tmp =
96 pars.insert(pars.begin() + par + 1, Paragraph());
98 // without doing that we get a crash when typing <Return> at the
100 tmp->layout(bparams.getLyXTextClass().defaultLayout());
101 // remember to set the inset_owner
102 tmp->setInsetOwner(pars[par].inInset());
104 if (bparams.tracking_changes)
107 // this is an idea for a more userfriendly layout handling, I will
108 // see what the users say
110 // layout stays the same with latex-environments
112 tmp->layout(pars[par].layout());
113 tmp->setLabelWidthString(pars[par].params().labelWidthString());
116 bool const isempty = (pars[par].allowEmpty() && pars[par].empty());
118 if (!isempty && (pars[par].size() > pos || pars[par].empty() || flag == 2)) {
119 tmp->layout(pars[par].layout());
120 tmp->params().align(pars[par].params().align());
121 tmp->setLabelWidthString(pars[par].params().labelWidthString());
123 tmp->params().depth(pars[par].params().depth());
124 tmp->params().noindent(pars[par].params().noindent());
126 // copy everything behind the break-position
127 // to the new paragraph
129 /* Note: if !keepempty, empty() == true, then we reach
130 * here with size() == 0. So pos_end becomes - 1. This
131 * doesn't cause problems because both loops below
132 * enforce pos <= pos_end and 0 <= pos
134 pos_type pos_end = pars[par].size() - 1;
136 for (pos_type i = pos, j = pos; i <= pos_end; ++i) {
137 Change::Type change = pars[par].lookupChange(i);
138 if (moveItem(pars[par], *tmp, bparams, i, j - pos)) {
139 tmp->setChange(j - pos, change);
144 for (pos_type i = pos_end; i >= pos; --i)
145 pars[par].eraseIntern(i);
151 pars[par].params().clear();
153 pars[par].layout(bparams.getLyXTextClass().defaultLayout());
155 // layout stays the same with latex-environments
157 pars[par].layout(tmp->layout());
158 pars[par].setLabelWidthString(tmp->params().labelWidthString());
159 pars[par].params().depth(tmp->params().depth());
162 // subtle, but needed to get empty pars working right
163 if (bparams.tracking_changes) {
164 if (!pars[par].size()) {
165 pars[par].cleanChanges();
166 } else if (!tmp->size()) {
173 void breakParagraphConservative(BufferParams const & bparams,
174 ParagraphList & pars, par_type par, pos_type pos)
176 // create a new paragraph
177 Paragraph & tmp = *pars.insert(pars.begin() + par + 1, Paragraph());
178 tmp.makeSameLayout(pars[par]);
180 // When can pos > size()?
181 // I guess pos == size() is possible.
182 if (pars[par].size() > pos) {
183 // copy everything behind the break-position to the new
185 pos_type pos_end = pars[par].size() - 1;
187 for (pos_type i = pos, j = pos; i <= pos_end; ++i)
188 if (moveItem(pars[par], tmp, bparams, i, j - pos))
191 for (pos_type k = pos_end; k >= pos; --k)
197 void mergeParagraph(BufferParams const & bparams,
198 ParagraphList & pars, par_type par)
200 Paragraph & next = pars[par + 1];
202 pos_type pos_end = next.size() - 1;
203 pos_type pos_insert = pars[par].size();
205 // ok, now copy the paragraph
206 for (pos_type i = 0, j = 0; i <= pos_end; ++i)
207 if (moveItem(next, pars[par], bparams, i, pos_insert + j))
210 pars.erase(pars.begin() + par + 1);
214 par_type depthHook(par_type pit,
215 ParagraphList const & pars, Paragraph::depth_type depth)
217 par_type newpit = pit;
222 while (newpit != 0 && pars[newpit].getDepth() > depth)
225 if (pars[newpit].getDepth() > depth)
232 par_type outerHook(par_type par, ParagraphList const & pars)
234 if (pars[par].getDepth() == 0)
236 return depthHook(par, pars, Paragraph::depth_type(pars[par].getDepth() - 1));
240 bool isFirstInSequence(par_type pit, ParagraphList const & pars)
242 par_type dhook = depthHook(pit, pars, pars[pit].getDepth());
244 || pars[dhook].layout() != pars[pit].layout()
245 || pars[dhook].getDepth() != pars[pit].getDepth();
249 int getEndLabel(par_type p, ParagraphList const & pars)
252 Paragraph::depth_type par_depth = pars[p].getDepth();
253 while (pit != par_type(pars.size())) {
254 LyXLayout_ptr const & layout = pars[pit].layout();
255 int const endlabeltype = layout->endlabeltype;
257 if (endlabeltype != END_LABEL_NO_LABEL) {
258 if (p + 1 == par_type(pars.size()))
261 Paragraph::depth_type const next_depth =
262 pars[p + 1].getDepth();
263 if (par_depth > next_depth ||
264 (par_depth == next_depth && layout != pars[p + 1].layout()))
270 pit = outerHook(pit, pars);
271 if (pit != par_type(pars.size()))
272 par_depth = pars[pit].getDepth();
274 return END_LABEL_NO_LABEL;
278 LyXFont const outerFont(par_type pit, ParagraphList const & pars)
280 Paragraph::depth_type par_depth = pars[pit].getDepth();
281 LyXFont tmpfont(LyXFont::ALL_INHERIT);
283 // Resolve against environment font information
284 while (pit != par_type(pars.size())
286 && !tmpfont.resolved()) {
287 pit = outerHook(pit, pars);
288 if (pit != par_type(pars.size())) {
289 tmpfont.realize(pars[pit].layout()->font);
290 par_depth = pars[pit].getDepth();
298 par_type outerPar(Buffer const & buf, InsetBase const * inset)
300 ParIterator pit = const_cast<Buffer &>(buf).par_iterator_begin();
301 ParIterator end = const_cast<Buffer &>(buf).par_iterator_end();
302 for ( ; pit != end; ++pit) {
304 // the second '=' below is intentional
305 for (int i = 0; (text = inset->getText(i)); ++i)
306 if (&text->paragraphs() == &pit.plist())
307 return pit.outerPar();
309 InsetList::iterator ii = pit->insetlist.begin();
310 InsetList::iterator iend = pit->insetlist.end();
311 for ( ; ii != iend; ++ii)
312 if (ii->inset == inset)
313 return pit.outerPar();
315 lyxerr << "outerPar: should not happen" << endl;
317 return buf.paragraphs().size(); // shut up compiler
321 Paragraph const & ownerPar(Buffer const & buf, InsetBase const * inset)
323 ParConstIterator pit = buf.par_iterator_begin();
324 ParConstIterator end = buf.par_iterator_end();
325 for ( ; pit != end; ++pit) {
327 // the second '=' below is intentional
328 for (int i = 0; (text = inset->getText(i)); ++i)
329 if (&text->paragraphs() == &pit.plist())
332 InsetList::const_iterator ii = pit->insetlist.begin();
333 InsetList::const_iterator iend = pit->insetlist.end();
334 for ( ; ii != iend; ++ii)
335 if (ii->inset == inset)
338 lyxerr << "ownerPar: should not happen" << endl;
340 return buf.paragraphs().front(); // shut up compiler
344 /// return the range of pars [beg, end[ owning the range of y [ystart, yend]
345 void getParsInRange(ParagraphList & pars, int ystart, int yend,
346 par_type & beg, par_type & end)
348 BOOST_ASSERT(!pars.empty());
349 par_type const endpar = pars.size();
350 par_type const begpar = 0;
352 for (beg = endpar - 1; beg != begpar && pars[beg].y > ystart; --beg)
355 for (end = beg ; end != endpar && pars[end].y <= yend; ++end)