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