]> git.lyx.org Git - lyx.git/blob - src/paragraph_funcs.C
remove unneeded includes
[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         // we will invalidate the row cache
72         par.rows().clear();
73
74         // without doing that we get a crash when typing <Return> at the
75         // end of a paragraph
76         tmp->layout(bparams.getLyXTextClass().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                         LyXFont changed = tmp->getFirstFontSettings(bparams);
130                         LyXFont 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                 par.params().clear();
140                 par.layout(bparams.getLyXTextClass().defaultLayout());
141         }
142
143         // layout stays the same with latex-environments
144         if (flag) {
145                 par.layout(tmp->layout());
146                 par.setLabelWidthString(tmp->params().labelWidthString());
147                 par.params().depth(tmp->params().depth());
148         }
149 }
150
151
152 void breakParagraphConservative(BufferParams const & bparams,
153         ParagraphList & pars, pit_type par_offset, pos_type pos)
154 {
155         // create a new paragraph
156         Paragraph & tmp = *pars.insert(boost::next(pars.begin(), par_offset + 1),
157                                        Paragraph());
158         Paragraph & par = pars[par_offset];
159
160         tmp.makeSameLayout(par);
161
162         BOOST_ASSERT(pos <= par.size());
163
164         if (pos < par.size()) {
165                 // move everything behind the break position to the new paragraph
166                 pos_type pos_end = par.size() - 1;
167
168                 for (pos_type i = pos, j = 0; i <= pos_end; ++i) {
169                         if (moveItem(par, pos, tmp, j, bparams)) {
170                                 ++j;
171                         }
172                 }
173                 // Move over the end-of-par change information
174                 tmp.setChange(tmp.size(), par.lookupChange(par.size()));
175                 par.setChange(par.size(), Change(bparams.trackChanges ?
176                                            Change::INSERTED : Change::UNCHANGED));
177         }
178 }
179
180
181 void mergeParagraph(BufferParams const & bparams,
182         ParagraphList & pars, pit_type par_offset)
183 {
184         Paragraph & next = pars[par_offset + 1];
185         Paragraph & par = pars[par_offset];
186
187         pos_type pos_end = next.size() - 1;
188         pos_type pos_insert = par.size();
189
190         // the imaginary end-of-paragraph character (at par.size()) has to be
191         // marked as unmodified. Otherwise, its change is adopted by the first
192         // character of the next paragraph.
193         if (par.lookupChange(par.size()).type != Change::UNCHANGED) {
194                 lyxerr[Debug::CHANGES] <<
195                    "merging par with inserted/deleted end-of-par character" << endl;
196                 par.setChange(par.size(), Change(Change::UNCHANGED));
197         }
198
199         Change change = next.lookupChange(next.size());
200
201         // move the content of the second paragraph to the end of the first one
202         for (pos_type i = 0, j = pos_insert; i <= pos_end; ++i) {
203                 if (moveItem(next, 0, par, j, bparams)) {
204                         ++j;
205                 }
206         }
207
208         // move the change of the end-of-paragraph character
209         par.setChange(par.size(), change);
210
211         pars.erase(boost::next(pars.begin(), par_offset + 1));
212 }
213
214
215 pit_type depthHook(pit_type pit, ParagraphList const & pars, depth_type depth)
216 {
217         pit_type newpit = pit;
218
219         if (newpit != 0)
220                 --newpit;
221
222         while (newpit != 0 && pars[newpit].getDepth() > depth)
223                 --newpit;
224
225         if (pars[newpit].getDepth() > depth)
226                 return pit;
227
228         return newpit;
229 }
230
231
232 pit_type outerHook(pit_type par_offset, ParagraphList const & pars)
233 {
234         Paragraph const & par = pars[par_offset];
235
236         if (par.getDepth() == 0)
237                 return pars.size();
238         return depthHook(par_offset, pars, depth_type(par.getDepth() - 1));
239 }
240
241
242 bool isFirstInSequence(pit_type par_offset, ParagraphList const & pars)
243 {
244         Paragraph const & par = pars[par_offset];
245
246         pit_type dhook_offset = depthHook(par_offset, pars, par.getDepth());
247
248         Paragraph const & dhook = pars[dhook_offset];
249
250         return dhook_offset == par_offset
251                 || dhook.layout() != par.layout()
252                 || dhook.getDepth() != par.getDepth();
253 }
254
255
256 int getEndLabel(pit_type p, ParagraphList const & pars)
257 {
258         pit_type pit = p;
259         depth_type par_depth = pars[p].getDepth();
260         while (pit != pit_type(pars.size())) {
261                 LyXLayout_ptr const & layout = pars[pit].layout();
262                 int const endlabeltype = layout->endlabeltype;
263
264                 if (endlabeltype != END_LABEL_NO_LABEL) {
265                         if (p + 1 == pit_type(pars.size()))
266                                 return endlabeltype;
267
268                         depth_type const next_depth =
269                                 pars[p + 1].getDepth();
270                         if (par_depth > next_depth ||
271                             (par_depth == next_depth && layout != pars[p + 1].layout()))
272                                 return endlabeltype;
273                         break;
274                 }
275                 if (par_depth == 0)
276                         break;
277                 pit = outerHook(pit, pars);
278                 if (pit != pit_type(pars.size()))
279                         par_depth = pars[pit].getDepth();
280         }
281         return END_LABEL_NO_LABEL;
282 }
283
284
285 LyXFont const outerFont(pit_type par_offset, ParagraphList const & pars)
286 {
287         depth_type par_depth = pars[par_offset].getDepth();
288         LyXFont tmpfont(LyXFont::ALL_INHERIT);
289
290         // Resolve against environment font information
291         while (par_offset != pit_type(pars.size())
292                && par_depth
293                && !tmpfont.resolved()) {
294                 par_offset = outerHook(par_offset, pars);
295                 if (par_offset != pit_type(pars.size())) {
296                         tmpfont.realize(pars[par_offset].layout()->font);
297                         par_depth = pars[par_offset].getDepth();
298                 }
299         }
300
301         return tmpfont;
302 }
303
304
305 /// return the number of InsetOptArg in a paragraph
306 int numberOfOptArgs(Paragraph const & par)
307 {
308         int num = 0;
309
310         InsetList::const_iterator it = par.insetlist.begin();
311         InsetList::const_iterator end = par.insetlist.end();
312         for (; it != end ; ++it) {
313                 if (it->inset->lyxCode() == InsetBase::OPTARG_CODE)
314                         ++num;
315         }
316         return num;
317 }
318
319
320 } // namespace lyx