]> git.lyx.org Git - lyx.git/blob - src/paragraph_funcs.C
Change tracking:
[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                         par.eraseIntern(i);
127         }
128
129         if (pos) {
130                 // Make sure that we keep the language when
131                 // breaking paragrpah.
132                 if (tmp->empty()) {
133                         LyXFont changed = tmp->getFirstFontSettings(bparams);
134                         LyXFont old = par.getFontSettings(bparams, par.size());
135                         changed.setLanguage(old.language());
136                         tmp->setFont(0, changed);
137                 }
138
139                 return;
140         }
141
142         if (!isempty) {
143                 par.params().clear();
144                 par.layout(bparams.getLyXTextClass().defaultLayout());
145         }
146
147         // layout stays the same with latex-environments
148         if (flag) {
149                 par.layout(tmp->layout());
150                 par.setLabelWidthString(tmp->params().labelWidthString());
151                 par.params().depth(tmp->params().depth());
152         }
153
154         // subtle, but needed to get empty pars working right
155         if (bparams.trackChanges) {
156                 // FIXME: Change tracking (MG)
157                 // if (!par.size())
158                 //      set 'par' text to INSERTED in CT mode; clear CT info otherwise
159                 // else if (!tmp->size())
160                 //      set 'tmp' text to INSERTED in CT mode; clear CT info otherwise
161         }
162 }
163
164
165 void breakParagraphConservative(BufferParams const & bparams,
166         ParagraphList & pars, pit_type par_offset, pos_type pos)
167 {
168         // create a new paragraph
169         Paragraph & tmp = *pars.insert(boost::next(pars.begin(), par_offset + 1),
170                                        Paragraph());
171         Paragraph & par = pars[par_offset];
172
173         tmp.makeSameLayout(par);
174
175         // When can pos > size()?
176         // I guess pos == size() is possible.
177         if (par.size() > pos) {
178                 // copy everything behind the break-position to the new
179                 // paragraph
180                 pos_type pos_end = par.size() - 1;
181
182                 for (pos_type i = pos, j = pos; i <= pos_end; ++i) {
183                         Change::Type change = par.lookupChange(i).type;
184                         // FIXME: change tracking (MG)
185                         if (moveItem(par, tmp, bparams, i, j - pos, Change(change)))
186                                 ++j;
187                 }
188                 // Move over end-of-par change attr
189                 // FIXME: change tracking (MG)
190                 tmp.setChange(tmp.size(), Change(par.lookupChange(par.size()).type));
191
192                 // If tracking changes, set all the text that is to be
193                 // erased to Type::INSERTED.
194                 for (pos_type k = pos_end; k >= pos; --k) {
195                         if (bparams.trackChanges)
196                                 // FIXME: Change tracking (MG)
197                                 par.setChange(k, Change(Change::INSERTED));
198                         par.erase(k);
199                 }
200         }
201 }
202
203
204 void mergeParagraph(BufferParams const & bparams,
205         ParagraphList & pars, pit_type par_offset)
206 {
207         Paragraph & next = pars[par_offset + 1];
208         Paragraph & par = pars[par_offset];
209
210         pos_type pos_end = next.size() - 1;
211         pos_type pos_insert = par.size();
212
213         // What happens is the following. Later on, moveItem() will copy
214         // over characters from the next paragraph to be inserted into this
215         // position. Now, if the first char to be so copied is "red" (i.e.,
216         // marked deleted) and the paragraph break is marked "blue",
217         // insertChar will trigger (eventually, through record(), and see
218         // del() and erase() in changes.C) a "hard" character deletion.
219         // Which doesn't make sense of course at this pos, but the effect is
220         // to shorten the change range to which this para break belongs, by
221         // one. It will (should) remain "orphaned", having no CT info to it,
222         // and check() in changes.C will assert. Setting the para break
223         // forcibly to "black" prevents this scenario. -- MV 13.3.2006
224         // FIXME: change tracking (MG)
225         par.setChange(par.size(), Change(Change::UNCHANGED));
226
227         Change::Type cr = next.lookupChange(next.size()).type;
228         // ok, now copy the paragraph
229         for (pos_type i = 0, j = 0; i <= pos_end; ++i) {
230                 Change::Type change = next.lookupChange(i).type;
231                 // FIXME: change tracking (MG)
232                 if (moveItem(next, par, bparams, i, pos_insert + j, Change(change)))
233                         ++j;
234         }
235         // Move the change status of "carriage return" over
236         // FIXME: change tracking (MG)
237         par.setChange(par.size(), Change(cr));
238
239         pars.erase(boost::next(pars.begin(), par_offset + 1));
240 }
241
242
243 pit_type depthHook(pit_type pit,
244         ParagraphList const & pars, Paragraph::depth_type depth)
245 {
246         pit_type newpit = pit;
247
248         if (newpit != 0)
249                 --newpit;
250
251         while (newpit != 0 && pars[newpit].getDepth() > depth)
252                 --newpit;
253
254         if (pars[newpit].getDepth() > depth)
255                 return pit;
256
257         return newpit;
258 }
259
260
261 pit_type outerHook(pit_type par_offset, ParagraphList const & pars)
262 {
263         Paragraph const & par = pars[par_offset];
264
265         if (par.getDepth() == 0)
266                 return pars.size();
267         return depthHook(par_offset, pars, Paragraph::depth_type(par.getDepth() - 1));
268 }
269
270
271 bool isFirstInSequence(pit_type par_offset, ParagraphList const & pars)
272 {
273         Paragraph const & par = pars[par_offset];
274
275         pit_type dhook_offset = depthHook(par_offset, pars, par.getDepth());
276
277         Paragraph const & dhook = pars[dhook_offset];
278
279         return dhook_offset == par_offset
280                 || dhook.layout() != par.layout()
281                 || dhook.getDepth() != par.getDepth();
282 }
283
284
285 int getEndLabel(pit_type p, ParagraphList const & pars)
286 {
287         pit_type pit = p;
288         Paragraph::depth_type par_depth = pars[p].getDepth();
289         while (pit != pit_type(pars.size())) {
290                 LyXLayout_ptr const & layout = pars[pit].layout();
291                 int const endlabeltype = layout->endlabeltype;
292
293                 if (endlabeltype != END_LABEL_NO_LABEL) {
294                         if (p + 1 == pit_type(pars.size()))
295                                 return endlabeltype;
296
297                         Paragraph::depth_type const next_depth =
298                                 pars[p + 1].getDepth();
299                         if (par_depth > next_depth ||
300                             (par_depth == next_depth && layout != pars[p + 1].layout()))
301                                 return endlabeltype;
302                         break;
303                 }
304                 if (par_depth == 0)
305                         break;
306                 pit = outerHook(pit, pars);
307                 if (pit != pit_type(pars.size()))
308                         par_depth = pars[pit].getDepth();
309         }
310         return END_LABEL_NO_LABEL;
311 }
312
313
314 LyXFont const outerFont(pit_type par_offset, ParagraphList const & pars)
315 {
316         Paragraph::depth_type par_depth = pars[par_offset].getDepth();
317         LyXFont tmpfont(LyXFont::ALL_INHERIT);
318
319         // Resolve against environment font information
320         while (par_offset != pit_type(pars.size())
321                && par_depth
322                && !tmpfont.resolved()) {
323                 par_offset = outerHook(par_offset, pars);
324                 if (par_offset != pit_type(pars.size())) {
325                         tmpfont.realize(pars[par_offset].layout()->font);
326                         par_depth = pars[par_offset].getDepth();
327                 }
328         }
329
330         return tmpfont;
331 }
332
333
334 /// return the number of InsetOptArg in a paragraph
335 int numberOfOptArgs(Paragraph const & par)
336 {
337         int num = 0;
338
339         InsetList::const_iterator it = par.insetlist.begin();
340         InsetList::const_iterator end = par.insetlist.end();
341         for (; it != end ; ++it) {
342                 if (it->inset->lyxCode() == InsetBase::OPTARG_CODE)
343                         ++num;
344         }
345         return num;
346 }