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