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