]> git.lyx.org Git - features.git/blob - src/paragraph_funcs.cpp
moveItem(): simplify a bit more and clarify comment.
[features.git] / src / paragraph_funcs.cpp
1 /**
2  * \file paragraph_funcs.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Lars Gullik Bjønnes
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "paragraph_funcs.h"
14
15 #include "BufferParams.h"
16 #include "Changes.h"
17 #include "InsetList.h"
18 #include "Layout.h"
19 #include "Paragraph.h"
20 #include "ParagraphParameters.h"
21 #include "Text.h"
22 #include "TextClass.h"
23
24 #include "support/debug.h"
25
26 #include "support/lassert.h"
27 #include <boost/next_prior.hpp>
28
29 using namespace std;
30
31 namespace lyx {
32
33 static bool moveItem(Paragraph & fromPar, pos_type fromPos,
34         Paragraph & toPar, pos_type toPos, BufferParams const & params)
35 {
36         // Note: moveItem() does not honour change tracking!
37         // Therefore, it should only be used for breaking and merging paragraphs
38
39         Font const tmpFont = fromPar.getFontSettings(params, fromPos);
40         Change const & tmpChange = fromPar.lookupChange(fromPos);
41
42         if (Inset * tmpInset = fromPar.getInset(fromPos)) {
43                 fromPar.releaseInset(fromPos);
44                 // The inset is not in fromPar any more.
45                 return toPar.insertInset(toPos, tmpInset, tmpFont, tmpChange);
46         }
47
48         char_type const tmpChar = fromPar.getChar(fromPos);
49         fromPar.eraseChar(fromPos, false);
50         toPar.insertChar(toPos, tmpChar, tmpFont, tmpChange);
51         return true;
52 }
53
54
55 void breakParagraph(BufferParams const & bparams,
56                     ParagraphList & pars, pit_type par_offset, pos_type pos, 
57                     bool keep_layout)
58 {
59         // create a new paragraph, and insert into the list
60         ParagraphList::iterator tmp =
61                 pars.insert(boost::next(pars.begin(), par_offset + 1),
62                             Paragraph());
63
64         Paragraph & par = pars[par_offset];
65
66         // remember to set the inset_owner
67         tmp->setInsetOwner(&par.inInset());
68         // without doing that we get a crash when typing <Return> at the
69         // end of a paragraph
70         tmp->setPlainOrDefaultLayout(bparams.documentClass());
71
72         // layout stays the same with latex-environments
73         if (keep_layout) {
74                 tmp->setLayout(par.layout());
75                 tmp->setLabelWidthString(par.params().labelWidthString());
76                 tmp->params().depth(par.params().depth());
77         } else if (par.params().depth() > 0) {
78                 Paragraph const & hook = pars[outerHook(par_offset, pars)];
79                 tmp->setLayout(hook.layout());
80                 // not sure the line below is useful
81                 tmp->setLabelWidthString(par.params().labelWidthString());
82                 tmp->params().depth(hook.params().depth());
83         }
84
85         bool const isempty = (par.allowEmpty() && par.empty());
86
87         if (!isempty && (par.size() > pos || par.empty())) {
88                 tmp->setLayout(par.layout());
89                 tmp->params().align(par.params().align());
90                 tmp->setLabelWidthString(par.params().labelWidthString());
91
92                 tmp->params().depth(par.params().depth());
93                 tmp->params().noindent(par.params().noindent());
94
95                 // move everything behind the break position
96                 // to the new paragraph
97
98                 /* Note: if !keepempty, empty() == true, then we reach
99                  * here with size() == 0. So pos_end becomes - 1. This
100                  * doesn't cause problems because both loops below
101                  * enforce pos <= pos_end and 0 <= pos
102                  */
103                 pos_type pos_end = par.size() - 1;
104
105                 for (pos_type i = pos, j = 0; i <= pos_end; ++i) {
106                         if (moveItem(par, pos, *tmp, j, bparams)) {
107                                 ++j;
108                         }
109                 }
110         }
111
112         // Move over the end-of-par change information
113         tmp->setChange(tmp->size(), par.lookupChange(par.size()));
114         par.setChange(par.size(), Change(bparams.trackChanges ?
115                                            Change::INSERTED : Change::UNCHANGED));
116
117         if (pos) {
118                 // Make sure that we keep the language when
119                 // breaking paragraph.
120                 if (tmp->empty()) {
121                         Font changed = tmp->getFirstFontSettings(bparams);
122                         Font old = par.getFontSettings(bparams, par.size());
123                         changed.setLanguage(old.language());
124                         tmp->setFont(0, changed);
125                 }
126
127                 return;
128         }
129
130         if (!isempty) {
131                 bool const soa = par.params().startOfAppendix();
132                 par.params().clear();
133                 // do not lose start of appendix marker (bug 4212)
134                 par.params().startOfAppendix(soa);
135                 par.setPlainOrDefaultLayout(bparams.documentClass());
136         }
137
138         // layout stays the same with latex-environments
139         if (keep_layout) {
140                 par.setLayout(tmp->layout());
141                 par.setLabelWidthString(tmp->params().labelWidthString());
142                 par.params().depth(tmp->params().depth());
143         }
144 }
145
146
147 void breakParagraphConservative(BufferParams const & bparams,
148         ParagraphList & pars, pit_type par_offset, pos_type pos)
149 {
150         // create a new paragraph
151         Paragraph & tmp = *pars.insert(boost::next(pars.begin(), par_offset + 1),
152                                        Paragraph());
153         Paragraph & par = pars[par_offset];
154
155         tmp.setInsetOwner(&par.inInset());
156         tmp.makeSameLayout(par);
157
158         LASSERT(pos <= par.size(), /**/);
159
160         if (pos < par.size()) {
161                 // move everything behind the break position to the new paragraph
162                 pos_type pos_end = par.size() - 1;
163
164                 for (pos_type i = pos, j = 0; i <= pos_end; ++i) {
165                         if (moveItem(par, pos, tmp, j, bparams)) {
166                                 ++j;
167                         }
168                 }
169                 // Move over the end-of-par change information
170                 tmp.setChange(tmp.size(), par.lookupChange(par.size()));
171                 par.setChange(par.size(), Change(bparams.trackChanges ?
172                                            Change::INSERTED : Change::UNCHANGED));
173         }
174 }
175
176
177 void mergeParagraph(BufferParams const & bparams,
178         ParagraphList & pars, pit_type par_offset)
179 {
180         Paragraph & next = pars[par_offset + 1];
181         Paragraph & par = pars[par_offset];
182
183         pos_type pos_end = next.size() - 1;
184         pos_type pos_insert = par.size();
185
186         // the imaginary end-of-paragraph character (at par.size()) has to be
187         // marked as unmodified. Otherwise, its change is adopted by the first
188         // character of the next paragraph.
189         if (par.lookupChange(par.size()).type != Change::UNCHANGED) {
190                 LYXERR(Debug::CHANGES,
191                    "merging par with inserted/deleted end-of-par character");
192                 par.setChange(par.size(), Change(Change::UNCHANGED));
193         }
194
195         Change change = next.lookupChange(next.size());
196
197         // move the content of the second paragraph to the end of the first one
198         for (pos_type i = 0, j = pos_insert; i <= pos_end; ++i) {
199                 if (moveItem(next, 0, par, j, bparams)) {
200                         ++j;
201                 }
202         }
203
204         // move the change of the end-of-paragraph character
205         par.setChange(par.size(), change);
206
207         pars.erase(boost::next(pars.begin(), par_offset + 1));
208 }
209
210
211 pit_type depthHook(pit_type pit, ParagraphList const & pars, depth_type depth)
212 {
213         pit_type newpit = pit;
214
215         if (newpit != 0)
216                 --newpit;
217
218         while (newpit != 0 && pars[newpit].getDepth() > depth)
219                 --newpit;
220
221         if (pars[newpit].getDepth() > depth)
222                 return pit;
223
224         return newpit;
225 }
226
227
228 pit_type outerHook(pit_type par_offset, ParagraphList const & pars)
229 {
230         Paragraph const & par = pars[par_offset];
231
232         if (par.getDepth() == 0)
233                 return pars.size();
234         return depthHook(par_offset, pars, depth_type(par.getDepth() - 1));
235 }
236
237
238 bool isFirstInSequence(pit_type par_offset, ParagraphList const & pars)
239 {
240         Paragraph const & par = pars[par_offset];
241
242         pit_type dhook_offset = depthHook(par_offset, pars, par.getDepth());
243
244         if (dhook_offset == par_offset)
245                 return true;
246
247         Paragraph const & dhook = pars[dhook_offset];
248
249         return dhook.layout() != par.layout()
250                 || dhook.getDepth() != par.getDepth();
251 }
252
253
254 int getEndLabel(pit_type p, ParagraphList const & pars)
255 {
256         pit_type pit = p;
257         depth_type par_depth = pars[p].getDepth();
258         while (pit != pit_type(pars.size())) {
259                 Layout const & layout = pars[pit].layout();
260                 int const endlabeltype = layout.endlabeltype;
261
262                 if (endlabeltype != END_LABEL_NO_LABEL) {
263                         if (p + 1 == pit_type(pars.size()))
264                                 return endlabeltype;
265
266                         depth_type const next_depth =
267                                 pars[p + 1].getDepth();
268                         if (par_depth > next_depth ||
269                             (par_depth == next_depth && layout != pars[p + 1].layout()))
270                                 return endlabeltype;
271                         break;
272                 }
273                 if (par_depth == 0)
274                         break;
275                 pit = outerHook(pit, pars);
276                 if (pit != pit_type(pars.size()))
277                         par_depth = pars[pit].getDepth();
278         }
279         return END_LABEL_NO_LABEL;
280 }
281
282
283 Font const outerFont(pit_type par_offset, ParagraphList const & pars)
284 {
285         depth_type par_depth = pars[par_offset].getDepth();
286         FontInfo tmpfont = inherit_font;
287
288         // Resolve against environment font information
289         while (par_offset != pit_type(pars.size())
290                && par_depth
291                && !tmpfont.resolved()) {
292                 par_offset = outerHook(par_offset, pars);
293                 if (par_offset != pit_type(pars.size())) {
294                         tmpfont.realize(pars[par_offset].layout().font);
295                         par_depth = pars[par_offset].getDepth();
296                 }
297         }
298
299         return Font(tmpfont);
300 }
301
302
303 void acceptChanges(ParagraphList & pars, BufferParams const & bparams)
304 {
305         pit_type pars_size = static_cast<pit_type>(pars.size());
306
307         // first, accept changes within each individual paragraph
308         // (do not consider end-of-par)
309         for (pit_type pit = 0; pit < pars_size; ++pit) {
310                 if (!pars[pit].empty())   // prevent assertion failure
311                         pars[pit].acceptChanges(bparams, 0, pars[pit].size());
312         }
313
314         // next, accept imaginary end-of-par characters
315         for (pit_type pit = 0; pit < pars_size; ++pit) {
316                 pos_type pos = pars[pit].size();
317
318                 if (pars[pit].isInserted(pos)) {
319                         pars[pit].setChange(pos, Change(Change::UNCHANGED));
320                 } else if (pars[pit].isDeleted(pos)) {
321                         if (pit == pars_size - 1) {
322                                 // we cannot remove a par break at the end of the last
323                                 // paragraph; instead, we mark it unchanged
324                                 pars[pit].setChange(pos, Change(Change::UNCHANGED));
325                         } else {
326                                 mergeParagraph(bparams, pars, pit);
327                                 --pit;
328                                 --pars_size;
329                         }
330                 }
331         }
332 }
333
334
335 } // namespace lyx