]> git.lyx.org Git - lyx.git/blob - src/paragraph.C
4176e2f9309ee997bca432e568430309c6960c57
[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 "encoding.h"
26 #include "debug.h"
27 #include "gettext.h"
28 #include "language.h"
29 #include "lyxfont.h"
30 #include "lyxrc.h"
31 #include "lyxrow.h"
32 #include "outputparams.h"
33 #include "paragraph_funcs.h"
34 #include "sgml.h"
35 #include "texrow.h"
36 #include "vspace.h"
37
38 #include "insets/insetbibitem.h"
39 #include "insets/insetoptarg.h"
40
41 #include "support/lstrings.h"
42 #include "support/textutils.h"
43 #include "support/std_sstream.h"
44
45 #include <boost/tuple/tuple.hpp>
46
47 #include <list>
48 #include <stack>
49
50 using lyx::pos_type;
51
52 using lyx::support::contains;
53 using lyx::support::subst;
54
55 using std::endl;
56 using std::list;
57 using std::stack;
58 using std::string;
59 using std::ostream;
60 using std::ostringstream;
61
62
63 Paragraph::Paragraph()
64         : y(0), height(0), begin_of_body_(0),
65           pimpl_(new Paragraph::Pimpl(this))
66 {
67         itemdepth = 0;
68         params().clear();
69 }
70
71
72 Paragraph::Paragraph(Paragraph const & par)
73         : y(0), height(0), text_(par.text_), begin_of_body_(par.begin_of_body_),
74           pimpl_(new Paragraph::Pimpl(*par.pimpl_, this))
75 {
76         itemdepth = 0;
77         // this is because of the dummy layout of the paragraphs that
78         // follow footnotes
79         layout_ = par.layout();
80
81         // copy everything behind the break-position to the new paragraph
82         insetlist = par.insetlist;
83         InsetList::iterator it = insetlist.begin();
84         InsetList::iterator end = insetlist.end();
85         for (; it != end; ++it) {
86                 // currently we hold Inset*, not InsetBase*
87                 it->inset = static_cast<InsetOld*>(it->inset->clone().release());
88         }
89 }
90
91
92 void Paragraph::operator=(Paragraph const & par)
93 {
94         // needed as we will destroy the pimpl_ before copying it
95         if (&par != this)
96                 return;
97
98         lyxerr << "Paragraph::operator=()" << endl;
99
100         text_ = par.text_;
101
102         delete pimpl_;
103         pimpl_ = new Pimpl(*par.pimpl_, this);
104
105         itemdepth = par.itemdepth;
106         // this is because of the dummy layout of the paragraphs that
107         // follow footnotes
108         layout_ = par.layout();
109
110         // copy everything behind the break-position to the new paragraph
111         insetlist = par.insetlist;
112         InsetList::iterator it = insetlist.begin();
113         InsetList::iterator end = insetlist.end();
114         for (; it != end; ++it) {
115                 it->inset = static_cast<InsetOld*>(it->inset->clone().release());
116         }
117 }
118
119
120 Paragraph::~Paragraph()
121 {
122         delete pimpl_;
123         //
124         //lyxerr << "Paragraph::paragraph_id = "
125         //       << Paragraph::paragraph_id << endl;
126 }
127
128
129 void Paragraph::write(Buffer const & buf, ostream & os,
130                           BufferParams const & bparams,
131                           depth_type & dth) const
132 {
133         // The beginning or end of a deeper (i.e. nested) area?
134         if (dth != params().depth()) {
135                 if (params().depth() > dth) {
136                         while (params().depth() > dth) {
137                                 os << "\n\\begin_deeper ";
138                                 ++dth;
139                         }
140                 } else {
141                         while (params().depth() < dth) {
142                                 os << "\n\\end_deeper ";
143                                 --dth;
144                         }
145                 }
146         }
147
148         // First write the layout
149         os << "\n\\begin_layout " << layout()->name() << '\n';
150
151         params().write(os);
152
153         LyXFont font1(LyXFont::ALL_INHERIT, bparams.language);
154
155         Change running_change = Change(Change::UNCHANGED);
156         lyx::time_type const curtime(lyx::current_time());
157
158         int column = 0;
159         for (pos_type i = 0; i < size(); ++i) {
160                 if (!i) {
161                         os << '\n';
162                         column = 0;
163                 }
164
165                 Change change = pimpl_->lookupChangeFull(i);
166                 Changes::lyxMarkChange(os, column, curtime, running_change, change);
167                 running_change = change;
168
169                 // Write font changes
170                 LyXFont font2 = getFontSettings(bparams, i);
171                 if (font2 != font1) {
172                         font2.lyxWriteChanges(font1, os);
173                         column = 0;
174                         font1 = font2;
175                 }
176
177                 value_type const c = getChar(i);
178                 switch (c) {
179                 case META_INSET:
180                 {
181                         InsetOld const * inset = getInset(i);
182                         if (inset)
183                                 if (inset->directWrite()) {
184                                         // international char, let it write
185                                         // code directly so it's shorter in
186                                         // the file
187                                         inset->write(buf, os);
188                                 } else {
189                                         os << "\n\\begin_inset ";
190                                         inset->write(buf, os);
191                                         os << "\n\\end_inset \n\n";
192                                         column = 0;
193                                 }
194                 }
195                 break;
196                 case '\\':
197                         os << "\n\\backslash \n";
198                         column = 0;
199                         break;
200                 case '.':
201                         if (i + 1 < size() && getChar(i + 1) == ' ') {
202                                 os << ".\n";
203                                 column = 0;
204                         } else
205                                 os << '.';
206                         break;
207                 default:
208                         if ((column > 70 && c == ' ')
209                             || column > 79) {
210                                 os << '\n';
211                                 column = 0;
212                         }
213                         // this check is to amend a bug. LyX sometimes
214                         // inserts '\0' this could cause problems.
215                         if (c != '\0')
216                                 os << c;
217                         else
218                                 lyxerr << "ERROR (Paragraph::writeFile):"
219                                         " NULL char in structure." << endl;
220                         ++column;
221                         break;
222                 }
223         }
224
225         // to make reading work properly
226         if (!size()) {
227                 running_change = pimpl_->lookupChange(0);
228                 Changes::lyxMarkChange(os, column, curtime,
229                         Change(Change::UNCHANGED), running_change);
230         }
231         Changes::lyxMarkChange(os, column, curtime,
232                 running_change, Change(Change::UNCHANGED));
233
234         os << "\n\\end_layout\n";
235 }
236
237
238 void Paragraph::validate(LaTeXFeatures & features) const
239 {
240         pimpl_->validate(features, *layout());
241 }
242
243
244 void Paragraph::eraseIntern(lyx::pos_type pos)
245 {
246         pimpl_->eraseIntern(pos);
247 }
248
249
250 bool Paragraph::erase(pos_type pos)
251 {
252         return pimpl_->erase(pos);
253 }
254
255
256 int Paragraph::erase(pos_type start, pos_type end)
257 {
258         return pimpl_->erase(start, end);
259 }
260
261
262 void Paragraph::insert(pos_type start, string const & str,
263                        LyXFont const & font)
264 {
265         int size = str.size();
266         for (int i = 0 ; i < size ; ++i)
267                 insertChar(start + i, str[i], font);
268 }
269
270
271 bool Paragraph::checkInsertChar(LyXFont & font)
272 {
273         if (pimpl_->inset_owner)
274                 return pimpl_->inset_owner->checkInsertChar(font);
275         return true;
276 }
277
278
279 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c)
280 {
281         insertChar(pos, c, LyXFont(LyXFont::ALL_INHERIT));
282 }
283
284
285 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c,
286                            LyXFont const & font, Change change)
287 {
288         pimpl_->insertChar(pos, c, font, change);
289 }
290
291
292 void Paragraph::insertInset(pos_type pos, InsetOld * inset)
293 {
294         insertInset(pos, inset, LyXFont(LyXFont::ALL_INHERIT));
295 }
296
297
298 void Paragraph::insertInset(pos_type pos, InsetOld * inset,
299         LyXFont const & font, Change change)
300 {
301         pimpl_->insertInset(pos, inset, font, change);
302 }
303
304
305 bool Paragraph::insetAllowed(InsetOld_code code)
306 {
307         //lyxerr << "Paragraph::InsertInsetAllowed" << endl;
308         if (pimpl_->inset_owner)
309                 return pimpl_->inset_owner->insetAllowed(code);
310         return true;
311 }
312
313
314 InsetOld * Paragraph::getInset(pos_type pos)
315 {
316         BOOST_ASSERT(pos < size());
317         return insetlist.get(pos);
318 }
319
320
321 InsetOld const * Paragraph::getInset(pos_type pos) const
322 {
323         BOOST_ASSERT(pos < size());
324         return insetlist.get(pos);
325 }
326
327
328 // Gets uninstantiated font setting at position.
329 LyXFont const Paragraph::getFontSettings(BufferParams const & bparams,
330                                          pos_type pos) const
331 {
332         BOOST_ASSERT(pos <= size());
333
334         Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
335         Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
336         for (; cit != end; ++cit)
337                 if (cit->pos() >= pos)
338                         break;
339
340         if (cit != end)
341                 return cit->font();
342
343         if (pos == size() && !empty())
344                 return getFontSettings(bparams, pos - 1);
345
346         return LyXFont(LyXFont::ALL_INHERIT, getParLanguage(bparams));
347 }
348
349
350 lyx::pos_type Paragraph::getEndPosOfFontSpan(lyx::pos_type pos) const
351 {
352         BOOST_ASSERT(pos <= size());
353
354         Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
355         Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
356         for (; cit != end; ++cit)
357                 if (cit->pos() >= pos)
358                         return cit->pos();
359
360         // This should not happen, but if so, we take no chances.
361         //lyxerr << "Paragraph::getEndPosOfFontSpan: This should not happen!"
362         //      << endl;
363         return pos;
364 }
365
366
367 // Gets uninstantiated font setting at position 0
368 LyXFont const Paragraph::getFirstFontSettings() const
369 {
370         if (!empty() && !pimpl_->fontlist.empty())
371                 return pimpl_->fontlist[0].font();
372
373         return LyXFont(LyXFont::ALL_INHERIT);
374 }
375
376
377 // Gets the fully instantiated font at a given position in a paragraph
378 // This is basically the same function as LyXText::GetFont() in text2.C.
379 // The difference is that this one is used for generating the LaTeX file,
380 // and thus cosmetic "improvements" are disallowed: This has to deliver
381 // the true picture of the buffer. (Asger)
382 LyXFont const Paragraph::getFont(BufferParams const & bparams, pos_type pos,
383                                  LyXFont const & outerfont) const
384 {
385         BOOST_ASSERT(pos >= 0);
386
387         LyXLayout_ptr const & lout = layout();
388
389         pos_type const body_pos = beginOfBody();
390
391         LyXFont layoutfont;
392         if (pos < body_pos)
393                 layoutfont = lout->labelfont;
394         else
395                 layoutfont = lout->font;
396
397         LyXFont font = getFontSettings(bparams, pos);
398         font.realize(layoutfont);
399         font.realize(outerfont);
400         font.realize(bparams.getLyXTextClass().defaultfont());
401
402         return font;
403 }
404
405
406 LyXFont const Paragraph::getLabelFont(BufferParams const & bparams,
407                                       LyXFont const & outerfont) const
408 {
409         LyXFont tmpfont = layout()->labelfont;
410         tmpfont.setLanguage(getParLanguage(bparams));
411         tmpfont.realize(outerfont);
412         tmpfont.realize(bparams.getLyXTextClass().defaultfont());
413         return tmpfont;
414 }
415
416
417 LyXFont const Paragraph::getLayoutFont(BufferParams const & bparams,
418                                        LyXFont const & outerfont) const
419 {
420         LyXFont tmpfont = layout()->font;
421         tmpfont.setLanguage(getParLanguage(bparams));
422         tmpfont.realize(outerfont);
423         tmpfont.realize(bparams.getLyXTextClass().defaultfont());
424         return tmpfont;
425 }
426
427
428 /// Returns the height of the highest font in range
429 LyXFont_size
430 Paragraph::highestFontInRange(pos_type startpos, pos_type endpos,
431                               LyXFont_size def_size) const
432 {
433         if (pimpl_->fontlist.empty())
434                 return def_size;
435
436         Pimpl::FontList::const_iterator end_it = pimpl_->fontlist.begin();
437         Pimpl::FontList::const_iterator const end = pimpl_->fontlist.end();
438         for (; end_it != end; ++end_it) {
439                 if (end_it->pos() >= endpos)
440                         break;
441         }
442
443         if (end_it != end)
444                 ++end_it;
445
446         Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
447         for (; cit != end; ++cit) {
448                 if (cit->pos() >= startpos)
449                         break;
450         }
451
452         LyXFont::FONT_SIZE maxsize = LyXFont::SIZE_TINY;
453         for (; cit != end_it; ++cit) {
454                 LyXFont::FONT_SIZE size = cit->font().size();
455                 if (size == LyXFont::INHERIT_SIZE)
456                         size = def_size;
457                 if (size > maxsize && size <= LyXFont::SIZE_HUGER)
458                         maxsize = size;
459         }
460         return maxsize;
461 }
462
463
464 Paragraph::value_type
465 Paragraph::getUChar(BufferParams const & bparams, pos_type pos) const
466 {
467         value_type c = getChar(pos);
468         if (!lyxrc.rtl_support)
469                 return c;
470
471         value_type uc = c;
472         switch (c) {
473         case '(':
474                 uc = ')';
475                 break;
476         case ')':
477                 uc = '(';
478                 break;
479         case '[':
480                 uc = ']';
481                 break;
482         case ']':
483                 uc = '[';
484                 break;
485         case '{':
486                 uc = '}';
487                 break;
488         case '}':
489                 uc = '{';
490                 break;
491         case '<':
492                 uc = '>';
493                 break;
494         case '>':
495                 uc = '<';
496                 break;
497         }
498         if (uc != c && getFontSettings(bparams, pos).isRightToLeft())
499                 return uc;
500         else
501                 return c;
502 }
503
504
505 void Paragraph::setFont(pos_type pos, LyXFont const & font)
506 {
507         BOOST_ASSERT(pos <= size());
508
509         // First, reduce font against layout/label font
510         // Update: The setCharFont() routine in text2.C already
511         // reduces font, so we don't need to do that here. (Asger)
512         // No need to simplify this because it will disappear
513         // in a new kernel. (Asger)
514         // Next search font table
515
516         Pimpl::FontList::iterator beg = pimpl_->fontlist.begin();
517         Pimpl::FontList::iterator it = beg;
518         Pimpl::FontList::iterator endit = pimpl_->fontlist.end();
519         for (; it != endit; ++it) {
520                 if (it->pos() >= pos)
521                         break;
522         }
523         unsigned int i = std::distance(beg, it);
524         bool notfound = (it == endit);
525
526         if (!notfound && pimpl_->fontlist[i].font() == font)
527                 return;
528
529         bool begin = pos == 0 || notfound ||
530                 (i > 0 && pimpl_->fontlist[i - 1].pos() == pos - 1);
531         // Is position pos is a beginning of a font block?
532         bool end = !notfound && pimpl_->fontlist[i].pos() == pos;
533         // Is position pos is the end of a font block?
534         if (begin && end) { // A single char block
535                 if (i + 1 < pimpl_->fontlist.size() &&
536                     pimpl_->fontlist[i + 1].font() == font) {
537                         // Merge the singleton block with the next block
538                         pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
539                         if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
540                                 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i - 1);
541                 } else if (i > 0 && pimpl_->fontlist[i - 1].font() == font) {
542                         // Merge the singleton block with the previous block
543                         pimpl_->fontlist[i - 1].pos(pos);
544                         pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
545                 } else
546                         pimpl_->fontlist[i].font(font);
547         } else if (begin) {
548                 if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
549                         pimpl_->fontlist[i - 1].pos(pos);
550                 else
551                         pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
552                                         Pimpl::FontTable(pos, font));
553         } else if (end) {
554                 pimpl_->fontlist[i].pos(pos - 1);
555                 if (!(i + 1 < pimpl_->fontlist.size() &&
556                       pimpl_->fontlist[i + 1].font() == font))
557                         pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
558                                         Pimpl::FontTable(pos, font));
559         } else { // The general case. The block is splitted into 3 blocks
560                 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
561                                 Pimpl::FontTable(pos - 1, pimpl_->fontlist[i].font()));
562                 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
563                                 Pimpl::FontTable(pos, font));
564         }
565 }
566
567
568 void Paragraph::makeSameLayout(Paragraph const & par)
569 {
570         layout(par.layout());
571         // move to pimpl?
572         params() = par.params();
573 }
574
575
576 int Paragraph::stripLeadingSpaces()
577 {
578         if (isFreeSpacing())
579                 return 0;
580
581         int i = 0;
582         while (!empty() && (isNewline(0) || isLineSeparator(0))) {
583                 pimpl_->eraseIntern(0);
584                 ++i;
585         }
586
587         return i;
588 }
589
590
591 bool Paragraph::hasSameLayout(Paragraph const & par) const
592 {
593         return
594                 par.layout() == layout() &&
595                 params().sameLayout(par.params());
596 }
597
598
599 Paragraph::depth_type Paragraph::getDepth() const
600 {
601         return params().depth();
602 }
603
604
605 Paragraph::depth_type Paragraph::getMaxDepthAfter() const
606 {
607         if (layout()->isEnvironment())
608                 return params().depth() + 1;
609         else
610                 return params().depth();
611 }
612
613
614 char Paragraph::getAlign() const
615 {
616         return params().align();
617 }
618
619
620 string const & Paragraph::getLabelstring() const
621 {
622         return params().labelString();
623 }
624
625
626 // the next two functions are for the manual labels
627 string const Paragraph::getLabelWidthString() const
628 {
629         if (!params().labelWidthString().empty())
630                 return params().labelWidthString();
631         else
632                 return _("Senseless with this layout!");
633 }
634
635
636 void Paragraph::setLabelWidthString(string const & s)
637 {
638         params().labelWidthString(s);
639 }
640
641
642 void Paragraph::applyLayout(LyXLayout_ptr const & new_layout)
643 {
644         layout(new_layout);
645         params().labelWidthString(string());
646         params().align(LYX_ALIGN_LAYOUT);
647         params().spaceTop(VSpace(VSpace::NONE));
648         params().spaceBottom(VSpace(VSpace::NONE));
649         params().spacing(Spacing(Spacing::Default));
650 }
651
652
653 int Paragraph::beginOfBody() const
654 {
655         return begin_of_body_;
656 }
657
658
659 void Paragraph::setBeginOfBody()
660 {
661         if (layout()->labeltype != LABEL_MANUAL) {
662                 begin_of_body_ = 0;
663                 return;
664         }
665
666         // Unroll the first two cycles of the loop
667         // and remember the previous character to
668         // remove unnecessary getChar() calls
669         pos_type i = 0;
670         pos_type end = size();
671         if (i < end && !isNewline(i)) {
672                 ++i;
673                 char previous_char = 0;
674                 char temp = 0;
675                 if (i < end) {
676                         previous_char = text_[i];
677                         if (!isNewline(i)) {
678                                 ++i;
679                                 while (i < end && previous_char != ' ') {
680                                         temp = text_[i];
681                                         if (isNewline(i))
682                                                 break;
683                                         ++i;
684                                         previous_char = temp;
685                                 }
686                         }
687                 }
688         }
689
690         begin_of_body_ = i;
691 }
692
693
694 // returns -1 if inset not found
695 int Paragraph::getPositionOfInset(InsetOld const * inset) const
696 {
697         // Find the entry.
698         InsetList::const_iterator it = insetlist.begin();
699         InsetList::const_iterator end = insetlist.end();
700         for (; it != end; ++it)
701                 if (it->inset == inset)
702                         return it->pos;
703         return -1;
704 }
705
706
707 InsetBibitem * Paragraph::bibitem() const
708 {
709         InsetList::const_iterator it = insetlist.begin();
710         if (it != insetlist.end() && it->inset->lyxCode() == InsetOld::BIBTEX_CODE)
711                 return static_cast<InsetBibitem *>(it->inset);
712         return 0;
713 }
714
715
716 namespace {
717
718 /* paragraphs inside floats need different alignment tags to avoid
719 unwanted space */
720
721 bool noTrivlistCentering(UpdatableInset const * inset)
722 {
723         if (inset && inset->owner()) {
724                 InsetOld::Code const code = inset->owner()->lyxCode();
725                 return code == InsetOld::FLOAT_CODE ||
726                         code == InsetOld::WRAP_CODE;
727         }
728         return false;
729 }
730
731
732 string correction(string const & orig)
733 {
734         if (orig == "flushleft")
735                 return "raggedright";
736         if (orig == "flushright")
737                 return "raggedleft";
738         if (orig == "center")
739                 return "centering";
740         return orig;
741 }
742
743
744 string const corrected_env(string const & suffix, string const & env,
745                            UpdatableInset const * inset)
746 {
747         string output = suffix + "{";
748         if (noTrivlistCentering(inset))
749                 output += correction(env);
750         else
751                 output += env;
752         return output + "}";
753 }
754
755 } // namespace anon
756
757
758 // This could go to ParagraphParameters if we want to
759 int Paragraph::startTeXParParams(BufferParams const & bparams,
760                                  ostream & os, bool moving_arg) const
761 {
762         int column = 0;
763
764         if (params().noindent()) {
765                 os << "\\noindent ";
766                 column += 10;
767         }
768
769         switch (params().align()) {
770         case LYX_ALIGN_NONE:
771         case LYX_ALIGN_BLOCK:
772         case LYX_ALIGN_LAYOUT:
773         case LYX_ALIGN_SPECIAL:
774                 break;
775         case LYX_ALIGN_LEFT:
776         case LYX_ALIGN_RIGHT:
777         case LYX_ALIGN_CENTER:
778                 if (moving_arg) {
779                         os << "\\protect";
780                         column = 8;
781                 }
782                 break;
783         }
784
785         switch (params().align()) {
786         case LYX_ALIGN_NONE:
787         case LYX_ALIGN_BLOCK:
788         case LYX_ALIGN_LAYOUT:
789         case LYX_ALIGN_SPECIAL:
790                 break;
791         case LYX_ALIGN_LEFT: {
792                 string output;
793                 UpdatableInset const * const inset = pimpl_->inset_owner;
794                 if (getParLanguage(bparams)->babel() != "hebrew")
795                         output = corrected_env("\\begin", "flushleft", inset);
796                 else
797                         output = corrected_env("\\begin", "flushright", inset);
798                 os << output;
799                 column += output.size();
800                 break;
801         } case LYX_ALIGN_RIGHT: {
802                 string output;
803                 UpdatableInset const * const inset = pimpl_->inset_owner;
804                 if (getParLanguage(bparams)->babel() != "hebrew")
805                         output = corrected_env("\\begin", "flushright", inset);
806                 else
807                         output = corrected_env("\\begin", "flushleft", inset);
808                 os << output;
809                 column += output.size();
810                 break;
811         } case LYX_ALIGN_CENTER: {
812                 string output;
813                 output = corrected_env("\\begin", "center", pimpl_->inset_owner);
814                 os << output;
815                 column += output.size();
816                 break;
817         }
818         }
819
820         return column;
821 }
822
823
824 // This could go to ParagraphParameters if we want to
825 int Paragraph::endTeXParParams(BufferParams const & bparams,
826                                ostream & os, bool moving_arg) const
827 {
828         int column = 0;
829
830         switch (params().align()) {
831         case LYX_ALIGN_NONE:
832         case LYX_ALIGN_BLOCK:
833         case LYX_ALIGN_LAYOUT:
834         case LYX_ALIGN_SPECIAL:
835                 break;
836         case LYX_ALIGN_LEFT:
837         case LYX_ALIGN_RIGHT:
838         case LYX_ALIGN_CENTER:
839                 if (moving_arg) {
840                         os << "\\protect";
841                         column = 8;
842                 }
843                 break;
844         }
845
846         switch (params().align()) {
847         case LYX_ALIGN_NONE:
848         case LYX_ALIGN_BLOCK:
849         case LYX_ALIGN_LAYOUT:
850         case LYX_ALIGN_SPECIAL:
851                 break;
852         case LYX_ALIGN_LEFT: {
853                 string output;
854                 UpdatableInset const * const inset = pimpl_->inset_owner;
855                 if (getParLanguage(bparams)->babel() != "hebrew")
856                         output = corrected_env("\\par\\end", "flushleft", inset);
857                 else
858                         output = corrected_env("\\par\\end", "flushright", inset);
859                 os << output;
860                 column += output.size();
861                 break;
862         } case LYX_ALIGN_RIGHT: {
863                 string output;
864                 UpdatableInset const * const inset = pimpl_->inset_owner;
865                 if (getParLanguage(bparams)->babel() != "hebrew")
866                         output = corrected_env("\\par\\end", "flushright", inset);
867                 else
868                         output = corrected_env("\\par\\end", "flushleft", inset);
869                 os << output;
870                 column += output.size();
871                 break;
872         } case LYX_ALIGN_CENTER: {
873                 string output;
874                 output = corrected_env("\\par\\end", "center", pimpl_->inset_owner);
875                 os << output;
876                 column += output.size();
877                 break;
878         }
879         }
880
881         return column;
882 }
883
884
885 // This one spits out the text of the paragraph
886 bool Paragraph::simpleTeXOnePar(Buffer const & buf,
887                                 BufferParams const & bparams,
888                                 LyXFont const & outerfont,
889                                 ostream & os, TexRow & texrow,
890                                 OutputParams const & runparams)
891 {
892         lyxerr[Debug::LATEX] << "SimpleTeXOnePar...     " << this << endl;
893
894         bool return_value = false;
895
896         LyXLayout_ptr style;
897
898         // well we have to check if we are in an inset with unlimited
899         // length (all in one row) if that is true then we don't allow
900         // any special options in the paragraph and also we don't allow
901         // any environment other then "Standard" to be valid!
902         bool asdefault =
903                 (inInset() && inInset()->forceDefaultParagraphs(inInset()));
904
905         if (asdefault) {
906                 style = bparams.getLyXTextClass().defaultLayout();
907         } else {
908                 style = layout();
909         }
910
911         LyXFont basefont;
912
913         // Maybe we have to create a optional argument.
914         pos_type body_pos = beginOfBody();
915         unsigned int column = 0;
916
917         if (body_pos > 0) {
918                 os << '[';
919                 ++column;
920                 basefont = getLabelFont(bparams, outerfont);
921         } else {
922                 basefont = getLayoutFont(bparams, outerfont);
923         }
924
925         bool moving_arg = runparams.moving_arg;
926         moving_arg |= style->needprotect;
927
928         // Which font is currently active?
929         LyXFont running_font(basefont);
930         // Do we have an open font change?
931         bool open_font = false;
932
933         Change::Type running_change = Change::UNCHANGED;
934
935         texrow.start(id(), 0);
936
937         // if the paragraph is empty, the loop will not be entered at all
938         if (empty()) {
939                 if (style->isCommand()) {
940                         os << '{';
941                         ++column;
942                 }
943                 if (!asdefault)
944                         column += startTeXParParams(bparams, os, moving_arg);
945         }
946
947         for (pos_type i = 0; i < size(); ++i) {
948                 ++column;
949                 // First char in paragraph or after label?
950                 if (i == body_pos) {
951                         if (body_pos > 0) {
952                                 if (open_font) {
953                                         column += running_font.latexWriteEndChanges(os, basefont, basefont);
954                                         open_font = false;
955                                 }
956                                 basefont = getLayoutFont(bparams, outerfont);
957                                 running_font = basefont;
958                                 os << ']';
959                                 ++column;
960                         }
961                         if (style->isCommand()) {
962                                 os << '{';
963                                 ++column;
964                         }
965
966                         if (!asdefault)
967                                 column += startTeXParParams(bparams, os,
968                                                             moving_arg);
969                 }
970
971                 value_type c = getChar(i);
972
973                 // Fully instantiated font
974                 LyXFont font = getFont(bparams, i, outerfont);
975
976                 LyXFont const last_font = running_font;
977
978                 // Spaces at end of font change are simulated to be
979                 // outside font change, i.e. we write "\textXX{text} "
980                 // rather than "\textXX{text }". (Asger)
981                 if (open_font && c == ' ' && i <= size() - 2) {
982                         LyXFont const & next_font = getFont(bparams, i + 1, outerfont);
983                         if (next_font != running_font && next_font != font) {
984                                 font = next_font;
985                         }
986                 }
987
988                 // We end font definition before blanks
989                 if (open_font &&
990                     (font != running_font ||
991                      font.language() != running_font.language()))
992                 {
993                         column += running_font.latexWriteEndChanges(os,
994                                                                     basefont,
995                                                                     (i == body_pos-1) ? basefont : font);
996                         running_font = basefont;
997                         open_font = false;
998                 }
999
1000                 // Blanks are printed before start of fontswitch
1001                 if (c == ' ') {
1002                         // Do not print the separation of the optional argument
1003                         if (i != body_pos - 1) {
1004                                 pimpl_->simpleTeXBlanks(os, texrow, i,
1005                                                        column, font, *style);
1006                         }
1007                 }
1008
1009                 // Do we need to change font?
1010                 if ((font != running_font ||
1011                      font.language() != running_font.language()) &&
1012                         i != body_pos - 1)
1013                 {
1014                         column += font.latexWriteStartChanges(os, basefont,
1015                                                               last_font);
1016                         running_font = font;
1017                         open_font = true;
1018                 }
1019
1020                 Change::Type change = pimpl_->lookupChange(i);
1021
1022                 column += Changes::latexMarkChange(os, running_change, change);
1023                 running_change = change;
1024
1025                 OutputParams rp = runparams;
1026                 rp.moving_arg = moving_arg;
1027                 rp.free_spacing = style->free_spacing;
1028                 pimpl_->simpleTeXSpecialChars(buf, bparams,
1029                                               os, texrow, runparams,
1030                                               font, running_font,
1031                                               basefont, outerfont, open_font,
1032                                               running_change,
1033                                               *style, i, column, c);
1034         }
1035
1036         column += Changes::latexMarkChange(os,
1037                         running_change, Change::UNCHANGED);
1038
1039         // If we have an open font definition, we have to close it
1040         if (open_font) {
1041 #ifdef FIXED_LANGUAGE_END_DETECTION
1042                 if (next_) {
1043                         running_font
1044                                 .latexWriteEndChanges(os, basefont,
1045                                                       next_->getFont(bparams,
1046                                                       0, outerfont));
1047                 } else {
1048                         running_font.latexWriteEndChanges(os, basefont,
1049                                                           basefont);
1050                 }
1051 #else
1052 #ifdef WITH_WARNINGS
1053 //#warning For now we ALWAYS have to close the foreign font settings if they are
1054 //#warning there as we start another \selectlanguage with the next paragraph if
1055 //#warning we are in need of this. This should be fixed sometime (Jug)
1056 #endif
1057                 running_font.latexWriteEndChanges(os, basefont,  basefont);
1058 #endif
1059         }
1060
1061         // Needed if there is an optional argument but no contents.
1062         if (body_pos > 0 && body_pos == size()) {
1063                 os << "]~";
1064                 return_value = false;
1065         }
1066
1067         if (!asdefault) {
1068                 column += endTeXParParams(bparams, os, moving_arg);
1069         }
1070
1071         lyxerr[Debug::LATEX] << "SimpleTeXOnePar...done " << this << endl;
1072         return return_value;
1073 }
1074
1075
1076 namespace {
1077
1078 // checks, if newcol chars should be put into this line
1079 // writes newline, if necessary.
1080 void sgmlLineBreak(ostream & os, string::size_type & colcount,
1081                           string::size_type newcol)
1082 {
1083         colcount += newcol;
1084         if (colcount > lyxrc.ascii_linelen) {
1085                 os << "\n";
1086                 colcount = newcol; // assume write after this call
1087         }
1088 }
1089
1090 enum PAR_TAG {
1091         NONE=0,
1092         TT = 1,
1093         SF = 2,
1094         BF = 4,
1095         IT = 8,
1096         SL = 16,
1097         EM = 32
1098 };
1099
1100
1101 string tag_name(PAR_TAG const & pt) {
1102         switch (pt) {
1103         case NONE: return "!-- --";
1104         case TT: return "tt";
1105         case SF: return "sf";
1106         case BF: return "bf";
1107         case IT: return "it";
1108         case SL: return "sl";
1109         case EM: return "em";
1110         }
1111         return "";
1112 }
1113
1114
1115 inline
1116 void operator|=(PAR_TAG & p1, PAR_TAG const & p2)
1117 {
1118         p1 = static_cast<PAR_TAG>(p1 | p2);
1119 }
1120
1121
1122 inline
1123 void reset(PAR_TAG & p1, PAR_TAG const & p2)
1124 {
1125         p1 = static_cast<PAR_TAG>(p1 & ~p2);
1126 }
1127
1128 } // anon
1129
1130
1131 // Handle internal paragraph parsing -- layout already processed.
1132 void Paragraph::simpleLinuxDocOnePar(Buffer const & buf,
1133                                      ostream & os,
1134                                      LyXFont const & outerfont,
1135                                      OutputParams const & runparams,
1136                                      lyx::depth_type /*depth*/) const
1137 {
1138         LyXLayout_ptr const & style = layout();
1139
1140         string::size_type char_line_count = 5;     // Heuristic choice ;-)
1141
1142         // gets paragraph main font
1143         LyXFont font_old;
1144         bool desc_on;
1145         if (style->labeltype == LABEL_MANUAL) {
1146                 font_old = style->labelfont;
1147                 desc_on = true;
1148         } else {
1149                 font_old = style->font;
1150                 desc_on = false;
1151         }
1152
1153         LyXFont::FONT_FAMILY family_type = LyXFont::ROMAN_FAMILY;
1154         LyXFont::FONT_SERIES series_type = LyXFont::MEDIUM_SERIES;
1155         LyXFont::FONT_SHAPE  shape_type  = LyXFont::UP_SHAPE;
1156         bool is_em = false;
1157
1158         stack<PAR_TAG> tag_state;
1159         // parsing main loop
1160         for (pos_type i = 0; i < size(); ++i) {
1161
1162                 PAR_TAG tag_close = NONE;
1163                 list < PAR_TAG > tag_open;
1164
1165                 LyXFont const font = getFont(buf.params(), i, outerfont);
1166
1167                 if (font_old.family() != font.family()) {
1168                         switch (family_type) {
1169                         case LyXFont::SANS_FAMILY:
1170                                 tag_close |= SF;
1171                                 break;
1172                         case LyXFont::TYPEWRITER_FAMILY:
1173                                 tag_close |= TT;
1174                                 break;
1175                         default:
1176                                 break;
1177                         }
1178
1179                         family_type = font.family();
1180
1181                         switch (family_type) {
1182                         case LyXFont::SANS_FAMILY:
1183                                 tag_open.push_back(SF);
1184                                 break;
1185                         case LyXFont::TYPEWRITER_FAMILY:
1186                                 tag_open.push_back(TT);
1187                                 break;
1188                         default:
1189                                 break;
1190                         }
1191                 }
1192
1193                 if (font_old.series() != font.series()) {
1194                         switch (series_type) {
1195                         case LyXFont::BOLD_SERIES:
1196                                 tag_close |= BF;
1197                                 break;
1198                         default:
1199                                 break;
1200                         }
1201
1202                         series_type = font.series();
1203
1204                         switch (series_type) {
1205                         case LyXFont::BOLD_SERIES:
1206                                 tag_open.push_back(BF);
1207                                 break;
1208                         default:
1209                                 break;
1210                         }
1211
1212                 }
1213
1214                 if (font_old.shape() != font.shape()) {
1215                         switch (shape_type) {
1216                         case LyXFont::ITALIC_SHAPE:
1217                                 tag_close |= IT;
1218                                 break;
1219                         case LyXFont::SLANTED_SHAPE:
1220                                 tag_close |= SL;
1221                                 break;
1222                         default:
1223                                 break;
1224                         }
1225
1226                         shape_type = font.shape();
1227
1228                         switch (shape_type) {
1229                         case LyXFont::ITALIC_SHAPE:
1230                                 tag_open.push_back(IT);
1231                                 break;
1232                         case LyXFont::SLANTED_SHAPE:
1233                                 tag_open.push_back(SL);
1234                                 break;
1235                         default:
1236                                 break;
1237                         }
1238                 }
1239                 // handle <em> tag
1240                 if (font_old.emph() != font.emph()) {
1241                         if (font.emph() == LyXFont::ON) {
1242                                 tag_open.push_back(EM);
1243                                 is_em = true;
1244                         }
1245                         else if (is_em) {
1246                                 tag_close |= EM;
1247                                 is_em = false;
1248                         }
1249                 }
1250
1251                 list < PAR_TAG > temp;
1252                 while (!tag_state.empty() && tag_close) {
1253                         PAR_TAG k =  tag_state.top();
1254                         tag_state.pop();
1255                         os << "</" << tag_name(k) << '>';
1256                         if (tag_close & k)
1257                                 reset(tag_close,k);
1258                         else
1259                                 temp.push_back(k);
1260                 }
1261
1262                 for(list< PAR_TAG >::const_iterator j = temp.begin();
1263                     j != temp.end(); ++j) {
1264                         tag_state.push(*j);
1265                         os << '<' << tag_name(*j) << '>';
1266                 }
1267
1268                 for(list< PAR_TAG >::const_iterator j = tag_open.begin();
1269                     j != tag_open.end(); ++j) {
1270                         tag_state.push(*j);
1271                         os << '<' << tag_name(*j) << '>';
1272                 }
1273
1274                 char c = getChar(i);
1275
1276                 if (c == Paragraph::META_INSET) {
1277                         InsetOld const * inset = getInset(i);
1278                         inset->linuxdoc(buf, os, runparams);
1279                         font_old = font;
1280                         continue;
1281                 }
1282
1283                 if (style->latexparam() == "CDATA") {
1284                         // "TeX"-Mode on == > SGML-Mode on.
1285                         if (c != '\0')
1286                                 os << c;
1287                         ++char_line_count;
1288                 } else {
1289                         bool ws;
1290                         string str;
1291                         boost::tie(ws, str) = sgml::escapeChar(c);
1292                         if (ws && !isFreeSpacing()) {
1293                                 // in freespacing mode, spaces are
1294                                 // non-breaking characters
1295                                 if (desc_on) {// if char is ' ' then...
1296
1297                                         ++char_line_count;
1298                                         sgmlLineBreak(os, char_line_count, 6);
1299                                         os << "</tag>";
1300                                         desc_on = false;
1301                                 } else  {
1302                                         sgmlLineBreak(os, char_line_count, 1);
1303                                         os << c;
1304                                 }
1305                         } else {
1306                                 os << str;
1307                                 char_line_count += str.length();
1308                         }
1309                 }
1310                 font_old = font;
1311         }
1312
1313         while (!tag_state.empty()) {
1314                 os << "</" << tag_name(tag_state.top()) << '>';
1315                 tag_state.pop();
1316         }
1317
1318         // resets description flag correctly
1319         if (desc_on) {
1320                 // <tag> not closed...
1321                 sgmlLineBreak(os, char_line_count, 6);
1322                 os << "</tag>";
1323         }
1324 }
1325
1326
1327 void Paragraph::simpleDocBookOnePar(Buffer const & buf,
1328                                     ostream & os,
1329                                     LyXFont const & outerfont,
1330                                     int & desc_on,
1331                                     OutputParams const & runparams,
1332                                     lyx::depth_type depth) const
1333 {
1334         bool emph_flag = false;
1335
1336         LyXLayout_ptr const & style = layout();
1337
1338         LyXFont font_old = (style->labeltype == LABEL_MANUAL ? style->labelfont : style->font);
1339
1340         int char_line_count = depth;
1341         //if (!style.free_spacing)
1342         //      os << string(depth,' ');
1343
1344         // parsing main loop
1345         for (pos_type i = 0; i < size(); ++i) {
1346                 LyXFont font = getFont(buf.params(), i, outerfont);
1347
1348                 // handle <emphasis> tag
1349                 if (font_old.emph() != font.emph()) {
1350                         if (font.emph() == LyXFont::ON) {
1351                                 if (style->latexparam() == "CDATA")
1352                                         os << "]]>";
1353                                 os << "<emphasis>";
1354                                 if (style->latexparam() == "CDATA")
1355                                         os << "<![CDATA[";
1356                                 emph_flag = true;
1357                         } else if (i) {
1358                                 if (style->latexparam() == "CDATA")
1359                                         os << "]]>";
1360                                 os << "</emphasis>";
1361                                 if (style->latexparam() == "CDATA")
1362                                         os << "<![CDATA[";
1363                                 emph_flag = false;
1364                         }
1365                 }
1366
1367
1368                 if (isInset(i)) {
1369                         InsetOld const * inset = getInset(i);
1370                         // don't print the inset in position 0 if desc_on == 3 (label)
1371                         if (i || desc_on != 3) {
1372                                 if (style->latexparam() == "CDATA")
1373                                         os << "]]>";
1374                                 inset->docbook(buf, os, runparams);
1375                                 if (style->latexparam() == "CDATA")
1376                                         os << "<![CDATA[";
1377                         }
1378                 } else {
1379                         char c = getChar(i);
1380                         bool ws;
1381                         string str;
1382                         boost::tie(ws, str) = sgml::escapeChar(c);
1383
1384                         if (style->pass_thru) {
1385                                 os << c;
1386                         } else if (isFreeSpacing() || c != ' ') {
1387                                         os << str;
1388                         } else if (desc_on == 1) {
1389                                 ++char_line_count;
1390                                 os << "\n</term><listitem><para>";
1391                                 desc_on = 2;
1392                         } else {
1393                                 os << ' ';
1394                         }
1395                 }
1396                 font_old = font;
1397         }
1398
1399         if (emph_flag) {
1400                 if (style->latexparam() == "CDATA")
1401                         os << "]]>";
1402                 os << "</emphasis>";
1403                 if (style->latexparam() == "CDATA")
1404                         os << "<![CDATA[";
1405         }
1406
1407         // resets description flag correctly
1408         if (desc_on == 1) {
1409                 // <term> not closed...
1410                 os << "</term>\n<listitem><para>&nbsp;</para>";
1411         }
1412         if (style->free_spacing)
1413                 os << '\n';
1414 }
1415
1416
1417 namespace {
1418
1419 /// return true if the char is a meta-character for an inset
1420 inline
1421 bool IsInsetChar(char c)
1422 {
1423         return (c == Paragraph::META_INSET);
1424 }
1425
1426 } // namespace anon
1427
1428
1429
1430 bool Paragraph::isHfill(pos_type pos) const
1431 {
1432         return IsInsetChar(getChar(pos))
1433                && getInset(pos)->lyxCode() == InsetOld::HFILL_CODE;
1434 }
1435
1436
1437 bool Paragraph::isInset(pos_type pos) const
1438 {
1439         return IsInsetChar(getChar(pos));
1440 }
1441
1442
1443 bool Paragraph::isNewline(pos_type pos) const
1444 {
1445         return IsInsetChar(getChar(pos))
1446                && getInset(pos)->lyxCode() == InsetOld::NEWLINE_CODE;
1447 }
1448
1449
1450 bool Paragraph::isSeparator(pos_type pos) const
1451 {
1452         return IsSeparatorChar(getChar(pos));
1453 }
1454
1455
1456 bool Paragraph::isLineSeparator(pos_type pos) const
1457 {
1458         value_type const c = getChar(pos);
1459         return IsLineSeparatorChar(c)
1460                 || (IsInsetChar(c) && getInset(pos) &&
1461                 getInset(pos)->isLineSeparator());
1462 }
1463
1464
1465 bool Paragraph::isKomma(pos_type pos) const
1466 {
1467         return IsKommaChar(getChar(pos));
1468 }
1469
1470
1471 /// Used by the spellchecker
1472 bool Paragraph::isLetter(pos_type pos) const
1473 {
1474         value_type const c = getChar(pos);
1475         if (IsLetterChar(c))
1476                 return true;
1477         if (isInset(pos))
1478                 return getInset(pos)->isLetter();
1479         // We want to pass the ' and escape chars to ispell
1480         string const extra = lyxrc.isp_esc_chars + '\'';
1481         return contains(extra, c);
1482 }
1483
1484
1485 bool Paragraph::isWord(pos_type pos) const
1486 {
1487         unsigned char const c = getChar(pos);
1488         return !(IsSeparatorChar(c)
1489                   || IsKommaChar(c)
1490                   || IsInsetChar(c));
1491 }
1492
1493
1494 Language const *
1495 Paragraph::getParLanguage(BufferParams const & bparams) const
1496 {
1497         if (!empty())
1498                 return getFirstFontSettings().language();
1499 #warning FIXME we should check the prev par as well (Lgb)
1500         return bparams.language;
1501 }
1502
1503
1504 bool Paragraph::isRightToLeftPar(BufferParams const & bparams) const
1505 {
1506         return lyxrc.rtl_support
1507                 && getParLanguage(bparams)->RightToLeft()
1508                 && !(inInset() && inInset()->owner() &&
1509                      inInset()->owner()->lyxCode() == InsetOld::ERT_CODE);
1510 }
1511
1512
1513 void Paragraph::changeLanguage(BufferParams const & bparams,
1514                                Language const * from, Language const * to)
1515 {
1516         for (pos_type i = 0; i < size(); ++i) {
1517                 LyXFont font = getFontSettings(bparams, i);
1518                 if (font.language() == from) {
1519                         font.setLanguage(to);
1520                         setFont(i, font);
1521                 }
1522         }
1523 }
1524
1525
1526 bool Paragraph::isMultiLingual(BufferParams const & bparams)
1527 {
1528         Language const * doc_language = bparams.language;
1529         Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
1530         Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
1531
1532         for (; cit != end; ++cit)
1533                 if (cit->font().language() != ignore_language &&
1534                     cit->font().language() != latex_language &&
1535                     cit->font().language() != doc_language)
1536                         return true;
1537         return false;
1538 }
1539
1540
1541 // Convert the paragraph to a string.
1542 // Used for building the table of contents
1543 string const Paragraph::asString(Buffer const & buffer,
1544                                  bool label) const
1545 {
1546         OutputParams runparams;
1547         return asString(buffer, runparams, label);
1548 }
1549
1550
1551 string const Paragraph::asString(Buffer const & buffer,
1552                                  OutputParams const & runparams,
1553                                  bool label) const
1554 {
1555 #if 0
1556         string s;
1557         if (label && !params().labelString().empty())
1558                 s += params().labelString() + ' ';
1559
1560         for (pos_type i = 0; i < size(); ++i) {
1561                 value_type c = getChar(i);
1562                 if (IsPrintable(c))
1563                         s += c;
1564                 else if (c == META_INSET &&
1565                          getInset(i)->lyxCode() == InsetOld::MATH_CODE) {
1566                         ostringstream os;
1567                         getInset(i)->plaintext(buffer, os, runparams);
1568                         s += subst(STRCONV(os.str()),'\n',' ');
1569                 }
1570         }
1571
1572         return s;
1573 #else
1574         // This should really be done by the caller and not here.
1575         string ret = asString(buffer, runparams, 0, size(), label);
1576         return subst(ret, '\n', ' ');
1577 #endif
1578 }
1579
1580
1581 string const Paragraph::asString(Buffer const & buffer,
1582                                  pos_type beg, pos_type end, bool label) const
1583 {
1584
1585         OutputParams const runparams;
1586         return asString(buffer, runparams, beg, end, label);
1587 }
1588
1589
1590 string const Paragraph::asString(Buffer const & buffer,
1591                                  OutputParams const & runparams,
1592                                  pos_type beg, pos_type end, bool label) const
1593 {
1594         ostringstream os;
1595
1596         if (beg == 0 && label && !params().labelString().empty())
1597                 os << params().labelString() << ' ';
1598
1599         for (pos_type i = beg; i < end; ++i) {
1600                 value_type const c = getUChar(buffer.params(), i);
1601                 if (IsPrintable(c))
1602                         os << c;
1603                 else if (c == META_INSET)
1604                         getInset(i)->plaintext(buffer, os, runparams);
1605         }
1606
1607         return os.str();
1608 }
1609
1610
1611 void Paragraph::setInsetOwner(UpdatableInset * inset)
1612 {
1613         pimpl_->inset_owner = inset;
1614         InsetList::iterator it = insetlist.begin();
1615         InsetList::iterator end = insetlist.end();
1616         for (; it != end; ++it)
1617                 if (it->inset)
1618                         it->inset->setOwner(inset);
1619 }
1620
1621
1622 void Paragraph::setContentsFromPar(Paragraph const & par)
1623 {
1624         pimpl_->setContentsFromPar(par);
1625 }
1626
1627
1628 void Paragraph::trackChanges(Change::Type type)
1629 {
1630         pimpl_->trackChanges(type);
1631 }
1632
1633
1634 void Paragraph::untrackChanges()
1635 {
1636         pimpl_->untrackChanges();
1637 }
1638
1639
1640 void Paragraph::cleanChanges()
1641 {
1642         pimpl_->cleanChanges();
1643 }
1644
1645
1646 Change::Type Paragraph::lookupChange(lyx::pos_type pos) const
1647 {
1648         BOOST_ASSERT(!size() || pos < size());
1649         return pimpl_->lookupChange(pos);
1650 }
1651
1652
1653 Change const Paragraph::lookupChangeFull(lyx::pos_type pos) const
1654 {
1655         BOOST_ASSERT(!size() || pos < size());
1656         return pimpl_->lookupChangeFull(pos);
1657 }
1658
1659
1660 bool Paragraph::isChanged(pos_type start, pos_type end) const
1661 {
1662         return pimpl_->isChanged(start, end);
1663 }
1664
1665
1666 bool Paragraph::isChangeEdited(pos_type start, pos_type end) const
1667 {
1668         return pimpl_->isChangeEdited(start, end);
1669 }
1670
1671
1672 void Paragraph::setChange(lyx::pos_type pos, Change::Type type)
1673 {
1674         pimpl_->setChange(pos, type);
1675
1676 }
1677
1678
1679 void Paragraph::markErased()
1680 {
1681         pimpl_->markErased();
1682 }
1683
1684
1685 void Paragraph::acceptChange(pos_type start, pos_type end)
1686 {
1687         return pimpl_->acceptChange(start, end);
1688 }
1689
1690
1691 void Paragraph::rejectChange(pos_type start, pos_type end)
1692 {
1693         return pimpl_->rejectChange(start, end);
1694 }
1695
1696
1697 Paragraph::value_type Paragraph::getChar(pos_type pos) const
1698 {
1699         // This is in the critical path!
1700         pos_type const siz = text_.size();
1701
1702         BOOST_ASSERT(0 <= pos);
1703         BOOST_ASSERT(pos <= siz);
1704
1705         if (pos == siz) {
1706                 lyxerr << "getChar() on pos " << pos << " in par id "
1707                        << id() << " of size " << siz
1708                        << "  is a bit silly !" << endl;
1709                 BOOST_ASSERT(false);
1710                 return '\0';
1711         }
1712
1713         return text_[pos];
1714 }
1715
1716
1717 int Paragraph::id() const
1718 {
1719         return pimpl_->id_;
1720 }
1721
1722
1723 LyXLayout_ptr const & Paragraph::layout() const
1724 {
1725 /*
1726         InsetOld * inset = inInset();
1727         if (inset && inset->lyxCode() == InsetOld::ENVIRONMENT_CODE)
1728                 return static_cast<InsetEnvironment*>(inset)->layout();
1729 */
1730         return layout_;
1731 }
1732
1733
1734 void Paragraph::layout(LyXLayout_ptr const & new_layout)
1735 {
1736         layout_ = new_layout;
1737 }
1738
1739
1740 UpdatableInset * Paragraph::inInset() const
1741 {
1742         return pimpl_->inset_owner;
1743 }
1744
1745
1746 void Paragraph::clearContents()
1747 {
1748         text_.clear();
1749 }
1750
1751
1752 void Paragraph::setChar(pos_type pos, value_type c)
1753 {
1754         text_[pos] = c;
1755 }
1756
1757
1758 ParagraphParameters & Paragraph::params()
1759 {
1760         return pimpl_->params;
1761 }
1762
1763
1764 ParagraphParameters const & Paragraph::params() const
1765 {
1766         return pimpl_->params;
1767 }
1768
1769
1770 bool Paragraph::isFreeSpacing() const
1771 {
1772         if (layout()->free_spacing)
1773                 return true;
1774
1775         // for now we just need this, later should we need this in some
1776         // other way we can always add a function to InsetOld too.
1777         if (pimpl_->inset_owner && pimpl_->inset_owner->owner())
1778                 return pimpl_->inset_owner->owner()->lyxCode() == InsetOld::ERT_CODE;
1779         return false;
1780 }
1781
1782
1783 bool Paragraph::allowEmpty() const
1784 {
1785         if (layout()->keepempty)
1786                 return true;
1787         if (pimpl_->inset_owner && pimpl_->inset_owner->owner())
1788                 return pimpl_->inset_owner->owner()->lyxCode() == InsetOld::ERT_CODE;
1789         return false;
1790 }
1791
1792
1793 RowList::iterator Paragraph::getRow(pos_type pos)
1794 {
1795         RowList::iterator rit = rows.end();
1796         RowList::iterator const begin = rows.begin();
1797
1798         for (--rit; rit != begin && rit->pos() > pos; --rit)
1799                 ;
1800
1801         return rit;
1802 }
1803
1804
1805 unsigned char Paragraph::transformChar(unsigned char c, pos_type pos) const
1806 {
1807         if (!Encodings::is_arabic(c))
1808                 if (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 && IsDigit(c))
1809                         return c + (0xb0 - '0');
1810                 else
1811                         return c;
1812
1813         unsigned char const prev_char = pos > 0 ? getChar(pos - 1) : ' ';
1814         unsigned char next_char = ' ';
1815
1816         for (pos_type i = pos + 1, end = size(); i < end; ++i) {
1817                 unsigned char const par_char = getChar(i);
1818                 if (!Encodings::IsComposeChar_arabic(par_char)) {
1819                         next_char = par_char;
1820                         break;
1821                 }
1822         }
1823
1824         if (Encodings::is_arabic(next_char)) {
1825                 if (Encodings::is_arabic(prev_char) &&
1826                         !Encodings::is_arabic_special(prev_char))
1827                         return Encodings::TransformChar(c, Encodings::FORM_MEDIAL);
1828                 else
1829                         return Encodings::TransformChar(c, Encodings::FORM_INITIAL);
1830         } else {
1831                 if (Encodings::is_arabic(prev_char) &&
1832                         !Encodings::is_arabic_special(prev_char))
1833                         return Encodings::TransformChar(c, Encodings::FORM_FINAL);
1834                 else
1835                         return Encodings::TransformChar(c, Encodings::FORM_ISOLATED);
1836         }
1837 }