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