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