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