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