]> git.lyx.org Git - lyx.git/blob - src/paragraph_funcs.C
hopefully fix tex2lyx linking.
[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 Change::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         // The imaginary end-of-paragraph character (at par.size()) has to be
204         // marked as unmodified. Otherwise, its change is adopted by the first
205         // character of the next paragraph.
206         
207         // FIXME: change tracking (MG)
208         par.setChange(par.size(), Change(Change::UNCHANGED));
209
210         Change change = next.lookupChange(next.size());
211         // ok, now copy the paragraph
212         for (pos_type i = 0, j = 0; i <= pos_end; ++i) {
213                 if (moveItem(next, i, par, pos_insert + j, bparams)) {
214                         ++j;
215                 }
216         }
217         // Move the change of the end-of-paragraph character
218         // FIXME: change tracking (MG)
219         par.setChange(par.size(), change);
220
221         pars.erase(boost::next(pars.begin(), par_offset + 1));
222 }
223
224
225 pit_type depthHook(pit_type pit, ParagraphList const & pars, depth_type depth)
226 {
227         pit_type newpit = pit;
228
229         if (newpit != 0)
230                 --newpit;
231
232         while (newpit != 0 && pars[newpit].getDepth() > depth)
233                 --newpit;
234
235         if (pars[newpit].getDepth() > depth)
236                 return pit;
237
238         return newpit;
239 }
240
241
242 pit_type outerHook(pit_type par_offset, ParagraphList const & pars)
243 {
244         Paragraph const & par = pars[par_offset];
245
246         if (par.getDepth() == 0)
247                 return pars.size();
248         return depthHook(par_offset, pars, depth_type(par.getDepth() - 1));
249 }
250
251
252 bool isFirstInSequence(pit_type par_offset, ParagraphList const & pars)
253 {
254         Paragraph const & par = pars[par_offset];
255
256         pit_type dhook_offset = depthHook(par_offset, pars, par.getDepth());
257
258         Paragraph const & dhook = pars[dhook_offset];
259
260         return dhook_offset == par_offset
261                 || dhook.layout() != par.layout()
262                 || dhook.getDepth() != par.getDepth();
263 }
264
265
266 int getEndLabel(pit_type p, ParagraphList const & pars)
267 {
268         pit_type pit = p;
269         depth_type par_depth = pars[p].getDepth();
270         while (pit != pit_type(pars.size())) {
271                 LyXLayout_ptr const & layout = pars[pit].layout();
272                 int const endlabeltype = layout->endlabeltype;
273
274                 if (endlabeltype != END_LABEL_NO_LABEL) {
275                         if (p + 1 == pit_type(pars.size()))
276                                 return endlabeltype;
277
278                         depth_type const next_depth =
279                                 pars[p + 1].getDepth();
280                         if (par_depth > next_depth ||
281                             (par_depth == next_depth && layout != pars[p + 1].layout()))
282                                 return endlabeltype;
283                         break;
284                 }
285                 if (par_depth == 0)
286                         break;
287                 pit = outerHook(pit, pars);
288                 if (pit != pit_type(pars.size()))
289                         par_depth = pars[pit].getDepth();
290         }
291         return END_LABEL_NO_LABEL;
292 }
293
294
295 LyXFont const outerFont(pit_type par_offset, ParagraphList const & pars)
296 {
297         depth_type par_depth = pars[par_offset].getDepth();
298         LyXFont tmpfont(LyXFont::ALL_INHERIT);
299
300         // Resolve against environment font information
301         while (par_offset != pit_type(pars.size())
302                && par_depth
303                && !tmpfont.resolved()) {
304                 par_offset = outerHook(par_offset, pars);
305                 if (par_offset != pit_type(pars.size())) {
306                         tmpfont.realize(pars[par_offset].layout()->font);
307                         par_depth = pars[par_offset].getDepth();
308                 }
309         }
310
311         return tmpfont;
312 }
313
314
315 /// return the number of InsetOptArg in a paragraph
316 int numberOfOptArgs(Paragraph const & par)
317 {
318         int num = 0;
319
320         InsetList::const_iterator it = par.insetlist.begin();
321         InsetList::const_iterator end = par.insetlist.end();
322         for (; it != end ; ++it) {
323                 if (it->inset->lyxCode() == InsetBase::OPTARG_CODE)
324                         ++num;
325         }
326         return num;
327 }
328
329
330 } // namespace lyx