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