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