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 if (!to.checkInsertChar(tmpfont))
88 to.insertChar(j, tmpchar, tmpfont, change);
96 void breakParagraph(BufferParams const & bparams,
97 ParagraphList & pars, pit_type par_offset, pos_type pos, int flag)
99 // create a new paragraph, and insert into the list
100 ParagraphList::iterator tmp =
101 pars.insert(pars.begin() + par_offset + 1, Paragraph());
103 Paragraph & par = pars[par_offset];
105 // we will invalidate the row cache
108 // without doing that we get a crash when typing <Return> at the
109 // end of a paragraph
110 tmp->layout(bparams.getLyXTextClass().defaultLayout());
111 // remember to set the inset_owner
112 tmp->setInsetOwner(par.inInset());
114 if (bparams.tracking_changes)
117 // this is an idea for a more userfriendly layout handling, I will
118 // see what the users say
120 // layout stays the same with latex-environments
122 tmp->layout(par.layout());
123 tmp->setLabelWidthString(par.params().labelWidthString());
126 bool const isempty = (par.allowEmpty() && par.empty());
128 if (!isempty && (par.size() > pos || par.empty() || flag == 2)) {
129 tmp->layout(par.layout());
130 tmp->params().align(par.params().align());
131 tmp->setLabelWidthString(par.params().labelWidthString());
133 tmp->params().depth(par.params().depth());
134 tmp->params().noindent(par.params().noindent());
136 // copy everything behind the break-position
137 // to the new paragraph
139 /* Note: if !keepempty, empty() == true, then we reach
140 * here with size() == 0. So pos_end becomes - 1. This
141 * doesn't cause problems because both loops below
142 * enforce pos <= pos_end and 0 <= pos
144 pos_type pos_end = par.size() - 1;
146 for (pos_type i = pos, j = pos; i <= pos_end; ++i) {
147 Change::Type change = par.lookupChange(i);
148 if (moveItem(par, *tmp, bparams, i, j - pos)) {
149 tmp->setChange(j - pos, change);
154 for (pos_type i = pos_end; i >= pos; --i)
159 // Make sure that we keep the language when
160 // breaking paragrpah.
162 LyXFont changed = tmp->getFirstFontSettings();
163 LyXFont old = par.getFontSettings(bparams, par.size());
164 changed.setLanguage(old.language());
165 tmp->setFont(0, changed);
172 par.params().clear();
173 par.layout(bparams.getLyXTextClass().defaultLayout());
176 // layout stays the same with latex-environments
178 par.layout(tmp->layout());
179 par.setLabelWidthString(tmp->params().labelWidthString());
180 par.params().depth(tmp->params().depth());
183 // subtle, but needed to get empty pars working right
184 if (bparams.tracking_changes) {
187 } else if (!tmp->size()) {
194 void breakParagraphConservative(BufferParams const & bparams,
195 ParagraphList & pars, pit_type par_offset, pos_type pos)
197 // create a new paragraph
198 Paragraph & tmp = *pars.insert(pars.begin() + par_offset + 1, Paragraph());
199 Paragraph & par = pars[par_offset];
201 if (bparams.tracking_changes)
204 tmp.makeSameLayout(par);
206 // When can pos > size()?
207 // I guess pos == size() is possible.
208 if (par.size() > pos) {
209 // copy everything behind the break-position to the new
211 pos_type pos_end = par.size() - 1;
213 for (pos_type i = pos, j = pos; i <= pos_end; ++i) {
214 Change::Type change = par.lookupChange(i);
215 if (moveItem(par, tmp, bparams, i, j - pos, change))
218 // If tracking changes, set all the text that is to be
219 // erased to Type::INSERTED.
220 for (pos_type k = pos_end; k >= pos; --k) {
221 if (bparams.tracking_changes)
222 par.setChange(k, Change::INSERTED);
229 void mergeParagraph(BufferParams const & bparams,
230 ParagraphList & pars, pit_type par_offset)
232 Paragraph & next = pars[par_offset + 1];
233 Paragraph & par = pars[par_offset];
235 pos_type pos_end = next.size() - 1;
236 pos_type pos_insert = par.size();
238 // ok, now copy the paragraph
239 for (pos_type i = 0, j = 0; i <= pos_end; ++i) {
240 Change::Type change = next.lookupChange(i);
241 if (moveItem(next, par, bparams, i, pos_insert + j, change))
245 pars.erase(pars.begin() + par_offset + 1);
249 pit_type depthHook(pit_type pit,
250 ParagraphList const & pars, Paragraph::depth_type depth)
252 pit_type newpit = pit;
257 while (newpit != 0 && pars[newpit].getDepth() > depth)
260 if (pars[newpit].getDepth() > depth)
267 pit_type outerHook(pit_type par_offset, ParagraphList const & pars)
269 Paragraph const & par = pars[par_offset];
271 if (par.getDepth() == 0)
273 return depthHook(par_offset, pars, Paragraph::depth_type(par.getDepth() - 1));
277 bool isFirstInSequence(pit_type par_offset, ParagraphList const & pars)
279 Paragraph const & par = pars[par_offset];
281 pit_type dhook_offset = depthHook(par_offset, pars, par.getDepth());
283 Paragraph const & dhook = pars[dhook_offset];
285 return dhook_offset == par_offset
286 || dhook.layout() != par.layout()
287 || dhook.getDepth() != par.getDepth();
291 int getEndLabel(pit_type p, ParagraphList const & pars)
294 Paragraph::depth_type par_depth = pars[p].getDepth();
295 while (pit != pit_type(pars.size())) {
296 LyXLayout_ptr const & layout = pars[pit].layout();
297 int const endlabeltype = layout->endlabeltype;
299 if (endlabeltype != END_LABEL_NO_LABEL) {
300 if (p + 1 == pit_type(pars.size()))
303 Paragraph::depth_type const next_depth =
304 pars[p + 1].getDepth();
305 if (par_depth > next_depth ||
306 (par_depth == next_depth && layout != pars[p + 1].layout()))
312 pit = outerHook(pit, pars);
313 if (pit != pit_type(pars.size()))
314 par_depth = pars[pit].getDepth();
316 return END_LABEL_NO_LABEL;
320 LyXFont const outerFont(pit_type par_offset, ParagraphList const & pars)
322 Paragraph::depth_type par_depth = pars[par_offset].getDepth();
323 LyXFont tmpfont(LyXFont::ALL_INHERIT);
325 // Resolve against environment font information
326 while (par_offset != pit_type(pars.size())
328 && !tmpfont.resolved()) {
329 par_offset = outerHook(par_offset, pars);
330 if (par_offset != pit_type(pars.size())) {
331 tmpfont.realize(pars[par_offset].layout()->font);
332 par_depth = pars[par_offset].getDepth();
340 /// return the number of InsetOptArg in a paragraph
341 int numberOfOptArgs(Paragraph const & par)
345 InsetList::const_iterator it = par.insetlist.begin();
346 InsetList::const_iterator end = par.insetlist.end();
347 for (; it != end ; ++it) {
348 if (it->inset->lyxCode() == InsetBase::OPTARG_CODE)