]> git.lyx.org Git - lyx.git/blob - src/paragraph_funcs.cpp
Rename .C ==> .cpp for files in src/
[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 "LyXText.h"
18 #include "Paragraph.h"
19 #include "ParagraphParameters.h"
20
21
22 namespace lyx {
23
24 using std::string;
25 using std::endl;
26
27
28 static bool moveItem(Paragraph & fromPar, pos_type fromPos,
29         Paragraph & toPar, pos_type toPos, BufferParams const & params)
30 {
31         // Note: moveItem() does not honour change tracking!
32         // Therefore, it should only be used for breaking and merging paragraphs
33
34         Paragraph::value_type const tmpChar = fromPar.getChar(fromPos);
35         LyXFont const tmpFont = fromPar.getFontSettings(params, fromPos);
36         Change const tmpChange = fromPar.lookupChange(fromPos);
37
38         if (tmpChar == Paragraph::META_INSET) {
39                 InsetBase * tmpInset = 0;
40                 if (fromPar.getInset(fromPos)) {
41                         // the inset is not in the paragraph any more
42                         tmpInset = fromPar.insetlist.release(fromPos);
43                 }
44
45                 fromPar.eraseChar(fromPos, false);
46
47                 if (!toPar.insetAllowed(tmpInset->lyxCode())) {
48                         delete tmpInset;
49                         return false;
50                 }
51
52                 toPar.insertInset(toPos, tmpInset, tmpFont, tmpChange);
53         } else {
54                 fromPar.eraseChar(fromPos, false);
55                 toPar.insertChar(toPos, tmpChar, tmpFont, tmpChange);
56         }
57
58         return true;
59 }
60
61
62 void breakParagraph(BufferParams const & bparams,
63         ParagraphList & pars, pit_type par_offset, pos_type pos, int flag)
64 {
65         // create a new paragraph, and insert into the list
66         ParagraphList::iterator tmp =
67                 pars.insert(boost::next(pars.begin(), par_offset + 1),
68                             Paragraph());
69
70         Paragraph & par = pars[par_offset];
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         if (par.lookupChange(par.size()).type != Change::UNCHANGED) {
192                 LYXERR(Debug::CHANGES) <<
193                    "merging par with inserted/deleted end-of-par character" << endl;
194                 par.setChange(par.size(), Change(Change::UNCHANGED));
195         }
196
197         Change change = next.lookupChange(next.size());
198
199         // move the content of the second paragraph to the end of the first one
200         for (pos_type i = 0, j = pos_insert; i <= pos_end; ++i) {
201                 if (moveItem(next, 0, par, j, bparams)) {
202                         ++j;
203                 }
204         }
205
206         // move the change of the end-of-paragraph character
207         par.setChange(par.size(), change);
208
209         pars.erase(boost::next(pars.begin(), par_offset + 1));
210 }
211
212
213 pit_type depthHook(pit_type pit, ParagraphList const & pars, depth_type depth)
214 {
215         pit_type newpit = pit;
216
217         if (newpit != 0)
218                 --newpit;
219
220         while (newpit != 0 && pars[newpit].getDepth() > depth)
221                 --newpit;
222
223         if (pars[newpit].getDepth() > depth)
224                 return pit;
225
226         return newpit;
227 }
228
229
230 pit_type outerHook(pit_type par_offset, ParagraphList const & pars)
231 {
232         Paragraph const & par = pars[par_offset];
233
234         if (par.getDepth() == 0)
235                 return pars.size();
236         return depthHook(par_offset, pars, depth_type(par.getDepth() - 1));
237 }
238
239
240 bool isFirstInSequence(pit_type par_offset, ParagraphList const & pars)
241 {
242         Paragraph const & par = pars[par_offset];
243
244         pit_type dhook_offset = depthHook(par_offset, pars, par.getDepth());
245
246         if (dhook_offset == par_offset)
247                 return true;
248
249         Paragraph const & dhook = pars[dhook_offset];
250
251         return 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