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 to.insertChar(j, tmpchar, tmpfont, change);
94 void breakParagraph(BufferParams const & bparams,
95 ParagraphList & pars, pit_type par_offset, pos_type pos, int flag)
97 // create a new paragraph, and insert into the list
98 ParagraphList::iterator tmp =
99 pars.insert(pars.begin() + par_offset + 1, Paragraph());
101 Paragraph & par = pars[par_offset];
103 // we will invalidate the row cache
106 // without doing that we get a crash when typing <Return> at the
107 // end of a paragraph
108 tmp->layout(bparams.getLyXTextClass().defaultLayout());
109 // remember to set the inset_owner
110 tmp->setInsetOwner(par.inInset());
112 if (bparams.tracking_changes)
115 // this is an idea for a more userfriendly layout handling, I will
116 // see what the users say
118 // layout stays the same with latex-environments
120 tmp->layout(par.layout());
121 tmp->setLabelWidthString(par.params().labelWidthString());
124 bool const isempty = (par.allowEmpty() && par.empty());
126 if (!isempty && (par.size() > pos || par.empty() || flag == 2)) {
127 tmp->layout(par.layout());
128 tmp->params().align(par.params().align());
129 tmp->setLabelWidthString(par.params().labelWidthString());
131 tmp->params().depth(par.params().depth());
132 tmp->params().noindent(par.params().noindent());
134 // copy everything behind the break-position
135 // to the new paragraph
137 /* Note: if !keepempty, empty() == true, then we reach
138 * here with size() == 0. So pos_end becomes - 1. This
139 * doesn't cause problems because both loops below
140 * enforce pos <= pos_end and 0 <= pos
142 pos_type pos_end = par.size() - 1;
144 for (pos_type i = pos, j = pos; i <= pos_end; ++i) {
145 Change::Type change = par.lookupChange(i);
146 if (moveItem(par, *tmp, bparams, i, j - pos)) {
147 tmp->setChange(j - pos, change);
152 for (pos_type i = pos_end; i >= pos; --i)
157 // Make sure that we keep the language when
158 // breaking paragrpah.
160 LyXFont changed = tmp->getFirstFontSettings();
161 LyXFont old = par.getFontSettings(bparams, par.size());
162 changed.setLanguage(old.language());
163 tmp->setFont(0, changed);
170 par.params().clear();
171 par.layout(bparams.getLyXTextClass().defaultLayout());
174 // layout stays the same with latex-environments
176 par.layout(tmp->layout());
177 par.setLabelWidthString(tmp->params().labelWidthString());
178 par.params().depth(tmp->params().depth());
181 // subtle, but needed to get empty pars working right
182 if (bparams.tracking_changes) {
185 } else if (!tmp->size()) {
192 void breakParagraphConservative(BufferParams const & bparams,
193 ParagraphList & pars, pit_type par_offset, pos_type pos)
195 // create a new paragraph
196 Paragraph & tmp = *pars.insert(pars.begin() + par_offset + 1, Paragraph());
197 Paragraph & par = pars[par_offset];
199 if (bparams.tracking_changes)
202 tmp.makeSameLayout(par);
204 // When can pos > size()?
205 // I guess pos == size() is possible.
206 if (par.size() > pos) {
207 // copy everything behind the break-position to the new
209 pos_type pos_end = par.size() - 1;
211 for (pos_type i = pos, j = pos; i <= pos_end; ++i) {
212 Change::Type change = par.lookupChange(i);
213 if (moveItem(par, tmp, bparams, i, j - pos, change))
216 // If tracking changes, set all the text that is to be
217 // erased to Type::INSERTED.
218 for (pos_type k = pos_end; k >= pos; --k) {
219 if (bparams.tracking_changes)
220 par.setChange(k, Change::INSERTED);
227 void mergeParagraph(BufferParams const & bparams,
228 ParagraphList & pars, pit_type par_offset)
230 Paragraph & next = pars[par_offset + 1];
231 Paragraph & par = pars[par_offset];
233 pos_type pos_end = next.size() - 1;
234 pos_type pos_insert = par.size();
236 // ok, now copy the paragraph
237 for (pos_type i = 0, j = 0; i <= pos_end; ++i) {
238 Change::Type change = next.lookupChange(i);
239 if (moveItem(next, par, bparams, i, pos_insert + j, change))
243 pars.erase(pars.begin() + par_offset + 1);
247 pit_type depthHook(pit_type pit,
248 ParagraphList const & pars, Paragraph::depth_type depth)
250 pit_type newpit = pit;
255 while (newpit != 0 && pars[newpit].getDepth() > depth)
258 if (pars[newpit].getDepth() > depth)
265 pit_type outerHook(pit_type par_offset, ParagraphList const & pars)
267 Paragraph const & par = pars[par_offset];
269 if (par.getDepth() == 0)
271 return depthHook(par_offset, pars, Paragraph::depth_type(par.getDepth() - 1));
275 bool isFirstInSequence(pit_type par_offset, ParagraphList const & pars)
277 Paragraph const & par = pars[par_offset];
279 pit_type dhook_offset = depthHook(par_offset, pars, par.getDepth());
281 Paragraph const & dhook = pars[dhook_offset];
283 return dhook_offset == par_offset
284 || dhook.layout() != par.layout()
285 || dhook.getDepth() != par.getDepth();
289 int getEndLabel(pit_type p, ParagraphList const & pars)
292 Paragraph::depth_type par_depth = pars[p].getDepth();
293 while (pit != pit_type(pars.size())) {
294 LyXLayout_ptr const & layout = pars[pit].layout();
295 int const endlabeltype = layout->endlabeltype;
297 if (endlabeltype != END_LABEL_NO_LABEL) {
298 if (p + 1 == pit_type(pars.size()))
301 Paragraph::depth_type const next_depth =
302 pars[p + 1].getDepth();
303 if (par_depth > next_depth ||
304 (par_depth == next_depth && layout != pars[p + 1].layout()))
310 pit = outerHook(pit, pars);
311 if (pit != pit_type(pars.size()))
312 par_depth = pars[pit].getDepth();
314 return END_LABEL_NO_LABEL;
318 LyXFont const outerFont(pit_type par_offset, ParagraphList const & pars)
320 Paragraph::depth_type par_depth = pars[par_offset].getDepth();
321 LyXFont tmpfont(LyXFont::ALL_INHERIT);
323 // Resolve against environment font information
324 while (par_offset != pit_type(pars.size())
326 && !tmpfont.resolved()) {
327 par_offset = outerHook(par_offset, pars);
328 if (par_offset != pit_type(pars.size())) {
329 tmpfont.realize(pars[par_offset].layout()->font);
330 par_depth = pars[par_offset].getDepth();
338 /// return the number of InsetOptArg in a paragraph
339 int numberOfOptArgs(Paragraph const & par)
343 InsetList::const_iterator it = par.insetlist.begin();
344 InsetList::const_iterator end = par.insetlist.end();
345 for (; it != end ; ++it) {
346 if (it->inset->lyxCode() == InsetBase::OPTARG_CODE)