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"
25 static bool moveItem(Paragraph & fromPar, pos_type fromPos,
26 Paragraph & toPar, pos_type toPos, BufferParams const & params)
28 // Note: moveItem() does not honour change tracking!
29 // Therefore, it should only be used for breaking and merging paragraphs
31 Paragraph::value_type const tmpChar = fromPar.getChar(fromPos);
32 LyXFont const tmpFont = fromPar.getFontSettings(params, fromPos);
33 Change const tmpChange = fromPar.lookupChange(fromPos);
35 if (tmpChar == Paragraph::META_INSET) {
36 InsetBase * tmpInset = 0;
37 if (fromPar.getInset(fromPos)) {
38 // the inset is not in the paragraph any more
39 tmpInset = fromPar.insetlist.release(fromPos);
42 fromPar.eraseChar(fromPos, false);
44 if (!toPar.insetAllowed(tmpInset->lyxCode())) {
49 toPar.insertInset(toPos, tmpInset, tmpFont, tmpChange);
51 fromPar.eraseChar(fromPos, false);
52 toPar.insertChar(toPos, tmpChar, tmpFont, tmpChange);
59 void breakParagraph(BufferParams const & bparams,
60 ParagraphList & pars, pit_type par_offset, pos_type pos, int flag)
62 // create a new paragraph, and insert into the list
63 ParagraphList::iterator tmp =
64 pars.insert(boost::next(pars.begin(), par_offset + 1),
67 Paragraph & par = pars[par_offset];
69 // we will invalidate the row cache
72 // without doing that we get a crash when typing <Return> at the
74 tmp->layout(bparams.getLyXTextClass().defaultLayout());
75 // remember to set the inset_owner
76 tmp->setInsetOwner(par.inInset());
78 // this is an idea for a more userfriendly layout handling, I will
79 // see what the users say
81 // layout stays the same with latex-environments
83 tmp->layout(par.layout());
84 tmp->setLabelWidthString(par.params().labelWidthString());
85 tmp->params().depth(par.params().depth());
86 } else if (par.params().depth() > 0) {
87 Paragraph const & hook = pars[outerHook(par_offset, pars)];
88 tmp->layout(hook.layout());
89 // not sure the line below is useful
90 tmp->setLabelWidthString(par.params().labelWidthString());
91 tmp->params().depth(hook.params().depth());
94 bool const isempty = (par.allowEmpty() && par.empty());
96 if (!isempty && (par.size() > pos || par.empty() || flag == 2)) {
97 tmp->layout(par.layout());
98 tmp->params().align(par.params().align());
99 tmp->setLabelWidthString(par.params().labelWidthString());
101 tmp->params().depth(par.params().depth());
102 tmp->params().noindent(par.params().noindent());
104 // copy everything behind the break-position
105 // to the new paragraph
107 /* Note: if !keepempty, empty() == true, then we reach
108 * here with size() == 0. So pos_end becomes - 1. This
109 * doesn't cause problems because both loops below
110 * enforce pos <= pos_end and 0 <= pos
112 pos_type pos_end = par.size() - 1;
114 for (pos_type i = pos, j = 0; i <= pos_end; ++i) {
115 if (moveItem(par, pos, *tmp, j, bparams)) {
122 // Make sure that we keep the language when
123 // breaking paragraph.
125 LyXFont changed = tmp->getFirstFontSettings(bparams);
126 LyXFont old = par.getFontSettings(bparams, par.size());
127 changed.setLanguage(old.language());
128 tmp->setFont(0, changed);
135 par.params().clear();
136 par.layout(bparams.getLyXTextClass().defaultLayout());
139 // layout stays the same with latex-environments
141 par.layout(tmp->layout());
142 par.setLabelWidthString(tmp->params().labelWidthString());
143 par.params().depth(tmp->params().depth());
146 // subtle, but needed to get empty pars working right
147 if (bparams.trackChanges) {
148 // FIXME: Change tracking (MG)
150 // set 'par' text to INSERTED in CT mode; clear CT info otherwise
151 // else if (!tmp->size())
152 // set 'tmp' text to INSERTED in CT mode; clear CT info otherwise
157 void breakParagraphConservative(BufferParams const & bparams,
158 ParagraphList & pars, pit_type par_offset, pos_type pos)
160 // create a new paragraph
161 Paragraph & tmp = *pars.insert(boost::next(pars.begin(), par_offset + 1),
163 Paragraph & par = pars[par_offset];
165 tmp.makeSameLayout(par);
167 BOOST_ASSERT(pos <= par.size());
169 if (pos < par.size()) {
170 // move everything behind the break position to the new paragraph
171 pos_type pos_end = par.size() - 1;
173 for (pos_type i = pos, j = 0; i <= pos_end; ++i) {
174 if (moveItem(par, pos, tmp, j, bparams)) {
178 // Move over the end-of-par change information
179 tmp.setChange(tmp.size(), par.lookupChange(par.size()));
180 par.setChange(par.size(), Change(bparams.trackChanges ?
181 Change::INSERTED : Change::UNCHANGED));
186 void mergeParagraph(BufferParams const & bparams,
187 ParagraphList & pars, pit_type par_offset)
189 Paragraph & next = pars[par_offset + 1];
190 Paragraph & par = pars[par_offset];
192 pos_type pos_end = next.size() - 1;
193 pos_type pos_insert = par.size();
195 // The imaginary end-of-paragraph character (at par.size()) has to be
196 // marked as unmodified. Otherwise, its change is adopted by the first
197 // character of the next paragraph.
199 // FIXME: change tracking (MG)
200 par.setChange(par.size(), Change(Change::UNCHANGED));
202 Change change = next.lookupChange(next.size());
203 // ok, now copy the paragraph
204 for (pos_type i = 0, j = pos_insert; i <= pos_end; ++i) {
205 if (moveItem(next, 0, par, j, bparams)) {
209 // Move the change of the end-of-paragraph character
210 // FIXME: change tracking (MG)
211 par.setChange(par.size(), change);
213 pars.erase(boost::next(pars.begin(), par_offset + 1));
217 pit_type depthHook(pit_type pit, ParagraphList const & pars, depth_type depth)
219 pit_type newpit = pit;
224 while (newpit != 0 && pars[newpit].getDepth() > depth)
227 if (pars[newpit].getDepth() > depth)
234 pit_type outerHook(pit_type par_offset, ParagraphList const & pars)
236 Paragraph const & par = pars[par_offset];
238 if (par.getDepth() == 0)
240 return depthHook(par_offset, pars, depth_type(par.getDepth() - 1));
244 bool isFirstInSequence(pit_type par_offset, ParagraphList const & pars)
246 Paragraph const & par = pars[par_offset];
248 pit_type dhook_offset = depthHook(par_offset, pars, par.getDepth());
250 Paragraph const & dhook = pars[dhook_offset];
252 return dhook_offset == par_offset
253 || dhook.layout() != par.layout()
254 || dhook.getDepth() != par.getDepth();
258 int getEndLabel(pit_type p, ParagraphList const & pars)
261 depth_type par_depth = pars[p].getDepth();
262 while (pit != pit_type(pars.size())) {
263 LyXLayout_ptr const & layout = pars[pit].layout();
264 int const endlabeltype = layout->endlabeltype;
266 if (endlabeltype != END_LABEL_NO_LABEL) {
267 if (p + 1 == pit_type(pars.size()))
270 depth_type const next_depth =
271 pars[p + 1].getDepth();
272 if (par_depth > next_depth ||
273 (par_depth == next_depth && layout != pars[p + 1].layout()))
279 pit = outerHook(pit, pars);
280 if (pit != pit_type(pars.size()))
281 par_depth = pars[pit].getDepth();
283 return END_LABEL_NO_LABEL;
287 LyXFont const outerFont(pit_type par_offset, ParagraphList const & pars)
289 depth_type par_depth = pars[par_offset].getDepth();
290 LyXFont tmpfont(LyXFont::ALL_INHERIT);
292 // Resolve against environment font information
293 while (par_offset != pit_type(pars.size())
295 && !tmpfont.resolved()) {
296 par_offset = outerHook(par_offset, pars);
297 if (par_offset != pit_type(pars.size())) {
298 tmpfont.realize(pars[par_offset].layout()->font);
299 par_depth = pars[par_offset].getDepth();
307 /// return the number of InsetOptArg in a paragraph
308 int numberOfOptArgs(Paragraph const & par)
312 InsetList::const_iterator it = par.insetlist.begin();
313 InsetList::const_iterator end = par.insetlist.end();
314 for (; it != end ; ++it) {
315 if (it->inset->lyxCode() == InsetBase::OPTARG_CODE)