]> git.lyx.org Git - lyx.git/blob - src/paragraph_funcs.C
make it compile again (hopefully)
[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 using lyx::pos_type;
20 using lyx::pit_type;
21
22 using std::string;
23
24
25 namespace {
26
27 bool moveItem(Paragraph & from, Paragraph & to,
28         BufferParams const & params, pos_type i, pos_type j,
29         Change change = Change(Change::INSERTED));
30
31 bool moveItem(Paragraph & from, Paragraph & to,
32         BufferParams const & params, pos_type i, pos_type j,
33         Change change)
34 {
35         Paragraph::value_type const tmpchar = from.getChar(i);
36         LyXFont tmpfont = from.getFontSettings(params, i);
37
38         if (tmpchar == Paragraph::META_INSET) {
39                 InsetBase * tmpinset = 0;
40                 if (from.getInset(i)) {
41                         // the inset is not in a paragraph anymore
42                         tmpinset = from.insetlist.release(i);
43                         from.insetlist.erase(i);
44                 }
45
46                 if (!to.insetAllowed(tmpinset->lyxCode())) {
47                         delete tmpinset;
48                         return false;
49                 }
50                 if (tmpinset)
51                         to.insertInset(j, tmpinset, tmpfont, change);
52         } else {
53                 to.insertChar(j, tmpchar, tmpfont, change);
54         }
55         return true;
56 }
57
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         // this is an idea for a more userfriendly layout handling, I will
81         // see what the users say
82
83         // layout stays the same with latex-environments
84         if (flag) {
85                 tmp->layout(par.layout());
86                 tmp->setLabelWidthString(par.params().labelWidthString());
87                 tmp->params().depth(par.params().depth());
88         } else if (par.params().depth() > 0) {
89                 Paragraph const & hook = pars[outerHook(par_offset, pars)];
90                 tmp->layout(hook.layout());
91                 // not sure the line below is useful
92                 tmp->setLabelWidthString(par.params().labelWidthString());
93                 tmp->params().depth(hook.params().depth());
94         }
95
96         bool const isempty = (par.allowEmpty() && par.empty());
97
98         if (!isempty && (par.size() > pos || par.empty() || flag == 2)) {
99                 tmp->layout(par.layout());
100                 tmp->params().align(par.params().align());
101                 tmp->setLabelWidthString(par.params().labelWidthString());
102
103                 tmp->params().depth(par.params().depth());
104                 tmp->params().noindent(par.params().noindent());
105
106                 // copy everything behind the break-position
107                 // to the new paragraph
108
109                 /* Note: if !keepempty, empty() == true, then we reach
110                  * here with size() == 0. So pos_end becomes - 1. This
111                  * doesn't cause problems because both loops below
112                  * enforce pos <= pos_end and 0 <= pos
113                  */
114                 pos_type pos_end = par.size() - 1;
115
116                 for (pos_type i = pos, j = pos; i <= pos_end; ++i) {
117                         Change::Type change = par.lookupChange(i).type;
118                         if (moveItem(par, *tmp, bparams, i, j - pos)) {
119                                 // FIXME: change tracking (MG)
120                                 tmp->setChange(j - pos, Change(change));
121                                 ++j;
122                         }
123                 }
124
125                 for (pos_type i = pos_end; i >= pos; --i)
126                         // FIXME: change tracking (MG)
127                         par.erase(i, false); // erase without change tracking
128         }
129
130         if (pos) {
131                 // Make sure that we keep the language when
132                 // breaking paragrpah.
133                 if (tmp->empty()) {
134                         LyXFont changed = tmp->getFirstFontSettings(bparams);
135                         LyXFont old = par.getFontSettings(bparams, par.size());
136                         changed.setLanguage(old.language());
137                         tmp->setFont(0, changed);
138                 }
139
140                 return;
141         }
142
143         if (!isempty) {
144                 par.params().clear();
145                 par.layout(bparams.getLyXTextClass().defaultLayout());
146         }
147
148         // layout stays the same with latex-environments
149         if (flag) {
150                 par.layout(tmp->layout());
151                 par.setLabelWidthString(tmp->params().labelWidthString());
152                 par.params().depth(tmp->params().depth());
153         }
154
155         // subtle, but needed to get empty pars working right
156         if (bparams.trackChanges) {
157                 // FIXME: Change tracking (MG)
158                 // if (!par.size())
159                 //      set 'par' text to INSERTED in CT mode; clear CT info otherwise
160                 // else if (!tmp->size())
161                 //      set 'tmp' text to INSERTED in CT mode; clear CT info otherwise
162         }
163 }
164
165
166 void breakParagraphConservative(BufferParams const & bparams,
167         ParagraphList & pars, pit_type par_offset, pos_type pos)
168 {
169         // create a new paragraph
170         Paragraph & tmp = *pars.insert(boost::next(pars.begin(), par_offset + 1),
171                                        Paragraph());
172         Paragraph & par = pars[par_offset];
173
174         tmp.makeSameLayout(par);
175
176         // When can pos > size()?
177         // I guess pos == size() is possible.
178         if (par.size() > pos) {
179                 // copy everything behind the break-position to the new
180                 // paragraph
181                 pos_type pos_end = par.size() - 1;
182
183                 for (pos_type i = pos, j = pos; i <= pos_end; ++i) {
184                         Change::Type change = par.lookupChange(i).type;
185                         // FIXME: change tracking (MG)
186                         if (moveItem(par, tmp, bparams, i, j - pos, Change(change)))
187                                 ++j;
188                 }
189                 // Move over end-of-par change attr
190                 // FIXME: change tracking (MG)
191                 tmp.setChange(tmp.size(), Change(par.lookupChange(par.size()).type));
192
193                 // If tracking changes, set all the text that is to be
194                 // erased to Type::INSERTED.
195                 for (pos_type k = pos_end; k >= pos; --k) {
196                         if (bparams.trackChanges)
197                                 // FIXME: Change tracking (MG)
198                                 par.setChange(k, Change(Change::INSERTED));
199                         // FIXME: change tracking (MG)
200                         par.erase(k, false);
201                 }
202         }
203 }
204
205
206 void mergeParagraph(BufferParams const & bparams,
207         ParagraphList & pars, pit_type par_offset)
208 {
209         Paragraph & next = pars[par_offset + 1];
210         Paragraph & par = pars[par_offset];
211
212         pos_type pos_end = next.size() - 1;
213         pos_type pos_insert = par.size();
214
215         // What happens is the following. Later on, moveItem() will copy
216         // over characters from the next paragraph to be inserted into this
217         // position. Now, if the first char to be so copied is "red" (i.e.,
218         // marked deleted) and the paragraph break is marked "blue",
219         // insertChar will trigger (eventually, through record(), and see
220         // del() and erase() in changes.C) a "hard" character deletion.
221         // Which doesn't make sense of course at this pos, but the effect is
222         // to shorten the change range to which this para break belongs, by
223         // one. It will (should) remain "orphaned", having no CT info to it,
224         // and check() in changes.C will assert. Setting the para break
225         // forcibly to "black" prevents this scenario. -- MV 13.3.2006
226         // FIXME: change tracking (MG)
227         par.setChange(par.size(), Change(Change::UNCHANGED));
228
229         Change::Type cr = next.lookupChange(next.size()).type;
230         // ok, now copy the paragraph
231         for (pos_type i = 0, j = 0; i <= pos_end; ++i) {
232                 Change::Type change = next.lookupChange(i).type;
233                 // FIXME: change tracking (MG)
234                 if (moveItem(next, par, bparams, i, pos_insert + j, Change(change)))
235                         ++j;
236         }
237         // Move the change status of "carriage return" over
238         // FIXME: change tracking (MG)
239         par.setChange(par.size(), Change(cr));
240
241         pars.erase(boost::next(pars.begin(), par_offset + 1));
242 }
243
244
245 pit_type depthHook(pit_type pit,
246         ParagraphList const & pars, Paragraph::depth_type depth)
247 {
248         pit_type newpit = pit;
249
250         if (newpit != 0)
251                 --newpit;
252
253         while (newpit != 0 && pars[newpit].getDepth() > depth)
254                 --newpit;
255
256         if (pars[newpit].getDepth() > depth)
257                 return pit;
258
259         return newpit;
260 }
261
262
263 pit_type outerHook(pit_type par_offset, ParagraphList const & pars)
264 {
265         Paragraph const & par = pars[par_offset];
266
267         if (par.getDepth() == 0)
268                 return pars.size();
269         return depthHook(par_offset, pars, Paragraph::depth_type(par.getDepth() - 1));
270 }
271
272
273 bool isFirstInSequence(pit_type par_offset, ParagraphList const & pars)
274 {
275         Paragraph const & par = pars[par_offset];
276
277         pit_type dhook_offset = depthHook(par_offset, pars, par.getDepth());
278
279         Paragraph const & dhook = pars[dhook_offset];
280
281         return dhook_offset == par_offset
282                 || dhook.layout() != par.layout()
283                 || dhook.getDepth() != par.getDepth();
284 }
285
286
287 int getEndLabel(pit_type p, ParagraphList const & pars)
288 {
289         pit_type pit = p;
290         Paragraph::depth_type par_depth = pars[p].getDepth();
291         while (pit != pit_type(pars.size())) {
292                 LyXLayout_ptr const & layout = pars[pit].layout();
293                 int const endlabeltype = layout->endlabeltype;
294
295                 if (endlabeltype != END_LABEL_NO_LABEL) {
296                         if (p + 1 == pit_type(pars.size()))
297                                 return endlabeltype;
298
299                         Paragraph::depth_type const next_depth =
300                                 pars[p + 1].getDepth();
301                         if (par_depth > next_depth ||
302                             (par_depth == next_depth && layout != pars[p + 1].layout()))
303                                 return endlabeltype;
304                         break;
305                 }
306                 if (par_depth == 0)
307                         break;
308                 pit = outerHook(pit, pars);
309                 if (pit != pit_type(pars.size()))
310                         par_depth = pars[pit].getDepth();
311         }
312         return END_LABEL_NO_LABEL;
313 }
314
315
316 LyXFont const outerFont(pit_type par_offset, ParagraphList const & pars)
317 {
318         Paragraph::depth_type par_depth = pars[par_offset].getDepth();
319         LyXFont tmpfont(LyXFont::ALL_INHERIT);
320
321         // Resolve against environment font information
322         while (par_offset != pit_type(pars.size())
323                && par_depth
324                && !tmpfont.resolved()) {
325                 par_offset = outerHook(par_offset, pars);
326                 if (par_offset != pit_type(pars.size())) {
327                         tmpfont.realize(pars[par_offset].layout()->font);
328                         par_depth = pars[par_offset].getDepth();
329                 }
330         }
331
332         return tmpfont;
333 }
334
335
336 /// return the number of InsetOptArg in a paragraph
337 int numberOfOptArgs(Paragraph const & par)
338 {
339         int num = 0;
340
341         InsetList::const_iterator it = par.insetlist.begin();
342         InsetList::const_iterator end = par.insetlist.end();
343         for (; it != end ; ++it) {
344                 if (it->inset->lyxCode() == InsetBase::OPTARG_CODE)
345                         ++num;
346         }
347         return num;
348 }