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