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