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 Paragraph::value_type const tmpChar = fromPar.getChar(fromPos);
29 LyXFont const tmpFont = fromPar.getFontSettings(params, fromPos);
30 Change const tmpChange = fromPar.lookupChange(fromPos);
32 if (tmpChar == Paragraph::META_INSET) {
33 InsetBase * tmpInset = 0;
34 if (fromPar.getInset(fromPos)) {
35 // the inset is not in a paragraph anymore
36 tmpInset = fromPar.insetlist.release(fromPos);
37 fromPar.insetlist.erase(fromPos);
40 if (!toPar.insetAllowed(tmpInset->lyxCode())) {
45 toPar.insertInset(toPos, tmpInset, tmpFont, tmpChange);
47 toPar.insertChar(toPos, tmpChar, tmpFont, tmpChange);
53 void breakParagraph(BufferParams const & bparams,
54 ParagraphList & pars, pit_type par_offset, pos_type pos, int flag)
56 // create a new paragraph, and insert into the list
57 ParagraphList::iterator tmp =
58 pars.insert(boost::next(pars.begin(), par_offset + 1),
61 Paragraph & par = pars[par_offset];
63 // we will invalidate the row cache
66 // without doing that we get a crash when typing <Return> at the
68 tmp->layout(bparams.getLyXTextClass().defaultLayout());
69 // remember to set the inset_owner
70 tmp->setInsetOwner(par.inInset());
72 // this is an idea for a more userfriendly layout handling, I will
73 // see what the users say
75 // layout stays the same with latex-environments
77 tmp->layout(par.layout());
78 tmp->setLabelWidthString(par.params().labelWidthString());
79 tmp->params().depth(par.params().depth());
80 } else if (par.params().depth() > 0) {
81 Paragraph const & hook = pars[outerHook(par_offset, pars)];
82 tmp->layout(hook.layout());
83 // not sure the line below is useful
84 tmp->setLabelWidthString(par.params().labelWidthString());
85 tmp->params().depth(hook.params().depth());
88 bool const isempty = (par.allowEmpty() && par.empty());
90 if (!isempty && (par.size() > pos || par.empty() || flag == 2)) {
91 tmp->layout(par.layout());
92 tmp->params().align(par.params().align());
93 tmp->setLabelWidthString(par.params().labelWidthString());
95 tmp->params().depth(par.params().depth());
96 tmp->params().noindent(par.params().noindent());
98 // copy everything behind the break-position
99 // to the new paragraph
101 /* Note: if !keepempty, empty() == true, then we reach
102 * here with size() == 0. So pos_end becomes - 1. This
103 * doesn't cause problems because both loops below
104 * enforce pos <= pos_end and 0 <= pos
106 pos_type pos_end = par.size() - 1;
108 for (pos_type i = pos, j = pos; i <= pos_end; ++i) {
109 if (moveItem(par, i, *tmp, j - pos, bparams)) {
114 for (pos_type i = pos_end; i >= pos; --i)
115 // FIXME: change tracking (MG)
116 par.eraseChar(i, false); // erase without change tracking
120 // Make sure that we keep the language when
121 // breaking paragrpah.
123 LyXFont changed = tmp->getFirstFontSettings(bparams);
124 LyXFont old = par.getFontSettings(bparams, par.size());
125 changed.setLanguage(old.language());
126 tmp->setFont(0, changed);
133 par.params().clear();
134 par.layout(bparams.getLyXTextClass().defaultLayout());
137 // layout stays the same with latex-environments
139 par.layout(tmp->layout());
140 par.setLabelWidthString(tmp->params().labelWidthString());
141 par.params().depth(tmp->params().depth());
144 // subtle, but needed to get empty pars working right
145 if (bparams.trackChanges) {
146 // FIXME: Change tracking (MG)
148 // set 'par' text to INSERTED in CT mode; clear CT info otherwise
149 // else if (!tmp->size())
150 // set 'tmp' text to INSERTED in CT mode; clear CT info otherwise
155 void breakParagraphConservative(BufferParams const & bparams,
156 ParagraphList & pars, pit_type par_offset, pos_type pos)
158 // create a new paragraph
159 Paragraph & tmp = *pars.insert(boost::next(pars.begin(), par_offset + 1),
161 Paragraph & par = pars[par_offset];
163 tmp.makeSameLayout(par);
165 // When can pos > size()?
166 // I guess pos == size() is possible.
167 if (par.size() > pos) {
168 // copy everything behind the break-position to the new
170 pos_type pos_end = par.size() - 1;
172 for (pos_type i = pos, j = pos; i <= pos_end; ++i) {
173 if (moveItem(par, i, tmp, j - pos, bparams)) {
177 // Move over end-of-par change attr
178 // FIXME: change tracking (MG)
179 tmp.setChange(tmp.size(), Change(par.lookupChange(par.size()).type));
181 // If tracking changes, set all the text that is to be
182 // erased to Change::INSERTED.
183 for (pos_type k = pos_end; k >= pos; --k) {
184 if (bparams.trackChanges)
185 // FIXME: Change tracking (MG)
186 par.setChange(k, Change(Change::INSERTED));
187 // FIXME: change tracking (MG)
188 par.eraseChar(k, false);
194 void mergeParagraph(BufferParams const & bparams,
195 ParagraphList & pars, pit_type par_offset)
197 Paragraph & next = pars[par_offset + 1];
198 Paragraph & par = pars[par_offset];
200 pos_type pos_end = next.size() - 1;
201 pos_type pos_insert = par.size();
203 // The imaginary end-of-paragraph character (at par.size()) has to be
204 // marked as unmodified. Otherwise, its change is adopted by the first
205 // character of the next paragraph.
207 // FIXME: change tracking (MG)
208 par.setChange(par.size(), Change(Change::UNCHANGED));
210 Change change = next.lookupChange(next.size());
211 // ok, now copy the paragraph
212 for (pos_type i = 0, j = 0; i <= pos_end; ++i) {
213 if (moveItem(next, i, par, pos_insert + j, bparams)) {
217 // Move the change of the end-of-paragraph character
218 // FIXME: change tracking (MG)
219 par.setChange(par.size(), change);
221 pars.erase(boost::next(pars.begin(), par_offset + 1));
225 pit_type depthHook(pit_type pit, ParagraphList const & pars, depth_type depth)
227 pit_type newpit = pit;
232 while (newpit != 0 && pars[newpit].getDepth() > depth)
235 if (pars[newpit].getDepth() > depth)
242 pit_type outerHook(pit_type par_offset, ParagraphList const & pars)
244 Paragraph const & par = pars[par_offset];
246 if (par.getDepth() == 0)
248 return depthHook(par_offset, pars, depth_type(par.getDepth() - 1));
252 bool isFirstInSequence(pit_type par_offset, ParagraphList const & pars)
254 Paragraph const & par = pars[par_offset];
256 pit_type dhook_offset = depthHook(par_offset, pars, par.getDepth());
258 Paragraph const & dhook = pars[dhook_offset];
260 return dhook_offset == par_offset
261 || dhook.layout() != par.layout()
262 || dhook.getDepth() != par.getDepth();
266 int getEndLabel(pit_type p, ParagraphList const & pars)
269 depth_type par_depth = pars[p].getDepth();
270 while (pit != pit_type(pars.size())) {
271 LyXLayout_ptr const & layout = pars[pit].layout();
272 int const endlabeltype = layout->endlabeltype;
274 if (endlabeltype != END_LABEL_NO_LABEL) {
275 if (p + 1 == pit_type(pars.size()))
278 depth_type const next_depth =
279 pars[p + 1].getDepth();
280 if (par_depth > next_depth ||
281 (par_depth == next_depth && layout != pars[p + 1].layout()))
287 pit = outerHook(pit, pars);
288 if (pit != pit_type(pars.size()))
289 par_depth = pars[pit].getDepth();
291 return END_LABEL_NO_LABEL;
295 LyXFont const outerFont(pit_type par_offset, ParagraphList const & pars)
297 depth_type par_depth = pars[par_offset].getDepth();
298 LyXFont tmpfont(LyXFont::ALL_INHERIT);
300 // Resolve against environment font information
301 while (par_offset != pit_type(pars.size())
303 && !tmpfont.resolved()) {
304 par_offset = outerHook(par_offset, pars);
305 if (par_offset != pit_type(pars.size())) {
306 tmpfont.realize(pars[par_offset].layout()->font);
307 par_depth = pars[par_offset].getDepth();
315 /// return the number of InsetOptArg in a paragraph
316 int numberOfOptArgs(Paragraph const & par)
320 InsetList::const_iterator it = par.insetlist.begin();
321 InsetList::const_iterator end = par.insetlist.end();
322 for (; it != end ; ++it) {
323 if (it->inset->lyxCode() == InsetBase::OPTARG_CODE)