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