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