]> git.lyx.org Git - lyx.git/blob - src/paragraph_funcs.C
f1af41bf81722557fb91c7ca04b25f9bfbc30eb1
[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
19
20 namespace lyx {
21
22 using std::string;
23
24
25 static bool moveItem(Paragraph & fromPar, pos_type fromPos,
26         Paragraph & toPar, pos_type toPos, BufferParams const & params)
27 {
28         // Note: moveItem() does not honour change tracking!
29         // Therefore, it should only be used for breaking and merging paragraphs
30
31         Paragraph::value_type const tmpChar = fromPar.getChar(fromPos);
32         LyXFont const tmpFont = fromPar.getFontSettings(params, fromPos);
33         Change const tmpChange = fromPar.lookupChange(fromPos);
34
35         if (tmpChar == Paragraph::META_INSET) {
36                 InsetBase * tmpInset = 0;
37                 if (fromPar.getInset(fromPos)) {
38                         // the inset is not in the paragraph any more
39                         tmpInset = fromPar.insetlist.release(fromPos);
40                 }
41
42                 fromPar.eraseChar(fromPos, false);
43
44                 if (!toPar.insetAllowed(tmpInset->lyxCode())) {
45                         delete tmpInset;
46                         return false;
47                 }
48
49                 toPar.insertInset(toPos, tmpInset, tmpFont, tmpChange);
50         } else {
51                 fromPar.eraseChar(fromPos, false);
52                 toPar.insertChar(toPos, tmpChar, tmpFont, tmpChange);
53         }
54
55         return true;
56 }
57
58
59 void breakParagraph(BufferParams const & bparams,
60         ParagraphList & pars, pit_type par_offset, pos_type pos, int flag)
61 {
62         // create a new paragraph, and insert into the list
63         ParagraphList::iterator tmp =
64                 pars.insert(boost::next(pars.begin(), par_offset + 1),
65                             Paragraph());
66
67         Paragraph & par = pars[par_offset];
68
69         // we will invalidate the row cache
70         par.rows().clear();
71
72         // without doing that we get a crash when typing <Return> at the
73         // end of a paragraph
74         tmp->layout(bparams.getLyXTextClass().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                         LyXFont changed = tmp->getFirstFontSettings(bparams);
128                         LyXFont 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                 par.params().clear();
138                 par.layout(bparams.getLyXTextClass().defaultLayout());
139         }
140
141         // layout stays the same with latex-environments
142         if (flag) {
143                 par.layout(tmp->layout());
144                 par.setLabelWidthString(tmp->params().labelWidthString());
145                 par.params().depth(tmp->params().depth());
146         }
147 }
148
149
150 void breakParagraphConservative(BufferParams const & bparams,
151         ParagraphList & pars, pit_type par_offset, pos_type pos)
152 {
153         // create a new paragraph
154         Paragraph & tmp = *pars.insert(boost::next(pars.begin(), par_offset + 1),
155                                        Paragraph());
156         Paragraph & par = pars[par_offset];
157
158         tmp.makeSameLayout(par);
159
160         BOOST_ASSERT(pos <= par.size());
161
162         if (pos < par.size()) {
163                 // move everything behind the break position to the new paragraph
164                 pos_type pos_end = par.size() - 1;
165
166                 for (pos_type i = pos, j = 0; i <= pos_end; ++i) {
167                         if (moveItem(par, pos, tmp, j, bparams)) {
168                                 ++j;
169                         }
170                 }
171                 // Move over the end-of-par change information
172                 tmp.setChange(tmp.size(), par.lookupChange(par.size()));
173                 par.setChange(par.size(), Change(bparams.trackChanges ?
174                                            Change::INSERTED : Change::UNCHANGED));
175         }
176 }
177
178
179 void mergeParagraph(BufferParams const & bparams,
180         ParagraphList & pars, pit_type par_offset)
181 {
182         Paragraph & next = pars[par_offset + 1];
183         Paragraph & par = pars[par_offset];
184
185         pos_type pos_end = next.size() - 1;
186         pos_type pos_insert = par.size();
187
188         // The imaginary end-of-paragraph character (at par.size()) has to be
189         // marked as unmodified. Otherwise, its change is adopted by the first
190         // character of the next paragraph.
191         
192         // FIXME: change tracking (MG)
193         par.setChange(par.size(), Change(Change::UNCHANGED));
194
195         Change change = next.lookupChange(next.size());
196         // ok, now copy the paragraph
197         for (pos_type i = 0, j = pos_insert; i <= pos_end; ++i) {
198                 if (moveItem(next, 0, par, j, bparams)) {
199                         ++j;
200                 }
201         }
202         // Move the change of the end-of-paragraph character
203         // FIXME: change tracking (MG)
204         par.setChange(par.size(), change);
205
206         pars.erase(boost::next(pars.begin(), par_offset + 1));
207 }
208
209
210 pit_type depthHook(pit_type pit, ParagraphList const & pars, depth_type depth)
211 {
212         pit_type newpit = pit;
213
214         if (newpit != 0)
215                 --newpit;
216
217         while (newpit != 0 && pars[newpit].getDepth() > depth)
218                 --newpit;
219
220         if (pars[newpit].getDepth() > depth)
221                 return pit;
222
223         return newpit;
224 }
225
226
227 pit_type outerHook(pit_type par_offset, ParagraphList const & pars)
228 {
229         Paragraph const & par = pars[par_offset];
230
231         if (par.getDepth() == 0)
232                 return pars.size();
233         return depthHook(par_offset, pars, depth_type(par.getDepth() - 1));
234 }
235
236
237 bool isFirstInSequence(pit_type par_offset, ParagraphList const & pars)
238 {
239         Paragraph const & par = pars[par_offset];
240
241         pit_type dhook_offset = depthHook(par_offset, pars, par.getDepth());
242
243         Paragraph const & dhook = pars[dhook_offset];
244
245         return dhook_offset == par_offset
246                 || dhook.layout() != par.layout()
247                 || dhook.getDepth() != par.getDepth();
248 }
249
250
251 int getEndLabel(pit_type p, ParagraphList const & pars)
252 {
253         pit_type pit = p;
254         depth_type par_depth = pars[p].getDepth();
255         while (pit != pit_type(pars.size())) {
256                 LyXLayout_ptr const & layout = pars[pit].layout();
257                 int const endlabeltype = layout->endlabeltype;
258
259                 if (endlabeltype != END_LABEL_NO_LABEL) {
260                         if (p + 1 == pit_type(pars.size()))
261                                 return endlabeltype;
262
263                         depth_type const next_depth =
264                                 pars[p + 1].getDepth();
265                         if (par_depth > next_depth ||
266                             (par_depth == next_depth && layout != pars[p + 1].layout()))
267                                 return endlabeltype;
268                         break;
269                 }
270                 if (par_depth == 0)
271                         break;
272                 pit = outerHook(pit, pars);
273                 if (pit != pit_type(pars.size()))
274                         par_depth = pars[pit].getDepth();
275         }
276         return END_LABEL_NO_LABEL;
277 }
278
279
280 LyXFont const outerFont(pit_type par_offset, ParagraphList const & pars)
281 {
282         depth_type par_depth = pars[par_offset].getDepth();
283         LyXFont tmpfont(LyXFont::ALL_INHERIT);
284
285         // Resolve against environment font information
286         while (par_offset != pit_type(pars.size())
287                && par_depth
288                && !tmpfont.resolved()) {
289                 par_offset = outerHook(par_offset, pars);
290                 if (par_offset != pit_type(pars.size())) {
291                         tmpfont.realize(pars[par_offset].layout()->font);
292                         par_depth = pars[par_offset].getDepth();
293                 }
294         }
295
296         return tmpfont;
297 }
298
299
300 /// return the number of InsetOptArg in a paragraph
301 int numberOfOptArgs(Paragraph const & par)
302 {
303         int num = 0;
304
305         InsetList::const_iterator it = par.insetlist.begin();
306         InsetList::const_iterator end = par.insetlist.end();
307         for (; it != end ; ++it) {
308                 if (it->inset->lyxCode() == InsetBase::OPTARG_CODE)
309                         ++num;
310         }
311         return num;
312 }
313
314
315 } // namespace lyx