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