]> git.lyx.org Git - lyx.git/blob - src/paragraph_funcs.C
clone NVI, other small stuff
[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::bformat;
42 using lyx::support::compare_ascii_no_case;
43 using lyx::support::compare_no_case;
44 using lyx::support::contains;
45 using lyx::support::split;
46 using lyx::support::subst;
47
48 using std::auto_ptr;
49 using std::endl;
50 using std::string;
51 using std::vector;
52 using std::istringstream;
53 using std::ostream;
54 using std::pair;
55
56
57 namespace {
58
59 bool moveItem(Paragraph & from, Paragraph & to,
60         BufferParams const & params, pos_type i, pos_type j)
61 {
62         Paragraph::value_type const tmpchar = from.getChar(i);
63         LyXFont tmpfont = from.getFontSettings(params, i);
64
65         if (tmpchar == Paragraph::META_INSET) {
66                 InsetBase * tmpinset = 0;
67                 if (from.getInset(i)) {
68                         // the inset is not in a paragraph anymore
69                         tmpinset = from.insetlist.release(i);
70                         from.insetlist.erase(i);
71                 }
72
73                 if (!to.insetAllowed(tmpinset->lyxCode())) {
74                         delete tmpinset;
75                         return false;
76                 }
77                 if (tmpinset)
78                         to.insertInset(j, tmpinset, tmpfont);
79         } else {
80                 if (!to.checkInsertChar(tmpfont))
81                         return false;
82                 to.insertChar(j, tmpchar, tmpfont);
83         }
84         return true;
85 }
86
87 }
88
89
90 void breakParagraph(BufferParams const & bparams,
91         ParagraphList & pars, par_type par_offset, pos_type pos, int flag)
92 {
93         // create a new paragraph, and insert into the list
94         ParagraphList::iterator tmp =
95                 pars.insert(pars.begin() + par_offset + 1, Paragraph());
96
97         Paragraph & par = pars[par_offset];
98
99         // without doing that we get a crash when typing <Return> at the
100         // end of a paragraph
101         tmp->layout(bparams.getLyXTextClass().defaultLayout());
102         // remember to set the inset_owner
103         tmp->setInsetOwner(par.inInset());
104
105         if (bparams.tracking_changes)
106                 tmp->trackChanges();
107
108         // this is an idea for a more userfriendly layout handling, I will
109         // see what the users say
110
111         // layout stays the same with latex-environments
112         if (flag) {
113                 tmp->layout(par.layout());
114                 tmp->setLabelWidthString(par.params().labelWidthString());
115         }
116
117         bool const isempty = (par.allowEmpty() && par.empty());
118
119         if (!isempty && (par.size() > pos || par.empty() || flag == 2)) {
120                 tmp->layout(par.layout());
121                 tmp->params().align(par.params().align());
122                 tmp->setLabelWidthString(par.params().labelWidthString());
123
124                 tmp->params().depth(par.params().depth());
125                 tmp->params().noindent(par.params().noindent());
126
127                 // copy everything behind the break-position
128                 // to the new paragraph
129
130                 /* Note: if !keepempty, empty() == true, then we reach
131                  * here with size() == 0. So pos_end becomes - 1. This
132                  * doesn't cause problems because both loops below
133                  * enforce pos <= pos_end and 0 <= pos
134                  */
135                 pos_type pos_end = par.size() - 1;
136
137                 for (pos_type i = pos, j = pos; i <= pos_end; ++i) {
138                         Change::Type change = par.lookupChange(i);
139                         if (moveItem(par, *tmp, bparams, i, j - pos)) {
140                                 tmp->setChange(j - pos, change);
141                                 ++j;
142                         }
143                 }
144
145                 for (pos_type i = pos_end; i >= pos; --i)
146                         par.eraseIntern(i);
147         }
148
149         if (pos) {
150                 // Make sure that we keep the language when
151                 // breaking paragrpah.
152                 if (tmp->empty()) {
153                         LyXFont changed = tmp->getFirstFontSettings();
154                         LyXFont old = par.getFontSettings(bparams, par.size());
155                         changed.setLanguage(old.language());
156                         tmp->setFont(0, changed);
157                 }
158
159                 return;
160         }
161
162         par.params().clear();
163
164         par.layout(bparams.getLyXTextClass().defaultLayout());
165
166         // layout stays the same with latex-environments
167         if (flag) {
168                 par.layout(tmp->layout());
169                 par.setLabelWidthString(tmp->params().labelWidthString());
170                 par.params().depth(tmp->params().depth());
171         }
172
173         // subtle, but needed to get empty pars working right
174         if (bparams.tracking_changes) {
175                 if (!par.size()) {
176                         par.cleanChanges();
177                 } else if (!tmp->size()) {
178                         tmp->cleanChanges();
179                 }
180         }
181 }
182
183
184 void breakParagraphConservative(BufferParams const & bparams,
185         ParagraphList & pars, par_type par_offset, pos_type pos)
186 {
187         // create a new paragraph
188         Paragraph & tmp = *pars.insert(pars.begin() + par_offset + 1, Paragraph());
189         Paragraph & par = pars[par_offset];
190
191         tmp.makeSameLayout(par);
192
193         // When can pos > size()?
194         // I guess pos == size() is possible.
195         if (par.size() > pos) {
196                 // copy everything behind the break-position to the new
197                 // paragraph
198                 pos_type pos_end = par.size() - 1;
199
200                 for (pos_type i = pos, j = pos; i <= pos_end; ++i)
201                         if (moveItem(par, tmp, bparams, i, j - pos))
202                                 ++j;
203
204                 for (pos_type k = pos_end; k >= pos; --k)
205                         par.erase(k);
206         }
207 }
208
209
210 void mergeParagraph(BufferParams const & bparams,
211         ParagraphList & pars, par_type par_offset)
212 {
213         Paragraph & next = pars[par_offset + 1];
214         Paragraph & par = pars[par_offset];
215
216         pos_type pos_end = next.size() - 1;
217         pos_type pos_insert = par.size();
218
219         // ok, now copy the paragraph
220         for (pos_type i = 0, j = 0; i <= pos_end; ++i)
221                 if (moveItem(next, par, bparams, i, pos_insert + j))
222                         ++j;
223
224         pars.erase(pars.begin() + par_offset + 1);
225 }
226
227
228 par_type depthHook(par_type pit,
229         ParagraphList const & pars, Paragraph::depth_type depth)
230 {
231         par_type newpit = pit;
232
233         if (newpit != 0)
234                 --newpit;
235
236         while (newpit != 0 && pars[newpit].getDepth() > depth)
237                 --newpit;
238
239         if (pars[newpit].getDepth() > depth)
240                 return pit;
241
242         return newpit;
243 }
244
245
246 par_type outerHook(par_type par_offset, ParagraphList const & pars)
247 {
248         Paragraph const & par = pars[par_offset];
249
250         if (par.getDepth() == 0)
251                 return pars.size();
252         return depthHook(par_offset, pars, Paragraph::depth_type(par.getDepth() - 1));
253 }
254
255
256 bool isFirstInSequence(par_type par_offset, ParagraphList const & pars)
257 {
258         Paragraph const & par = pars[par_offset];
259
260         par_type dhook_offset = depthHook(par_offset, pars, par.getDepth());
261
262         Paragraph const & dhook = pars[dhook_offset];
263
264         return dhook_offset == par_offset
265                 || dhook.layout() != par.layout()
266                 || dhook.getDepth() != par.getDepth();
267 }
268
269
270 int getEndLabel(par_type p, ParagraphList const & pars)
271 {
272         par_type pit = p;
273         Paragraph::depth_type par_depth = pars[p].getDepth();
274         while (pit != par_type(pars.size())) {
275                 LyXLayout_ptr const & layout = pars[pit].layout();
276                 int const endlabeltype = layout->endlabeltype;
277
278                 if (endlabeltype != END_LABEL_NO_LABEL) {
279                         if (p + 1 == par_type(pars.size()))
280                                 return endlabeltype;
281
282                         Paragraph::depth_type const next_depth =
283                                 pars[p + 1].getDepth();
284                         if (par_depth > next_depth ||
285                             (par_depth == next_depth && layout != pars[p + 1].layout()))
286                                 return endlabeltype;
287                         break;
288                 }
289                 if (par_depth == 0)
290                         break;
291                 pit = outerHook(pit, pars);
292                 if (pit != par_type(pars.size()))
293                         par_depth = pars[pit].getDepth();
294         }
295         return END_LABEL_NO_LABEL;
296 }
297
298
299 LyXFont const outerFont(par_type par_offset, ParagraphList const & pars)
300 {
301         Paragraph::depth_type par_depth = pars[par_offset].getDepth();
302         LyXFont tmpfont(LyXFont::ALL_INHERIT);
303
304         // Resolve against environment font information
305         while (par_offset != par_type(pars.size())
306                && par_depth
307                && !tmpfont.resolved()) {
308                 par_offset = outerHook(par_offset, pars);
309                 if (par_offset != par_type(pars.size())) {
310                         tmpfont.realize(pars[par_offset].layout()->font);
311                         par_depth = pars[par_offset].getDepth();
312                 }
313         }
314
315         return tmpfont;
316 }
317
318
319 par_type outerPar(Buffer const & buf, InsetBase const * inset)
320 {
321         ParIterator pit = const_cast<Buffer &>(buf).par_iterator_begin();
322         ParIterator end = const_cast<Buffer &>(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.outerPar();
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.outerPar();
335         }
336         lyxerr << "outerPar: should not happen" << endl;
337         BOOST_ASSERT(false);
338         return buf.paragraphs().size(); // 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 }
356
357
358 /// return the number of InsetOptArg in a paragraph
359 int numberOfOptArgs(Paragraph const & par)
360 {
361         int num = 0;
362         
363         InsetList::const_iterator it = par.insetlist.begin();
364         InsetList::const_iterator end = par.insetlist.end();
365         for (; it != end ; ++it) {
366                 if (it->inset->lyxCode() == InsetBase::OPTARG_CODE) 
367                         ++num;
368         }
369         return num;
370 }
371
372