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::bformat;
42 using lyx::support::compare_ascii_no_case;
43 using lyx::support::compare_no_case;
44 using lyx::support::contains;
45 using lyx::support::split;
46 using lyx::support::subst;
52 using std::istringstream;
59 bool moveItem(Paragraph & from, Paragraph & to,
60 BufferParams const & params, pos_type i, pos_type j)
62 Paragraph::value_type const tmpchar = from.getChar(i);
63 LyXFont tmpfont = from.getFontSettings(params, i);
65 if (tmpchar == Paragraph::META_INSET) {
66 InsetBase * tmpinset = 0;
67 if (from.getInset(i)) {
68 // the inset is not in a paragraph anymore
69 tmpinset = from.insetlist.release(i);
70 from.insetlist.erase(i);
73 if (!to.insetAllowed(tmpinset->lyxCode())) {
78 to.insertInset(j, tmpinset, tmpfont);
80 if (!to.checkInsertChar(tmpfont))
82 to.insertChar(j, tmpchar, tmpfont);
90 void breakParagraph(BufferParams const & bparams,
91 ParagraphList & pars, par_type par_offset, pos_type pos, int flag)
93 // create a new paragraph, and insert into the list
94 ParagraphList::iterator tmp =
95 pars.insert(pars.begin() + par_offset + 1, Paragraph());
97 Paragraph & par = pars[par_offset];
99 // without doing that we get a crash when typing <Return> at the
100 // end of a paragraph
101 tmp->layout(bparams.getLyXTextClass().defaultLayout());
102 // remember to set the inset_owner
103 tmp->setInsetOwner(par.inInset());
105 if (bparams.tracking_changes)
108 // this is an idea for a more userfriendly layout handling, I will
109 // see what the users say
111 // layout stays the same with latex-environments
113 tmp->layout(par.layout());
114 tmp->setLabelWidthString(par.params().labelWidthString());
117 bool const isempty = (par.allowEmpty() && par.empty());
119 if (!isempty && (par.size() > pos || par.empty() || flag == 2)) {
120 tmp->layout(par.layout());
121 tmp->params().align(par.params().align());
122 tmp->setLabelWidthString(par.params().labelWidthString());
124 tmp->params().depth(par.params().depth());
125 tmp->params().noindent(par.params().noindent());
127 // copy everything behind the break-position
128 // to the new paragraph
130 /* Note: if !keepempty, empty() == true, then we reach
131 * here with size() == 0. So pos_end becomes - 1. This
132 * doesn't cause problems because both loops below
133 * enforce pos <= pos_end and 0 <= pos
135 pos_type pos_end = par.size() - 1;
137 for (pos_type i = pos, j = pos; i <= pos_end; ++i) {
138 Change::Type change = par.lookupChange(i);
139 if (moveItem(par, *tmp, bparams, i, j - pos)) {
140 tmp->setChange(j - pos, change);
145 for (pos_type i = pos_end; i >= pos; --i)
150 // Make sure that we keep the language when
151 // breaking paragrpah.
153 LyXFont changed = tmp->getFirstFontSettings();
154 LyXFont old = par.getFontSettings(bparams, par.size());
155 changed.setLanguage(old.language());
156 tmp->setFont(0, changed);
162 par.params().clear();
164 par.layout(bparams.getLyXTextClass().defaultLayout());
166 // layout stays the same with latex-environments
168 par.layout(tmp->layout());
169 par.setLabelWidthString(tmp->params().labelWidthString());
170 par.params().depth(tmp->params().depth());
173 // subtle, but needed to get empty pars working right
174 if (bparams.tracking_changes) {
177 } else if (!tmp->size()) {
184 void breakParagraphConservative(BufferParams const & bparams,
185 ParagraphList & pars, par_type par_offset, pos_type pos)
187 // create a new paragraph
188 Paragraph & tmp = *pars.insert(pars.begin() + par_offset + 1, Paragraph());
189 Paragraph & par = pars[par_offset];
191 tmp.makeSameLayout(par);
193 // When can pos > size()?
194 // I guess pos == size() is possible.
195 if (par.size() > pos) {
196 // copy everything behind the break-position to the new
198 pos_type pos_end = par.size() - 1;
200 for (pos_type i = pos, j = pos; i <= pos_end; ++i)
201 if (moveItem(par, tmp, bparams, i, j - pos))
204 for (pos_type k = pos_end; k >= pos; --k)
210 void mergeParagraph(BufferParams const & bparams,
211 ParagraphList & pars, par_type par_offset)
213 Paragraph & next = pars[par_offset + 1];
214 Paragraph & par = pars[par_offset];
216 pos_type pos_end = next.size() - 1;
217 pos_type pos_insert = par.size();
219 // ok, now copy the paragraph
220 for (pos_type i = 0, j = 0; i <= pos_end; ++i)
221 if (moveItem(next, par, bparams, i, pos_insert + j))
224 pars.erase(pars.begin() + par_offset + 1);
228 par_type depthHook(par_type pit,
229 ParagraphList const & pars, Paragraph::depth_type depth)
231 par_type newpit = pit;
236 while (newpit != 0 && pars[newpit].getDepth() > depth)
239 if (pars[newpit].getDepth() > depth)
246 par_type outerHook(par_type par_offset, ParagraphList const & pars)
248 Paragraph const & par = pars[par_offset];
250 if (par.getDepth() == 0)
252 return depthHook(par_offset, pars, Paragraph::depth_type(par.getDepth() - 1));
256 bool isFirstInSequence(par_type par_offset, ParagraphList const & pars)
258 Paragraph const & par = pars[par_offset];
260 par_type dhook_offset = depthHook(par_offset, pars, par.getDepth());
262 Paragraph const & dhook = pars[dhook_offset];
264 return dhook_offset == par_offset
265 || dhook.layout() != par.layout()
266 || dhook.getDepth() != par.getDepth();
270 int getEndLabel(par_type p, ParagraphList const & pars)
273 Paragraph::depth_type par_depth = pars[p].getDepth();
274 while (pit != par_type(pars.size())) {
275 LyXLayout_ptr const & layout = pars[pit].layout();
276 int const endlabeltype = layout->endlabeltype;
278 if (endlabeltype != END_LABEL_NO_LABEL) {
279 if (p + 1 == par_type(pars.size()))
282 Paragraph::depth_type const next_depth =
283 pars[p + 1].getDepth();
284 if (par_depth > next_depth ||
285 (par_depth == next_depth && layout != pars[p + 1].layout()))
291 pit = outerHook(pit, pars);
292 if (pit != par_type(pars.size()))
293 par_depth = pars[pit].getDepth();
295 return END_LABEL_NO_LABEL;
299 LyXFont const outerFont(par_type par_offset, ParagraphList const & pars)
301 Paragraph::depth_type par_depth = pars[par_offset].getDepth();
302 LyXFont tmpfont(LyXFont::ALL_INHERIT);
304 // Resolve against environment font information
305 while (par_offset != par_type(pars.size())
307 && !tmpfont.resolved()) {
308 par_offset = outerHook(par_offset, pars);
309 if (par_offset != par_type(pars.size())) {
310 tmpfont.realize(pars[par_offset].layout()->font);
311 par_depth = pars[par_offset].getDepth();
319 par_type outerPar(Buffer const & buf, InsetBase const * inset)
321 ParIterator pit = const_cast<Buffer &>(buf).par_iterator_begin();
322 ParIterator end = const_cast<Buffer &>(buf).par_iterator_end();
323 for ( ; pit != end; ++pit) {
325 // the second '=' below is intentional
326 for (int i = 0; (text = inset->getText(i)); ++i)
327 if (&text->paragraphs() == &pit.plist())
328 return pit.outerPar();
330 InsetList::const_iterator ii = pit->insetlist.begin();
331 InsetList::const_iterator iend = pit->insetlist.end();
332 for ( ; ii != iend; ++ii)
333 if (ii->inset == inset)
334 return pit.outerPar();
336 lyxerr << "outerPar: should not happen" << endl;
338 return buf.paragraphs().size(); // shut up compiler
342 /// return the range of pars [beg, end[ owning the range of y [ystart, yend]
343 void getParsInRange(ParagraphList & pars, int ystart, int yend,
344 par_type & beg, par_type & end)
346 BOOST_ASSERT(!pars.empty());
347 par_type const endpar = pars.size();
348 par_type const begpar = 0;
350 for (beg = endpar - 1; beg != begpar && pars[beg].y > ystart; --beg)
353 for (end = beg ; end != endpar && pars[end].y <= yend; ++end)