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