]> git.lyx.org Git - lyx.git/blob - src/paragraph_funcs.C
- Link against qt-mt333.lib which is what the current qt3 cvs produces
[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 {
63         Paragraph::value_type 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, pit_type par_offset, 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_offset + 1, Paragraph());
97
98         Paragraph & par = pars[par_offset];
99
100         // we will invalidate the row cache
101         par.rows().clear();
102
103         // without doing that we get a crash when typing <Return> at the
104         // end of a paragraph
105         tmp->layout(bparams.getLyXTextClass().defaultLayout());
106         // remember to set the inset_owner
107         tmp->setInsetOwner(par.inInset());
108
109         if (bparams.tracking_changes)
110                 tmp->trackChanges();
111
112         // this is an idea for a more userfriendly layout handling, I will
113         // see what the users say
114
115         // layout stays the same with latex-environments
116         if (flag) {
117                 tmp->layout(par.layout());
118                 tmp->setLabelWidthString(par.params().labelWidthString());
119         }
120
121         bool const isempty = (par.allowEmpty() && par.empty());
122
123         if (!isempty && (par.size() > pos || par.empty() || flag == 2)) {
124                 tmp->layout(par.layout());
125                 tmp->params().align(par.params().align());
126                 tmp->setLabelWidthString(par.params().labelWidthString());
127
128                 tmp->params().depth(par.params().depth());
129                 tmp->params().noindent(par.params().noindent());
130
131                 // copy everything behind the break-position
132                 // to the new paragraph
133
134                 /* Note: if !keepempty, empty() == true, then we reach
135                  * here with size() == 0. So pos_end becomes - 1. This
136                  * doesn't cause problems because both loops below
137                  * enforce pos <= pos_end and 0 <= pos
138                  */
139                 pos_type pos_end = par.size() - 1;
140
141                 for (pos_type i = pos, j = pos; i <= pos_end; ++i) {
142                         Change::Type change = par.lookupChange(i);
143                         if (moveItem(par, *tmp, bparams, i, j - pos)) {
144                                 tmp->setChange(j - pos, change);
145                                 ++j;
146                         }
147                 }
148
149                 for (pos_type i = pos_end; i >= pos; --i)
150                         par.eraseIntern(i);
151         }
152
153         if (pos) {
154                 // Make sure that we keep the language when
155                 // breaking paragrpah.
156                 if (tmp->empty()) {
157                         LyXFont changed = tmp->getFirstFontSettings();
158                         LyXFont old = par.getFontSettings(bparams, par.size());
159                         changed.setLanguage(old.language());
160                         tmp->setFont(0, changed);
161                 }
162
163                 return;
164         }
165
166         par.params().clear();
167
168         par.layout(bparams.getLyXTextClass().defaultLayout());
169
170         // layout stays the same with latex-environments
171         if (flag) {
172                 par.layout(tmp->layout());
173                 par.setLabelWidthString(tmp->params().labelWidthString());
174                 par.params().depth(tmp->params().depth());
175         }
176
177         // subtle, but needed to get empty pars working right
178         if (bparams.tracking_changes) {
179                 if (!par.size()) {
180                         par.cleanChanges();
181                 } else if (!tmp->size()) {
182                         tmp->cleanChanges();
183                 }
184         }
185 }
186
187
188 void breakParagraphConservative(BufferParams const & bparams,
189         ParagraphList & pars, pit_type par_offset, pos_type pos)
190 {
191         // create a new paragraph
192         Paragraph & tmp = *pars.insert(pars.begin() + par_offset + 1, Paragraph());
193         Paragraph & par = pars[par_offset];
194
195         tmp.makeSameLayout(par);
196
197         // When can pos > size()?
198         // I guess pos == size() is possible.
199         if (par.size() > pos) {
200                 // copy everything behind the break-position to the new
201                 // paragraph
202                 pos_type pos_end = par.size() - 1;
203
204                 for (pos_type i = pos, j = pos; i <= pos_end; ++i)
205                         if (moveItem(par, tmp, bparams, i, j - pos))
206                                 ++j;
207
208                 for (pos_type k = pos_end; k >= pos; --k)
209                         par.erase(k);
210         }
211 }
212
213
214 void mergeParagraph(BufferParams const & bparams,
215         ParagraphList & pars, pit_type par_offset)
216 {
217         Paragraph & next = pars[par_offset + 1];
218         Paragraph & par = pars[par_offset];
219
220         pos_type pos_end = next.size() - 1;
221         pos_type pos_insert = par.size();
222
223         // ok, now copy the paragraph
224         for (pos_type i = 0, j = 0; i <= pos_end; ++i)
225                 if (moveItem(next, par, bparams, i, pos_insert + j))
226                         ++j;
227
228         pars.erase(pars.begin() + par_offset + 1);
229 }
230
231
232 pit_type depthHook(pit_type pit,
233         ParagraphList const & pars, Paragraph::depth_type depth)
234 {
235         pit_type newpit = pit;
236
237         if (newpit != 0)
238                 --newpit;
239
240         while (newpit != 0 && pars[newpit].getDepth() > depth)
241                 --newpit;
242
243         if (pars[newpit].getDepth() > depth)
244                 return pit;
245
246         return newpit;
247 }
248
249
250 pit_type outerHook(pit_type par_offset, ParagraphList const & pars)
251 {
252         Paragraph const & par = pars[par_offset];
253
254         if (par.getDepth() == 0)
255                 return pars.size();
256         return depthHook(par_offset, pars, Paragraph::depth_type(par.getDepth() - 1));
257 }
258
259
260 bool isFirstInSequence(pit_type par_offset, ParagraphList const & pars)
261 {
262         Paragraph const & par = pars[par_offset];
263
264         pit_type dhook_offset = depthHook(par_offset, pars, par.getDepth());
265
266         Paragraph const & dhook = pars[dhook_offset];
267
268         return dhook_offset == par_offset
269                 || dhook.layout() != par.layout()
270                 || dhook.getDepth() != par.getDepth();
271 }
272
273
274 int getEndLabel(pit_type p, ParagraphList const & pars)
275 {
276         pit_type pit = p;
277         Paragraph::depth_type par_depth = pars[p].getDepth();
278         while (pit != pit_type(pars.size())) {
279                 LyXLayout_ptr const & layout = pars[pit].layout();
280                 int const endlabeltype = layout->endlabeltype;
281
282                 if (endlabeltype != END_LABEL_NO_LABEL) {
283                         if (p + 1 == pit_type(pars.size()))
284                                 return endlabeltype;
285
286                         Paragraph::depth_type const next_depth =
287                                 pars[p + 1].getDepth();
288                         if (par_depth > next_depth ||
289                             (par_depth == next_depth && layout != pars[p + 1].layout()))
290                                 return endlabeltype;
291                         break;
292                 }
293                 if (par_depth == 0)
294                         break;
295                 pit = outerHook(pit, pars);
296                 if (pit != pit_type(pars.size()))
297                         par_depth = pars[pit].getDepth();
298         }
299         return END_LABEL_NO_LABEL;
300 }
301
302
303 LyXFont const outerFont(pit_type par_offset, ParagraphList const & pars)
304 {
305         Paragraph::depth_type par_depth = pars[par_offset].getDepth();
306         LyXFont tmpfont(LyXFont::ALL_INHERIT);
307
308         // Resolve against environment font information
309         while (par_offset != pit_type(pars.size())
310                && par_depth
311                && !tmpfont.resolved()) {
312                 par_offset = outerHook(par_offset, pars);
313                 if (par_offset != pit_type(pars.size())) {
314                         tmpfont.realize(pars[par_offset].layout()->font);
315                         par_depth = pars[par_offset].getDepth();
316                 }
317         }
318
319         return tmpfont;
320 }
321
322
323 /// return the number of InsetOptArg in a paragraph
324 int numberOfOptArgs(Paragraph const & par)
325 {
326         int num = 0;
327
328         InsetList::const_iterator it = par.insetlist.begin();
329         InsetList::const_iterator end = par.insetlist.end();
330         for (; it != end ; ++it) {
331                 if (it->inset->lyxCode() == InsetBase::OPTARG_CODE)
332                         ++num;
333         }
334         return num;
335 }