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"
15 #include "bufferparams.h"
17 #include "paragraph_pimpl.h"
27 static bool moveItem(Paragraph & fromPar, pos_type fromPos,
28 Paragraph & toPar, pos_type toPos, BufferParams const & params)
30 // Note: moveItem() does not honour change tracking!
31 // Therefore, it should only be used for breaking and merging paragraphs
33 Paragraph::value_type const tmpChar = fromPar.getChar(fromPos);
34 LyXFont const tmpFont = fromPar.getFontSettings(params, fromPos);
35 Change const tmpChange = fromPar.lookupChange(fromPos);
37 if (tmpChar == Paragraph::META_INSET) {
38 InsetBase * tmpInset = 0;
39 if (fromPar.getInset(fromPos)) {
40 // the inset is not in the paragraph any more
41 tmpInset = fromPar.insetlist.release(fromPos);
44 fromPar.eraseChar(fromPos, false);
46 if (!toPar.insetAllowed(tmpInset->lyxCode())) {
51 toPar.insertInset(toPos, tmpInset, tmpFont, tmpChange);
53 fromPar.eraseChar(fromPos, false);
54 toPar.insertChar(toPos, tmpChar, tmpFont, tmpChange);
61 void breakParagraph(BufferParams const & bparams,
62 ParagraphList & pars, pit_type par_offset, pos_type pos, int flag)
64 // create a new paragraph, and insert into the list
65 ParagraphList::iterator tmp =
66 pars.insert(boost::next(pars.begin(), par_offset + 1),
69 Paragraph & par = pars[par_offset];
71 // without doing that we get a crash when typing <Return> at the
73 tmp->layout(bparams.getLyXTextClass().defaultLayout());
74 // remember to set the inset_owner
75 tmp->setInsetOwner(par.inInset());
77 // layout stays the same with latex-environments
79 tmp->layout(par.layout());
80 tmp->setLabelWidthString(par.params().labelWidthString());
81 tmp->params().depth(par.params().depth());
82 } else if (par.params().depth() > 0) {
83 Paragraph const & hook = pars[outerHook(par_offset, pars)];
84 tmp->layout(hook.layout());
85 // not sure the line below is useful
86 tmp->setLabelWidthString(par.params().labelWidthString());
87 tmp->params().depth(hook.params().depth());
90 bool const isempty = (par.allowEmpty() && par.empty());
92 if (!isempty && (par.size() > pos || par.empty() || flag == 2)) {
93 tmp->layout(par.layout());
94 tmp->params().align(par.params().align());
95 tmp->setLabelWidthString(par.params().labelWidthString());
97 tmp->params().depth(par.params().depth());
98 tmp->params().noindent(par.params().noindent());
100 // move everything behind the break position
101 // to the new paragraph
103 /* Note: if !keepempty, empty() == true, then we reach
104 * here with size() == 0. So pos_end becomes - 1. This
105 * doesn't cause problems because both loops below
106 * enforce pos <= pos_end and 0 <= pos
108 pos_type pos_end = par.size() - 1;
110 for (pos_type i = pos, j = 0; i <= pos_end; ++i) {
111 if (moveItem(par, pos, *tmp, j, bparams)) {
117 // Move over the end-of-par change information
118 tmp->setChange(tmp->size(), par.lookupChange(par.size()));
119 par.setChange(par.size(), Change(bparams.trackChanges ?
120 Change::INSERTED : Change::UNCHANGED));
123 // Make sure that we keep the language when
124 // breaking paragraph.
126 LyXFont changed = tmp->getFirstFontSettings(bparams);
127 LyXFont old = par.getFontSettings(bparams, par.size());
128 changed.setLanguage(old.language());
129 tmp->setFont(0, changed);
136 par.params().clear();
137 par.layout(bparams.getLyXTextClass().defaultLayout());
140 // layout stays the same with latex-environments
142 par.layout(tmp->layout());
143 par.setLabelWidthString(tmp->params().labelWidthString());
144 par.params().depth(tmp->params().depth());
149 void breakParagraphConservative(BufferParams const & bparams,
150 ParagraphList & pars, pit_type par_offset, pos_type pos)
152 // create a new paragraph
153 Paragraph & tmp = *pars.insert(boost::next(pars.begin(), par_offset + 1),
155 Paragraph & par = pars[par_offset];
157 tmp.makeSameLayout(par);
159 BOOST_ASSERT(pos <= par.size());
161 if (pos < par.size()) {
162 // move everything behind the break position to the new paragraph
163 pos_type pos_end = par.size() - 1;
165 for (pos_type i = pos, j = 0; i <= pos_end; ++i) {
166 if (moveItem(par, pos, tmp, j, bparams)) {
170 // Move over the end-of-par change information
171 tmp.setChange(tmp.size(), par.lookupChange(par.size()));
172 par.setChange(par.size(), Change(bparams.trackChanges ?
173 Change::INSERTED : Change::UNCHANGED));
178 void mergeParagraph(BufferParams const & bparams,
179 ParagraphList & pars, pit_type par_offset)
181 Paragraph & next = pars[par_offset + 1];
182 Paragraph & par = pars[par_offset];
184 pos_type pos_end = next.size() - 1;
185 pos_type pos_insert = par.size();
187 // the imaginary end-of-paragraph character (at par.size()) has to be
188 // marked as unmodified. Otherwise, its change is adopted by the first
189 // character of the next paragraph.
190 if (par.lookupChange(par.size()).type != Change::UNCHANGED) {
191 lyxerr[Debug::CHANGES] <<
192 "merging par with inserted/deleted end-of-par character" << endl;
193 par.setChange(par.size(), Change(Change::UNCHANGED));
196 Change change = next.lookupChange(next.size());
198 // move the content of the second paragraph to the end of the first one
199 for (pos_type i = 0, j = pos_insert; i <= pos_end; ++i) {
200 if (moveItem(next, 0, par, j, bparams)) {
205 // move the change of the end-of-paragraph character
206 par.setChange(par.size(), change);
208 pars.erase(boost::next(pars.begin(), par_offset + 1));
212 pit_type depthHook(pit_type pit, ParagraphList const & pars, depth_type depth)
214 pit_type newpit = pit;
219 while (newpit != 0 && pars[newpit].getDepth() > depth)
222 if (pars[newpit].getDepth() > depth)
229 pit_type outerHook(pit_type par_offset, ParagraphList const & pars)
231 Paragraph const & par = pars[par_offset];
233 if (par.getDepth() == 0)
235 return depthHook(par_offset, pars, depth_type(par.getDepth() - 1));
239 bool isFirstInSequence(pit_type par_offset, ParagraphList const & pars)
241 Paragraph const & par = pars[par_offset];
243 pit_type dhook_offset = depthHook(par_offset, pars, par.getDepth());
245 if (dhook_offset == par_offset)
248 Paragraph const & dhook = pars[dhook_offset];
250 return dhook.layout() != par.layout()
251 || dhook.getDepth() != par.getDepth();
255 int getEndLabel(pit_type p, ParagraphList const & pars)
258 depth_type par_depth = pars[p].getDepth();
259 while (pit != pit_type(pars.size())) {
260 LyXLayout_ptr const & layout = pars[pit].layout();
261 int const endlabeltype = layout->endlabeltype;
263 if (endlabeltype != END_LABEL_NO_LABEL) {
264 if (p + 1 == pit_type(pars.size()))
267 depth_type const next_depth =
268 pars[p + 1].getDepth();
269 if (par_depth > next_depth ||
270 (par_depth == next_depth && layout != pars[p + 1].layout()))
276 pit = outerHook(pit, pars);
277 if (pit != pit_type(pars.size()))
278 par_depth = pars[pit].getDepth();
280 return END_LABEL_NO_LABEL;
284 LyXFont const outerFont(pit_type par_offset, ParagraphList const & pars)
286 depth_type par_depth = pars[par_offset].getDepth();
287 LyXFont tmpfont(LyXFont::ALL_INHERIT);
289 // Resolve against environment font information
290 while (par_offset != pit_type(pars.size())
292 && !tmpfont.resolved()) {
293 par_offset = outerHook(par_offset, pars);
294 if (par_offset != pit_type(pars.size())) {
295 tmpfont.realize(pars[par_offset].layout()->font);
296 par_depth = pars[par_offset].getDepth();
304 /// return the number of InsetOptArg in a paragraph
305 int numberOfOptArgs(Paragraph const & par)
309 InsetList::const_iterator it = par.insetlist.begin();
310 InsetList::const_iterator end = par.insetlist.end();
311 for (; it != end ; ++it) {
312 if (it->inset->lyxCode() == InsetBase::OPTARG_CODE)