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