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