]> git.lyx.org Git - lyx.git/blob - src/paragraph_funcs.C
24da4d5e9908612819b1cbe08bd5446b651c92c2
[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                 }
72
73                 if (!to.insetAllowed(tmpinset->lyxCode()))
74                         return false;
75                 to.insertInset(j, tmpinset, tmpfont);
76         } else {
77                 if (!to.checkInsertChar(tmpfont))
78                         return false;
79                 to.insertChar(j, tmpchar, tmpfont);
80         }
81         return true;
82 }
83
84 }
85
86
87 void breakParagraph(BufferParams const & bparams,
88         ParagraphList & pars, par_type par, pos_type pos, int flag)
89 {
90         // create a new paragraph, and insert into the list
91         ParagraphList::iterator tmp =
92                 pars.insert(pars.begin() + par + 1, Paragraph());
93
94         // without doing that we get a crash when typing <Return> at the
95         // end of a paragraph
96         tmp->layout(bparams.getLyXTextClass().defaultLayout());
97         // remember to set the inset_owner
98         tmp->setInsetOwner(pars[par].inInset());
99
100         if (bparams.tracking_changes)
101                 tmp->trackChanges();
102
103         // this is an idea for a more userfriendly layout handling, I will
104         // see what the users say
105
106         // layout stays the same with latex-environments
107         if (flag) {
108                 tmp->layout(pars[par].layout());
109                 tmp->setLabelWidthString(pars[par].params().labelWidthString());
110         }
111
112         bool const isempty = (pars[par].allowEmpty() && pars[par].empty());
113
114         if (!isempty && (pars[par].size() > pos || pars[par].empty() || flag == 2)) {
115                 tmp->layout(pars[par].layout());
116                 tmp->params().align(pars[par].params().align());
117                 tmp->setLabelWidthString(pars[par].params().labelWidthString());
118
119                 tmp->params().depth(pars[par].params().depth());
120                 tmp->params().noindent(pars[par].params().noindent());
121
122                 // copy everything behind the break-position
123                 // to the new paragraph
124
125 #ifdef WITH_WARNINGS
126 #warning this seems wrong
127 #endif
128                 /* FIXME: if !keepempty, empty() == true, then we reach
129                  * here with size() == 0. So pos_end becomes - 1. Why
130                  * doesn't this cause problems ???
131                  */
132                 pos_type pos_end = pars[par].size() - 1;
133                 pos_type i = pos;
134                 pos_type j = pos;
135
136                 for (; 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 (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 != 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 == 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 != 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 != pars.size() && par_depth && !tmpfont.resolved()) {
285                 pit = outerHook(pit, pars);
286                 if (pit != pars.size()) {
287                         tmpfont.realize(pars[pit].layout()->font);
288                         par_depth = pars[pit].getDepth();
289                 }
290         }
291
292         return tmpfont;
293 }
294
295
296 par_type outerPar(Buffer const & buf, InsetBase const * inset)
297 {
298         ParIterator pit = const_cast<Buffer &>(buf).par_iterator_begin();
299         ParIterator end = const_cast<Buffer &>(buf).par_iterator_end();
300         for ( ; pit != end; ++pit) {
301                 LyXText * text;
302                 // the second '=' below is intentional
303                 for (int i = 0; (text = inset->getText(i)); ++i)
304                         if (&text->paragraphs() == &pit.plist())
305                                 return pit.outerPar();
306
307                 InsetList::iterator ii = pit->insetlist.begin();
308                 InsetList::iterator iend = pit->insetlist.end();
309                 for ( ; ii != iend; ++ii)
310                         if (ii->inset == inset)
311                                 return pit.outerPar();
312         }
313         lyxerr << "outerPar: should not happen" << endl;
314         BOOST_ASSERT(false);
315         return buf.paragraphs().size(); // shut up compiler
316 }
317
318
319 Paragraph const & ownerPar(Buffer const & buf, InsetBase const * inset)
320 {
321         ParConstIterator pit = buf.par_iterator_begin();
322         ParConstIterator end = buf.par_iterator_end();
323         for ( ; pit != end; ++pit) {
324                 LyXText * text;
325                 // the second '=' below is intentional
326                 for (int i = 0; (text = inset->getText(i)); ++i)
327                         if (&text->paragraphs() == &pit.plist())
328                                 return *pit;
329
330                 InsetList::const_iterator ii = pit->insetlist.begin();
331                 InsetList::const_iterator iend = pit->insetlist.end();
332                 for ( ; ii != iend; ++ii)
333                         if (ii->inset == inset)
334                                 return *pit;
335         }
336         lyxerr << "ownerPar: should not happen" << endl;
337         BOOST_ASSERT(false);
338         return buf.paragraphs().front(); // shut up compiler
339 }
340
341
342 /// return the range of pars [beg, end[ owning the range of y [ystart, yend] 
343 void getParsInRange(ParagraphList & pars, int ystart, int yend,
344         par_type & beg, par_type & end)
345 {
346         BOOST_ASSERT(!pars.empty());
347         par_type const endpar = pars.size();
348         par_type const begpar = 0;
349
350         for (beg = endpar - 1; beg != begpar && pars[beg].y > ystart; --beg)
351                 ;
352
353         for (end = beg ; end != endpar && pars[end].y <= yend; ++end)
354                 ;
355 }