]> git.lyx.org Git - lyx.git/blob - src/paragraph_funcs.C
change "support/std_sstream.h" to <sstream>
[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 "lyxtext.h"
23 #include "outputparams.h"
24 #include "paragraph_pimpl.h"
25 #include "pariterator.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
34 #include <sstream>
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                 /* Note: if !keepempty, empty() == true, then we reach
130                  * here with size() == 0. So pos_end becomes - 1. This
131                  * doesn't cause problems because both loops below
132                  * enforce pos <= pos_end and 0 <= pos
133                  */
134                 pos_type pos_end = pars[par].size() - 1;
135
136                 for (pos_type i = pos, j = pos; i <= pos_end; ++i) {
137                         Change::Type change = pars[par].lookupChange(i);
138                         if (moveItem(pars[par], *tmp, bparams, i, j - pos)) {
139                                 tmp->setChange(j - pos, change);
140                                 ++j;
141                         }
142                 }
143
144                 for (pos_type i = pos_end; i >= pos; --i)
145                         pars[par].eraseIntern(i);
146         }
147
148         if (pos)
149                 return;
150
151         pars[par].params().clear();
152
153         pars[par].layout(bparams.getLyXTextClass().defaultLayout());
154
155         // layout stays the same with latex-environments
156         if (flag) {
157                 pars[par].layout(tmp->layout());
158                 pars[par].setLabelWidthString(tmp->params().labelWidthString());
159                 pars[par].params().depth(tmp->params().depth());
160         }
161
162         // subtle, but needed to get empty pars working right
163         if (bparams.tracking_changes) {
164                 if (!pars[par].size()) {
165                         pars[par].cleanChanges();
166                 } else if (!tmp->size()) {
167                         tmp->cleanChanges();
168                 }
169         }
170 }
171
172
173 void breakParagraphConservative(BufferParams const & bparams,
174         ParagraphList & pars, par_type par, pos_type pos)
175 {
176         // create a new paragraph
177         Paragraph & tmp = *pars.insert(pars.begin() + par + 1, Paragraph());
178         tmp.makeSameLayout(pars[par]);
179
180         // When can pos > size()?
181         // I guess pos == size() is possible.
182         if (pars[par].size() > pos) {
183                 // copy everything behind the break-position to the new
184                 // paragraph
185                 pos_type pos_end = pars[par].size() - 1;
186
187                 for (pos_type i = pos, j = pos; i <= pos_end; ++i)
188                         if (moveItem(pars[par], tmp, bparams, i, j - pos))
189                                 ++j;
190
191                 for (pos_type k = pos_end; k >= pos; --k)
192                         pars[par].erase(k);
193         }
194 }
195
196
197 void mergeParagraph(BufferParams const & bparams,
198         ParagraphList & pars, par_type par)
199 {
200         Paragraph & next = pars[par + 1];
201
202         pos_type pos_end = next.size() - 1;
203         pos_type pos_insert = pars[par].size();
204
205         // ok, now copy the paragraph
206         for (pos_type i = 0, j = 0; i <= pos_end; ++i)
207                 if (moveItem(next, pars[par], bparams, i, pos_insert + j))
208                         ++j;
209
210         pars.erase(pars.begin() + par + 1);
211 }
212
213
214 par_type depthHook(par_type pit,
215         ParagraphList const & pars, Paragraph::depth_type depth)
216 {
217         par_type newpit = pit;
218
219         if (newpit != 0)
220                 --newpit;
221
222         while (newpit != 0 && pars[newpit].getDepth() > depth)
223                 --newpit;
224
225         if (pars[newpit].getDepth() > depth)
226                 return pit;
227
228         return newpit;
229 }
230
231
232 par_type outerHook(par_type par, ParagraphList const & pars)
233 {
234         if (pars[par].getDepth() == 0)
235                 return pars.size();
236         return depthHook(par, pars, Paragraph::depth_type(pars[par].getDepth() - 1));
237 }
238
239
240 bool isFirstInSequence(par_type pit, ParagraphList const & pars)
241 {
242         par_type dhook = depthHook(pit, pars, pars[pit].getDepth());
243         return dhook == pit
244                 || pars[dhook].layout() != pars[pit].layout()
245                 || pars[dhook].getDepth() != pars[pit].getDepth();
246 }
247
248
249 int getEndLabel(par_type p, ParagraphList const & pars)
250 {
251         par_type pit = p;
252         Paragraph::depth_type par_depth = pars[p].getDepth();
253         while (pit != par_type(pars.size())) {
254                 LyXLayout_ptr const & layout = pars[pit].layout();
255                 int const endlabeltype = layout->endlabeltype;
256
257                 if (endlabeltype != END_LABEL_NO_LABEL) {
258                         if (p + 1 == par_type(pars.size()))
259                                 return endlabeltype;
260
261                         Paragraph::depth_type const next_depth =
262                                 pars[p + 1].getDepth();
263                         if (par_depth > next_depth ||
264                             (par_depth == next_depth && layout != pars[p + 1].layout()))
265                                 return endlabeltype;
266                         break;
267                 }
268                 if (par_depth == 0)
269                         break;
270                 pit = outerHook(pit, pars);
271                 if (pit != par_type(pars.size()))
272                         par_depth = pars[pit].getDepth();
273         }
274         return END_LABEL_NO_LABEL;
275 }
276
277
278 LyXFont const outerFont(par_type pit, ParagraphList const & pars)
279 {
280         Paragraph::depth_type par_depth = pars[pit].getDepth();
281         LyXFont tmpfont(LyXFont::ALL_INHERIT);
282
283         // Resolve against environment font information
284         while (pit != par_type(pars.size())
285                && par_depth
286                && !tmpfont.resolved()) {
287                 pit = outerHook(pit, pars);
288                 if (pit != par_type(pars.size())) {
289                         tmpfont.realize(pars[pit].layout()->font);
290                         par_depth = pars[pit].getDepth();
291                 }
292         }
293
294         return tmpfont;
295 }
296
297
298 par_type outerPar(Buffer const & buf, InsetBase const * inset)
299 {
300         ParIterator pit = const_cast<Buffer &>(buf).par_iterator_begin();
301         ParIterator end = const_cast<Buffer &>(buf).par_iterator_end();
302         for ( ; pit != end; ++pit) {
303                 LyXText * text;
304                 // the second '=' below is intentional
305                 for (int i = 0; (text = inset->getText(i)); ++i)
306                         if (&text->paragraphs() == &pit.plist())
307                                 return pit.outerPar();
308
309                 InsetList::const_iterator ii = pit->insetlist.begin();
310                 InsetList::const_iterator iend = pit->insetlist.end();
311                 for ( ; ii != iend; ++ii)
312                         if (ii->inset == inset)
313                                 return pit.outerPar();
314         }
315         lyxerr << "outerPar: should not happen" << endl;
316         BOOST_ASSERT(false);
317         return buf.paragraphs().size(); // shut up compiler
318 }
319
320
321 Paragraph const & ownerPar(Buffer const & buf, InsetBase const * inset)
322 {
323         ParConstIterator pit = buf.par_iterator_begin();
324         ParConstIterator end = buf.par_iterator_end();
325         for ( ; pit != end; ++pit) {
326                 LyXText * text;
327                 // the second '=' below is intentional
328                 for (int i = 0; (text = inset->getText(i)); ++i)
329                         if (&text->paragraphs() == &pit.plist())
330                                 return *pit;
331
332                 InsetList::const_iterator ii = pit->insetlist.begin();
333                 InsetList::const_iterator iend = pit->insetlist.end();
334                 for ( ; ii != iend; ++ii)
335                         if (ii->inset == inset)
336                                 return *pit;
337         }
338         lyxerr << "ownerPar: should not happen" << endl;
339         BOOST_ASSERT(false);
340         return buf.paragraphs().front(); // shut up compiler
341 }
342
343
344 /// return the range of pars [beg, end[ owning the range of y [ystart, yend]
345 void getParsInRange(ParagraphList & pars, int ystart, int yend,
346         par_type & beg, par_type & end)
347 {
348         BOOST_ASSERT(!pars.empty());
349         par_type const endpar = pars.size();
350         par_type const begpar = 0;
351
352         for (beg = endpar - 1; beg != begpar && pars[beg].y > ystart; --beg)
353                 ;
354
355         for (end = beg ; end != endpar && pars[end].y <= yend; ++end)
356                 ;
357 }