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