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