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