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