]> git.lyx.org Git - lyx.git/blob - src/paragraph_funcs.C
Squash a couple of warnings, one at compile time and one at run time.
[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 "iterators.h"
22 #include "language.h"
23 #include "lyxtext.h"
24 #include "outputparams.h"
25 #include "paragraph_pimpl.h"
26 #include "sgml.h"
27 #include "texrow.h"
28 #include "vspace.h"
29
30 #include "support/filetools.h"
31 #include "support/lstrings.h"
32 #include "support/lyxlib.h"
33 #include "support/std_sstream.h"
34
35 #include <vector>
36
37 using lyx::pos_type;
38 using lyx::par_type;
39
40 using lyx::support::ascii_lowercase;
41 using lyx::support::atoi;
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 {
63         char const tmpchar = from.getChar(i);
64         LyXFont tmpfont = from.getFontSettings(params, i);
65
66         if (tmpchar == Paragraph::META_INSET) {
67                 InsetBase * tmpinset = 0;
68                 if (from.getInset(i)) {
69                         // the inset is not in a paragraph anymore
70                         tmpinset = from.insetlist.release(i);
71                         from.insetlist.erase(i);
72                 }
73
74                 if (!to.insetAllowed(tmpinset->lyxCode())) {
75                         delete tmpinset;
76                         return false;
77                 }
78                 if (tmpinset)
79                         to.insertInset(j, tmpinset, tmpfont);
80         } else {
81                 if (!to.checkInsertChar(tmpfont))
82                         return false;
83                 to.insertChar(j, tmpchar, tmpfont);
84         }
85         return true;
86 }
87
88 }
89
90
91 void breakParagraph(BufferParams const & bparams,
92         ParagraphList & pars, par_type par, pos_type pos, int flag)
93 {
94         // create a new paragraph, and insert into the list
95         ParagraphList::iterator tmp =
96                 pars.insert(pars.begin() + par + 1, Paragraph());
97
98         // without doing that we get a crash when typing <Return> at the
99         // end of a paragraph
100         tmp->layout(bparams.getLyXTextClass().defaultLayout());
101         // remember to set the inset_owner
102         tmp->setInsetOwner(pars[par].inInset());
103
104         if (bparams.tracking_changes)
105                 tmp->trackChanges();
106
107         // this is an idea for a more userfriendly layout handling, I will
108         // see what the users say
109
110         // layout stays the same with latex-environments
111         if (flag) {
112                 tmp->layout(pars[par].layout());
113                 tmp->setLabelWidthString(pars[par].params().labelWidthString());
114         }
115
116         bool const isempty = (pars[par].allowEmpty() && pars[par].empty());
117
118         if (!isempty && (pars[par].size() > pos || pars[par].empty() || flag == 2)) {
119                 tmp->layout(pars[par].layout());
120                 tmp->params().align(pars[par].params().align());
121                 tmp->setLabelWidthString(pars[par].params().labelWidthString());
122
123                 tmp->params().depth(pars[par].params().depth());
124                 tmp->params().noindent(pars[par].params().noindent());
125
126                 // copy everything behind the break-position
127                 // to the new paragraph
128
129 #ifdef WITH_WARNINGS
130 #warning this seems wrong
131 #endif
132                 /* FIXME: if !keepempty, empty() == true, then we reach
133                  * here with size() == 0. So pos_end becomes - 1. Why
134                  * doesn't this cause problems ???
135                  */
136                 pos_type pos_end = pars[par].size() - 1;
137                 pos_type i = pos;
138                 pos_type j = pos;
139
140                 for (; i <= pos_end; ++i) {
141                         Change::Type change = pars[par].lookupChange(i);
142                         if (moveItem(pars[par], *tmp, bparams, i, j - pos)) {
143                                 tmp->setChange(j - pos, change);
144                                 ++j;
145                         }
146                 }
147
148                 for (i = pos_end; i >= pos; --i)
149                         pars[par].eraseIntern(i);
150         }
151
152         if (pos)
153                 return;
154
155         pars[par].params().clear();
156
157         pars[par].layout(bparams.getLyXTextClass().defaultLayout());
158
159         // layout stays the same with latex-environments
160         if (flag) {
161                 pars[par].layout(tmp->layout());
162                 pars[par].setLabelWidthString(tmp->params().labelWidthString());
163                 pars[par].params().depth(tmp->params().depth());
164         }
165
166         // subtle, but needed to get empty pars working right
167         if (bparams.tracking_changes) {
168                 if (!pars[par].size()) {
169                         pars[par].cleanChanges();
170                 } else if (!tmp->size()) {
171                         tmp->cleanChanges();
172                 }
173         }
174 }
175
176
177 void breakParagraphConservative(BufferParams const & bparams,
178         ParagraphList & pars, par_type par, pos_type pos)
179 {
180         // create a new paragraph
181         Paragraph & tmp = *pars.insert(pars.begin() + par + 1, Paragraph());
182         tmp.makeSameLayout(pars[par]);
183
184         // When can pos > size()?
185         // I guess pos == size() is possible.
186         if (pars[par].size() > pos) {
187                 // copy everything behind the break-position to the new
188                 // paragraph
189                 pos_type pos_end = pars[par].size() - 1;
190
191                 for (pos_type i = pos, j = pos; i <= pos_end; ++i)
192                         if (moveItem(pars[par], tmp, bparams, i, j - pos))
193                                 ++j;
194
195                 for (pos_type k = pos_end; k >= pos; --k)
196                         pars[par].erase(k);
197         }
198 }
199
200
201 void mergeParagraph(BufferParams const & bparams,
202         ParagraphList & pars, par_type par)
203 {
204         Paragraph & next = pars[par + 1];
205
206         pos_type pos_end = next.size() - 1;
207         pos_type pos_insert = pars[par].size();
208
209         // ok, now copy the paragraph
210         for (pos_type i = 0, j = 0; i <= pos_end; ++i)
211                 if (moveItem(next, pars[par], bparams, i, pos_insert + j))
212                         ++j;
213
214         pars.erase(pars.begin() + par + 1);
215 }
216
217
218 par_type depthHook(par_type pit,
219         ParagraphList const & pars, Paragraph::depth_type depth)
220 {
221         par_type newpit = pit;
222
223         if (newpit != 0)
224                 --newpit;
225
226         while (newpit != 0 && pars[newpit].getDepth() > depth)
227                 --newpit;
228
229         if (pars[newpit].getDepth() > depth)
230                 return pit;
231
232         return newpit;
233 }
234
235
236 par_type outerHook(par_type par, ParagraphList const & pars)
237 {
238         if (pars[par].getDepth() == 0)
239                 return pars.size();
240         return depthHook(par, pars, Paragraph::depth_type(pars[par].getDepth() - 1));
241 }
242
243
244 bool isFirstInSequence(par_type pit, ParagraphList const & pars)
245 {
246         par_type dhook = depthHook(pit, pars, pars[pit].getDepth());
247         return dhook == pit
248                 || pars[dhook].layout() != pars[pit].layout()
249                 || pars[dhook].getDepth() != pars[pit].getDepth();
250 }
251
252
253 int getEndLabel(par_type p, ParagraphList const & pars)
254 {
255         par_type pit = p;
256         Paragraph::depth_type par_depth = pars[p].getDepth();
257         while (pit != par_type(pars.size())) {
258                 LyXLayout_ptr const & layout = pars[pit].layout();
259                 int const endlabeltype = layout->endlabeltype;
260
261                 if (endlabeltype != END_LABEL_NO_LABEL) {
262                         if (p + 1 == par_type(pars.size()))
263                                 return endlabeltype;
264
265                         Paragraph::depth_type const next_depth =
266                                 pars[p + 1].getDepth();
267                         if (par_depth > next_depth ||
268                             (par_depth == next_depth && layout != pars[p + 1].layout()))
269                                 return endlabeltype;
270                         break;
271                 }
272                 if (par_depth == 0)
273                         break;
274                 pit = outerHook(pit, pars);
275                 if (pit != par_type(pars.size()))
276                         par_depth = pars[pit].getDepth();
277         }
278         return END_LABEL_NO_LABEL;
279 }
280
281
282 LyXFont const outerFont(par_type pit, ParagraphList const & pars)
283 {
284         Paragraph::depth_type par_depth = pars[pit].getDepth();
285         LyXFont tmpfont(LyXFont::ALL_INHERIT);
286
287         // Resolve against environment font information
288         while (pit != par_type(pars.size())
289                && par_depth
290                && !tmpfont.resolved()) {
291                 pit = outerHook(pit, pars);
292                 if (pit != par_type(pars.size())) {
293                         tmpfont.realize(pars[pit].layout()->font);
294                         par_depth = pars[pit].getDepth();
295                 }
296         }
297
298         return tmpfont;
299 }
300
301
302 par_type outerPar(Buffer const & buf, InsetBase const * inset)
303 {
304         ParIterator pit = const_cast<Buffer &>(buf).par_iterator_begin();
305         ParIterator end = const_cast<Buffer &>(buf).par_iterator_end();
306         for ( ; pit != end; ++pit) {
307                 LyXText * text;
308                 // the second '=' below is intentional
309                 for (int i = 0; (text = inset->getText(i)); ++i)
310                         if (&text->paragraphs() == &pit.plist())
311                                 return pit.outerPar();
312
313                 InsetList::iterator ii = pit->insetlist.begin();
314                 InsetList::iterator iend = pit->insetlist.end();
315                 for ( ; ii != iend; ++ii)
316                         if (ii->inset == inset)
317                                 return pit.outerPar();
318         }
319         lyxerr << "outerPar: should not happen" << endl;
320         BOOST_ASSERT(false);
321         return buf.paragraphs().size(); // shut up compiler
322 }
323
324
325 Paragraph const & ownerPar(Buffer const & buf, InsetBase const * inset)
326 {
327         ParConstIterator pit = buf.par_iterator_begin();
328         ParConstIterator end = buf.par_iterator_end();
329         for ( ; pit != end; ++pit) {
330                 LyXText * text;
331                 // the second '=' below is intentional
332                 for (int i = 0; (text = inset->getText(i)); ++i)
333                         if (&text->paragraphs() == &pit.plist())
334                                 return *pit;
335
336                 InsetList::const_iterator ii = pit->insetlist.begin();
337                 InsetList::const_iterator iend = pit->insetlist.end();
338                 for ( ; ii != iend; ++ii)
339                         if (ii->inset == inset)
340                                 return *pit;
341         }
342         lyxerr << "ownerPar: should not happen" << endl;
343         BOOST_ASSERT(false);
344         return buf.paragraphs().front(); // shut up compiler
345 }
346
347
348 /// return the range of pars [beg, end[ owning the range of y [ystart, yend] 
349 void getParsInRange(ParagraphList & pars, int ystart, int yend,
350         par_type & beg, par_type & end)
351 {
352         BOOST_ASSERT(!pars.empty());
353         par_type const endpar = pars.size();
354         par_type const begpar = 0;
355
356         for (beg = endpar - 1; beg != begpar && pars[beg].y > ystart; --beg)
357                 ;
358
359         for (end = beg ; end != endpar && pars[end].y <= yend; ++end)
360                 ;
361 }