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)
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, pit_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 // we will invalidate the row cache
103 // without doing that we get a crash when typing <Return> at the
104 // end of a paragraph
105 tmp->layout(bparams.getLyXTextClass().defaultLayout());
106 // remember to set the inset_owner
107 tmp->setInsetOwner(par.inInset());
109 if (bparams.tracking_changes)
112 // this is an idea for a more userfriendly layout handling, I will
113 // see what the users say
115 // layout stays the same with latex-environments
117 tmp->layout(par.layout());
118 tmp->setLabelWidthString(par.params().labelWidthString());
121 bool const isempty = (par.allowEmpty() && par.empty());
123 if (!isempty && (par.size() > pos || par.empty() || flag == 2)) {
124 tmp->layout(par.layout());
125 tmp->params().align(par.params().align());
126 tmp->setLabelWidthString(par.params().labelWidthString());
128 tmp->params().depth(par.params().depth());
129 tmp->params().noindent(par.params().noindent());
131 // copy everything behind the break-position
132 // to the new paragraph
134 /* Note: if !keepempty, empty() == true, then we reach
135 * here with size() == 0. So pos_end becomes - 1. This
136 * doesn't cause problems because both loops below
137 * enforce pos <= pos_end and 0 <= pos
139 pos_type pos_end = par.size() - 1;
141 for (pos_type i = pos, j = pos; i <= pos_end; ++i) {
142 Change::Type change = par.lookupChange(i);
143 if (moveItem(par, *tmp, bparams, i, j - pos)) {
144 tmp->setChange(j - pos, change);
149 for (pos_type i = pos_end; i >= pos; --i)
154 // Make sure that we keep the language when
155 // breaking paragrpah.
157 LyXFont changed = tmp->getFirstFontSettings();
158 LyXFont old = par.getFontSettings(bparams, par.size());
159 changed.setLanguage(old.language());
160 tmp->setFont(0, changed);
166 par.params().clear();
168 par.layout(bparams.getLyXTextClass().defaultLayout());
170 // layout stays the same with latex-environments
172 par.layout(tmp->layout());
173 par.setLabelWidthString(tmp->params().labelWidthString());
174 par.params().depth(tmp->params().depth());
177 // subtle, but needed to get empty pars working right
178 if (bparams.tracking_changes) {
181 } else if (!tmp->size()) {
188 void breakParagraphConservative(BufferParams const & bparams,
189 ParagraphList & pars, pit_type par_offset, pos_type pos)
191 // create a new paragraph
192 Paragraph & tmp = *pars.insert(pars.begin() + par_offset + 1, Paragraph());
193 Paragraph & par = pars[par_offset];
195 tmp.makeSameLayout(par);
197 // When can pos > size()?
198 // I guess pos == size() is possible.
199 if (par.size() > pos) {
200 // copy everything behind the break-position to the new
202 pos_type pos_end = par.size() - 1;
204 for (pos_type i = pos, j = pos; i <= pos_end; ++i)
205 if (moveItem(par, tmp, bparams, i, j - pos))
208 for (pos_type k = pos_end; k >= pos; --k)
214 void mergeParagraph(BufferParams const & bparams,
215 ParagraphList & pars, pit_type par_offset)
217 Paragraph & next = pars[par_offset + 1];
218 Paragraph & par = pars[par_offset];
220 pos_type pos_end = next.size() - 1;
221 pos_type pos_insert = par.size();
223 // ok, now copy the paragraph
224 for (pos_type i = 0, j = 0; i <= pos_end; ++i)
225 if (moveItem(next, par, bparams, i, pos_insert + j))
228 pars.erase(pars.begin() + par_offset + 1);
232 pit_type depthHook(pit_type pit,
233 ParagraphList const & pars, Paragraph::depth_type depth)
235 pit_type newpit = pit;
240 while (newpit != 0 && pars[newpit].getDepth() > depth)
243 if (pars[newpit].getDepth() > depth)
250 pit_type outerHook(pit_type par_offset, ParagraphList const & pars)
252 Paragraph const & par = pars[par_offset];
254 if (par.getDepth() == 0)
256 return depthHook(par_offset, pars, Paragraph::depth_type(par.getDepth() - 1));
260 bool isFirstInSequence(pit_type par_offset, ParagraphList const & pars)
262 Paragraph const & par = pars[par_offset];
264 pit_type dhook_offset = depthHook(par_offset, pars, par.getDepth());
266 Paragraph const & dhook = pars[dhook_offset];
268 return dhook_offset == par_offset
269 || dhook.layout() != par.layout()
270 || dhook.getDepth() != par.getDepth();
274 int getEndLabel(pit_type p, ParagraphList const & pars)
277 Paragraph::depth_type par_depth = pars[p].getDepth();
278 while (pit != pit_type(pars.size())) {
279 LyXLayout_ptr const & layout = pars[pit].layout();
280 int const endlabeltype = layout->endlabeltype;
282 if (endlabeltype != END_LABEL_NO_LABEL) {
283 if (p + 1 == pit_type(pars.size()))
286 Paragraph::depth_type const next_depth =
287 pars[p + 1].getDepth();
288 if (par_depth > next_depth ||
289 (par_depth == next_depth && layout != pars[p + 1].layout()))
295 pit = outerHook(pit, pars);
296 if (pit != pit_type(pars.size()))
297 par_depth = pars[pit].getDepth();
299 return END_LABEL_NO_LABEL;
303 LyXFont const outerFont(pit_type par_offset, ParagraphList const & pars)
305 Paragraph::depth_type par_depth = pars[par_offset].getDepth();
306 LyXFont tmpfont(LyXFont::ALL_INHERIT);
308 // Resolve against environment font information
309 while (par_offset != pit_type(pars.size())
311 && !tmpfont.resolved()) {
312 par_offset = outerHook(par_offset, pars);
313 if (par_offset != pit_type(pars.size())) {
314 tmpfont.realize(pars[par_offset].layout()->font);
315 par_depth = pars[par_offset].getDepth();
323 /// return the number of InsetOptArg in a paragraph
324 int numberOfOptArgs(Paragraph const & par)
328 InsetList::const_iterator it = par.insetlist.begin();
329 InsetList::const_iterator end = par.insetlist.end();
330 for (; it != end ; ++it) {
331 if (it->inset->lyxCode() == InsetBase::OPTARG_CODE)