]> git.lyx.org Git - lyx.git/blob - src/paragraph.C
ccd07dab819bd54ea80bd506c54535dc27373e7a
[lyx.git] / src / paragraph.C
1 /**
2  * \file paragraph.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Asger Alstrup
7  * \author Lars Gullik Bjønnes
8  * \author Jean-Marc Lasgouttes
9  * \author Angus Leeming
10  * \author John Levon
11  * \author André Pönitz
12  * \author Dekel Tsur
13  * \author Jürgen Vigna
14  *
15  * Full author contact details are available in file CREDITS.
16  */
17
18 #include <config.h>
19
20 #include "paragraph.h"
21 #include "paragraph_pimpl.h"
22
23 #include "buffer.h"
24 #include "bufferparams.h"
25 #include "BufferView.h"
26 #include "changes.h"
27 #include "encoding.h"
28 #include "debug.h"
29 #include "gettext.h"
30 #include "language.h"
31 #include "latexrunparams.h"
32 #include "layout.h"
33 #include "lyxrc.h"
34 #include "paragraph_funcs.h"
35 #include "ParameterStruct.h"
36 #include "texrow.h"
37
38 #include "Lsstream.h"
39
40 #include "insets/insetbibitem.h"
41 #include "insets/insetoptarg.h"
42 #include "insets/insetenv.h"
43
44 #include "support/filetools.h"
45 #include "support/lstrings.h"
46 #include "support/lyxmanip.h"
47 #include "support/FileInfo.h"
48 #include "support/LAssert.h"
49 #include "support/textutils.h"
50
51 #include <algorithm>
52 #include <fstream>
53 #include <csignal>
54 #include <ctime>
55
56 using namespace lyx::support;
57
58 using std::ostream;
59 using std::endl;
60 using std::fstream;
61 using std::ios;
62 using std::lower_bound;
63 using std::upper_bound;
64
65 using lyx::pos_type;
66
67
68 Paragraph::Paragraph()
69         : y(0), pimpl_(new Paragraph::Pimpl(this))
70 {
71         enumdepth = 0;
72         itemdepth = 0;
73         params().clear();
74 }
75
76
77 Paragraph::Paragraph(Paragraph const & lp)
78         : y(0), pimpl_(new Paragraph::Pimpl(*lp.pimpl_, this))
79 {
80         enumdepth = 0;
81         itemdepth = 0;
82         // this is because of the dummy layout of the paragraphs that
83         // follow footnotes
84         layout_ = lp.layout();
85
86         // copy everything behind the break-position to the new paragraph
87         insetlist = lp.insetlist;
88         InsetList::iterator it = insetlist.begin();
89         InsetList::iterator end = insetlist.end();
90         for (; it != end; ++it) {
91                 // currently we hold Inset*, not InsetBase*
92                 it->inset = static_cast<InsetOld*>(it->inset->clone().release());
93         }
94 }
95
96
97 void Paragraph::operator=(Paragraph const & lp)
98 {
99         // needed as we will destroy the pimpl_ before copying it
100         if (&lp != this)
101                 return;
102
103         lyxerr << "Paragraph::operator=()" << endl;
104
105         delete pimpl_;
106         pimpl_ = new Pimpl(*lp.pimpl_, this);
107
108         enumdepth = lp.enumdepth;
109         itemdepth = lp.itemdepth;
110         // this is because of the dummy layout of the paragraphs that
111         // follow footnotes
112         layout_ = lp.layout();
113
114         // copy everything behind the break-position to the new paragraph
115         insetlist = lp.insetlist;
116         InsetList::iterator it = insetlist.begin();
117         InsetList::iterator end = insetlist.end();
118         for (; it != end; ++it) {
119                 it->inset = static_cast<InsetOld*>(it->inset->clone().release());
120         }
121 }
122
123 // the destructor removes the new paragraph from the list
124 Paragraph::~Paragraph()
125 {
126         delete pimpl_;
127         //
128         //lyxerr << "Paragraph::paragraph_id = "
129         //       << Paragraph::paragraph_id << endl;
130 }
131
132
133 void Paragraph::write(Buffer const & buf, ostream & os,
134                           BufferParams const & bparams,
135                           depth_type & dth) const
136 {
137         // The beginning or end of a deeper (i.e. nested) area?
138         if (dth != params().depth()) {
139                 if (params().depth() > dth) {
140                         while (params().depth() > dth) {
141                                 os << "\n\\begin_deeper ";
142                                 ++dth;
143                         }
144                 } else {
145                         while (params().depth() < dth) {
146                                 os << "\n\\end_deeper ";
147                                 --dth;
148                         }
149                 }
150         }
151
152         // First write the layout
153         os << "\n\\begin_layout " << layout()->name() << '\n';
154
155         params().write(os);
156
157         LyXFont font1(LyXFont::ALL_INHERIT, bparams.language);
158
159         Change running_change = Change(Change::UNCHANGED);
160         lyx::time_type const curtime(lyx::current_time());
161
162         int column = 0;
163         for (pos_type i = 0; i < size(); ++i) {
164                 if (!i) {
165                         os << '\n';
166                         column = 0;
167                 }
168
169                 Change change = pimpl_->lookupChangeFull(i);
170                 Changes::lyxMarkChange(os, column, curtime, running_change, change);
171                 running_change = change;
172
173                 // Write font changes
174                 LyXFont font2 = getFontSettings(bparams, i);
175                 if (font2 != font1) {
176                         font2.lyxWriteChanges(font1, os);
177                         column = 0;
178                         font1 = font2;
179                 }
180
181                 value_type const c = getChar(i);
182                 switch (c) {
183                 case META_INSET:
184                 {
185                         InsetOld const * inset = getInset(i);
186                         if (inset)
187                                 if (inset->directWrite()) {
188                                         // international char, let it write
189                                         // code directly so it's shorter in
190                                         // the file
191                                         inset->write(buf, os);
192                                 } else {
193                                         os << "\n\\begin_inset ";
194                                         inset->write(buf, os);
195                                         os << "\n\\end_inset \n\n";
196                                         column = 0;
197                                 }
198                 }
199                 break;
200                 case '\\':
201                         os << "\n\\backslash \n";
202                         column = 0;
203                         break;
204                 case '.':
205                         if (i + 1 < size() && getChar(i + 1) == ' ') {
206                                 os << ".\n";
207                                 column = 0;
208                         } else
209                                 os << '.';
210                         break;
211                 default:
212                         if ((column > 70 && c == ' ')
213                             || column > 79) {
214                                 os << '\n';
215                                 column = 0;
216                         }
217                         // this check is to amend a bug. LyX sometimes
218                         // inserts '\0' this could cause problems.
219                         if (c != '\0')
220                                 os << c;
221                         else
222                                 lyxerr << "ERROR (Paragraph::writeFile):"
223                                         " NULL char in structure." << endl;
224                         ++column;
225                         break;
226                 }
227         }
228
229         // to make reading work properly
230         if (!size()) {
231                 running_change = pimpl_->lookupChange(0);
232                 Changes::lyxMarkChange(os, column, curtime,
233                         Change(Change::UNCHANGED), running_change);
234         }
235         Changes::lyxMarkChange(os, column, curtime,
236                 running_change, Change(Change::UNCHANGED));
237
238         os << "\n\\end_layout\n";
239 }
240
241
242 void Paragraph::validate(LaTeXFeatures & features) const
243 {
244         pimpl_->validate(features, *layout());
245 }
246
247
248 void Paragraph::eraseIntern(lyx::pos_type pos)
249 {
250         pimpl_->eraseIntern(pos);
251 }
252
253
254 bool Paragraph::erase(pos_type pos)
255 {
256         return pimpl_->erase(pos);
257 }
258
259
260 int Paragraph::erase(pos_type start, pos_type end)
261 {
262         return pimpl_->erase(start, end);
263 }
264
265
266 bool Paragraph::checkInsertChar(LyXFont & font)
267 {
268         if (pimpl_->inset_owner)
269                 return pimpl_->inset_owner->checkInsertChar(font);
270         return true;
271 }
272
273
274 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c)
275 {
276         insertChar(pos, c, LyXFont(LyXFont::ALL_INHERIT));
277 }
278
279
280 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c,
281                            LyXFont const & font, Change change)
282 {
283         pimpl_->insertChar(pos, c, font, change);
284 }
285
286
287 void Paragraph::insertInset(pos_type pos, InsetOld * inset)
288 {
289         insertInset(pos, inset, LyXFont(LyXFont::ALL_INHERIT));
290 }
291
292
293 void Paragraph::insertInset(pos_type pos, InsetOld * inset,
294         LyXFont const & font, Change change)
295 {
296         pimpl_->insertInset(pos, inset, font, change);
297 }
298
299
300 bool Paragraph::insetAllowed(InsetOld::Code code)
301 {
302         //lyxerr << "Paragraph::InsertInsetAllowed" << endl;
303         if (pimpl_->inset_owner)
304                 return pimpl_->inset_owner->insetAllowed(code);
305         return true;
306 }
307
308
309 InsetOld * Paragraph::getInset(pos_type pos)
310 {
311         Assert(pos < size());
312         return insetlist.get(pos);
313 }
314
315
316 InsetOld const * Paragraph::getInset(pos_type pos) const
317 {
318         Assert(pos < size());
319         return insetlist.get(pos);
320 }
321
322
323 // Gets uninstantiated font setting at position.
324 LyXFont const Paragraph::getFontSettings(BufferParams const & bparams,
325                                          pos_type pos) const
326 {
327         Assert(pos <= size());
328
329         Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
330         Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
331         for (; cit != end; ++cit)
332                 if (cit->pos() >= pos)
333                         break;
334
335         if (cit != end)
336                 return cit->font();
337
338         if (pos == size() && !empty())
339                 return getFontSettings(bparams, pos - 1);
340
341         return LyXFont(LyXFont::ALL_INHERIT, getParLanguage(bparams));
342 }
343
344
345 lyx::pos_type Paragraph::getEndPosOfFontSpan(lyx::pos_type pos) const
346 {
347         Assert(pos <= size());
348
349         Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
350         Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
351         for (; cit != end; ++cit)
352                 if (cit->pos() >= pos)
353                         return cit->pos();
354
355         // This should not happen, but if so, we take no chances.
356         //lyxerr << "Paragraph::getEndPosOfFontSpan: This should not happen!"
357         //      << endl;
358         return pos;
359 }
360
361
362 // Gets uninstantiated font setting at position 0
363 LyXFont const Paragraph::getFirstFontSettings() const
364 {
365         if (!empty() && !pimpl_->fontlist.empty())
366                 return pimpl_->fontlist[0].font();
367
368         return LyXFont(LyXFont::ALL_INHERIT);
369 }
370
371
372 // Gets the fully instantiated font at a given position in a paragraph
373 // This is basically the same function as LyXText::GetFont() in text2.C.
374 // The difference is that this one is used for generating the LaTeX file,
375 // and thus cosmetic "improvements" are disallowed: This has to deliver
376 // the true picture of the buffer. (Asger)
377 LyXFont const Paragraph::getFont(BufferParams const & bparams, pos_type pos,
378                                  LyXFont const & outerfont) const
379 {
380         Assert(pos >= 0);
381
382         LyXLayout_ptr const & lout = layout();
383
384         pos_type const body_pos = beginningOfBody();
385
386         LyXFont layoutfont;
387         if (pos < body_pos)
388                 layoutfont = lout->labelfont;
389         else
390                 layoutfont = lout->font;
391
392         LyXFont tmpfont = getFontSettings(bparams, pos);
393         tmpfont.realize(layoutfont);
394         tmpfont.realize(outerfont);
395         tmpfont.realize(bparams.getLyXTextClass().defaultfont());
396
397         return tmpfont;
398 }
399
400
401 LyXFont const Paragraph::getLabelFont(BufferParams const & bparams,
402                                       LyXFont const & outerfont) const
403 {
404         LyXFont tmpfont = layout()->labelfont;
405         tmpfont.setLanguage(getParLanguage(bparams));
406         tmpfont.realize(outerfont);
407         tmpfont.realize(bparams.getLyXTextClass().defaultfont());
408         return tmpfont;
409 }
410
411
412 LyXFont const Paragraph::getLayoutFont(BufferParams const & bparams,
413                                        LyXFont const & outerfont) const
414 {
415         LyXFont tmpfont = layout()->font;
416         tmpfont.setLanguage(getParLanguage(bparams));
417         tmpfont.realize(outerfont);
418         tmpfont.realize(bparams.getLyXTextClass().defaultfont());
419         return tmpfont;
420 }
421
422
423 /// Returns the height of the highest font in range
424 LyXFont::FONT_SIZE
425 Paragraph::highestFontInRange(pos_type startpos, pos_type endpos,
426                               LyXFont::FONT_SIZE const def_size) const
427 {
428         if (pimpl_->fontlist.empty())
429                 return def_size;
430
431         Pimpl::FontList::const_iterator end_it = pimpl_->fontlist.begin();
432         Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
433         for (; end_it != end; ++end_it) {
434                 if (end_it->pos() >= endpos)
435                         break;
436         }
437
438         if (end_it != end)
439                 ++end_it;
440
441         Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
442         for (; cit != end; ++cit) {
443                 if (cit->pos() >= startpos)
444                         break;
445         }
446
447         LyXFont::FONT_SIZE maxsize = LyXFont::SIZE_TINY;
448         for (; cit != end_it; ++cit) {
449                 LyXFont::FONT_SIZE size = cit->font().size();
450                 if (size == LyXFont::INHERIT_SIZE)
451                         size = def_size;
452                 if (size > maxsize && size <= LyXFont::SIZE_HUGER)
453                         maxsize = size;
454         }
455         return maxsize;
456 }
457
458
459 Paragraph::value_type
460 Paragraph::getUChar(BufferParams const & bparams, pos_type pos) const
461 {
462         value_type c = getChar(pos);
463         if (!lyxrc.rtl_support)
464                 return c;
465
466         value_type uc = c;
467         switch (c) {
468         case '(':
469                 uc = ')';
470                 break;
471         case ')':
472                 uc = '(';
473                 break;
474         case '[':
475                 uc = ']';
476                 break;
477         case ']':
478                 uc = '[';
479                 break;
480         case '{':
481                 uc = '}';
482                 break;
483         case '}':
484                 uc = '{';
485                 break;
486         case '<':
487                 uc = '>';
488                 break;
489         case '>':
490                 uc = '<';
491                 break;
492         }
493         if (uc != c && getFontSettings(bparams, pos).isRightToLeft())
494                 return uc;
495         else
496                 return c;
497 }
498
499
500 void Paragraph::setFont(pos_type pos, LyXFont const & font)
501 {
502         Assert(pos <= size());
503
504         // First, reduce font against layout/label font
505         // Update: The SetCharFont() routine in text2.C already
506         // reduces font, so we don't need to do that here. (Asger)
507         // No need to simplify this because it will disappear
508         // in a new kernel. (Asger)
509         // Next search font table
510
511         Pimpl::FontList::iterator beg = pimpl_->fontlist.begin();
512         Pimpl::FontList::iterator it = beg;
513         Pimpl::FontList::iterator endit = pimpl_->fontlist.end();
514         for (; it != endit; ++it) {
515                 if (it->pos() >= pos)
516                         break;
517         }
518         unsigned int i = std::distance(beg, it);
519         bool notfound = (it == endit);
520
521         if (!notfound && pimpl_->fontlist[i].font() == font)
522                 return;
523
524         bool begin = pos == 0 || notfound ||
525                 (i > 0 && pimpl_->fontlist[i - 1].pos() == pos - 1);
526         // Is position pos is a beginning of a font block?
527         bool end = !notfound && pimpl_->fontlist[i].pos() == pos;
528         // Is position pos is the end of a font block?
529         if (begin && end) { // A single char block
530                 if (i + 1 < pimpl_->fontlist.size() &&
531                     pimpl_->fontlist[i + 1].font() == font) {
532                         // Merge the singleton block with the next block
533                         pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
534                         if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
535                                 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i - 1);
536                 } else if (i > 0 && pimpl_->fontlist[i - 1].font() == font) {
537                         // Merge the singleton block with the previous block
538                         pimpl_->fontlist[i - 1].pos(pos);
539                         pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
540                 } else
541                         pimpl_->fontlist[i].font(font);
542         } else if (begin) {
543                 if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
544                         pimpl_->fontlist[i - 1].pos(pos);
545                 else
546                         pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
547                                         Pimpl::FontTable(pos, font));
548         } else if (end) {
549                 pimpl_->fontlist[i].pos(pos - 1);
550                 if (!(i + 1 < pimpl_->fontlist.size() &&
551                       pimpl_->fontlist[i + 1].font() == font))
552                         pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
553                                         Pimpl::FontTable(pos, font));
554         } else { // The general case. The block is splitted into 3 blocks
555                 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
556                                 Pimpl::FontTable(pos - 1, pimpl_->fontlist[i].font()));
557                 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
558                                 Pimpl::FontTable(pos, font));
559         }
560 }
561
562
563 void Paragraph::makeSameLayout(Paragraph const & par)
564 {
565         layout(par.layout());
566         // move to pimpl?
567         params() = par.params();
568 }
569
570
571 int Paragraph::stripLeadingSpaces()
572 {
573         if (isFreeSpacing())
574                 return 0;
575
576         int i = 0;
577         while (!empty() && (isNewline(0) || isLineSeparator(0))) {
578                 pimpl_->eraseIntern(0);
579                 ++i;
580         }
581
582         return i;
583 }
584
585
586 bool Paragraph::hasSameLayout(Paragraph const & par) const
587 {
588         return
589                 par.layout() == layout() &&
590                 params().sameLayout(par.params());
591 }
592
593
594 Paragraph::depth_type Paragraph::getDepth() const
595 {
596         return params().depth();
597 }
598
599
600 Paragraph::depth_type Paragraph::getMaxDepthAfter() const
601 {
602         if (layout()->isEnvironment())
603                 return params().depth() + 1;
604         else
605                 return params().depth();
606 }
607
608
609 char Paragraph::getAlign() const
610 {
611         return params().align();
612 }
613
614
615 string const & Paragraph::getLabelstring() const
616 {
617         return params().labelString();
618 }
619
620
621 // the next two functions are for the manual labels
622 string const Paragraph::getLabelWidthString() const
623 {
624         if (!params().labelWidthString().empty())
625                 return params().labelWidthString();
626         else
627                 return _("Senseless with this layout!");
628 }
629
630
631 void Paragraph::setLabelWidthString(string const & s)
632 {
633         params().labelWidthString(s);
634 }
635
636
637 void Paragraph::applyLayout(LyXLayout_ptr const & new_layout)
638 {
639         layout(new_layout);
640         params().labelWidthString(string());
641         params().align(LYX_ALIGN_LAYOUT);
642         params().spaceTop(VSpace(VSpace::NONE));
643         params().spaceBottom(VSpace(VSpace::NONE));
644         params().spacing(Spacing(Spacing::Default));
645 }
646
647
648 int Paragraph::beginningOfBody() const
649 {
650         if (layout()->labeltype != LABEL_MANUAL)
651                 return 0;
652
653         // Unroll the first two cycles of the loop
654         // and remember the previous character to
655         // remove unnecessary GetChar() calls
656         pos_type i = 0;
657         if (i < size() && !isNewline(i)) {
658                 ++i;
659                 char previous_char = 0;
660                 char temp = 0;
661                 if (i < size()) {
662                         previous_char = getChar(i);
663                         if (!isNewline(i)) {
664                                 ++i;
665                                 while (i < size() && previous_char != ' ') {
666                                         temp = getChar(i);
667                                         if (isNewline(i))
668                                                 break;
669
670                                         ++i;
671                                         previous_char = temp;
672                                 }
673                         }
674                 }
675         }
676
677         return i;
678 }
679
680
681 // returns -1 if inset not found
682 int Paragraph::getPositionOfInset(InsetOld const * inset) const
683 {
684         // Find the entry.
685         InsetList::const_iterator it = insetlist.begin();
686         InsetList::const_iterator end = insetlist.end();
687         for (; it != end; ++it)
688                 if (it->inset == inset)
689                         return it->pos;
690         return -1;
691 }
692
693
694 InsetBibitem * Paragraph::bibitem() const
695 {
696         InsetList::const_iterator it = insetlist.begin();
697         if (it != insetlist.end() && it->inset->lyxCode() == InsetOld::BIBTEX_CODE)
698                 return static_cast<InsetBibitem *>(it->inset);
699         return 0;
700 }
701
702
703
704 // This could go to ParagraphParameters if we want to
705 int Paragraph::startTeXParParams(BufferParams const & bparams,
706                                  ostream & os, bool moving_arg) const
707 {
708         int column = 0;
709
710         if (params().noindent()) {
711                 os << "\\noindent ";
712                 column += 10;
713         }
714
715         switch (params().align()) {
716         case LYX_ALIGN_NONE:
717         case LYX_ALIGN_BLOCK:
718         case LYX_ALIGN_LAYOUT:
719         case LYX_ALIGN_SPECIAL:
720                 break;
721         case LYX_ALIGN_LEFT:
722         case LYX_ALIGN_RIGHT:
723         case LYX_ALIGN_CENTER:
724                 if (moving_arg) {
725                         os << "\\protect";
726                         column = 8;
727                 }
728                 break;
729         }
730
731         switch (params().align()) {
732         case LYX_ALIGN_NONE:
733         case LYX_ALIGN_BLOCK:
734         case LYX_ALIGN_LAYOUT:
735         case LYX_ALIGN_SPECIAL:
736                 break;
737         case LYX_ALIGN_LEFT:
738                 if (getParLanguage(bparams)->babel() != "hebrew") {
739                         os << "\\begin{flushleft}";
740                         column += 17;
741                 } else {
742                         os << "\\begin{flushright}";
743                         column += 18;
744                 }
745                 break;
746         case LYX_ALIGN_RIGHT:
747                 if (getParLanguage(bparams)->babel() != "hebrew") {
748                         os << "\\begin{flushright}";
749                         column += 18;
750                 } else {
751                         os << "\\begin{flushleft}";
752                         column += 17;
753                 }
754                 break;
755         case LYX_ALIGN_CENTER:
756                 os << "\\begin{center}";
757                 column += 14;
758                 break;
759         }
760
761         return column;
762 }
763
764
765 // This could go to ParagraphParameters if we want to
766 int Paragraph::endTeXParParams(BufferParams const & bparams,
767                                ostream & os, bool moving_arg) const
768 {
769         int column = 0;
770
771         switch (params().align()) {
772         case LYX_ALIGN_NONE:
773         case LYX_ALIGN_BLOCK:
774         case LYX_ALIGN_LAYOUT:
775         case LYX_ALIGN_SPECIAL:
776                 break;
777         case LYX_ALIGN_LEFT:
778         case LYX_ALIGN_RIGHT:
779         case LYX_ALIGN_CENTER:
780                 if (moving_arg) {
781                         os << "\\protect";
782                         column = 8;
783                 }
784                 break;
785         }
786
787         switch (params().align()) {
788         case LYX_ALIGN_NONE:
789         case LYX_ALIGN_BLOCK:
790         case LYX_ALIGN_LAYOUT:
791         case LYX_ALIGN_SPECIAL:
792                 break;
793         case LYX_ALIGN_LEFT:
794                 if (getParLanguage(bparams)->babel() != "hebrew") {
795                         os << "\\end{flushleft}";
796                         column = 15;
797                 } else {
798                         os << "\\end{flushright}";
799                         column = 16;
800                 }
801                 break;
802         case LYX_ALIGN_RIGHT:
803                 if (getParLanguage(bparams)->babel() != "hebrew") {
804                         os << "\\end{flushright}";
805                         column+= 16;
806                 } else {
807                         os << "\\end{flushleft}";
808                         column = 15;
809                 }
810                 break;
811         case LYX_ALIGN_CENTER:
812                 os << "\\end{center}";
813                 column = 12;
814                 break;
815         }
816         return column;
817 }
818
819
820 // This one spits out the text of the paragraph
821 bool Paragraph::simpleTeXOnePar(Buffer const & buf,
822                                 BufferParams const & bparams,
823                                 LyXFont const & outerfont,
824                                 ostream & os, TexRow & texrow,
825                                 LatexRunParams const & runparams)
826 {
827         lyxerr[Debug::LATEX] << "SimpleTeXOnePar...     " << this << endl;
828
829         bool return_value = false;
830
831         LyXLayout_ptr style;
832
833         // well we have to check if we are in an inset with unlimited
834         // lenght (all in one row) if that is true then we don't allow
835         // any special options in the paragraph and also we don't allow
836         // any environment other then "Standard" to be valid!
837         bool asdefault =
838                 (inInset() && inInset()->forceDefaultParagraphs(inInset()));
839
840         if (asdefault) {
841                 style = bparams.getLyXTextClass().defaultLayout();
842         } else {
843                 style = layout();
844         }
845
846         LyXFont basefont;
847
848         // Maybe we have to create a optional argument.
849         pos_type body_pos;
850
851         // FIXME: can we actually skip this check and just call
852         // beginningOfBody() ??
853         if (style->labeltype != LABEL_MANUAL) {
854                 body_pos = 0;
855         } else {
856                 body_pos = beginningOfBody();
857         }
858
859         unsigned int column = 0;
860
861         if (body_pos > 0) {
862                 os << '[';
863                 ++column;
864                 basefont = getLabelFont(bparams, outerfont);
865         } else {
866                 basefont = getLayoutFont(bparams, outerfont);
867         }
868
869         bool moving_arg = runparams.moving_arg;
870         moving_arg |= style->needprotect;
871
872         // Which font is currently active?
873         LyXFont running_font(basefont);
874         // Do we have an open font change?
875         bool open_font = false;
876
877         Change::Type running_change = Change::UNCHANGED;
878
879         texrow.start(id(), 0);
880
881         // if the paragraph is empty, the loop will not be entered at all
882         if (empty()) {
883                 if (style->isCommand()) {
884                         os << '{';
885                         ++column;
886                 }
887                 if (!asdefault)
888                         column += startTeXParParams(bparams, os, moving_arg);
889
890         }
891
892         for (pos_type i = 0; i < size(); ++i) {
893                 ++column;
894                 // First char in paragraph or after label?
895                 if (i == body_pos) {
896                         if (body_pos > 0) {
897                                 if (open_font) {
898                                         column += running_font.latexWriteEndChanges(os, basefont, basefont);
899                                         open_font = false;
900                                 }
901                                 basefont = getLayoutFont(bparams, outerfont);
902                                 running_font = basefont;
903                                 os << ']';
904                                 ++column;
905                         }
906                         if (style->isCommand()) {
907                                 os << '{';
908                                 ++column;
909                         }
910
911                         if (!asdefault)
912                                 column += startTeXParParams(bparams, os,
913                                                             moving_arg);
914                 }
915
916                 value_type c = getChar(i);
917
918                 // Fully instantiated font
919                 LyXFont font = getFont(bparams, i, outerfont);
920
921                 LyXFont const last_font = running_font;
922
923                 // Spaces at end of font change are simulated to be
924                 // outside font change, i.e. we write "\textXX{text} "
925                 // rather than "\textXX{text }". (Asger)
926                 if (open_font && c == ' ' && i <= size() - 2) {
927                         LyXFont const & next_font = getFont(bparams, i + 1, outerfont);
928                         if (next_font != running_font
929                             && next_font != font) {
930                                 font = next_font;
931                         }
932                 }
933
934                 // We end font definition before blanks
935                 if (open_font &&
936                     (font != running_font ||
937                      font.language() != running_font.language()))
938                 {
939                         column += running_font.latexWriteEndChanges(os,
940                                                                     basefont,
941                                                                     (i == body_pos-1) ? basefont : font);
942                         running_font = basefont;
943                         open_font = false;
944                 }
945
946                 // Blanks are printed before start of fontswitch
947                 if (c == ' ') {
948                         // Do not print the separation of the optional argument
949                         if (i != body_pos - 1) {
950                                 pimpl_->simpleTeXBlanks(os, texrow, i,
951                                                        column, font, *style);
952                         }
953                 }
954
955                 // Do we need to change font?
956                 if ((font != running_font ||
957                      font.language() != running_font.language()) &&
958                         i != body_pos - 1)
959                 {
960                         column += font.latexWriteStartChanges(os, basefont,
961                                                               last_font);
962                         running_font = font;
963                         open_font = true;
964                 }
965
966                 Change::Type change = pimpl_->lookupChange(i);
967
968                 column += Changes::latexMarkChange(os, running_change, change);
969                 running_change = change;
970
971                 LatexRunParams rp = runparams;
972                 rp.moving_arg = moving_arg;
973                 rp.free_spacing = style->free_spacing;
974                 pimpl_->simpleTeXSpecialChars(buf, bparams,
975                                               os, texrow, runparams,
976                                               font, running_font,
977                                               basefont, outerfont, open_font,
978                                               running_change,
979                                               *style, i, column, c);
980         }
981
982         column += Changes::latexMarkChange(os,
983                         running_change, Change::UNCHANGED);
984
985         // If we have an open font definition, we have to close it
986         if (open_font) {
987 #ifdef FIXED_LANGUAGE_END_DETECTION
988                 if (next_) {
989                         running_font
990                                 .latexWriteEndChanges(os, basefont,
991                                                       next_->getFont(bparams,
992                                                       0, outerfont));
993                 } else {
994                         running_font.latexWriteEndChanges(os, basefont,
995                                                           basefont);
996                 }
997 #else
998 #ifdef WITH_WARNINGS
999 //#warning For now we ALWAYS have to close the foreign font settings if they are
1000 //#warning there as we start another \selectlanguage with the next paragraph if
1001 //#warning we are in need of this. This should be fixed sometime (Jug)
1002 #endif
1003                 running_font.latexWriteEndChanges(os, basefont,  basefont);
1004 #endif
1005         }
1006
1007         // Needed if there is an optional argument but no contents.
1008         if (body_pos > 0 && body_pos == size()) {
1009                 os << "]~";
1010                 return_value = false;
1011         }
1012
1013         if (!asdefault) {
1014                 column += endTeXParParams(bparams, os, moving_arg);
1015         }
1016
1017         lyxerr[Debug::LATEX] << "SimpleTeXOnePar...done " << this << endl;
1018         return return_value;
1019 }
1020
1021
1022
1023
1024 bool Paragraph::isHfill(pos_type pos) const
1025 {
1026         return IsInsetChar(getChar(pos))
1027                && getInset(pos)->lyxCode() == InsetOld::HFILL_CODE;
1028 }
1029
1030
1031 bool Paragraph::isInset(pos_type pos) const
1032 {
1033         return IsInsetChar(getChar(pos));
1034 }
1035
1036
1037 bool Paragraph::isNewline(pos_type pos) const
1038 {
1039         return IsInsetChar(getChar(pos))
1040                && getInset(pos)->lyxCode() == InsetOld::NEWLINE_CODE;
1041 }
1042
1043
1044 bool Paragraph::isSeparator(pos_type pos) const
1045 {
1046         return IsSeparatorChar(getChar(pos));
1047 }
1048
1049
1050 bool Paragraph::isLineSeparator(pos_type pos) const
1051 {
1052         value_type const c = getChar(pos);
1053         return IsLineSeparatorChar(c)
1054                 || (IsInsetChar(c) && getInset(pos) &&
1055                 getInset(pos)->isLineSeparator());
1056 }
1057
1058
1059 bool Paragraph::isKomma(pos_type pos) const
1060 {
1061         return IsKommaChar(getChar(pos));
1062 }
1063
1064
1065 /// Used by the spellchecker
1066 bool Paragraph::isLetter(pos_type pos) const
1067 {
1068         value_type const c = getChar(pos);
1069         if (IsLetterChar(c))
1070                 return true;
1071         if (isInset(pos))
1072                 return getInset(pos)->isLetter();
1073         // We want to pass the ' and escape chars to ispell
1074         string const extra = lyxrc.isp_esc_chars + '\'';
1075         return contains(extra, c);
1076 }
1077
1078
1079 bool Paragraph::isWord(pos_type pos) const
1080 {
1081         return IsWordChar(getChar(pos)) ;
1082 }
1083
1084
1085 Language const *
1086 Paragraph::getParLanguage(BufferParams const & bparams) const
1087 {
1088         if (!empty())
1089                 return getFirstFontSettings().language();
1090 #warning FIXME we should check the prev par as well (Lgb)
1091         return bparams.language;
1092 }
1093
1094
1095 bool Paragraph::isRightToLeftPar(BufferParams const & bparams) const
1096 {
1097         return lyxrc.rtl_support
1098                 && getParLanguage(bparams)->RightToLeft()
1099                 && !(inInset() && inInset()->owner() &&
1100                      inInset()->owner()->lyxCode() == InsetOld::ERT_CODE);
1101 }
1102
1103
1104 void Paragraph::changeLanguage(BufferParams const & bparams,
1105                                Language const * from, Language const * to)
1106 {
1107         for (pos_type i = 0; i < size(); ++i) {
1108                 LyXFont font = getFontSettings(bparams, i);
1109                 if (font.language() == from) {
1110                         font.setLanguage(to);
1111                         setFont(i, font);
1112                 }
1113         }
1114 }
1115
1116
1117 bool Paragraph::isMultiLingual(BufferParams const & bparams)
1118 {
1119         Language const * doc_language = bparams.language;
1120         Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
1121         Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
1122
1123         for (; cit != end; ++cit)
1124                 if (cit->font().language() != ignore_language &&
1125                     cit->font().language() != latex_language &&
1126                     cit->font().language() != doc_language)
1127                         return true;
1128         return false;
1129 }
1130
1131
1132 // Convert the paragraph to a string.
1133 // Used for building the table of contents
1134 string const Paragraph::asString(Buffer const & buffer, bool label) const
1135 {
1136 #if 0
1137         string s;
1138         if (label && !params().labelString().empty())
1139                 s += params().labelString() + ' ';
1140
1141         for (pos_type i = 0; i < size(); ++i) {
1142                 value_type c = getChar(i);
1143                 if (IsPrintable(c))
1144                         s += c;
1145                 else if (c == META_INSET &&
1146                          getInset(i)->lyxCode() == InsetOld::MATH_CODE) {
1147                         ostringstream ost;
1148                         getInset(i)->ascii(buffer, ost);
1149                         s += subst(STRCONV(ost.str()),'\n',' ');
1150                 }
1151         }
1152
1153         return s;
1154 #else
1155         // This should really be done by the caller and not here.
1156         string ret(asString(buffer, 0, size(), label));
1157         return subst(ret, '\n', ' ');
1158 #endif
1159 }
1160
1161
1162 string const Paragraph::asString(Buffer const & buffer,
1163                                  pos_type beg, pos_type end, bool label) const
1164 {
1165         ostringstream os;
1166
1167         if (beg == 0 && label && !params().labelString().empty())
1168                 os << params().labelString() << ' ';
1169
1170         for (pos_type i = beg; i < end; ++i) {
1171                 value_type const c = getUChar(buffer.params, i);
1172                 if (IsPrintable(c))
1173                         os << c;
1174                 else if (c == META_INSET)
1175                         getInset(i)->ascii(buffer, os);
1176         }
1177
1178         return STRCONV(os.str());
1179 }
1180
1181
1182 void Paragraph::setInsetOwner(UpdatableInset * inset)
1183 {
1184         pimpl_->inset_owner = inset;
1185         InsetList::iterator it = insetlist.begin();
1186         InsetList::iterator end = insetlist.end();
1187         for (; it != end; ++it)
1188                 if (it->inset)
1189                         it->inset->setOwner(inset);
1190 }
1191
1192
1193 void Paragraph::deleteInsetsLyXText(BufferView * bv)
1194 {
1195         insetlist.deleteInsetsLyXText(bv);
1196 }
1197
1198
1199 void Paragraph::setContentsFromPar(Paragraph const & par)
1200 {
1201         pimpl_->setContentsFromPar(par);
1202 }
1203
1204
1205 void Paragraph::trackChanges(Change::Type type)
1206 {
1207         pimpl_->trackChanges(type);
1208 }
1209
1210
1211 void Paragraph::untrackChanges()
1212 {
1213         pimpl_->untrackChanges();
1214 }
1215
1216
1217 void Paragraph::cleanChanges()
1218 {
1219         pimpl_->cleanChanges();
1220 }
1221
1222
1223 Change::Type Paragraph::lookupChange(lyx::pos_type pos) const
1224 {
1225         Assert(!size() || pos < size());
1226         return pimpl_->lookupChange(pos);
1227 }
1228
1229
1230 Change const Paragraph::lookupChangeFull(lyx::pos_type pos) const
1231 {
1232         Assert(!size() || pos < size());
1233         return pimpl_->lookupChangeFull(pos);
1234 }
1235
1236
1237 bool Paragraph::isChanged(pos_type start, pos_type end) const
1238 {
1239         return pimpl_->isChanged(start, end);
1240 }
1241
1242
1243 bool Paragraph::isChangeEdited(pos_type start, pos_type end) const
1244 {
1245         return pimpl_->isChangeEdited(start, end);
1246 }
1247
1248
1249 void Paragraph::setChange(lyx::pos_type pos, Change::Type type)
1250 {
1251         pimpl_->setChange(pos, type);
1252
1253 }
1254
1255
1256 void Paragraph::markErased()
1257 {
1258         pimpl_->markErased();
1259 }
1260
1261
1262 void Paragraph::acceptChange(pos_type start, pos_type end)
1263 {
1264         return pimpl_->acceptChange(start, end);
1265 }
1266
1267
1268 void Paragraph::rejectChange(pos_type start, pos_type end)
1269 {
1270         return pimpl_->rejectChange(start, end);
1271 }
1272
1273
1274 lyx::pos_type Paragraph::size() const
1275 {
1276         return pimpl_->size();
1277 }
1278
1279
1280 bool Paragraph::empty() const
1281 {
1282         return pimpl_->empty();
1283 }
1284
1285
1286 Paragraph::value_type Paragraph::getChar(pos_type pos) const
1287 {
1288         return pimpl_->getChar(pos);
1289 }
1290
1291
1292 int Paragraph::id() const
1293 {
1294         return pimpl_->id_;
1295 }
1296
1297
1298 void Paragraph::id(int i)
1299 {
1300         pimpl_->id_ = i;
1301 }
1302
1303
1304 LyXLayout_ptr const & Paragraph::layout() const
1305 {
1306 /*
1307         InsetOld * inset = inInset();
1308         if (inset && inset->lyxCode() == InsetOld::ENVIRONMENT_CODE)
1309                 return static_cast<InsetEnvironment*>(inset)->layout();
1310 */
1311         return layout_;
1312 }
1313
1314
1315 void Paragraph::layout(LyXLayout_ptr const & new_layout)
1316 {
1317         layout_ = new_layout;
1318 }
1319
1320
1321 UpdatableInset * Paragraph::inInset() const
1322 {
1323         return pimpl_->inset_owner;
1324 }
1325
1326
1327 void Paragraph::clearContents()
1328 {
1329         pimpl_->clear();
1330 }
1331
1332 void Paragraph::setChar(pos_type pos, value_type c)
1333 {
1334         pimpl_->setChar(pos, c);
1335 }
1336
1337
1338 ParagraphParameters & Paragraph::params()
1339 {
1340         return pimpl_->params;
1341 }
1342
1343
1344 ParagraphParameters const & Paragraph::params() const
1345 {
1346         return pimpl_->params;
1347 }
1348
1349
1350 bool Paragraph::isFreeSpacing() const
1351 {
1352         if (layout()->free_spacing)
1353                 return true;
1354
1355         // for now we just need this, later should we need this in some
1356         // other way we can always add a function to InsetOld too.
1357         if (pimpl_->inset_owner && pimpl_->inset_owner->owner())
1358                 return pimpl_->inset_owner->owner()->lyxCode() == InsetOld::ERT_CODE;
1359         return false;
1360 }
1361
1362
1363 bool Paragraph::allowEmpty() const
1364 {
1365         if (layout()->keepempty)
1366                 return true;
1367         if (pimpl_->inset_owner && pimpl_->inset_owner->owner())
1368                 return pimpl_->inset_owner->owner()->lyxCode() == InsetOld::ERT_CODE;
1369         return false;
1370 }