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