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"
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 Paragraph::value_type 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_offset, 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_offset + 1, Paragraph());
98 Paragraph & par = pars[par_offset];
100 // without doing that we get a crash when typing <Return> at the
101 // end of a paragraph
102 tmp->layout(bparams.getLyXTextClass().defaultLayout());
103 // remember to set the inset_owner
104 tmp->setInsetOwner(par.inInset());
106 if (bparams.tracking_changes)
109 // this is an idea for a more userfriendly layout handling, I will
110 // see what the users say
112 // layout stays the same with latex-environments
114 tmp->layout(par.layout());
115 tmp->setLabelWidthString(par.params().labelWidthString());
118 bool const isempty = (par.allowEmpty() && par.empty());
120 if (!isempty && (par.size() > pos || par.empty() || flag == 2)) {
121 tmp->layout(par.layout());
122 tmp->params().align(par.params().align());
123 tmp->setLabelWidthString(par.params().labelWidthString());
125 tmp->params().depth(par.params().depth());
126 tmp->params().noindent(par.params().noindent());
128 // copy everything behind the break-position
129 // to the new paragraph
131 /* Note: if !keepempty, empty() == true, then we reach
132 * here with size() == 0. So pos_end becomes - 1. This
133 * doesn't cause problems because both loops below
134 * enforce pos <= pos_end and 0 <= pos
136 pos_type pos_end = par.size() - 1;
138 for (pos_type i = pos, j = pos; i <= pos_end; ++i) {
139 Change::Type change = par.lookupChange(i);
140 if (moveItem(par, *tmp, bparams, i, j - pos)) {
141 tmp->setChange(j - pos, change);
146 for (pos_type i = pos_end; i >= pos; --i)
151 // Make sure that we keep the language when
152 // breaking paragrpah.
154 LyXFont changed = tmp->getFirstFontSettings();
155 LyXFont old = par.getFontSettings(bparams, par.size());
156 changed.setLanguage(old.language());
157 tmp->setFont(0, changed);
163 par.params().clear();
165 par.layout(bparams.getLyXTextClass().defaultLayout());
167 // layout stays the same with latex-environments
169 par.layout(tmp->layout());
170 par.setLabelWidthString(tmp->params().labelWidthString());
171 par.params().depth(tmp->params().depth());
174 // subtle, but needed to get empty pars working right
175 if (bparams.tracking_changes) {
178 } else if (!tmp->size()) {
185 void breakParagraphConservative(BufferParams const & bparams,
186 ParagraphList & pars, par_type par_offset, pos_type pos)
188 // create a new paragraph
189 Paragraph & tmp = *pars.insert(pars.begin() + par_offset + 1, Paragraph());
190 Paragraph & par = pars[par_offset];
192 tmp.makeSameLayout(par);
194 // When can pos > size()?
195 // I guess pos == size() is possible.
196 if (par.size() > pos) {
197 // copy everything behind the break-position to the new
199 pos_type pos_end = par.size() - 1;
201 for (pos_type i = pos, j = pos; i <= pos_end; ++i)
202 if (moveItem(par, tmp, bparams, i, j - pos))
205 for (pos_type k = pos_end; k >= pos; --k)
211 void mergeParagraph(BufferParams const & bparams,
212 ParagraphList & pars, par_type par_offset)
214 Paragraph & next = pars[par_offset + 1];
215 Paragraph & par = pars[par_offset];
217 pos_type pos_end = next.size() - 1;
218 pos_type pos_insert = par.size();
220 // ok, now copy the paragraph
221 for (pos_type i = 0, j = 0; i <= pos_end; ++i)
222 if (moveItem(next, par, bparams, i, pos_insert + j))
225 pars.erase(pars.begin() + par_offset + 1);
229 par_type depthHook(par_type pit,
230 ParagraphList const & pars, Paragraph::depth_type depth)
232 par_type newpit = pit;
237 while (newpit != 0 && pars[newpit].getDepth() > depth)
240 if (pars[newpit].getDepth() > depth)
247 par_type outerHook(par_type par_offset, ParagraphList const & pars)
249 Paragraph const & par = pars[par_offset];
251 if (par.getDepth() == 0)
253 return depthHook(par_offset, pars, Paragraph::depth_type(par.getDepth() - 1));
257 bool isFirstInSequence(par_type par_offset, ParagraphList const & pars)
259 Paragraph const & par = pars[par_offset];
261 par_type dhook_offset = depthHook(par_offset, pars, par.getDepth());
263 Paragraph const & dhook = pars[dhook_offset];
265 return dhook_offset == par_offset
266 || dhook.layout() != par.layout()
267 || dhook.getDepth() != par.getDepth();
271 int getEndLabel(par_type p, ParagraphList const & pars)
274 Paragraph::depth_type par_depth = pars[p].getDepth();
275 while (pit != par_type(pars.size())) {
276 LyXLayout_ptr const & layout = pars[pit].layout();
277 int const endlabeltype = layout->endlabeltype;
279 if (endlabeltype != END_LABEL_NO_LABEL) {
280 if (p + 1 == par_type(pars.size()))
283 Paragraph::depth_type const next_depth =
284 pars[p + 1].getDepth();
285 if (par_depth > next_depth ||
286 (par_depth == next_depth && layout != pars[p + 1].layout()))
292 pit = outerHook(pit, pars);
293 if (pit != par_type(pars.size()))
294 par_depth = pars[pit].getDepth();
296 return END_LABEL_NO_LABEL;
300 LyXFont const outerFont(par_type par_offset, ParagraphList const & pars)
302 Paragraph::depth_type par_depth = pars[par_offset].getDepth();
303 LyXFont tmpfont(LyXFont::ALL_INHERIT);
305 // Resolve against environment font information
306 while (par_offset != par_type(pars.size())
308 && !tmpfont.resolved()) {
309 par_offset = outerHook(par_offset, pars);
310 if (par_offset != par_type(pars.size())) {
311 tmpfont.realize(pars[par_offset].layout()->font);
312 par_depth = pars[par_offset].getDepth();
320 par_type outerPar(Buffer const & buf, InsetBase const * inset)
322 ParIterator pit = const_cast<Buffer &>(buf).par_iterator_begin();
323 ParIterator end = const_cast<Buffer &>(buf).par_iterator_end();
324 for ( ; pit != end; ++pit) {
326 // the second '=' below is intentional
327 for (int i = 0; (text = inset->getText(i)); ++i)
328 if (&text->paragraphs() == &pit.plist())
329 return pit.outerPar();
331 InsetList::const_iterator ii = pit->insetlist.begin();
332 InsetList::const_iterator iend = pit->insetlist.end();
333 for ( ; ii != iend; ++ii)
334 if (ii->inset == inset)
335 return pit.outerPar();
337 lyxerr << "outerPar: should not happen" << endl;
339 return buf.paragraphs().size(); // shut up compiler
343 /// return the range of pars [beg, end[ owning the range of y [ystart, yend]
344 void getParsInRange(ParagraphList & pars, int ystart, int yend,
345 par_type & beg, par_type & end)
347 BOOST_ASSERT(!pars.empty());
348 par_type const endpar = pars.size();
349 par_type const begpar = 0;
351 for (beg = endpar - 1; beg != begpar && pars[beg].y > ystart; --beg)
354 for (end = beg ; end != endpar && pars[end].y <= yend; ++end)